From f64b6be332be68497594393d9eb6d16551f75223 Mon Sep 17 00:00:00 2001
From: xiaoyong931011 <15274802129@163.com>
Date: Mon, 17 Jul 2023 17:08:30 +0800
Subject: [PATCH] 用户登录获取用户的微信昵称和头像
---
src/main/java/cc/mrbird/febs/rabbit/enumerates/RabbitQueueEnum.java | 1
src/main/java/cc/mrbird/febs/common/configure/RabbitConfigure.java | 18 +++++++++
src/main/java/cc/mrbird/febs/rabbit/consumer/AgentConsumer.java | 10 +++++
src/main/java/cc/mrbird/febs/mall/service/impl/AgentServiceImpl.java | 33 ++++++++++++++++
src/main/java/cc/mrbird/febs/rabbit/constants/QueueConstants.java | 1
src/main/java/cc/mrbird/febs/rabbit/producter/AgentProducer.java | 5 ++
src/main/java/cc/mrbird/febs/mall/service/IAgentService.java | 2 +
src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallMemberServiceImpl.java | 1
8 files changed, 71 insertions(+), 0 deletions(-)
diff --git a/src/main/java/cc/mrbird/febs/common/configure/RabbitConfigure.java b/src/main/java/cc/mrbird/febs/common/configure/RabbitConfigure.java
index afddeb0..42e4064 100644
--- a/src/main/java/cc/mrbird/febs/common/configure/RabbitConfigure.java
+++ b/src/main/java/cc/mrbird/febs/common/configure/RabbitConfigure.java
@@ -158,4 +158,22 @@
}
/** 新用户注册 end**/
+
+ /** 用户信息 start**/
+ @Bean
+ public DirectExchange memberInfoExchange() {
+ return new DirectExchange(RabbitQueueEnum.YWG_MEMBER_INFO.getExchange());
+ }
+
+ @Bean
+ public Queue memberInfoQueue() {
+ return new Queue(RabbitQueueEnum.YWG_MEMBER_INFO.getQueue());
+ }
+
+ @Bean
+ public Binding memberInfoBind() {
+ return BindingBuilder.bind(memberInfoQueue()).to(memberInfoExchange()).with(RabbitQueueEnum.YWG_MEMBER_INFO.getRoute());
+ }
+ /** 新用户注册 end**/
+
}
diff --git a/src/main/java/cc/mrbird/febs/mall/service/IAgentService.java b/src/main/java/cc/mrbird/febs/mall/service/IAgentService.java
index a9a7103..875e530 100644
--- a/src/main/java/cc/mrbird/febs/mall/service/IAgentService.java
+++ b/src/main/java/cc/mrbird/febs/mall/service/IAgentService.java
@@ -11,4 +11,6 @@
void addMember(String memberOpenId);
void addMemberScan(String str);
+
+ void updateMemberInfo(String str);
}
diff --git a/src/main/java/cc/mrbird/febs/mall/service/impl/AgentServiceImpl.java b/src/main/java/cc/mrbird/febs/mall/service/impl/AgentServiceImpl.java
index 694cdee..a5baa88 100644
--- a/src/main/java/cc/mrbird/febs/mall/service/impl/AgentServiceImpl.java
+++ b/src/main/java/cc/mrbird/febs/mall/service/impl/AgentServiceImpl.java
@@ -5,6 +5,7 @@
import cc.mrbird.febs.common.enumerates.MoneyFlowTypeEnum;
import cc.mrbird.febs.common.exception.FebsException;
import cc.mrbird.febs.common.utils.AppContants;
+import cc.mrbird.febs.common.utils.HttpCurlUtil;
import cc.mrbird.febs.common.utils.ShareCodeUtil;
import cc.mrbird.febs.mall.entity.*;
import cc.mrbird.febs.mall.mapper.*;
@@ -24,6 +25,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
+import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
@@ -283,4 +285,35 @@
mallMemberWalletMapper.insert(wallet);
return;
}
+
+ @Override
+ public void updateMemberInfo(String str) {
+ String[] split = StrUtil.split(str, "-");
+ String accessToken = split[0];
+ String openId = split[1];
+ MallMember mallMember = memberMapper.selectMemberByOpenId(openId);
+ if (ObjectUtil.isNotEmpty(mallMember)) {
+ return;
+ }
+ String requrl = "https://api.weixin.qq.com/sns/userinfo?access_token="+accessToken+"&openid="+openId+"&lang=zh_CN";
+ String reslutData = null;
+ try {
+ reslutData = HttpCurlUtil.sendGetHttp(requrl, null);
+ } catch (IOException e) {
+ e.printStackTrace();
+ return;
+ }
+ net.sf.json.JSONObject json = net.sf.json.JSONObject.fromObject(reslutData);
+ log.info("微信获取用户信息={}", json);
+
+ if (json.containsKey("errcode")) {
+ log.info("微信登录获取到异常信息errcode");
+ return;
+ }
+ String nickname = json.getString("nickname");
+ String headImgUrl = json.getString("headimgurl");
+ mallMember.setName(nickname);
+ mallMember.setAvatar(headImgUrl);
+ memberMapper.updateById(mallMember);
+ }
}
diff --git a/src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallMemberServiceImpl.java b/src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallMemberServiceImpl.java
index 9d7d7bf..cf063cf 100644
--- a/src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallMemberServiceImpl.java
+++ b/src/main/java/cc/mrbird/febs/mall/service/impl/ApiMallMemberServiceImpl.java
@@ -621,6 +621,7 @@
authInfo.put("rasToken", generateAsaToken(token));
authInfo.put("accessToken",accessToken);
authInfo.put("openId",openId);
+ agentProducer.sendMemberInfoMsg(accessToken+"-"+openId);
febsResponse.success().data(authInfo);
} else {
return febsResponse.fail().message("自动登录失败");
diff --git a/src/main/java/cc/mrbird/febs/rabbit/constants/QueueConstants.java b/src/main/java/cc/mrbird/febs/rabbit/constants/QueueConstants.java
index 6f420fe..a8fd333 100644
--- a/src/main/java/cc/mrbird/febs/rabbit/constants/QueueConstants.java
+++ b/src/main/java/cc/mrbird/febs/rabbit/constants/QueueConstants.java
@@ -15,4 +15,5 @@
*/
public static final String YWG_MEMBER_SUB = "queue_ywg_member_sub";
public static final String YWG_MEMBER_SUB_SCAN = "queue_ywg_member_sub_scan";
+ public static final String YWG_MEMBER_INFO = "queue_ywg_member_info";
}
diff --git a/src/main/java/cc/mrbird/febs/rabbit/consumer/AgentConsumer.java b/src/main/java/cc/mrbird/febs/rabbit/consumer/AgentConsumer.java
index 474f46f..6026574 100644
--- a/src/main/java/cc/mrbird/febs/rabbit/consumer/AgentConsumer.java
+++ b/src/main/java/cc/mrbird/febs/rabbit/consumer/AgentConsumer.java
@@ -92,4 +92,14 @@
log.error("收到新增用户扫码:", e);
}
}
+
+ @RabbitListener(queues = QueueConstants.YWG_MEMBER_INFO)
+ public void getMemberInfoMsg(String str) {
+ log.info("收到用户信息:{}", str);
+ try {
+ agentService.updateMemberInfo(str);
+ } catch (Exception e) {
+ log.error("收到用户信息:", e);
+ }
+ }
}
diff --git a/src/main/java/cc/mrbird/febs/rabbit/enumerates/RabbitQueueEnum.java b/src/main/java/cc/mrbird/febs/rabbit/enumerates/RabbitQueueEnum.java
index 3149d76..a46e1a5 100644
--- a/src/main/java/cc/mrbird/febs/rabbit/enumerates/RabbitQueueEnum.java
+++ b/src/main/java/cc/mrbird/febs/rabbit/enumerates/RabbitQueueEnum.java
@@ -6,6 +6,7 @@
@Getter
public enum RabbitQueueEnum {
+ YWG_MEMBER_INFO("exchange_ywg_member_info", "route_key_ywg_member_info",QueueConstants.YWG_MEMBER_INFO),
YWG_MEMBER_SUB_SCAN("exchange_ywg_member_sub_scan", "route_key_ywg_member_sub_scan",QueueConstants.YWG_MEMBER_SUB_SCAN),
YWG_MEMBER_SUB("exchange_ywg_member_sub", "route_key_ywg_member_sub",QueueConstants.YWG_MEMBER_SUB),
diff --git a/src/main/java/cc/mrbird/febs/rabbit/producter/AgentProducer.java b/src/main/java/cc/mrbird/febs/rabbit/producter/AgentProducer.java
index f68e238..d4832e7 100644
--- a/src/main/java/cc/mrbird/febs/rabbit/producter/AgentProducer.java
+++ b/src/main/java/cc/mrbird/febs/rabbit/producter/AgentProducer.java
@@ -86,4 +86,9 @@
log.info("新增用户扫码:{}", str);
rabbitTemplate.convertAndSend(RabbitQueueEnum.YWG_MEMBER_SUB_SCAN.getExchange(), RabbitQueueEnum.YWG_MEMBER_SUB_SCAN.getRoute(), str);
}
+
+ public void sendMemberInfoMsg(String str) {
+ log.info("更新用户信息:{}", str);
+ rabbitTemplate.convertAndSend(RabbitQueueEnum.YWG_MEMBER_INFO.getExchange(), RabbitQueueEnum.YWG_MEMBER_INFO.getRoute(), str);
+ }
}
--
Gitblit v1.9.1