Browse Source

添加 关系 公众号

DESKTOP-USV654P\pc 1 year ago
parent
commit
1cb8826dd0

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

@@ -53,7 +53,11 @@ public class WeChatUtil {
             return null;
         }
         String openId = jsonObject.get("openid").toString();
-        String[] arrayRefVar = { openId,""};
+        String unionid = "";
+        if (jsonObject.containsKey("unionid")) {
+            unionid = jsonObject.get("unionid").toString();
+        }
+        String[] arrayRefVar = {openId, unionid};
         return arrayRefVar;
     }
 

+ 7 - 0
src/main/java/com/xjrsoft/module/system/controller/LoginController.java

@@ -81,6 +81,13 @@ public class LoginController {
         return R.ok(loginService.loginByCode(dto));
     }
 
+    @PostMapping("/bindOpenid")
+    @ApiOperation(value = "登录", notes = "code")
+    @XjrLog(value = "code换Openid并绑定")
+    public R bindOpenid(@RequestBody @Valid LoginByCodeDto dto) throws Exception {
+        return R.ok(loginService.bindOpenid(dto));
+    }
+
     @GetMapping("/imgcaptcha")
     @ApiOperation(value = "图形验证码", notes = "图形验证码")
     public R imgCaptcha() {

+ 3 - 0
src/main/java/com/xjrsoft/module/system/service/ILoginService.java

@@ -6,6 +6,7 @@ import com.xjrsoft.module.system.dto.LoginCaptchaDto;
 import com.xjrsoft.module.system.dto.LoginDto;
 import com.xjrsoft.module.system.vo.CreateTokenVo;
 import com.xjrsoft.module.system.vo.ImgCaptchaVo;
+import com.xjrsoft.module.system.vo.LoginByCodeVo;
 import com.xjrsoft.module.system.vo.LoginVo;
 import org.springframework.stereotype.Service;
 
@@ -19,6 +20,8 @@ public interface ILoginService {
 
     LoginVo loginByCode(LoginByCodeDto dto) throws Exception;
 
+    LoginByCodeVo bindOpenid(LoginByCodeDto dto) throws Exception;
+
     LoginVo loginByCaptcha(LoginCaptchaDto dto) throws Exception;
 
     CreateTokenVo createToken(CreateTokenDto dto);

+ 13 - 0
src/main/java/com/xjrsoft/module/system/service/impl/LoginServiceImpl.java

@@ -142,6 +142,19 @@ public class LoginServiceImpl implements ILoginService {
         return result;
     }
 
+    @Override
+    public LoginByCodeVo bindOpenid(LoginByCodeDto dto) throws Exception {
+        LoginByCodeVo result = new LoginByCodeVo();
+        String[] ids = weChatUtil.getMpOpenid(dto.getCode());
+        if (ids == null) throw new MyException("code无效");
+        User user = userService.getOne(Wrappers.lambdaQuery(User.class).eq(User::getUnionId, ids[1]));
+        if (user == null) throw new MyException("code无效");
+        user.setOpenId(ids[0]);
+        userService.updateById(user);
+        result.setToken(ids[0]);
+        return result;
+    }
+
     /**
      * 手机验证码登录
      *