mirror of
http://git.coreshop.cn/jianweie/coreshoppro.git
synced 2026-02-05 18:19:48 +08:00
【新增】实现支付宝小程序适配。
This commit is contained in:
@@ -15,8 +15,11 @@ using System.Globalization;
|
||||
using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Linq;
|
||||
using System.Security.Claims;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Aop.Api;
|
||||
using Aop.Api.Util;
|
||||
using CoreCms.Net.Auth.HttpContextUser;
|
||||
using CoreCms.Net.Auth.Policys;
|
||||
using CoreCms.Net.Caching.AccressToken;
|
||||
@@ -27,6 +30,8 @@ using CoreCms.Net.Loging;
|
||||
using CoreCms.Net.Model.Entities;
|
||||
using CoreCms.Net.Model.Entities.Expression;
|
||||
using CoreCms.Net.Model.FromBody;
|
||||
using CoreCms.Net.Model.Options;
|
||||
using CoreCms.Net.Model.ViewModels.AliPay;
|
||||
using CoreCms.Net.Model.ViewModels.DTO;
|
||||
using CoreCms.Net.Model.ViewModels.UI;
|
||||
using CoreCms.Net.Utility.Extensions;
|
||||
@@ -92,6 +97,8 @@ namespace CoreCms.Net.Web.WebApi.Controllers
|
||||
private readonly IWeChatApiHttpClientFactory _weChatApiHttpClientFactory;
|
||||
private readonly WeChatOptions _weChatOptions;
|
||||
private readonly IRedisOperationRepository _redisOperationRepository;
|
||||
private readonly ICoreCmsAliPayUserInfoServices _aliPayUserInfoServices;
|
||||
private readonly AliPayOptions _aliPayOptions;
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
@@ -122,7 +129,7 @@ namespace CoreCms.Net.Web.WebApi.Controllers
|
||||
, ICoreCmsSettingServices settingServices
|
||||
, ICoreCmsServicesServices servicesServices
|
||||
, IOptions<WeChatOptions> weChatOptions
|
||||
, ICoreCmsUserServicesOrderServices userServicesOrderServices, ICoreCmsUserServicesTicketServices userServicesTicketServices, ICoreCmsStoreServices storeServices, ICoreCmsCouponServices couponServices, ICoreCmsOrderServices orderServices, IWeChatApiHttpClientFactory weChatApiHttpClientFactory, IRedisOperationRepository redisOperationRepository)
|
||||
, ICoreCmsUserServicesOrderServices userServicesOrderServices, ICoreCmsUserServicesTicketServices userServicesTicketServices, ICoreCmsStoreServices storeServices, ICoreCmsCouponServices couponServices, ICoreCmsOrderServices orderServices, IWeChatApiHttpClientFactory weChatApiHttpClientFactory, IRedisOperationRepository redisOperationRepository, ICoreCmsAliPayUserInfoServices aliPayUserInfoServices, IOptions<AliPayOptions> aliPayOptions)
|
||||
{
|
||||
_user = user;
|
||||
_userWeChatInfoServices = userWeChatInfoServices;
|
||||
@@ -155,6 +162,8 @@ namespace CoreCms.Net.Web.WebApi.Controllers
|
||||
_orderServices = orderServices;
|
||||
_weChatApiHttpClientFactory = weChatApiHttpClientFactory;
|
||||
_redisOperationRepository = redisOperationRepository;
|
||||
_aliPayUserInfoServices = aliPayUserInfoServices;
|
||||
_aliPayOptions = aliPayOptions.Value;
|
||||
_weChatOptions = weChatOptions.Value;
|
||||
|
||||
}
|
||||
@@ -533,7 +542,7 @@ namespace CoreCms.Net.Web.WebApi.Controllers
|
||||
/// <param name="entity"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<WebApiCallBack> SmsLogin([FromBody] FMWxAccountCreate entity)
|
||||
public async Task<WebApiCallBack> SmsLogin([FromBody] FMComAccountCreate entity)
|
||||
{
|
||||
var jm = await _userServices.SmsLogin(entity, (int)GlobalEnumVars.LoginType.Sms, entity.platform);
|
||||
return jm;
|
||||
@@ -573,7 +582,7 @@ namespace CoreCms.Net.Web.WebApi.Controllers
|
||||
return jm;
|
||||
}
|
||||
|
||||
var data = new FMWxAccountCreate
|
||||
var data = new FMComAccountCreate
|
||||
{
|
||||
mobile = phoneNumber.phoneNumber,
|
||||
invitecode = entity.invitecode,
|
||||
@@ -587,6 +596,124 @@ namespace CoreCms.Net.Web.WebApi.Controllers
|
||||
|
||||
#endregion
|
||||
|
||||
#region 支付宝小程序授权拉取手机号码
|
||||
|
||||
/// <summary>
|
||||
/// 支付宝小程序授权拉取手机号码
|
||||
/// </summary>
|
||||
/// <param name="entity"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPost]
|
||||
public async Task<WebApiCallBack> DecryptPhoneNumberByAli([FromBody] FMAliLoginDecryptPhoneNumber entity)
|
||||
{
|
||||
var jm = new WebApiCallBack();
|
||||
|
||||
var userInfo = await _aliPayUserInfoServices.QueryByClauseAsync(p => p.userId == entity.sessionAuthId || p.openId == entity.sessionAuthId);
|
||||
if (userInfo == null)
|
||||
{
|
||||
jm.status = false;
|
||||
jm.msg = "支付宝用户信息获取失败";
|
||||
return jm;
|
||||
}
|
||||
|
||||
//1. 获取验签和解密所需要的参数
|
||||
var content = entity.encryptedData;
|
||||
// 是否为加密报文
|
||||
var isDataEncrypted = !content.StartsWith("{", StringComparison.Ordinal);
|
||||
var signCheckPass = false;
|
||||
//2. 验签
|
||||
var signContent = content;
|
||||
var decryptKey = _aliPayOptions.AESKey;
|
||||
// 如果是加密的报文则需要在密文的前后添加双引号
|
||||
if (isDataEncrypted)
|
||||
{
|
||||
signContent = "\"" + signContent + "\"";
|
||||
}
|
||||
try
|
||||
{
|
||||
signCheckPass = AlipaySignature.RSACheckContent(signContent, entity.sign, _aliPayOptions.AliPublicKey, "UTF-8", "RSA2", false);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//验签异常, 日志
|
||||
jm.status = false;
|
||||
jm.msg = "验签失败";
|
||||
jm.code = 500;
|
||||
jm.otherData = ex;
|
||||
NLogUtil.WriteAll(LogLevel.Error, LogType.Web, "小程序接口", "支付宝小程序授权拉取手机号码", ex);
|
||||
return jm;
|
||||
}
|
||||
if (!signCheckPass)
|
||||
{
|
||||
//验签不通过(异常或者报文被篡改),终止流程(不需要做解密)
|
||||
jm.status = false;
|
||||
jm.msg = "验签失败";
|
||||
jm.code = 500;
|
||||
jm.otherData = "验签不通过(异常或者报文被篡改),终止流程(不需要做解密) ";
|
||||
return jm;
|
||||
}
|
||||
//3. 解密
|
||||
string plainData = null;
|
||||
if (isDataEncrypted)
|
||||
{
|
||||
try
|
||||
{
|
||||
plainData = AlipayEncrypt.AesDencrypt(decryptKey, content, "UTF-8");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//解密异常, 记录日志
|
||||
jm.status = false;
|
||||
jm.msg = "解密异常";
|
||||
jm.code = 500;
|
||||
jm.otherData = ex;
|
||||
NLogUtil.WriteAll(LogLevel.Error, LogType.Web, "小程序接口", "支付宝小程序授权拉取手机号码", ex);
|
||||
return jm;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
plainData = content;
|
||||
}
|
||||
|
||||
if (!string.IsNullOrEmpty(plainData))
|
||||
{
|
||||
var obj = JsonConvert.DeserializeObject<AlipayEncryptDTO>(plainData);
|
||||
if (obj is { code: "10000", msg: "Success" } && !string.IsNullOrEmpty(obj.mobile))
|
||||
{
|
||||
jm.status = true;
|
||||
jm.msg = "获取手机号码成功";
|
||||
jm.data = obj;
|
||||
|
||||
var data = new FMComAccountCreate
|
||||
{
|
||||
mobile = obj.mobile,
|
||||
invitecode = entity.invitecode,
|
||||
sessionAuthId = entity.sessionAuthId
|
||||
};
|
||||
|
||||
jm = await _userServices.SmsLogin(data, (int)GlobalEnumVars.LoginType.AliPhoneNumber);
|
||||
}
|
||||
else
|
||||
{
|
||||
jm.status = false;
|
||||
jm.msg = "数据解码失败";
|
||||
jm.data = obj;
|
||||
jm.otherData = plainData;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
jm.status = false;
|
||||
jm.msg = "获取手机号码错误,解密失败。";
|
||||
jm.code = 500;
|
||||
}
|
||||
|
||||
return jm;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 用户短信注册并返回jwt token(弃用)======================================================
|
||||
/// <summary>
|
||||
/// 用户短信注册并返回jwt token(弃用)
|
||||
@@ -595,7 +722,7 @@ namespace CoreCms.Net.Web.WebApi.Controllers
|
||||
/// <returns></returns>
|
||||
[Obsolete]
|
||||
[HttpPost]
|
||||
public async Task<WebApiCallBack> SmsLogin2([FromBody] FMWxAccountCreate entity)
|
||||
public async Task<WebApiCallBack> SmsLogin2([FromBody] FMComAccountCreate entity)
|
||||
{
|
||||
var jm = new WebApiCallBack();
|
||||
if (!CommonHelper.IsMobile(entity.mobile))
|
||||
|
||||
Reference in New Issue
Block a user