Ver Fonte

添加微信小程序接口

DESKTOP-USV654P\pc há 1 ano atrás
pai
commit
bc3cac1563

+ 34 - 5
src/main/java/com/xjrsoft/common/utils/WeChatUtil.java

@@ -27,14 +27,20 @@ public class WeChatUtil {
     @Value("${xjrsoft.enterpriseWeChat.agentid}")
     public Integer agentid;
 
-    @Value("${xjrsoft.mpWeixin.appKey}")
+    @Value("${xjrsoft.mpWeChat.appKey}")
     public String mpAppKey;
 
-    @Value("${xjrsoft.mpWeixin.appSecret}")
+    @Value("${xjrsoft.mpWeChat.appSecret}")
     public String mpAppSecret;
 
-    //获取Openid
-    public  String getOpenid(String code) {
+    @Value("${xjrsoft.appletWeChat.appKey}")
+    public String appletAppKey;
+
+    @Value("${xjrsoft.appletWeChat.appSecret}")
+    public String appletAppSecret;
+
+    //公众号获取Openid
+    public  String[] getMpOpenid(String code) {
         HashMap<String, Object> paramMap = new HashMap<>();
         paramMap.put("appid", mpAppKey);
         paramMap.put("secret", mpAppSecret);
@@ -47,7 +53,30 @@ public class WeChatUtil {
             return null;
         }
         String openId = jsonObject.get("openid").toString();
-        return openId;
+        String[] arrayRefVar = { openId,""};
+        return arrayRefVar;
+    }
+
+    // 小程序
+    public  String[] getOpenid(String code) {
+        HashMap<String, Object> paramMap = new HashMap<>();
+        paramMap.put("appid", appletAppKey);
+        paramMap.put("secret", appletAppSecret);
+        paramMap.put("js_code", code);
+        paramMap.put("grant_type", "authorization_code");
+        String result = HttpUtil.get("https://api.weixin.qq.com/sns/jscode2session", paramMap);
+        JSONObject jsonObject = JSONObject.parseObject(result);
+        if (jsonObject.containsKey("errcode")) {
+            //throw new MyException(result);
+            return null;
+        }
+        String openId = jsonObject.get("openid").toString();
+        String unionid = "";
+        if (jsonObject.containsKey("unionid")) {
+            unionid = jsonObject.get("unionid").toString();
+        }
+        String[] arrayRefVar = {openId, unionid};
+        return arrayRefVar;
     }
 
     //获取通讯录或发消息token

+ 8 - 6
src/main/java/com/xjrsoft/module/organization/controller/UserController.java

@@ -352,27 +352,29 @@ public class UserController {
     }
 
     @PostMapping("/bind-openid")
-    @ApiOperation(value = "绑定微信")
+    @ApiOperation(value = "绑定微信 UnionId")
     public  R bindOpenid(@RequestBody BindOpenidDto dto) {
-        User user = userService.getOne(Wrappers.<User>query().lambda().select(User::getId, User::getOpenId).eq(User::getId, dto.getId()), false);
+        User user = userService.getOne(Wrappers.<User>query().lambda().select(User::getId, User::getOpenId,User::getUnionId).eq(User::getId, dto.getId()), false);
         if (user != null) {
-            if (StrUtil.isNotBlank(user.getOpenId())) {
+            if (StrUtil.isNotBlank(user.getUnionId())) {
                 return R.error("该用户已经绑定微信!");
             }
             User updateUser = new User();
             updateUser.setId(dto.getId());
-            updateUser.setOpenId(dto.getOpenid());
+//            updateUser.setOpenId(dto.getOpenid());
+            updateUser.setUnionId(dto.getOpenid());
             return R.ok(userService.updateById(updateUser));
         }
         return R.error("该用户不存在!");
     }
 
     @GetMapping("/unbind-openid")
-    @ApiOperation(value = "取消绑定微信")
+    @ApiOperation(value = "取消绑定微信 UnionId")
     public  R unbindOpenid(@RequestParam Long id) {
         User updateUser = new User();
         updateUser.setId(id);
-        updateUser.setOpenId("");
+//        updateUser.setOpenId("");
+        updateUser.setUnionId("");
         return R.ok(userService.updateById(updateUser));
     }
 

+ 3 - 1
src/main/java/com/xjrsoft/module/organization/vo/UserInfoVo.java

@@ -88,10 +88,12 @@ public class UserInfoVo {
     private String className;
 
     /**
-     * 班级名称
+     *
      */
     private String openId;
 
+    private  String unionId;
+
 
     /**
      * 所有部门信息

+ 11 - 4
src/main/java/com/xjrsoft/module/system/service/impl/LoginServiceImpl.java

@@ -14,6 +14,7 @@ import cn.hutool.core.util.StrUtil;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
 import com.baomidou.mybatisplus.core.toolkit.StringPool;
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.xjrsoft.common.constant.GlobalConstant;
 import com.xjrsoft.common.enums.EnabledMark;
@@ -118,14 +119,20 @@ public class LoginServiceImpl implements ILoginService {
     @Override
     public LoginByCodeVo loginByCode(LoginByCodeDto dto) throws Exception {
         LoginByCodeVo result = new LoginByCodeVo();
-        String openId = weChatUtil.getOpenid(dto.getCode());
-        if (openId == null) throw new MyException("code无效");
+        String[] ids = weChatUtil.getOpenid(dto.getCode());
+        if (ids == null) throw new MyException("code无效");
 
         LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
-        queryWrapper.eq(User::getOpenId, openId);
+        if (StringUtils.isNotEmpty(ids[1])) {
+            queryWrapper.eq(User::getUnionId, ids[1]);
+            result.setUnionId(ids[1]);
+        } else {
+            queryWrapper.eq(User::getOpenId, ids[0]);
+            result.setOpenid(ids[0]);
+        }
         User loginUser = userService.getOne(queryWrapper);
 
-        result.setOpenid(openId);
+//        result.setOpenid(openId);
         if (loginUser == null) {
             return result;
         }

+ 2 - 0
src/main/java/com/xjrsoft/module/system/vo/LoginByCodeVo.java

@@ -8,4 +8,6 @@ public class LoginByCodeVo extends LoginVo {
      * openid
      */
     private String openid;
+
+    private  String unionId;
 }

+ 4 - 1
src/main/resources/application-dev.yml

@@ -100,9 +100,12 @@ xjrsoft:
     secret: 02YXuJw_vYYJiDQvG-eKlXCUTDAaAJ1z7m9t8_hqoa0
     secret1: z3R17Od4ROW1zPisguiQmTfx0NvcYKmoIu19-82vdtU
     agentid: 1000003
-  mpWeixin:
+  mpWeChat:
     appKey: wx93175d7fe68cd170
     appSecret: 4bab1c5ccd9d4d1d7854464d7251becf
+  appletWeChat:
+    appKey: wx72e974483a9174e4
+    appSecret: 3bbe99f6964c9f4fc11a8aa1224ac4b3
   dingtalk:
     appKey: dingaex2gok1rllumlqs
     appSecret: Nv2pe-UoR0Z_Iw8d29laZfL3kH6ElmhZpRdPwI7SvmKJmaCI29qCrSlFEiMM88MB

+ 4 - 1
src/main/resources/application-prod.yml

@@ -98,9 +98,12 @@ xjrsoft:
     secret: 02YXuJw_vYYJiDQvG-eKlXCUTDAaAJ1z7m9t8_hqoa0
     secret1: z3R17Od4ROW1zPisguiQmTfx0NvcYKmoIu19-82vdtU
     agentid: 1000003
-  mpWeixin:
+  mpWeChat:
     appKey: wx93175d7fe68cd170
     appSecret: 4bab1c5ccd9d4d1d7854464d7251becf
+  appletWeChat:
+    appKey: wx72e974483a9174e4
+    appSecret: 3bbe99f6964c9f4fc11a8aa1224ac4b3
   dingtalk:
     appKey: dingaex2gok1rllumlqs
     appSecret: Nv2pe-UoR0Z_Iw8d29laZfL3kH6ElmhZpRdPwI7SvmKJmaCI29qCrSlFEiMM88MB