From 4c513094abfb03afa78f0b6aeab0de7f80f3e816 Mon Sep 17 00:00:00 2001 From: zhongzb <972627721@qq.com> Date: Wed, 10 May 2023 14:07:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8D=E5=AD=97=E9=87=8D=E5=A4=8Dbug?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../event/listener/UserRegisterListener.java | 2 +- .../custom/user/service/WxMsgService.java | 23 ++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/common/event/listener/UserRegisterListener.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/common/event/listener/UserRegisterListener.java index 4f3c32c..450611a 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/common/event/listener/UserRegisterListener.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/common/event/listener/UserRegisterListener.java @@ -36,7 +36,7 @@ public class UserRegisterListener { @Async @EventListener(classes = UserRegisterEvent.class) - public void sendBadge(UserOnlineEvent event) { + public void sendBadge(UserRegisterEvent event) { User user = event.getUser(); int count = userDao.count(); if (count <= 10) { diff --git a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/WxMsgService.java b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/WxMsgService.java index 9d2c196..207f964 100644 --- a/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/WxMsgService.java +++ b/mallchat-custom-server/src/main/java/com/abin/mallchat/custom/user/service/WxMsgService.java @@ -1,6 +1,7 @@ package com.abin.mallchat.custom.user.service; import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.StrUtil; import com.abin.mallchat.common.common.config.ThreadPoolConfig; import com.abin.mallchat.common.user.dao.UserDao; @@ -17,6 +18,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Lazy; +import org.springframework.dao.DuplicateKeyException; import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.stereotype.Service; @@ -70,7 +72,7 @@ public class WxMsgService { //保存openid和场景code的关系,后续才能通知到前端 OPENID_EVENT_CODE_MAP.put(fromUser, eventKey); //授权流程,给用户发送授权消息,并且异步通知前端扫码成功 - threadPoolTaskExecutor.execute(()->webSocketService.scanSuccess(eventKey)); + threadPoolTaskExecutor.execute(() -> webSocketService.scanSuccess(eventKey)); String skipUrl = String.format(URL, wxMpService.getWxMpConfigStorage().getAppId(), URLEncoder.encode(callback + "/wx/portal/public/callBack")); WxMpXmlOutMessage.TEXT().build(); return new TextBuilder().build("请点击链接授权:登录", wxMpXmlMessage, wxMpService); @@ -88,14 +90,29 @@ public class WxMsgService { */ public void authorize(WxOAuth2UserInfo userInfo) { User user = userDao.getByOpenId(userInfo.getOpenid()); - User update = UserAdapter.buildAuthorizeUser(user.getId(), userInfo); //更新用户信息 - userDao.updateById(update); + fillUserInfo(user.getId(), userInfo); //触发用户登录成功操作 Integer eventKey = OPENID_EVENT_CODE_MAP.get(userInfo.getOpenid()); login(user.getId(), eventKey); } + private void fillUserInfo(Long uid, WxOAuth2UserInfo userInfo) { + User update = UserAdapter.buildAuthorizeUser(uid, userInfo); + for (int i = 0; i < 5; i++) { + try { + userDao.updateById(update); + update.setName("名字重置" + RandomUtil.randomInt(100000)); + return; + } catch (DuplicateKeyException e) { + log.info("fill userInfo duplicate uid:{},info:{}", uid, userInfo); + } catch (Exception e) { + log.error("fill userInfo fail uid:{},info:{}", uid, userInfo); + } + } + + } + private void login(Long uid, Integer eventKey) { User user = userDao.getById(uid); //调用用户登录模块