feat: 完成微信小程序消息推送

This commit is contained in:
橙子
2024-11-09 19:05:42 +08:00
parent 3fbaffe9a2
commit 83fb93da11
10 changed files with 213 additions and 10 deletions

View File

@@ -49,7 +49,7 @@ public class WeChatMiniProgramAccountService : ApplicationService
var openId = (await _weChatMiniProgramManager.Code2SessionAsync(new Code2SessionInput(intput.JsCode)))
.openid;
var authInfo = await _authService.TryGetByOpenIdAsync(openId, AuthTypeConst.WeChatMiniProgram);
var authInfo = await _authService.TryGetAuthInfoAsync(openId, AuthTypeConst.WeChatMiniProgram);
if (authInfo is null)
{
throw new UserFriendlyException("该小程序没有绑定任何账号", "2000", "Auth未找到对应关系");
@@ -90,7 +90,7 @@ public class WeChatMiniProgramAccountService : ApplicationService
//是否已经授权过绑定过auth
bool isAuthed =true;
//如果openId没有绑定过代表第一次进入否则就是临时账号进行绑定
var authInfo= await _authService.TryGetByOpenIdAsync(openId,AuthTypeConst.WeChatMiniProgram);
var authInfo= await _authService.TryGetAuthInfoAsync(openId,AuthTypeConst.WeChatMiniProgram);
//从来没绑定过
if (authInfo is null)
{

View File

@@ -0,0 +1,56 @@
using Volo.Abp.DependencyInjection;
using Volo.Abp.Domain.Services;
using Volo.Abp.EventBus;
using Yi.Framework.DigitalCollectibles.Domain.Shared.Consts;
using Yi.Framework.DigitalCollectibles.Domain.Shared.Etos;
using Yi.Framework.Rbac.Application.Contracts.IServices;
using Yi.Framework.WeChat.MiniProgram;
using Yi.Framework.WeChat.MiniProgram.HttpModels;
namespace Yi.Framework.DigitalCollectibles.Domain.Managers;
public class WeChatMiniProgramNoticeEventHandler : ILocalEventHandler<WeChatMiniProgramNoticeEto>, ITransientDependency
{
private readonly IWeChatMiniProgramManager _weChatMiniProgramManager;
private readonly IAuthService _authService;
public WeChatMiniProgramNoticeEventHandler(IWeChatMiniProgramManager weChatMiniProgramManager,
IAuthService authService)
{
_weChatMiniProgramManager = weChatMiniProgramManager;
_authService = authService;
}
public async Task HandleEventAsync(WeChatMiniProgramNoticeEto eventData)
{
var authInfo = await _authService.TryGetAuthInfoAsync(null, AuthTypeConst.WeChatMiniProgram, eventData.UserId);
await SendAsync(authInfo.OpenId, eventData.Title);
}
/// <summary>
/// 像用户发送微信消息
/// </summary>
/// <param name="userId"></param>
public async Task SendAsync(string openId, string title)
{
//成功挖到矿,可以发消息给用户了
await _weChatMiniProgramManager.SendSubscribeNoticeAsync(new SubscribeNoticeInput
{
touser = openId,
data = new Dictionary<string, keyValueItem>()
{
//活动名称
{ "thing9", new keyValueItem("恭喜挖到新的数字藏品") },
//奖品名称
{ "thing1", new keyValueItem(title) },
//中奖时间
{ "date5", new keyValueItem(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")) },
//温馨提醒
{ "thing4", new keyValueItem("点击前往小程序,可在仓库或者记录中查看") },
}
});
}
}

View File

@@ -0,0 +1,7 @@
namespace Yi.Framework.DigitalCollectibles.Domain.Shared.Etos;
public class WeChatMiniProgramNoticeEto
{
public Guid UserId { get; set; }
public String Title { get; set; }
}

View File

@@ -1,11 +1,14 @@
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus;
using Volo.Abp.EventBus.Local;
using Yi.Framework.DigitalCollectibles.Domain.Entities;
using Yi.Framework.DigitalCollectibles.Domain.Entities.Record;
using Yi.Framework.DigitalCollectibles.Domain.Managers;
using Yi.Framework.DigitalCollectibles.Domain.Shared.Consts;
using Yi.Framework.DigitalCollectibles.Domain.Shared.Etos;
using Yi.Framework.SqlSugarCore.Abstractions;
using Yi.Framework.WeChat.MiniProgram;
using Yi.Framework.WeChat.MiniProgram.HttpModels;
namespace Yi.Framework.DigitalCollectibles.Domain.EventHandlers;
@@ -18,13 +21,18 @@ public class SuccessMiningEventHandler : ILocalEventHandler<SuccessMiningEto>, I
private ISqlSugarRepository<CollectiblesAggregateRoot> _repository;
private readonly ISqlSugarRepository<CollectiblesUserStoreAggregateRoot> _userStoreRepository;
private readonly ISqlSugarRepository<MiningPoolRecordAggregateRoot> _miningPoolRecordRepository;
private readonly ILocalEventBus _localEvent;
public SuccessMiningEventHandler(MiningPoolManager miningPoolManager,
ISqlSugarRepository<CollectiblesAggregateRoot> repository, ISqlSugarRepository<CollectiblesUserStoreAggregateRoot> userStoreRepository, ISqlSugarRepository<MiningPoolRecordAggregateRoot> miningPoolRecordRepository)
ISqlSugarRepository<CollectiblesAggregateRoot> repository,
ISqlSugarRepository<CollectiblesUserStoreAggregateRoot> userStoreRepository,
ISqlSugarRepository<MiningPoolRecordAggregateRoot> miningPoolRecordRepository, ILocalEventBus localEvent)
{
_miningPoolManager = miningPoolManager;
_repository = repository;
_userStoreRepository = userStoreRepository;
_miningPoolRecordRepository = miningPoolRecordRepository;
_localEvent = localEvent;
}
public async Task HandleEventAsync(SuccessMiningEto eventData)
@@ -36,7 +44,7 @@ public class SuccessMiningEventHandler : ILocalEventHandler<SuccessMiningEto>, I
//新增全世界发现
currentCollectibles.FindTotal += 1;
await _repository.UpdateAsync(currentCollectibles);
//使用结果新增给对应的用户
await _userStoreRepository.InsertAsync(new CollectiblesUserStoreAggregateRoot
{
@@ -44,8 +52,16 @@ public class SuccessMiningEventHandler : ILocalEventHandler<SuccessMiningEto>, I
CollectiblesId = eventData.CollectiblesId,
IsRead = false
});
//新增一条挖矿记录
await _miningPoolRecordRepository.InsertAsync(new MiningPoolRecordAggregateRoot(eventData.UserId,eventData.CollectiblesId));
await _miningPoolRecordRepository.InsertAsync(
new MiningPoolRecordAggregateRoot(eventData.UserId, eventData.CollectiblesId));
//给挖到矿的用户,发送微信小程序通知
await _localEvent.PublishAsync(new WeChatMiniProgramNoticeEto
{
UserId = eventData.UserId,
Title = $"{currentCollectibles.Rarity.GetRarityName()}-{currentCollectibles.Name}"
},false);
}
}

View File

@@ -5,6 +5,6 @@ namespace Yi.Framework.Rbac.Application.Contracts.IServices;
public interface IAuthService
{
Task<AuthOutputDto?> TryGetByOpenIdAsync(string openId, string authType);
Task<AuthOutputDto?> TryGetAuthInfoAsync(string? openId, string authType, Guid? userId = null);
Task<AuthOutputDto> CreateAsync(AuthCreateOrUpdateInputDto input);
}

View File

@@ -113,9 +113,11 @@ namespace Yi.Framework.Rbac.Application.Services.Authentication
return (await GetListAsync(input)).Items;
}
public async Task<AuthOutputDto?> TryGetByOpenIdAsync(string openId, string authType)
public async Task<AuthOutputDto?> TryGetAuthInfoAsync(string? openId, string authType,Guid? userId=null)
{
var entity = await _repository._DbQueryable.Where(x => x.OpenId == openId)
var entity = await _repository._DbQueryable
.WhereIF(openId is not null, x => x.OpenId == openId)
.WhereIF(userId is not null,x => x.UserId == userId)
.Where(x => x.AuthType == authType)
.FirstAsync();
var output = await MapToGetOutputDtoAsync(entity);