Commit 4676ab69 authored by 黄奎's avatar 黄奎
parents 169ace6c 8a5750d4
......@@ -30,6 +30,7 @@ namespace EduSpider.WebApi.Controllers
[Autowired]
public IAccountService accountService { get; set; }
#region 账号密码登录
/// <summary>
/// 账号密码登录
/// </summary>
......@@ -94,7 +95,9 @@ namespace EduSpider.WebApi.Controllers
return ApiResult.Success(data: obj);
}
}
#endregion
#region 手机号码登录
/// <summary>
/// 根据手机号码一键登录
/// </summary>
......@@ -260,5 +263,154 @@ namespace EduSpider.WebApi.Controllers
return ApiResult.Success(data: userinfo);
}
#endregion
#endregion
#region 授权一键登录
/// <summary>
/// 微信授权 获取手机号码 一键登录
/// </summary>
/// <returns></returns>
[HttpGet]
[HttpPost]
[AllowAnonymous]
public ApiResult LoginByAuthorize()
{
JObject parms = JObject.Parse(RequestParm.Msg.ToString());
string code = parms.GetString("code");
string encryptedData = parms.GetString("encryptedData");
string iv = parms.GetString("iv");
//string WeChatName = jobj.GetString("WeChatName");
//string WeChatPhoto = jobj.GetString("WeChatPhoto");
//获取手机号码等信息
var rmodel = GetWeiXinMobileInfo(code, encryptedData, iv);
string account = rmodel.phoneNumber;//手机号码
string OpenId = rmodel.openid;
string UnionId = rmodel.unionid;
if (string.IsNullOrEmpty(account))
{
return ApiResult.Failed(message: "手机号码登录失败、跳转至账号登录", new { Error = -1 });
}
var model = accountService.GetAccountList(new RB_Account_Extend()
{
Account = account.Trim()
})?.FirstOrDefault();
if (model == null)
{
return ApiResult.Failed(message: "手机号码登录失败、跳转至账号登录", new { Error = -1 });
}
else
{
if (!string.IsNullOrEmpty(model.OpenId))//UnionId是否为空,为空则绑定手机号与UnionId
{
if (OpenId != model.OpenId)
{
//return ApiResult.Failed("手机号与绑定的微信账户不一致", new { Error = 0 });
}
}
else
{
model.UnionId = UnionId;
model.OpenId = OpenId;
accountService.UpdateAccountUnionId(model);
}
if (model.Status == Utility.Enum.DateStateEnum.Delete)
{
return ApiResult.Failed(message: $"此账号【{ account }】已禁用", new { Error = 2 });
}
#region 获取进阶思维小程序端token
BaseUserInfo UserInfo = new() { BaseUserId = model.Id };
string token = JwtHelper.CreateToken(UserInfo, Config.JwtSecretKey, Config.JwtExpirTime);
#endregion
Model.Cache.UserInfo obj = new()
{
Id = model.Id,
AccountType = model.AccountType,
AccountName = model.AccountName,
Token = token,
ApiRequestFromEnum = Utility.Enum.ApiRequestFromEnum.MiniProgram,
AccountId = model.AccountId,
UserMobile = model.Account,
UserIcon = model.UserIcon,
};
Cache.User.UserReidsCache.UserInfoSet(Cache.CacheKey.User_Login_Key + model.Id, obj, Config.JwtExpirTime);
return ApiResult.Success(data: obj);
}
}
/// <summary>
/// 获取手机号码等信息
/// </summary>
/// <returns></returns>
private static result GetWeiXinMobileInfo(string code, string encryptedData, string iv)
{
result res = new()
{
phoneNumber = "",
openid = ""
};
try
{
res = WeChatHelper.GetWechatMobile(encryptedData, code, iv);
}
catch
{
LogHelper.WriteError("GetGuestWeiXinMobile:1" + JsonHelper.Serialize(res));
try
{
res = WeChatHelper.GetWechatMobile(encryptedData, code, iv);
}
catch
{
LogHelper.WriteError("GetGuestWeiXinMobile:2" + JsonHelper.Serialize(res));
}
}
if (res.phoneNumber == null || string.IsNullOrEmpty(res.phoneNumber))
{
Thread.Sleep(10);
try
{
res = WeChatHelper.GetWechatMobile(encryptedData, code, iv);
}
catch
{
LogHelper.WriteError("GetGuestWeiXinMobile:3" + JsonHelper.Serialize(res));
try
{
res = WeChatHelper.GetWechatMobile(encryptedData, code, iv);
}
catch
{
LogHelper.WriteError("GetGuestWeiXinMobile:4" + JsonHelper.Serialize(res));
}
}
}
if (res.phoneNumber == null || string.IsNullOrEmpty(res.phoneNumber))
{
Thread.Sleep(10);
try
{
res = WeChatHelper.GetWechatMobile(encryptedData, code, iv);
}
catch
{
LogHelper.WriteError("GetGuestWeiXinMobile:5" + JsonHelper.Serialize(res));
try
{
res = WeChatHelper.GetWechatMobile(encryptedData, code, iv);
}
catch
{
LogHelper.WriteError("GetGuestWeiXinMobile:6" + JsonHelper.Serialize(res));
}
}
}
return res;
}
#endregion
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment