From 83fb93da11ff0ee378097c1bd9a7ca75ea532c8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Sat, 9 Nov 2024 19:05:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E6=88=90=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=E5=B0=8F=E7=A8=8B=E5=BA=8F=E6=B6=88=E6=81=AF=E6=8E=A8=E9=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HttpModels/SubscribeNoticeHttpModel.cs | 71 +++++++++++++++++++ .../IWeChatMiniProgramManager.cs | 7 ++ .../WeChatMiniProgramManager.cs | 29 ++++++++ .../WeChatMiniProgramOptions.cs | 15 ++++ .../WeChatMiniProgramAccountService.cs | 4 +- .../WeChatMiniProgramNoticeEventHandler.cs | 56 +++++++++++++++ .../Etos/WeChatMiniProgramNoticeEto.cs | 7 ++ .../SuccessMiningEventHandler.cs | 26 +++++-- .../IServices/IAuthService.cs | 2 +- .../Services/Authentication/AuthService.cs | 6 +- 10 files changed, 213 insertions(+), 10 deletions(-) create mode 100644 Yi.Abp.Net8/framework/Yi.Framework.WeChat.MiniProgram/HttpModels/SubscribeNoticeHttpModel.cs create mode 100644 Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application/WeChatMiniProgramNoticeEventHandler.cs create mode 100644 Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain.Shared/Etos/WeChatMiniProgramNoticeEto.cs diff --git a/Yi.Abp.Net8/framework/Yi.Framework.WeChat.MiniProgram/HttpModels/SubscribeNoticeHttpModel.cs b/Yi.Abp.Net8/framework/Yi.Framework.WeChat.MiniProgram/HttpModels/SubscribeNoticeHttpModel.cs new file mode 100644 index 00000000..77a0f067 --- /dev/null +++ b/Yi.Abp.Net8/framework/Yi.Framework.WeChat.MiniProgram/HttpModels/SubscribeNoticeHttpModel.cs @@ -0,0 +1,71 @@ +using Yi.Framework.WeChat.MiniProgram.Abstract; + +namespace Yi.Framework.WeChat.MiniProgram.HttpModels; + +public class SubscribeNoticeRequest +{ + /// + ///用户openid,可以是小程序的openid,也可以是mp_template_msg.appid对应的公众号的openid + /// + public string touser { get; set; } + + /// + /// 小程序模板id + /// + public string template_id { get; set; } + + + + /// + /// 小程序模板消息的数据 + /// + public Dictionary data { get; set; } + + /// + /// 默认为正式版 + /// + public string miniprogram_state { get; set; } = "formal"; + + /// + /// 默认为中文 + /// + public string lang { get; set; } = "zh_CN"; +} + + +public class SubscribeNoticeInput +{ + /// + ///用户openid,可以是小程序的openid,也可以是mp_template_msg.appid对应的公众号的openid + /// + public string touser { get; set; } + + /// + /// 小程序模板id + /// + public string template_id { get; set; } + + /// + /// 公众号模板消息的数据 + /// + public Dictionary data { get; set; } +} + +public class SubscribeNoticeResponse : IErrorObjct +{ + public int errcode { get; set; } + public string errmsg { get; set; } +} + + + + +public class keyValueItem +{ + public keyValueItem(string value) + { + this.value = value; + } + + public string value { get; set; } +} \ No newline at end of file diff --git a/Yi.Abp.Net8/framework/Yi.Framework.WeChat.MiniProgram/IWeChatMiniProgramManager.cs b/Yi.Abp.Net8/framework/Yi.Framework.WeChat.MiniProgram/IWeChatMiniProgramManager.cs index 3c6d55aa..fd48e934 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.WeChat.MiniProgram/IWeChatMiniProgramManager.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.WeChat.MiniProgram/IWeChatMiniProgramManager.cs @@ -10,4 +10,11 @@ public interface IWeChatMiniProgramManager /// /// Task Code2SessionAsync(Code2SessionInput input); + + /// + /// 向用户发送订阅消息,要openid + /// + /// + /// + Task SendSubscribeNoticeAsync(SubscribeNoticeInput input); } \ No newline at end of file diff --git a/Yi.Abp.Net8/framework/Yi.Framework.WeChat.MiniProgram/WeChatMiniProgramManager.cs b/Yi.Abp.Net8/framework/Yi.Framework.WeChat.MiniProgram/WeChatMiniProgramManager.cs index c1099585..d5f5685f 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.WeChat.MiniProgram/WeChatMiniProgramManager.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.WeChat.MiniProgram/WeChatMiniProgramManager.cs @@ -1,5 +1,6 @@ using System.Net.Http.Json; using Microsoft.Extensions.Options; +using Newtonsoft.Json; using Volo.Abp.DependencyInjection; using Yi.Framework.Core.Extensions; using Yi.Framework.WeChat.MiniProgram.HttpModels; @@ -44,4 +45,32 @@ public class WeChatMiniProgramManager : IWeChatMiniProgramManager, ISingletonDep return responseBody; } } + + + + /// + /// 发送模板订阅消息 + /// + /// + public async Task SendSubscribeNoticeAsync(SubscribeNoticeInput input) + { + var token = await _weChatToken.GetTokenAsync(); + string url = $"https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token={token}"; + var req = new SubscribeNoticeRequest + { + touser = input.touser, + template_id = input.template_id, + data = input.data, + miniprogram_state = _options.Notice.State??"formal" + }; + req.template_id=req.template_id?? _options.Notice.TemplateId; + + using (HttpClient httpClient = new HttpClient()) + { + var body =new StringContent(JsonConvert.SerializeObject(req)); + HttpResponseMessage response = await httpClient.PostAsync(url, body); + var responseBody = await response.Content.ReadFromJsonAsync(); + responseBody.ValidateSuccess(); + } + } } \ No newline at end of file diff --git a/Yi.Abp.Net8/framework/Yi.Framework.WeChat.MiniProgram/WeChatMiniProgramOptions.cs b/Yi.Abp.Net8/framework/Yi.Framework.WeChat.MiniProgram/WeChatMiniProgramOptions.cs index 95ffbb53..1deb19af 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.WeChat.MiniProgram/WeChatMiniProgramOptions.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.WeChat.MiniProgram/WeChatMiniProgramOptions.cs @@ -12,4 +12,19 @@ public class WeChatMiniProgramOptions /// public string AppSecret { get; set; } + /// + /// 消息 + /// + public WeChatMiniProgramNoticeItem Notice { get; set; } + +} + +public class WeChatMiniProgramNoticeItem +{ + /// + /// 模板id + /// + public string TemplateId { get; set; } + + public string State { get; set; } } \ No newline at end of file diff --git a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application/Services/Account/WeChatMiniProgramAccountService.cs b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application/Services/Account/WeChatMiniProgramAccountService.cs index 38a689f8..2e883417 100644 --- a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application/Services/Account/WeChatMiniProgramAccountService.cs +++ b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application/Services/Account/WeChatMiniProgramAccountService.cs @@ -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) { diff --git a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application/WeChatMiniProgramNoticeEventHandler.cs b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application/WeChatMiniProgramNoticeEventHandler.cs new file mode 100644 index 00000000..14bb092d --- /dev/null +++ b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Application/WeChatMiniProgramNoticeEventHandler.cs @@ -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, 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); + } + + /// + /// 像用户发送微信消息 + /// + /// + public async Task SendAsync(string openId, string title) + { + //成功挖到矿,可以发消息给用户了 + await _weChatMiniProgramManager.SendSubscribeNoticeAsync(new SubscribeNoticeInput + { + touser = openId, + data = new Dictionary() + { + //活动名称 + { "thing9", new keyValueItem("恭喜挖到新的数字藏品") }, + + //奖品名称 + { "thing1", new keyValueItem(title) }, + + //中奖时间 + { "date5", new keyValueItem(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")) }, + + //温馨提醒 + { "thing4", new keyValueItem("点击前往小程序,可在仓库或者记录中查看") }, + } + }); + } +} \ No newline at end of file diff --git a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain.Shared/Etos/WeChatMiniProgramNoticeEto.cs b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain.Shared/Etos/WeChatMiniProgramNoticeEto.cs new file mode 100644 index 00000000..9e753340 --- /dev/null +++ b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain.Shared/Etos/WeChatMiniProgramNoticeEto.cs @@ -0,0 +1,7 @@ +namespace Yi.Framework.DigitalCollectibles.Domain.Shared.Etos; + +public class WeChatMiniProgramNoticeEto +{ + public Guid UserId { get; set; } + public String Title { get; set; } +} \ No newline at end of file diff --git a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain/EventHandlers/SuccessMiningEventHandler.cs b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain/EventHandlers/SuccessMiningEventHandler.cs index c172bf43..f62c8352 100644 --- a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain/EventHandlers/SuccessMiningEventHandler.cs +++ b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain/EventHandlers/SuccessMiningEventHandler.cs @@ -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, I private ISqlSugarRepository _repository; private readonly ISqlSugarRepository _userStoreRepository; private readonly ISqlSugarRepository _miningPoolRecordRepository; + private readonly ILocalEventBus _localEvent; + public SuccessMiningEventHandler(MiningPoolManager miningPoolManager, - ISqlSugarRepository repository, ISqlSugarRepository userStoreRepository, ISqlSugarRepository miningPoolRecordRepository) + ISqlSugarRepository repository, + ISqlSugarRepository userStoreRepository, + ISqlSugarRepository 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, I //新增全世界发现 currentCollectibles.FindTotal += 1; await _repository.UpdateAsync(currentCollectibles); - + //使用结果新增给对应的用户 await _userStoreRepository.InsertAsync(new CollectiblesUserStoreAggregateRoot { @@ -44,8 +52,16 @@ public class SuccessMiningEventHandler : ILocalEventHandler, 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); } } \ No newline at end of file diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application.Contracts/IServices/IAuthService.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application.Contracts/IServices/IAuthService.cs index 72239f33..ea386a54 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application.Contracts/IServices/IAuthService.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application.Contracts/IServices/IAuthService.cs @@ -5,6 +5,6 @@ namespace Yi.Framework.Rbac.Application.Contracts.IServices; public interface IAuthService { - Task TryGetByOpenIdAsync(string openId, string authType); + Task TryGetAuthInfoAsync(string? openId, string authType, Guid? userId = null); Task CreateAsync(AuthCreateOrUpdateInputDto input); } \ No newline at end of file diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/Authentication/AuthService.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/Authentication/AuthService.cs index d7607840..879b3c4c 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/Authentication/AuthService.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/Authentication/AuthService.cs @@ -113,9 +113,11 @@ namespace Yi.Framework.Rbac.Application.Services.Authentication return (await GetListAsync(input)).Items; } - public async Task TryGetByOpenIdAsync(string openId, string authType) + public async Task 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);