Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

更新小程序绑定手机号功能 #44

Merged
merged 3 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>

<AbpVersion>5.1.3</AbpVersion>
<EasyAbpAbpWeChatModuleVersion>1.9.0</EasyAbpAbpWeChatModuleVersion>
<EasyAbpAbpWeChatModuleVersion>1.9.1</EasyAbpAbpWeChatModuleVersion>

</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,5 @@ public class BindPhoneNumberInput

[Required]
public string Code { get; set; }

[Required]
public string EncryptedData { get; set; }

[Required]
public string Iv { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using EasyAbp.Abp.WeChat.MiniProgram;
using EasyAbp.Abp.WeChat.MiniProgram.Infrastructure;
using EasyAbp.Abp.WeChat.MiniProgram.Infrastructure.OptionsResolve.Contributors;
using EasyAbp.Abp.WeChat.MiniProgram.Services.Login;
using EasyAbp.Abp.WeChat.MiniProgram.Services.PhoneNumber;
using EasyAbp.WeChatManagement.Common.WeChatApps;
using EasyAbp.WeChatManagement.MiniPrograms.Identity.Dtos;
using Microsoft.AspNetCore.Authorization;
Expand All @@ -17,26 +20,27 @@ namespace EasyAbp.WeChatManagement.MiniPrograms.Identity
[Authorize]
public class ProfileAppService : MiniProgramsAppService, IProfileAppService
{
private readonly LoginService _loginService;
private readonly PhoneNumberService _phoneNumberService;
private readonly IOptions<IdentityOptions> _identityOptions;
private readonly IdentityUserManager _identityUserManager;
private readonly IJsonSerializer _jsonSerializer;
private readonly IWeChatAppRepository _weChatAppRepository;
private readonly IWeChatMiniProgramAsyncLocal _weChatMiniProgramAsyncLocal;

public ProfileAppService(
LoginService loginService,
IOptions<IdentityOptions> identityOptions,
IdentityUserManager identityUserManager,
IJsonSerializer jsonSerializer,
IWeChatAppRepository weChatAppRepository)
IWeChatAppRepository weChatAppRepository,
PhoneNumberService phoneNumberService,
IWeChatMiniProgramAsyncLocal weChatMiniProgramAsyncLocal)
{
_loginService = loginService;
;
_identityOptions = identityOptions;
_identityUserManager = identityUserManager;
_jsonSerializer = jsonSerializer;
_weChatAppRepository = weChatAppRepository;
_phoneNumberService = phoneNumberService;
_weChatMiniProgramAsyncLocal = weChatMiniProgramAsyncLocal;
}

/// <summary>
/// 通过微信开放能力获取并给当前用户绑定手机号,更新信息:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/getPhoneNumber.html
/// </summary>
Expand All @@ -47,33 +51,42 @@ public ProfileAppService(
public async Task BindPhoneNumberAsync(BindPhoneNumberInput input)
{
await _identityOptions.SetAsync();

var user = await _identityUserManager.GetByIdAsync(CurrentUser.GetId());

var miniProgram = await _weChatAppRepository.GetMiniProgramAppByAppIdAsync(input.AppId);

var response = await _loginService.Code2SessionAsync(miniProgram.AppId, miniProgram.AppSecret, input.Code);

if (response.ErrorCode != 0)
var options = new AbpWeChatMiniProgramOptions
{
throw new BusinessException(message: $"WeChat error: [{response.ErrorCode}]: {response.ErrorMessage}");
}
OpenAppId = miniProgram.OpenAppIdOrName,
AppId = miniProgram.AppId,
AppSecret = miniProgram.AppSecret,
EncodingAesKey = miniProgram.EncodingAesKey,
Token = miniProgram.Token
};

var decryptedData = _jsonSerializer.Deserialize<Dictionary<string, object>>(AesHelper
.AesDecrypt(input.EncryptedData, input.Iv, response.SessionKey));
using (_weChatMiniProgramAsyncLocal.Change(options))
{
var response = await _phoneNumberService.GetPhoneNumberAsync(input.Code);

var phoneNumber = decryptedData["phoneNumber"] as string;
if (response.ErrorCode != 0)
{
throw new BusinessException(message: $"WeChat error: [{response.ErrorCode}]: {response.ErrorMessage}");
}

var phoneNumber = response.PhoneInfo.PhoneNumber;

_identityUserManager.RegisterTokenProvider(TokenOptions.DefaultPhoneProvider,
new StaticPhoneNumberTokenProvider());
_identityUserManager.RegisterTokenProvider(TokenOptions.DefaultPhoneProvider,
new StaticPhoneNumberTokenProvider());

var token = await _identityUserManager.GenerateChangePhoneNumberTokenAsync(user, phoneNumber);
var token = await _identityUserManager.GenerateChangePhoneNumberTokenAsync(user, phoneNumber);

var identityResult = await _identityUserManager.ChangePhoneNumberAsync(user, phoneNumber, token);
var identityResult = await _identityUserManager.ChangePhoneNumberAsync(user, phoneNumber, token);

if (!identityResult.Succeeded)
{
throw new AbpIdentityResultException(identityResult);
if (!identityResult.Succeeded)
{
throw new AbpIdentityResultException(identityResult);
}
}
}
}
Expand Down