Browse Source

fix:扫码添加分销人员

wrj 1 year ago
parent
commit
21d0781ac2

+ 33 - 31
nightFragrance-admin/src/main/java/com/ylx/web/controller/massage/WeChatController.java

@@ -25,6 +25,7 @@ import com.ylx.massage.service.CouponReceiveService;
 import com.ylx.massage.service.TWxUserService;
 import com.ylx.massage.service.TbFileService;
 import com.ylx.massage.utils.DateTimeUtils;
+import com.ylx.massage.utils.StringUtilsMassage;
 import com.ylx.massage.utils.WeChatUtil;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
@@ -69,11 +70,7 @@ public class WeChatController extends BaseController {
      */
     private String IMG_PATH = "D:\\Users\\";
 
-//    private final WeChatUserService weChatUserService;
 
-    //    public WeChatController(WeChatUserService weChatUserService) {
-//        this.weChatUserService = weChatUserService;
-//    }
     @Resource
     private WeChatUtil weChatUtil;
 
@@ -145,6 +142,7 @@ public class WeChatController extends BaseController {
 
     /**
      * 处理微信公众号请求信息
+     *
      * @param request
      * @return
      */
@@ -162,12 +160,12 @@ public class WeChatController extends BaseController {
             isr = new InputStreamReader(is, "utf-8");
             br = new BufferedReader(isr);
             String str = null;
-            StringBuffer returnXml= new StringBuffer();
+            StringBuffer returnXml = new StringBuffer();
             while ((str = br.readLine()) != null) {
                 //返回的是xml数据
                 returnXml.append(str);
             }
-            log.info("微信端返回的xml数据:{}", returnXml.toString());
+            log.info("微信端返回的xml数据:{}", returnXml);
             Map<String, String> encryptMap = WeChatUtil.xmlToMap(returnXml.toString());
             // 得到公众号传来的加密信息并解密,得到的是明文xml数据
 //            String decryptXml = WXPublicUtils.decrypt(encryptMap.get("Encrypt"));
@@ -197,27 +195,11 @@ public class WeChatController extends BaseController {
                 // 区分事件推送
                 String event = encryptMap.get("Event");
                 if ("subscribe".equals(event)) { // 订阅事件 或 未关注扫描二维码事件
-                    // 返回消息时ToUserName的值与FromUserName的互换
-                    Map<String, String> returnMap = new HashMap<>();
-                    returnMap.put("ToUserName", encryptMap.get("FromUserName"));
-                    returnMap.put("FromUserName", encryptMap.get("ToUserName"));
-                    returnMap.put("CreateTime", new Date().getTime()+"");
-                    returnMap.put("MsgType", "text");
-                    returnMap.put("Content", "https://www.baidu.com");
-                    String encryptMsg = weChatUtil.mapToXml(returnMap).toString();
-                    return encryptMsg;
-                }  else if ("unsubscribe".equals(event)) { // 取消订阅事件
+                    return getString(encryptMap);
+                } else if ("unsubscribe".equals(event)) { // 取消订阅事件
                     // todo 处理取消订阅事件
                 } else if ("SCAN".equals(event)) { // 已关注扫描二维码事件
-                    // 返回消息时ToUserName的值与FromUserName的互换
-                    Map<String, String> returnMap = new HashMap<>();
-                    returnMap.put("ToUserName", encryptMap.get("FromUserName"));
-                    returnMap.put("FromUserName", encryptMap.get("ToUserName"));
-                    returnMap.put("CreateTime", new Date().getTime()+"");
-                    returnMap.put("MsgType", "text");
-                    returnMap.put("Content", "https://www.baidu.com");
-                    String encryptMsg = WeChatUtil.mapToXml(returnMap).toString();
-                    return encryptMsg;
+                    return getString(encryptMap);
                 } else if ("LOCATION".equals(event)) { // 上报地理位置事件
                     // todo 处理上报地理位置事件
                 } else if ("CLICK".equals(event)) { // 点击菜单拉取消息时的事件推送事件
@@ -241,6 +223,28 @@ public class WeChatController extends BaseController {
         }
         return null;
     }
+
+    private String getString(Map<String, String> encryptMap) throws Exception {
+        // 返回消息时ToUserName的值与FromUserName的互换
+        Map<String, String> returnMap = new HashMap<>();
+
+        //添加新用户
+        TWxUser fromUserName = wxUserService.getByOpenId(encryptMap.get("FromUserName"));
+        if (fromUserName == null) {
+            fromUserName = new TWxUser();
+        }
+        fromUserName.setcOpenid(encryptMap.get("FromUserName"));
+        fromUserName.setcUpUser(StringUtilsMassage.afterString(encryptMap.get("Encrypt"), "qrscene_"));
+        wxUserService.saveOrUpdate(fromUserName);
+
+        returnMap.put("ToUserName", encryptMap.get("FromUserName"));
+        returnMap.put("FromUserName", encryptMap.get("ToUserName"));
+        returnMap.put("CreateTime", new Date().getTime() + "");
+        returnMap.put("MsgType", "text");
+        returnMap.put("Content", "欢迎来到舒压乐园");
+        String encryptMsg = weChatUtil.mapToXml(returnMap).toString();
+        return encryptMsg;
+    }
     /**
      * 获取微信code
      *
@@ -284,7 +288,7 @@ public class WeChatController extends BaseController {
         LambdaQueryWrapper<TWxUser> objectLambdaQueryWrapper = new LambdaQueryWrapper<>();
         objectLambdaQueryWrapper.eq(TWxUser::getcOpenid, openid);
         TWxUser user = wxUserService.getOne(objectLambdaQueryWrapper);
-        if (user == null) {
+        if (user == null || user.getcNickName() == null) {
             user = new TWxUser();
             user.setcOpenid(openid);
             user.setcNickName(jsonObject.get("nickname").toString());
@@ -339,14 +343,12 @@ public class WeChatController extends BaseController {
 
     @ApiOperation("获取公众号二维码")
     @RequestMapping(value = "getwxQrCode", method = RequestMethod.GET)
-    public String getWxQrCodeUtil(@RequestParam String openId) {
+    public Map<?, ?> getWxQrCodeUtil(@RequestParam String openId) {
 
         //获取access_token
         String token = weChatUtil.getToken();
         //获取的二维码ticket
-        Map<?, ?> url = weChatUtil.getUrl(token, openId);
-        return url.toString();
-
+        return weChatUtil.getUrl(token, openId);
     }
 
     @ApiOperation("获取公众号网页二维码")
@@ -356,7 +358,7 @@ public class WeChatController extends BaseController {
         // 设置边距,即二维码和背景之间的边距
         config.setMargin(1);
         // 生成二维码到文件,也可以到流
-        String code = "https://www.baidu.com?openId="+openId;
+        String code = "https://www.baidu.com?openId=" + openId;
         log.info("code:{}", code);
         String str = IMG_PATH;
         File generate = QrCodeUtil.generate(code, config, FileUtil.file(RuoYiConfig.getUploadPath() + "/code.png"));

+ 39 - 0
nightFragrance-massage/src/main/java/com/ylx/massage/utils/StringUtilsMassage.java

@@ -0,0 +1,39 @@
+package com.ylx.massage.utils;
+
+import com.ylx.common.utils.StringUtils;
+
+/**
+ * @author jianlong
+ * @date 2024-06-14 10:40
+ */
+public class StringUtilsMassage {
+
+
+    public static String afterChart(String str, String targetChar) {
+        int index = str.indexOf(targetChar);
+        if (index != -1) {
+            return str.substring(index + 1);  // 输出: World!
+        } else {
+            return StringUtils.EMPTY;
+        }
+    }
+
+
+    public static String afterString(String originalString,String delimiter) {
+
+        // 查找delimiter的位置
+        int index = originalString.indexOf(delimiter);
+        if (index != -1) { // 如果找到了分隔符
+            // 分隔符后面的字符串从index + delimiter.length()开始
+            return originalString.substring(index + delimiter.length());
+        } else {
+            return originalString;
+        }
+    }
+
+    public static void main(String[] args) {
+        String s = afterString("o-HEJ6RAjBDjFvuZcAdHRX8mIZXw", "qrscene_");
+        System.out.println(s);
+    }
+
+}

+ 2 - 0
nightFragrance-massage/src/main/java/com/ylx/massage/utils/WeChatUtil.java

@@ -9,6 +9,7 @@ import cn.hutool.http.HttpUtil;
 import com.alibaba.fastjson2.JSONObject;
 import com.ylx.common.config.WechatAccountConfig;
 import com.ylx.common.exception.ServiceException;
+import com.ylx.common.utils.StringUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.stereotype.Component;
 import org.w3c.dom.Node;
@@ -263,4 +264,5 @@ public class WeChatUtil {
         return output;
     }
 
+
 }