From 93dea4fa46bd870c3d20f04700b9cf250322f2b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=A9=99=E5=AD=90?= <454313500@qq.com> Date: Sun, 10 Nov 2024 13:41:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E5=BE=AE=E4=BF=A1?= =?UTF-8?q?=E9=80=9A=E7=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WeChatMiniProgramAccountService.cs | 63 ++++++++++++++----- .../WeChatMiniProgramNoticeEventHandler.cs | 61 ++++++++++++------ .../Caches/WeChatNoticeCacheItem.cs | 11 ++++ Yi.Bbs.Vue3/src/layout/AppHeader.vue | 19 +++--- .../src/views/shop/components/ShopCard.vue | 37 ++++++++++- 5 files changed, 145 insertions(+), 46 deletions(-) create mode 100644 Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain.Shared/Caches/WeChatNoticeCacheItem.cs 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 2e883417..f418e7a4 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 @@ -1,10 +1,12 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Volo.Abp.Application.Services; +using Volo.Abp.Caching; using Volo.Abp.EventBus.Local; using Volo.Abp.Users; using Yi.Framework.Bbs.Domain.Shared.Etos; using Yi.Framework.DigitalCollectibles.Application.Contracts.Dtos.Account; +using Yi.Framework.DigitalCollectibles.Domain.Shared.Caches; using Yi.Framework.DigitalCollectibles.Domain.Shared.Consts; using Yi.Framework.DigitalCollectibles.Domain.Shared.Enums; using Yi.Framework.DigitalCollectibles.Domain.Shared.Etos; @@ -25,6 +27,8 @@ public class WeChatMiniProgramAccountService : ApplicationService private readonly IAuthService _authService; private readonly IAccountService _accountService; private readonly ILocalEventBus _localEventBus; + private readonly IDistributedCache _noticeCache; + public WeChatMiniProgramAccountService(IWeChatMiniProgramManager weChatMiniProgramManager, IAuthService authService, IAccountService accountService, ILocalEventBus localEventBus) { @@ -34,6 +38,32 @@ public class WeChatMiniProgramAccountService : ApplicationService _localEventBus = localEventBus; } + /// + /// 设置用户一次性订阅状态 + /// + [HttpPut("wechat/mini-program/notice/subscribe")] + [Authorize] + public async Task PutSubscribeNoticeStateAsync() + { + var userId = CurrentUser.GetId(); + await _noticeCache.SetAsync($"MiniProgram:notice:{userId}", new WeChatNoticeCacheItem(true)); + } + + [HttpGet("wechat/mini-program/notice")] + [Authorize] + public async Task GetSubscribeNoticeStateAsync() + { + var userId = CurrentUser.GetId(); + var notice = await _noticeCache.GetAsync($"MiniProgram:notice:{userId}"); + if (notice is not null) + { + return true; + } + + return false; + } + + /// /// 使用小程序jsCode登录意社区账号 /// @@ -88,34 +118,33 @@ public class WeChatMiniProgramAccountService : ApplicationService var openId = (await _weChatMiniProgramManager.Code2SessionAsync(new Code2SessionInput(input.JsCode))).openid; //是否已经授权过绑定过auth - bool isAuthed =true; - //如果openId没有绑定过,代表第一次进入,否则就是临时账号进行绑定 - var authInfo= await _authService.TryGetAuthInfoAsync(openId,AuthTypeConst.WeChatMiniProgram); - //从来没绑定过 - if (authInfo is null) - { - isAuthed = false; - } + bool isAuthed = true; + //如果openId没有绑定过,代表第一次进入,否则就是临时账号进行绑定 + var authInfo = await _authService.TryGetAuthInfoAsync(openId, AuthTypeConst.WeChatMiniProgram); + //从来没绑定过 + if (authInfo is null) + { + isAuthed = false; + } + //账号绑定,不管什么情况,都将jscode与phone用户建立关系即可 await PostBindToAuthAsync(userInfo.User.Id, openId, userInfo.User.UserName); - + //发送账号绑定的事件,不同领域对账号数据进行迁移 //bbs:钱钱 (累加),禁用临时账号(修改) //dc: 价值、积分 (累加) - + //只有之前授权绑定过,才需要将临时账号进行账号数据转移, if (isAuthed) { await _localEventBus.PublishAsync(new BindAccountEto { NewUserId = userInfo.User.Id, - OldUserId =authInfo.UserId - },false); + OldUserId = authInfo.UserId + }, false); } - - } - + private async Task PostBindToAuthAsync(Guid userId, string openId, string? name = null) { await _authService.CreateAsync(new AuthCreateOrUpdateInputDto @@ -136,13 +165,13 @@ public class WeChatMiniProgramAccountService : ApplicationService { //先校验code,openId var openId = (await _weChatMiniProgramManager.Code2SessionAsync(new Code2SessionInput(input.JsCode))).openid; - + //走普通注册流程 //同时再加一个小程序绑定即可 var userName = GenerateRandomString(6); await _accountService.PostTempRegisterAsync(new RegisterDto { - UserName =$"ls_{userName}", + UserName = $"ls_{userName}", Password = GenerateRandomString(20), Nick = $"临时账号-{userName}" }); 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 index 14bb092d..1713cfa1 100644 --- 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 @@ -1,6 +1,9 @@ -using Volo.Abp.DependencyInjection; +using Microsoft.Extensions.Logging; +using Volo.Abp.Caching; +using Volo.Abp.DependencyInjection; using Volo.Abp.Domain.Services; using Volo.Abp.EventBus; +using Yi.Framework.DigitalCollectibles.Domain.Shared.Caches; using Yi.Framework.DigitalCollectibles.Domain.Shared.Consts; using Yi.Framework.DigitalCollectibles.Domain.Shared.Etos; using Yi.Framework.Rbac.Application.Contracts.IServices; @@ -13,18 +16,30 @@ public class WeChatMiniProgramNoticeEventHandler : ILocalEventHandler _logger; + private readonly IDistributedCache _noticeCache; public WeChatMiniProgramNoticeEventHandler(IWeChatMiniProgramManager weChatMiniProgramManager, - IAuthService authService) + IAuthService authService, ILogger logger, IDistributedCache noticeCache) { _weChatMiniProgramManager = weChatMiniProgramManager; _authService = authService; + _logger = logger; + _noticeCache = noticeCache; } public async Task HandleEventAsync(WeChatMiniProgramNoticeEto eventData) { - var authInfo = await _authService.TryGetAuthInfoAsync(null, AuthTypeConst.WeChatMiniProgram, eventData.UserId); - await SendAsync(authInfo.OpenId, eventData.Title); + //需要判断该用户是否已经订阅 + var noticeCache= await _noticeCache.GetAsync($"MiniProgram:notice:{eventData.UserId}"); + //判断用户是否点击了一次性消息订阅 + if (noticeCache is not null) + { + var authInfo = await _authService.TryGetAuthInfoAsync(null, AuthTypeConst.WeChatMiniProgram, eventData.UserId); + await SendAsync(authInfo.OpenId, eventData.Title); + //发送完成之后,删除缓存 + await _noticeCache.RemoveAsync($"MiniProgram:notice:{eventData.UserId}"); + } + } /// @@ -33,24 +48,32 @@ public class WeChatMiniProgramNoticeEventHandler : ILocalEventHandler public async Task SendAsync(string openId, string title) { - //成功挖到矿,可以发消息给用户了 - await _weChatMiniProgramManager.SendSubscribeNoticeAsync(new SubscribeNoticeInput + try { - touser = openId, - data = new Dictionary() + //成功挖到矿,可以发消息给用户了 + await _weChatMiniProgramManager.SendSubscribeNoticeAsync(new SubscribeNoticeInput { - //活动名称 - { "thing9", new keyValueItem("恭喜挖到新的数字藏品") }, + touser = openId, + data = new Dictionary() + { + //活动名称 + { "thing9", new keyValueItem("恭喜挖到新的数字藏品") }, - //奖品名称 - { "thing1", new keyValueItem(title) }, + //奖品名称 + { "thing1", new keyValueItem(title) }, - //中奖时间 - { "date5", new keyValueItem(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")) }, + //中奖时间 + { "date5", new keyValueItem(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")) }, - //温馨提醒 - { "thing4", new keyValueItem("点击前往小程序,可在仓库或者记录中查看") }, - } - }); + //温馨提醒 + { "thing4", new keyValueItem("点击前往小程序,可在仓库或者记录中查看") }, + } + }); + } + catch (Exception e) + { + _logger.LogError($"微信通知提醒失败,错误信息:{e.Message},堆栈:{e.InnerException}"); + } + } } \ No newline at end of file diff --git a/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain.Shared/Caches/WeChatNoticeCacheItem.cs b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain.Shared/Caches/WeChatNoticeCacheItem.cs new file mode 100644 index 00000000..445e89a4 --- /dev/null +++ b/Yi.Abp.Net8/module/digital-collectibles/Yi.Framework.DigitalCollectibles.Domain.Shared/Caches/WeChatNoticeCacheItem.cs @@ -0,0 +1,11 @@ +namespace Yi.Framework.DigitalCollectibles.Domain.Shared.Caches; + +public class WeChatNoticeCacheItem +{ + public WeChatNoticeCacheItem(bool isSubscribe) + { + IsSubscribe = isSubscribe; + } + + public bool IsSubscribe { get; set; } +} \ No newline at end of file diff --git a/Yi.Bbs.Vue3/src/layout/AppHeader.vue b/Yi.Bbs.Vue3/src/layout/AppHeader.vue index f0f61f5f..3fd8cdb8 100644 --- a/Yi.Bbs.Vue3/src/layout/AppHeader.vue +++ b/Yi.Bbs.Vue3/src/layout/AppHeader.vue @@ -15,12 +15,14 @@ style="color: red;font-weight: bolder;font-size: large;">开始 数字藏品 - - - 前端 - 后端 - 运维 - + 商城 + + + + + + @@ -232,7 +234,10 @@ const enterStart = () => { } const enterWatermelon=()=>{ - alert("即将发布,敬请期待~") + alert("真即将发布,敬请期待~") +} +const enterShop=()=>{ + alert("真即将发布,敬请期待~") } diff --git a/Yi.Bbs.Vue3/src/views/shop/components/ShopCard.vue b/Yi.Bbs.Vue3/src/views/shop/components/ShopCard.vue index f62cba78..06ce6574 100644 --- a/Yi.Bbs.Vue3/src/views/shop/components/ShopCard.vue +++ b/Yi.Bbs.Vue3/src/views/shop/components/ShopCard.vue @@ -1,5 +1,6 @@  -