Pārlūkot izejas kodu

微信公众号关注,取关回调

小么熊🐻 1 gadu atpakaļ
vecāks
revīzija
dc5ccda677

+ 97 - 0
nbjk-admin/src/main/java/com/miaxis/app/controller/wx/WxgzhController.java

@@ -0,0 +1,97 @@
+package com.miaxis.app.controller.wx;
+
+import com.alibaba.fastjson.JSONObject;
+import com.miaxis.common.constant.Constants;
+import com.miaxis.common.core.domain.Response;
+import com.miaxis.common.utils.wx.SignUtil;
+import com.miaxis.wx.service.IWxGzhService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.RequiredArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.servlet.http.HttpServletRequest;
+
+/**
+ * 微信公众号相关
+ * @author wwl
+ * @version 1.0
+ * @date 2021/10/26 9:28
+ */
+@RestController
+@RequiredArgsConstructor
+@RequestMapping(Constants.OPEN_PREFIX + "/wx/gzh")
+@Api(tags={"【H5-wx微信公众号相关】"})
+@Slf4j
+public class WxgzhController {
+
+    private final IWxGzhService wxGzhService;
+
+
+    private final HttpServletRequest request;
+
+
+
+
+    /**
+     * 获取微信验签
+     * @param request
+     * @return
+     */
+    @GetMapping(value = "/notifyMsg")
+    @ApiOperation("获取微信验签")
+    public String checkSign(HttpServletRequest request) {
+        log.info("-----------进入---------");
+        try {
+            String signature = request.getParameter("signature");
+            String timestamp = request.getParameter("timestamp");
+            String nonce = request.getParameter("nonce");
+            String echostr = request.getParameter("echostr");
+            log.info("本身" + signature);
+            log.info("-----------微信验签---------");
+            return SignUtil.checkSignature(signature, timestamp, nonce) == true ? echostr : null;
+        } catch (Exception e) {
+            log.error("验证公众号token失败", e);
+        }
+        return null;
+    }
+
+
+    /**
+     * 处理服务器推送消息
+     * @param request
+     * @return
+     */
+    @PostMapping(value = "/notifyMsg")
+    @ApiOperation("处理服务器推送消息")
+    public String dealNotify(HttpServletRequest request) {
+        log.info("-----------进入---------");
+        try {
+            log.info("-----------推送消息处理---------");
+            return wxGzhService.handlePublicMsg(request);
+        } catch (Exception e) {
+            log.error("处理推送消息失败", e);
+        }
+        return null;
+    }
+
+    /**
+     * 获取公众号访问jspapi 需要的参数
+     * @return
+     */
+    @GetMapping(value = "/jspapi")
+    @ApiOperation("获取jspapi参数")
+    public Response<JSONObject> getJspapi(String url) {
+
+        return Response.success(wxGzhService.getJspapi(url));
+
+
+    }
+
+
+}

+ 21 - 37
nbjk-service/src/main/java/com/miaxis/wx/service/impl/WxGzhServiceImpl.java

@@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.miaxis.common.constant.Constants;
 import com.miaxis.common.core.domain.entity.SysDictData;
 import com.miaxis.common.core.domain.entity.UserInfo;
+import com.miaxis.common.utils.ServletUtils;
 import com.miaxis.common.utils.StringUtils;
 import com.miaxis.common.utils.wx.MessageUtil;
 import com.miaxis.feign.dto.WxMessageCusom;
@@ -111,12 +112,9 @@ public class WxGzhServiceImpl implements IWxGzhService {
             cn.hutool.json.JSONObject decryptMap = XML.toJSONObject(IOUtils.toString(request.getInputStream()));
             cn.hutool.json.JSONObject jsonObjectData = decryptMap.getJSONObject("xml");
 
-
 //            cn.hutool.json.JSONObject jsonObjectData = new cn.hutool.json.JSONObject("{\"CreateTime\":1666169539,\"ToUserName\":\"gh_8e0c71ce615b\",\"MsgType\":\"text\",\"MsgId\":23853813438069304,\"Content\":\"a12345\",\"FromUserName\":\"o_DwY68a6UHmFhmovdFB0gANaC_U\"}");
             log.info("2-----jsonObjectData------"+jsonObjectData);
 
-
-
             //开发者微信号
             String toUserName = jsonObjectData.getStr("ToUserName");
             //发送方帐号(一个OpenID)
@@ -129,20 +127,7 @@ public class WxGzhServiceImpl implements IWxGzhService {
 
             // 普通消息
             if (MessageUtil.MESSAGE_TEXT.equals(msgType)) { // 文本消息
-                List<SysDictData> configs = dictTypeService.selectDictDataByType("partner_config");
-                String parentUnionId = null ;
-                for (SysDictData sysDictData:configs){
-                    String dictValue = sysDictData.getDictValue();
-                    String[] values = dictValue.split(",");
-                    String password = values[0];
-                    if (jsonObjectData.getStr("Content").equals(password)){
-                        parentUnionId = sysDictData.getDictLabel();
-                        break;
-                    }
-                }
-
-
-
+                return MessageUtil.initText(fromUserName, toUserName, "欢迎财仝驾助,右下角菜单可以理论训练,观看教学视频。");
             } else if (MessageUtil.MESSAGE_IMAGE.equals(msgType)) { // 图片消息
                 log.info("2.2...");
                 return MessageUtil.initText(fromUserName, toUserName, "抱歉,暂时无法识别图片信息!");
@@ -153,20 +138,20 @@ public class WxGzhServiceImpl implements IWxGzhService {
                 String event = jsonObjectData.getStr("Event");
                 if (MessageUtil.MESSAGE_SUBSCRIBE.equals(event)) { // 关注事件 或 扫描二维码关注事件
                     log.info("3.1...");
-
                     String unionId = userInfoJson.getString("unionid");
-                    //如果是新用户,则保存用户
-//                    UserInfo user = userService.getStudentByUnionid(unionId);
-//                    if (user == null){
-//                        UserInfo userInfo = new UserInfo();
-//                        userInfo.setGzhOpenid(fromUserName);
-//                        userInfo.setUnionId(unionId);
-//                        userInfo.setThirdPlatform("weixin");
-//                        userInfoService.save(userInfo);
-//                    }else if(user.getGzhOpenid()==null){
-//                        user.setGzhOpenid(fromUserName);
-//                        userInfoService.updateById(user);
-//                    }
+                    String openId = userInfoJson.getString("openid");
+                    UserInfo userInfoDB = userService.getStudentByUnionId(unionId);
+                    if (userInfoDB == null) {
+                        UserInfo userInfo = new UserInfo();
+                        userInfo.setUnionId(unionId);
+                        userInfo.setGzhOpenid(openId);
+                        userInfo.setIsVip(0);
+                        userService.saveUserInfo(userInfo);
+                    }else {
+                        userInfoDB.setUnionId(unionId);
+                        userInfoDB.setGzhOpenid(openId);
+                        userService.updateStudent(userInfoDB);
+                    }
 
                     //存在Ticket为扫码关注
                     if (org.apache.commons.lang3.StringUtils.isNotEmpty(jsonObjectData.getStr("Ticket"))){
@@ -176,15 +161,14 @@ public class WxGzhServiceImpl implements IWxGzhService {
                     }else {
                         result =  MessageUtil.initText(fromUserName, toUserName, "欢迎关注财仝驾考,您的学车好伙伴!");
                     }
-
-
                     return result;
-
-
-
                 }  else if (MessageUtil.MESSAGE_UNSUBSCRIBE.equals(event)) { // 取消订阅事件
-                    // todo 处理取消订阅事件
-
+                    log.info("3.2...");
+                    UserInfo userInfoDB = userService.getStudentByOpenId(fromUserName);
+                    if (userInfoDB == null) {
+                        userInfoDB.setGzhOpenid(null);
+                        userService.updateStudent(userInfoDB);
+                    }
                 } else if (MessageUtil.MESSAGE_SCAN.equals(event)) { // 已关注扫描二维码事件