调整微信登录

This commit is contained in:
CaiYanPeng 2021-06-07 13:51:13 +08:00
parent 5671b35dce
commit 432e02e3de
4 changed files with 44 additions and 41 deletions

View File

@ -11,14 +11,6 @@ namespace Assets.Scripts.Apis
{ {
public class ThirdPartApi : ApiBase public class ThirdPartApi : ApiBase
{ {
public async Task<JObject> GetWxAccessToken(string code)
{
return await GetThirdPartAsync<JObject>($"https://api.weixin.qq.com/sns/oauth2/access_token?appid={App.WxAppId}&secret={App.WxAppSecret}&code={code}&grant_type=authorization_code");
}
public async Task<JObject> GetWxUserInfo(string accessToken, string openId)
{
return await GetThirdPartAsync<JObject>($"https://api.weixin.qq.com/sns/userinfo?access_token=" + accessToken + "&openid=" + openId);
}
public async Task<IpToGeoModel> GetLocationInfo() public async Task<IpToGeoModel> GetLocationInfo()
{ {
return await GetThirdPartAsync<IpToGeoModel>("https://ip.seeip.org/geoip"); return await GetThirdPartAsync<IpToGeoModel>("https://ip.seeip.org/geoip");

View File

@ -185,7 +185,25 @@ namespace Assets.Scripts.Apis
} }
return r; return r;
} }
public async Task<JsonResult<object>> OnWebWxLoginCheckUnionIdByCode(string code,string state)
{
var r = await GetAsync<JsonResult<object>>($"NoAuth/v1/OnWebWxLogin?code={code}&state={state}");
if (r.result)
{
var data = JObject.FromObject(r.data);
if (data.Value<string>("success") != null && data.Value<string>("success") == "False")
{
return r;
}
else
{
var user = data.ToObject<UserResultModel>();
user.cookie = cookies[0].Value;
r.data = user;
}
}
return r;
}
public async Task<JsonResult<object>> OnWebWxLoginCheckUnionId(string unionId, string openId) public async Task<JsonResult<object>> OnWebWxLoginCheckUnionId(string unionId, string openId)
{ {
var r = await PostAsync<JsonResult<object>>("NoAuth/OnWebWxLoginCheckUnionId", new { unionId, openId }); var r = await PostAsync<JsonResult<object>>("NoAuth/OnWebWxLoginCheckUnionId", new { unionId, openId });
@ -205,11 +223,11 @@ namespace Assets.Scripts.Apis
} }
return r; return r;
} }
public async Task<JsonResult<object>> OnWebWxLogin(string Phone, string Captcha public async Task<JsonResult<object>> OnWebWxLogin(string Phone, string Captcha,
, string UnionId, string WxWebOpenId, string WxHeadImg, string NickName, int? Sex, string Password) string Password, string UnionId, string WxWebOpenId)
{ {
var r = await PostAsync<JsonResult<object>>("NoAuth/OnWebWxLogin", var r = await PostAsync<JsonResult<object>>("NoAuth/v1/OnWebWxLogin",
new { Phone, Captcha, UnionId, WxWebOpenId, WxHeadImg, NickName, Sex, Pwd = Password }); new { Phone, Captcha, UnionId, openId = WxWebOpenId, Pwd = Password });
if (r.result) if (r.result)
{ {
var u = JObject.FromObject(r.data); var u = JObject.FromObject(r.data);

View File

@ -18,8 +18,7 @@ public static class App
public static int RouteIdParam = 0; public static int RouteIdParam = 0;
public static UserResultModel CurrentUser { get; set; } public static UserResultModel CurrentUser { get; set; }
public static string WxAppId = "wx9e8ca734e024f084"; public static string WxAppId = "wx9e8ca734e024f084";
public static string WxAppSecret = "906abafb2a18bc7a66ccfa5550f859b6";
public static string MapBoxAccessToken => "pk.eyJ1IjoiYW5keXNqdCIsImEiOiJja2ZhajE5OGwwamRiMnltcW96bHk0ZWFuIn0.GvKanc6UveWSvIjS9HfBPA"; public static string MapBoxAccessToken => "pk.eyJ1IjoiYW5keXNqdCIsImEiOiJja2ZhajE5OGwwamRiMnltcW96bHk0ZWFuIn0.GvKanc6UveWSvIjS9HfBPA";
public static double latitude { get; internal set; } public static double latitude { get; internal set; }

View File

@ -254,8 +254,8 @@ public class LoginController : MonoBehaviour
{ {
if (wxLock) return; if (wxLock) return;
wxLock = true; wxLock = true;
wxState = (DateTime.Now.ToUniversalTime().Ticks / 10000 * new System.Random().Next(1, 5)).ToString(); //wxState = (DateTime.Now.ToUniversalTime().Ticks / 10000 * new System.Random().Next(1, 5)).ToString();
wxBrowser.Url = $"https://open.weixin.qq.com/connect/qrconnect?appid={App.WxAppId}&redirect_uri=https%3A%2F%2Fwx.powerfun.com.cn%2FNoAuth%2Fv1%2FWxWebLogin&response_type=code&scope=snsapi_login&state={wxState}#wechat_redirect"; wxBrowser.Url = $"https://open.weixin.qq.com/connect/qrconnect?appid={App.WxAppId}&redirect_uri=https%3A%2F%2Fwx.powerfun.com.cn%2FNoAuth%2Fv1%2FWxWebLogin&response_type=code&scope=snsapi_login&state=unity#wechat_redirect";
AdjustWxQrCode(); AdjustWxQrCode();
//wxBrowser.RegisterFunction("isFinish", args => //wxBrowser.RegisterFunction("isFinish", args =>
//{ //{
@ -536,28 +536,28 @@ public class LoginController : MonoBehaviour
{ {
//AdjustWxQrCode(); //AdjustWxQrCode();
} }
//Debug.Log(wxBrowser.Url);
var rgx = Regex.Match(wxBrowser.Url, @"https\:\/\/wx.powerfun.com.cn/NoAuth/v1/WxWebLogin\?code=(.*?)&state=(.*?)$"); var rgx = Regex.Match(wxBrowser.Url, @"https\:\/\/wx.powerfun.com.cn/NoAuth/v1/WxWebLogin\?code=(.*?)&state=(.*?)$");
var groups = rgx.Groups; var groups = rgx.Groups;
if (groups.Count == 3) if (groups.Count == 3)
{ {
wxBrowser.LoadHTML("<div />"); //wxBrowser.LoadHTML("<div />");
var state = groups[2].Value; var state = groups[2].Value;
if (state == wxState) if (state == "unity")
{ {
var acjson = await ConfigHelper.thirdPartApi.GetWxAccessToken(groups[1].Value); //wxBrowser.
var infojson = await ConfigHelper.thirdPartApi.GetWxUserInfo( //var acjson = await ConfigHelper.thirdPartApi.GetWxAccessToken(groups[1].Value);
acjson.Value<string>("access_token"), acjson.Value<string>("openid")); //var infojson = await ConfigHelper.thirdPartApi.GetWxUserInfo(
wxInfoJson = infojson; // acjson.Value<string>("access_token"), acjson.Value<string>("openid"));
string unionId = wxInfoJson.Value<string>("unionid"),
openId = wxInfoJson.Value<string>("openid"); //string unionId = wxInfoJson.Value<string>("unionid"),
var r = await ConfigHelper.userApi.OnWebWxLoginCheckUnionId(unionId, openId); // openId = wxInfoJson.Value<string>("openid");
var r = await ConfigHelper.userApi.OnWebWxLoginCheckUnionIdByCode(groups[1].Value, groups[2].Value);
if (r.result) if (r.result)
{ {
JObject data = JObject.FromObject(r.data); JObject data = JObject.FromObject(r.data);
if (data.Value<string>("success") != null && data.Value<string>("success") == "False") if (data.Value<string>("success") != null && data.Value<string>("success") == "False")
{ {
wxInfoJson = data;
wxLogin2.gameObject.SetActive(true); wxLogin2.gameObject.SetActive(true);
Utils.DisplayImage(wxLogin2.Find("Avatar").GetComponent<RawImage>(), Utils.DisplayImage(wxLogin2.Find("Avatar").GetComponent<RawImage>(),
wxInfoJson.Value<string>("headimgurl")); wxInfoJson.Value<string>("headimgurl"));
@ -572,7 +572,7 @@ public class LoginController : MonoBehaviour
} }
else else
{ {
RefreshWx3(data.ToObject<UserResultModel>(),1); RefreshWx3(data.ToObject<UserResultModel>(), 1);
wxLogin3.gameObject.SetActive(true); wxLogin3.gameObject.SetActive(true);
//Utils.DisplayImage(StartCoroutine, //Utils.DisplayImage(StartCoroutine,
//wxLogin3.Find("Avatar").GetComponent<RawImage>(), //wxLogin3.Find("Avatar").GetComponent<RawImage>(),
@ -701,12 +701,9 @@ public class LoginController : MonoBehaviour
{ {
r = await ConfigHelper.userApi.OnWebWxLogin(Email.text, r = await ConfigHelper.userApi.OnWebWxLogin(Email.text,
Captcha.text, Captcha.text,
wxInfoJson.Value<string>("unionid"), Password.text,
wxInfoJson.Value<string>("openid"), wxInfoJson.Value<string>("unionId"),
wxInfoJson.Value<string>("headimgurl"), wxInfoJson.Value<string>("openId"));
wxInfoJson.Value<string>("nickname"),
wxInfoJson.Value<int>("sex"),
Password.text);
} }
if (r != null && r.result) if (r != null && r.result)
{ {
@ -729,12 +726,9 @@ public class LoginController : MonoBehaviour
var Captcha = signForm.captcha; var Captcha = signForm.captcha;
var r = await ConfigHelper.userApi.OnWebWxLogin(Email.text, var r = await ConfigHelper.userApi.OnWebWxLogin(Email.text,
Captcha.text, Captcha.text,
wxInfoJson.Value<string>("unionid"), "",
wxInfoJson.Value<string>("openid"), wxInfoJson.Value<string>("unionId"),
wxInfoJson.Value<string>("headimgurl"), wxInfoJson.Value<string>("openId"));
wxInfoJson.Value<string>("nickname"),
wxInfoJson.Value<int>("sex"),
"");
if (r.result) if (r.result)
{ {
var u = JObject.FromObject(r.data).ToObject<UserResultModel>(); var u = JObject.FromObject(r.data).ToObject<UserResultModel>();