Эх сурвалжийг харах

微信绑定改为后端判断

孙伟 3 жил өмнө
parent
commit
02dc16a150

+ 26 - 0
application-facade/src/main/java/com/factory/controller/wx/WxController.java

@@ -76,5 +76,31 @@ public class WxController {
         }
         return ResponseBeanBuilder.ok();
     }
+    @ApiOperation(value = "检测用户是否绑定微信")
+    @PostMapping("/bind/check")
+    public ResponseBean bindWechatUserCheck(@RequestBody UserBindReq userBindReq, HttpServletResponse response, HttpServletRequest request) throws Throwable {
+        Integer checked = 0;//默认为绑定
+        try {
+            response.setContentType("text/html");
+            request.setCharacterEncoding("UTF-8");
+            response.setCharacterEncoding("UTF-8");
+            Map params = new HashMap();
+            params.put("secret", appsecret);
+            params.put("appid", appid);
+            params.put("grant_type", "authorization_code");
+            params.put("js_code", userBindReq.getCode());
+            params.put("connect_redirect", "1");
+            String result = HttpCilentUntil.httpGetToString(
+                    "https://api.weixin.qq.com/sns/jscode2session", params);
+            JSONObject jsonObject = JSON.parseObject(result);
+            String openid = jsonObject.get("openid").toString();
+
+            Long userId = (Long) request.getAttribute("LOGIN_USER_ID");//获取用户id
+           checked = wxService.selectBindWeChatUserCount(openid, userId);
+        } catch (Exception e) {
+            return ResponseBeanBuilder.fail("小程序绑定用户失败");
+        }
+        return ResponseBeanBuilder.ok(checked);
+    }
 
 }

+ 4 - 4
application-facade/src/main/java/com/factory/util/client/PushWxMessage.java

@@ -21,8 +21,8 @@ public class PushWxMessage {
          String pushUrl = new StringBuffer("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=").append(access_token).toString();
          result = HttpCilentUntil.httpJsonToString(pushUrl, JSON.toJSONString(template));
          jsonObject = JSON.parseObject(result);
-         String errCode = jsonObject.get("errCode").toString();
-         String errMsg = jsonObject.get("errMsg").toString();
+         String errCode = jsonObject.get("errcode").toString();
+         String errMsg = jsonObject.get("errmsg").toString();
          System.out.println(errCode);
          return errCode;
      }catch (Throwable e){
@@ -32,13 +32,13 @@ public class PushWxMessage {
    }
 
     public static void main(String[] args) {
-        AppEntry app=AppEntry.builder().appId("wx7811e5dd1856711c").secret("47dbeb1f0dfac08a963a09301c7eb50c").build();
+        AppEntry app=AppEntry.builder().appId("wxfd6e42e05b91163c").secret("7c4e39a33bf8b66beb919b9ad0a15832").build();
         HashMap<String,ValEntry> dataMap=new HashMap();
         dataMap.put("thing1",ValEntry.builder().value("this is one").build());
         dataMap.put("thing2",ValEntry.builder().value("this is two").build());
         dataMap.put("time3",ValEntry.builder().value("2021-07-23 20:00").build());
         dataMap.put("thing4",ValEntry.builder().value("Bella").build());
-        TemplateEntry tpl=TemplateEntry.builder().template_id("3RatcsOA-T0Dsgevr-1UGvYzZvhS0UY9A86fnTFax50").touser("openid").data(dataMap).build();
+        TemplateEntry tpl=TemplateEntry.builder().template_id("3RatcsOA-T0Dsgevr-1UGvYzZvhS0UY9A86fnTFax50").touser("oc6xx5Z4DGNuRYKp7EHANCkq5mII").page("pages/login/index").data(dataMap).build();
         PushWxMessage.sendMessage(app,tpl);
     }
 

+ 8 - 0
application-facade/src/main/java/com/factory/wx/service/WxService.java

@@ -22,4 +22,12 @@ public interface WxService {
      * @param userId
      */
     ResponseBean bindWeChatUser(String code, Long userId);
+
+    /**
+     * 查询用户是否绑定
+     * @param openid
+     * @param userId
+     * @return
+     */
+    Integer selectBindWeChatUserCount(String openid, Long userId);
 }

+ 5 - 0
application-facade/src/main/java/com/factory/wx/service/impl/WxServiceImpl.java

@@ -58,4 +58,9 @@ public class WxServiceImpl implements WxService {
         }
         return ResponseBeanBuilder.ok();
     }
+
+    @Override
+    public Integer selectBindWeChatUserCount(String openid, Long userId) {
+        return wxMapper.selectCount(Wrappers.<UsUserWechat>lambdaQuery().eq(UsUserWechat::getUserId, userId).eq(UsUserWechat::getOpenid, openid));
+    }
 }