mirror of
http://git.coreshop.cn/jianweie/coreshoppro.git
synced 2026-05-07 01:47:20 +08:00
【新增】增加对接微信公众号通过code换取网页授权access_token,并存储微信用户数据方法。
This commit is contained in:
@@ -25,6 +25,13 @@ namespace CoreCms.Net.Model.FromBody
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
[Required(ErrorMessage = "请提交合法数据")]
|
[Required(ErrorMessage = "请提交合法数据")]
|
||||||
public string code { get; set; }
|
public string code { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 来源类型(对标GlobalEnumVars下的UserAccountTypes)
|
||||||
|
/// </summary>
|
||||||
|
public int type { get; set; } = 2;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -177,6 +177,9 @@ namespace CoreCms.Net.Web.WebApi.Controllers
|
|||||||
if (redisUserLock)
|
if (redisUserLock)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
{
|
||||||
|
|
||||||
|
if (entity.type == (int)GlobalEnumVars.UserAccountTypes.微信小程序)
|
||||||
{
|
{
|
||||||
var client = _weChatApiHttpClientFactory.CreateWxOpenClient();
|
var client = _weChatApiHttpClientFactory.CreateWxOpenClient();
|
||||||
var accessToken = WeChatCacheAccessTokenHelper.GetWxOpenAccessToken();
|
var accessToken = WeChatCacheAccessTokenHelper.GetWxOpenAccessToken();
|
||||||
@@ -275,6 +278,112 @@ namespace CoreCms.Net.Web.WebApi.Controllers
|
|||||||
jm.msg = response.ErrorMessage;
|
jm.msg = response.ErrorMessage;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else if (entity.type == (int)GlobalEnumVars.UserAccountTypes.微信公众号)
|
||||||
|
{
|
||||||
|
var client = _weChatApiHttpClientFactory.CreateWeXinClient();
|
||||||
|
var accessToken = WeChatCacheAccessTokenHelper.GetWeChatAccessToken();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
var request = new SnsOAuth2AccessTokenRequest()
|
||||||
|
{
|
||||||
|
Code = entity.code,
|
||||||
|
AccessToken = accessToken,
|
||||||
|
GrantType = "authorization_code"
|
||||||
|
};
|
||||||
|
|
||||||
|
var response = await client.ExecuteSnsOAuth2AccessTokenAsync(request, HttpContext.RequestAborted);
|
||||||
|
if (response.ErrorCode == (int)WeChatReturnCode.ReturnCode.请求成功)
|
||||||
|
{
|
||||||
|
var userInfo = await _userWeChatInfoServices.QueryByClauseAsync(p => p.openid == response.OpenId);
|
||||||
|
if (userInfo == null)
|
||||||
|
{
|
||||||
|
userInfo = new CoreCmsUserWeChatInfo
|
||||||
|
{
|
||||||
|
openid = response.OpenId,
|
||||||
|
type = (int)GlobalEnumVars.UserAccountTypes.微信公众号,
|
||||||
|
sessionKey = response.AccessToken,
|
||||||
|
gender = 1,
|
||||||
|
createTime = DateTime.Now,
|
||||||
|
unionId = response.UnionId
|
||||||
|
};
|
||||||
|
await _userWeChatInfoServices.InsertAsync(userInfo);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (userInfo.sessionKey != response.AccessToken)
|
||||||
|
{
|
||||||
|
await _userWeChatInfoServices.UpdateAsync(
|
||||||
|
p => new CoreCmsUserWeChatInfo() { sessionKey = response.AccessToken, updateTime = DateTime.Now },
|
||||||
|
p => p.openid == userInfo.openid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (userInfo is { userId: > 0 })
|
||||||
|
{
|
||||||
|
var user = await _userServices.QueryByClauseAsync(p => p.id == userInfo.userId);
|
||||||
|
if (user != null)
|
||||||
|
{
|
||||||
|
if (user.status == (int)GlobalEnumVars.UserStatus.停用)
|
||||||
|
{
|
||||||
|
jm.status = false;
|
||||||
|
jm.msg = "您的账号已经被禁用。";
|
||||||
|
return jm;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (user.isDelete == true)
|
||||||
|
{
|
||||||
|
jm.status = false;
|
||||||
|
jm.msg = "您的账号已经被禁用。";
|
||||||
|
return jm;
|
||||||
|
}
|
||||||
|
|
||||||
|
var claims = new List<Claim> {
|
||||||
|
new Claim(ClaimTypes.Name, user.nickName),
|
||||||
|
new Claim(JwtRegisteredClaimNames.Jti, user.id.ToString()),
|
||||||
|
new Claim(ClaimTypes.Expiration, DateTime.Now.AddSeconds(_permissionRequirement.Expiration.TotalSeconds).ToString(CultureInfo.InvariantCulture)) };
|
||||||
|
|
||||||
|
//用户标识
|
||||||
|
var identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme);
|
||||||
|
identity.AddClaims(claims);
|
||||||
|
jm.status = true;
|
||||||
|
jm.data = new
|
||||||
|
{
|
||||||
|
auth = JwtToken.BuildJwtToken(claims.ToArray(), _permissionRequirement),
|
||||||
|
user
|
||||||
|
};
|
||||||
|
jm.otherData = response.OpenId;
|
||||||
|
|
||||||
|
//录入登录日志
|
||||||
|
var log = new CoreCmsUserLog
|
||||||
|
{
|
||||||
|
userId = user.id,
|
||||||
|
state = (int)GlobalEnumVars.UserLogTypes.登录,
|
||||||
|
ip = _httpContextAccessor.HttpContext?.Connection.RemoteIpAddress != null ? _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.MapToIPv4().ToString() : "127.0.0.1",
|
||||||
|
createTime = DateTime.Now,
|
||||||
|
parameters = GlobalEnumVars.UserLogTypes.登录.ToString()
|
||||||
|
};
|
||||||
|
await _userLogServices.InsertAsync(log);
|
||||||
|
|
||||||
|
return jm;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//注意:生产环境下SessionKey属于敏感信息,不能进行传输!
|
||||||
|
//return new JsonResult(new { success = true, msg = "OK", sessionAuthId = sessionBag.Key, sessionKey = sessionBag.SessionKey, data = jsonResult, sessionBag = sessionBag });
|
||||||
|
jm.status = true;
|
||||||
|
jm.data = response.OpenId;
|
||||||
|
jm.otherData = response.OpenId;
|
||||||
|
//jm.methodDescription = JsonConvert.SerializeObject(sessionBag);
|
||||||
|
jm.msg = "OK";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
jm.msg = response.ErrorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
jm.msg = "数据处理异常";
|
jm.msg = "数据处理异常";
|
||||||
|
|||||||
Reference in New Issue
Block a user