he2802 2 년 전
부모
커밋
c2330b32d0

+ 9 - 0
zhongzheng-api/src/main/java/com/zhongzheng/controller/user/UserController.java

@@ -186,6 +186,15 @@ public class UserController extends BaseController {
         return AjaxResult.success();
     }
 
+    @ApiOperation("获取公众号openid")
+    @PostMapping("/wx/gzh_bind")
+    public AjaxResult wx_gzh_bind(@RequestBody WxLoginBody loginBody)
+    {
+        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
+        loginBody.setUserId(loginUser.getUser().getUserId());
+        return AjaxResult.success(wxLoginService.bindGzhUnionIdUser(loginBody));
+    }
+
     @ApiOperation("检查是否绑定公众号")
     @PreAuthorize("@ss.hasPermi('system:user:list')")
     @GetMapping("/checkBindGzh")

+ 9 - 0
zhongzheng-api/src/main/java/com/zhongzheng/controller/wx/WxLoginController.java

@@ -103,6 +103,15 @@ public class WxLoginController
         return AjaxResult.success(openId);
     }
 
+    @ApiOperation("考前获取用户小程序openid")
+    @PostMapping("/wx/exam/getOpenid")
+    public AjaxResult<Void> getExamOpenid(@RequestBody WxLoginBody loginBody)
+    {
+        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
+        String openId = wxLoginService.getExamWxOpenId(loginBody,loginUser.getUser().getUserId());
+        return AjaxResult.success(openId);
+    }
+
     @ApiOperation("小程序校验PC登录二维码")
     @PostMapping("/scan_login_check")
     public AjaxResult scanLoginCheck(@RequestBody WxLoginBody loginBody)

+ 38 - 0
zhongzheng-framework/src/main/java/com/zhongzheng/framework/web/service/WxLoginService.java

@@ -386,6 +386,20 @@ public class WxLoginService implements IWxLoginService {
         return openId;
     }
 
+    public String getExamWxOpenId(WxLoginBody loginBody, Long userId) {
+        initData();
+        String openId;
+
+        //每次更新最新openid
+        String param = String.format(wxAuthParam, appid, appsrcret, loginBody.getCode());
+        String resultString = HttpUtils.sendGet(wxAuthUrl, param);
+        //解析json
+        JSONObject jsonObject = (JSONObject) JSONObject.parse(resultString);
+        String session_key = String.valueOf(jsonObject.get("session_key"));
+        openId = String.valueOf(jsonObject.get("openid"));
+        return openId;
+    }
+
 
     //小程序获取用户信息
     public User getWxUnionIdUser(WxLoginBody loginBody) {
@@ -1274,6 +1288,30 @@ public class WxLoginService implements IWxLoginService {
         return user;
     }
 
+    //获取公众号openid
+    public String bindGzhUnionIdUser(WxLoginBody loginBody) {
+        if(Validator.isEmpty(loginBody.getCode())){
+            throw new CustomException("code不能为空");
+        }
+        initData();
+        String param = String.format(gzh_wxAuthParam, gzh_appid, gzh_appsrcret, loginBody.getCode());
+        String resultString = HttpUtils.sendGet(gzh_wxAuthUrl, param);
+        //解析json
+        JSONObject jsonObject = (JSONObject) JSONObject.parse(resultString);
+        String access_token = String.valueOf(jsonObject.get("access_token"));
+        if(access_token.equals("null")){
+            throw new CustomException( String.valueOf(jsonObject.get("errmsg")));
+        }
+        String openId = String.valueOf(jsonObject.get("openid"));
+
+        String userInfoParam = String.format(gzh_wxUserInfoParam, access_token, openId);
+        String userInfoResultString = HttpUtils.sendGet(gzh_wxUserInfoUrl, userInfoParam);
+
+        //解析json
+        JSONObject jsonObject1 = (JSONObject) JSONObject.parse(userInfoResultString);
+        return openId;
+    }
+
     public Boolean checkBindOpenId(WxIdBody loginBody) {
         if(Validator.isEmpty(loginBody.getOpenid())||Validator.isEmpty(loginBody.getFromPlat())){
             throw new CustomException("参数缺失");