he2802 %!s(int64=2) %!d(string=hai) anos
pai
achega
7192ea8c3b

+ 23 - 1
zhongzheng-api/src/main/java/com/zhongzheng/controller/user/UserController.java

@@ -9,6 +9,7 @@ import com.zhongzheng.common.core.domain.model.LoginUser;
 import com.zhongzheng.common.enums.BusinessType;
 import com.zhongzheng.common.utils.DateUtils;
 import com.zhongzheng.common.utils.ServletUtils;
+import com.zhongzheng.framework.web.service.WxLoginService;
 import com.zhongzheng.framework.web.service.WxTokenService;
 import com.zhongzheng.modules.user.bo.*;
 import com.zhongzheng.modules.user.domain.UserWxFollow;
@@ -18,6 +19,7 @@ import com.zhongzheng.modules.user.service.IUserVisitLogService;
 import com.zhongzheng.modules.user.service.IUserWxFollowService;
 import com.zhongzheng.modules.user.vo.RanKingUser;
 import com.zhongzheng.modules.user.vo.UserVo;
+import com.zhongzheng.modules.wx.bo.WxLoginBody;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import lombok.RequiredArgsConstructor;
@@ -48,7 +50,7 @@ public class UserController extends BaseController {
 
     private final IUserVisitLogService iUserVisitLogService;
 
-
+    private final WxLoginService wxLoginService;
     /**
      * 修改客户端用户
      */
@@ -157,4 +159,24 @@ public class UserController extends BaseController {
         }
         return AjaxResult.success(needUpdate);
     }
+
+    @ApiOperation("购买前获取公众号openid")
+    @PostMapping("/gzh_bind")
+    public AjaxResult gzh_bind(@RequestBody WxLoginBody loginBody)
+    {
+        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
+        loginBody.setUserId(loginUser.getUser().getUserId());
+        wxLoginService.bindWxGzhUnionIdUser(loginBody);
+        return AjaxResult.success();
+    }
+
+    @ApiOperation("检查是否绑定公众号")
+    @PreAuthorize("@ss.hasPermi('system:user:list')")
+    @GetMapping("/checkBindGzh")
+    public AjaxResult<String> checkBindGzh()
+    {
+        ClientLoginUser loginUser = wxTokenService.getLoginUser(ServletUtils.getRequest());
+        UserVo userVo = iUserService.queryById(loginUser.getUser().getUserId());
+        return AjaxResult.success("成功",userVo.getGzhOpenId());
+    }
 }

+ 31 - 1
zhongzheng-framework/src/main/java/com/zhongzheng/framework/web/service/WxLoginService.java

@@ -27,6 +27,7 @@ import com.zhongzheng.modules.course.bo.SectionWatchPerBo;
 import com.zhongzheng.modules.goods.domain.Goods;
 import com.zhongzheng.modules.goods.service.IGoodsService;
 import com.zhongzheng.modules.goods.vo.GoodsVo;
+import com.zhongzheng.modules.order.domain.Order;
 import com.zhongzheng.modules.system.service.ISysConfigService;
 import com.zhongzheng.modules.user.bo.UserWxFollowAddBo;
 import com.zhongzheng.modules.user.domain.User;
@@ -1206,7 +1207,6 @@ public class WxLoginService implements IWxLoginService {
         initData();
         String param = String.format(gzh_wxAuthParam, gzh_appid, gzh_appsrcret, loginBody.getCode());
         String resultString = HttpUtils.sendGet(gzh_wxAuthUrl, param);
-        System.out.println(resultString);
         //解析json
         JSONObject jsonObject = (JSONObject) JSONObject.parse(resultString);
         String access_token = String.valueOf(jsonObject.get("access_token"));
@@ -1232,6 +1232,36 @@ public class WxLoginService implements IWxLoginService {
         return user;
     }
 
+    //公众号获取用户信息
+    public User bindWxGzhUnionIdUser(WxLoginBody loginBody) {
+        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);
+        String unionId = String.valueOf(jsonObject1.get("unionid"));
+        User user = iUserService.getOne(new LambdaQueryWrapper<User>().eq(User::getUserId,loginBody.getUserId()));
+        if (user == null) {
+            throw new CustomException("您尚未注册,请前往小程序注册");
+        }
+        if (!Validator.isNotNull(user.getGzhOpenId())) {
+            user.setGzhOpenId(openId);
+            iUserService.updateById(user);
+        }
+        return user;
+    }
+
     public String obtainWxPhone(String iv, String encryptedData, String session_key) {
         String userInfo = null;
         JSONObject userInfoJSON = null;

+ 3 - 0
zhongzheng-system/src/main/java/com/zhongzheng/modules/wx/bo/WxLoginBody.java

@@ -51,4 +51,7 @@ public class WxLoginBody {
     @ApiModelProperty("身份证号码")
     private String idcard;
 
+    @ApiModelProperty("用户ID")
+    private Long userId;
+
 }