From 767826297be7359e15b95a04cb0314615e4c7c8e Mon Sep 17 00:00:00 2001 From: hurui <177768073@qq.com> Date: Fri, 3 Dec 2021 17:50:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/HighUserCardController.java | 4 +- .../controller/HighUserCommonController.java | 8 +- .../cweb/controller/SendSmsController.java | 2 +- .../cweb/controller/SmsContentController.java | 2 +- .../com/cweb/controller/WechatController.java | 353 +++--------------- 5 files changed, 55 insertions(+), 314 deletions(-) diff --git a/hai-cweb/src/main/java/com/cweb/controller/HighUserCardController.java b/hai-cweb/src/main/java/com/cweb/controller/HighUserCardController.java index f003a776..94873a11 100644 --- a/hai-cweb/src/main/java/com/cweb/controller/HighUserCardController.java +++ b/hai-cweb/src/main/java/com/cweb/controller/HighUserCardController.java @@ -142,7 +142,7 @@ public class HighUserCardController { } // 获取手机号验证码 - String phoneSmsCode = (String) redisUtil.get(body.getString("phone")); + String phoneSmsCode = (String) redisUtil.get("SMS_"+body.getString("phone")); // 验证码校验 if (StringUtils.isNotBlank(phoneSmsCode) && Objects.equals(phoneSmsCode,body.getString("smsCode"))) { @@ -153,7 +153,7 @@ public class HighUserCardController { userCard.setType(UserCardType.type1.getType()); highUserCardService.editCard(userCard); - redisUtil.del(body.getString("phone")); + redisUtil.del("SMS_"+body.getString("phone")); // 定义个人所有数据 HighUserModel highUserModel = new HighUserModel(); diff --git a/hai-cweb/src/main/java/com/cweb/controller/HighUserCommonController.java b/hai-cweb/src/main/java/com/cweb/controller/HighUserCommonController.java index 264287f3..8b235965 100644 --- a/hai-cweb/src/main/java/com/cweb/controller/HighUserCommonController.java +++ b/hai-cweb/src/main/java/com/cweb/controller/HighUserCommonController.java @@ -59,13 +59,13 @@ public class HighUserCommonController { try { // 获取手机号验证码 - String phoneSmsCode = (String) redisUtil.get(phone); + String phoneSmsCode = (String) redisUtil.get("SMS_"+phone); // 校验验证码 if (StringUtils.isBlank(phoneSmsCode) || !Objects.equals(phoneSmsCode,code)) { log.error("HighUserController --> bindUserPhone() error!", "验证码错误"); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "验证码错误"); } - redisUtil.del(phone); + redisUtil.del("SMS_"+phone); HighUser user = highUserService.findByPhone(phone); if(user == null) { @@ -87,7 +87,7 @@ public class HighUserCommonController { HighUser detailData = highUserService.getDetailDataByUser(user.getId()); detailData.setPassword(null); highUserModel.setHighUser(detailData); - SessionObject so = new SessionObject(StringUtils.isNotBlank(user.getUnionId())?user.getUnionId():detailData.getId().toString(), 1 , highUserModel); + SessionObject so = new SessionObject(user.getPhone(), 1 , highUserModel); userCenter.save(request, response, so); return ResponseMsgUtil.success(so); } catch (Exception e) { @@ -145,7 +145,7 @@ public class HighUserCommonController { HighUser detailData = highUserService.getDetailDataByUser(user.getId()); detailData.setPassword(null); highUserModel.setHighUser(detailData); - SessionObject so = new SessionObject(StringUtils.isNotBlank(user.getUnionId())?user.getUnionId():detailData.getId().toString(), 1 , highUserModel); + SessionObject so = new SessionObject(user.getPhone(), 1 , highUserModel); userCenter.save(request, response, so); return ResponseMsgUtil.success(so); } catch (Exception e) { diff --git a/hai-cweb/src/main/java/com/cweb/controller/SendSmsController.java b/hai-cweb/src/main/java/com/cweb/controller/SendSmsController.java index 4b4588dc..7170ad3a 100644 --- a/hai-cweb/src/main/java/com/cweb/controller/SendSmsController.java +++ b/hai-cweb/src/main/java/com/cweb/controller/SendSmsController.java @@ -93,7 +93,7 @@ public class SendSmsController { JSONObject object = HttpsUtils.doPostSendSms(url, map); String code = object.getString("code"); if (code.equals("000000")) { - redisUtil.set(phone,smsCode, 60*10); + redisUtil.set("SMS_"+phone,smsCode, 60*10); return ResponseMsgUtil.success("发送成功"); } return ResponseMsgUtil.success(object); diff --git a/hai-cweb/src/main/java/com/cweb/controller/SmsContentController.java b/hai-cweb/src/main/java/com/cweb/controller/SmsContentController.java index c9b6597f..c7ec16ca 100644 --- a/hai-cweb/src/main/java/com/cweb/controller/SmsContentController.java +++ b/hai-cweb/src/main/java/com/cweb/controller/SmsContentController.java @@ -170,7 +170,7 @@ public class SmsContentController { e.printStackTrace(); } } - redisUtil.set(phone,smsCode, 60*10); + redisUtil.set("SMS_"+phone,smsCode, 60*10); return ResponseMsgUtil.success("发送成功"); } catch (Exception e) { diff --git a/hai-cweb/src/main/java/com/cweb/controller/WechatController.java b/hai-cweb/src/main/java/com/cweb/controller/WechatController.java index 067bf234..4227ab5a 100644 --- a/hai-cweb/src/main/java/com/cweb/controller/WechatController.java +++ b/hai-cweb/src/main/java/com/cweb/controller/WechatController.java @@ -87,36 +87,6 @@ public class WechatController { } } - @RequestMapping(value = "/login", method = RequestMethod.GET) - @ResponseBody - @ApiOperation(value = "小程序授权用户登录接口") - public ResponseData login(@RequestParam(value = "openId", required = true) String openId, - HttpServletRequest request, HttpServletResponse response) { - try { - //校验code,openId不能同时为空 - if (StringUtils.isEmpty(openId)) { - throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.REQ_PARAMS_ERROR, ""); - } - HighUser highUser = highUserService.findByOpenId(openId); - if (highUser == null) { - log.error("login error!", "未找到用户"); - throw ErrorHelp.genException(SysCode.System, ErrorCode.UN_MEMBER_ERROR, ""); - } - - // 定义个人所有数据 - HighUserModel highUserModel = new HighUserModel(); - HighUser detailData = highUserService.getDetailDataByUser(highUser.getId()); - detailData.setPassword(null); - highUserModel.setHighUser(detailData); - SessionObject so = new SessionObject(highUser.getUnionId(), 1 , highUserModel); - userCenter.save(request, response, so); - - return ResponseMsgUtil.success(so); - } catch (Exception e) { - return ResponseMsgUtil.exception(e); - } - } - @RequestMapping(value = "/loginByPhone", method = RequestMethod.GET) @ResponseBody @ApiOperation(value = "小程序根据手机号登录或注册") @@ -136,7 +106,7 @@ public class WechatController { throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.REQ_PARAMS_ERROR, ""); } //请求微信api,获取用户session_key以及openId - Object skObject = redisUtil.hget(WX_OPENID_SESSION_REDIS,openId); + Object skObject = redisUtil.hget(WX_OPENID_SESSION_REDIS, openId); if (skObject == null){ throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.WECHAT_LOGIN_ERROR); } @@ -148,37 +118,25 @@ public class WechatController { throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.WECHAT_DECRYPT_ERROR, "用户phoneNumber解析失败"); } - HighUser user = highUserService.findByOpenId(session.getOpenid()); - if (user != null) { - if (StringUtils.isBlank(user.getPhone())) { - if (highUserService.findByPhone(phoneNoInfo.getPurePhoneNumber()) != null) { - throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.COMMON_ERROR, "手机号已被绑定,请更换后重新授权"); - } - user.setPhone(phoneNoInfo.getPurePhoneNumber()); - highUserService.updateUser(user); - } + // 根据手机号查询用户 + HighUser user = highUserService.findByPhone(phoneNoInfo.getPurePhoneNumber()); + if (user == null) { + user = new HighUser(); + user.setName("用户" + IDGenerator.nextId(5)); + user.setPhone(phoneNoInfo.getPurePhoneNumber()); + user.setRegTime(new Date()); + user.setGold(0); + user.setOpenId(session.getOpenid()); + user.setUnionId(session.getUnionid()); + user.setStatus(1); + // 用户信息完整状态 + // 0:完整 + // 1:需要填写用户信息 + // 2:需要填写手机号 + user.setInfoCompleteStatus(1); + highUserService.insertUser(user); } else { - // 根据手机号查询用户 - user = highUserService.findByPhone(phoneNoInfo.getPurePhoneNumber()); - if (user == null) { - user = new HighUser(); - user.setName("用户" + IDGenerator.nextId(5)); - user.setPhone(phoneNoInfo.getPurePhoneNumber()); - user.setRegTime(new Date()); - user.setGold(0); - user.setOpenId(session.getOpenid()); - user.setUnionId(session.getUnionid()); - user.setStatus(1); - // 用户信息完整状态 - // 0:完整 - // 1:需要填写用户信息 - // 2:需要填写手机号 - user.setInfoCompleteStatus(1); - highUserService.insertUser(user); - } else { - if (StringUtils.isBlank(user.getName())) { - user.setName("用户" + IDGenerator.nextId(5)); - } + if (session.getOpenid() == null) { user.setOpenId(session.getOpenid()); user.setUnionId(session.getUnionid()); highUserService.updateUser(user); @@ -190,62 +148,13 @@ public class WechatController { HighUser detailData = highUserService.getDetailDataByUser(user.getId()); detailData.setPassword(null); highUserModel.setHighUser(detailData); - SessionObject so = new SessionObject(user.getUnionId(), 1 , highUserModel); - userCenter.save(request, response, so); - - return ResponseMsgUtil.success(so); - - } catch (Exception e) { - return ResponseMsgUtil.exception(e); - } - } - - @RequestMapping(value = "/getPhoneNumber", method = RequestMethod.GET) - @ResponseBody - @ApiOperation(value = "小程序获取用户手机号码") - public ResponseData getPhoneNumber(@RequestParam(value = "encryptedData", required = true) String encryptedData, - @RequestParam(value = "iv", required = true) String iv, - @RequestParam(value = "openId", required = true) String openId, - HttpServletRequest request, HttpServletResponse response) { - try { - log.error("origin encryptedData:" + encryptedData + ";iv:" + iv); - //校验openId不能为空 - //encryptedData,iv与unionId不能同时为空 - encryptedData = encryptedData.replace(" ", "+"); - iv = iv.replace(" ", "+"); - log.error("dest encryptedData:" + encryptedData + ";iv:" + iv); - if (StringUtils.isEmpty(openId) - || StringUtils.isEmpty(encryptedData) || StringUtils.isEmpty(iv)) { - throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.REQ_PARAMS_ERROR, ""); - } - //请求微信api,获取用户session_key以及openId - Object skObject = redisUtil.hget(WX_OPENID_SESSION_REDIS,openId); - if (skObject == null){ - throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.WECHAT_LOGIN_ERROR); - } - WxMaJscode2SessionResult session = (WxMaJscode2SessionResult)skObject; - - final WxMaService wxService = WxMaConfiguration.getMaService(); - WxMaPhoneNumberInfo phoneNoInfo = wxService.getUserService().getPhoneNoInfo(session.getSessionKey(), encryptedData, iv); - if (phoneNoInfo == null || StringUtils.isEmpty(phoneNoInfo.getPurePhoneNumber())) { - throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.WECHAT_DECRYPT_ERROR, "用户phoneNumber解析失败"); - } - - HighUser user = highUserService.bindUserPhone(phoneNoInfo.getPurePhoneNumber(), request); - - // 定义个人所有数据 - HighUserModel highUserModel = new HighUserModel(); - HighUser detailData = highUserService.getDetailDataByUser(user.getId()); - detailData.setPassword(null); - highUserModel.setHighUser(detailData); - SessionObject so = new SessionObject(user.getUnionId(), 1 , highUserModel); + SessionObject so = new SessionObject(user.getPhone(), 1 , highUserModel); userCenter.save(request, response, so); return ResponseMsgUtil.success(so); } catch (Exception e) { return ResponseMsgUtil.exception(e); - } } @@ -257,12 +166,16 @@ public class WechatController { @RequestParam(value = "openId", required = true) String openId, HttpServletRequest request, HttpServletResponse response) { try { + + // 用户 + SessionObject sessionObject = userCenter.getSessionObject(request); + HighUserModel userInfoModel = (HighUserModel) sessionObject.getObject(); + //校验openId不能为空 //encryptedData,iv与unionId不能同时为空 encryptedData = encryptedData.replace(" ", "+"); iv = iv.replace(" ", "+"); - if (StringUtils.isEmpty(openId) - || StringUtils.isEmpty(encryptedData) || StringUtils.isEmpty(iv)) { + if (StringUtils.isEmpty(openId) || StringUtils.isEmpty(encryptedData) || StringUtils.isEmpty(iv)) { throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.REQ_PARAMS_ERROR, ""); } //请求微信api,获取用户session_key以及openId @@ -277,46 +190,36 @@ public class WechatController { if (userInfo == null) { throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.WECHAT_DECRYPT_ERROR, "用户phoneNumber解析失败"); } - if (StringUtils.isBlank(session.getUnionid())) { - throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.WECHAT_DECRYPT_ERROR, "获取UnionId失败"); - } - HighUser user = highUserService.findByOpenId(session.getOpenid()); + + HighUser user = highUserService.findByUserId(userInfoModel.getHighUser().getId()); if (user == null) { - user = new HighUser(); - user.setUnionId(session.getUnionid()); - user.setOpenId(openId); - user.setRegTime(new Date()); - user.setStatus(1); - user.setGold(0); - user.setHeaderImg(userInfo.getAvatarUrl()); - user.setName(userInfo.getNickName()); - user.setInfoCompleteStatus(2); - highUserService.insertUser(user); - } else { - // 用户信息完整状态 - // 0:完整 - // 1:需要填写用户信息 - // 2:需要填写手机号 - if (user.getInfoCompleteStatus() == 1) { - if (StringUtils.isNotBlank(user.getPhone())) { - user.setInfoCompleteStatus(0); - } else { - user.setInfoCompleteStatus(2); - } + throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.COMMON_ERROR, "未找到用户信息"); + } + // 用户信息完整状态 + // 0:完整 + // 1:需要填写用户信息 + // 2:需要填写手机号 + if (user.getInfoCompleteStatus() == 1) { + if (StringUtils.isNotBlank(user.getPhone())) { + user.setInfoCompleteStatus(0); + } else { + user.setInfoCompleteStatus(2); } - user.setUnionId(session.getUnionid()); - user.setHeaderImg(userInfo.getAvatarUrl()); - user.setName(userInfo.getNickName()); - user.setOpenId(userInfo.getOpenId()); - highUserService.updateUser(user); } + user.setName(userInfo.getNickName()); + user.setHeaderImg(userInfo.getAvatarUrl()); + highUserService.updateUser(user); // 定义个人所有数据 HighUserModel highUserModel = new HighUserModel(); HighUser detailData = highUserService.getDetailDataByUser(user.getId()); detailData.setPassword(null); highUserModel.setHighUser(detailData); - SessionObject so = new SessionObject(user.getUnionId(), 1 , highUserModel); + if (user.getPhone() == null) { + log.error("login error!", "未绑定手机号"); + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未绑定手机号"); + } + SessionObject so = new SessionObject(user.getPhone(), 1 , highUserModel); userCenter.save(request, response, so); return ResponseMsgUtil.success(so); @@ -326,39 +229,6 @@ public class WechatController { } } - - @RequestMapping(value = "/getH5Code", method = RequestMethod.GET) - @ResponseBody - @ApiOperation(value = "【H5】获取Code") - public ResponseData getH5Code() { - try { - Map params = new LinkedHashMap<>(); - params.put("appid", SysConst.getSysConfig().getWxH5AppId()); - params.put("redirect_uri", URLEncoder.encode("https://hsgcs.dctpay.com/H5/index.html#/", "utf-8")); - //params.put("redirect_uri", "https://hsgcs.dctpay.com/H5/index.html#/"); - params.put("response_type", "code"); - params.put("scope", "snsapi_userinfo"); - params.put("state", "STATE"); - - String apiUrl = "https://open.weixin.qq.com/connect/oauth2/authorize"; - StringBuffer param = new StringBuffer(); - int i = 0; - for (String key : params.keySet()) { - if (i == 0) - param.append("?"); - else - param.append("&"); - param.append(key).append("=").append(params.get(key)); - i++; - } - apiUrl += param + "#wechat_redirect"; - return ResponseMsgUtil.success(apiUrl); - - } catch (Exception e) { - return ResponseMsgUtil.exception(e); - } - } - @RequestMapping(value = "/getH5AccessToken", method = RequestMethod.GET) @ResponseBody @ApiOperation(value = "【H5】获取AccessToken") @@ -375,133 +245,4 @@ public class WechatController { return ResponseMsgUtil.exception(e); } } - - @RequestMapping(value = "/getH5UserInfo", method = RequestMethod.GET) - @ResponseBody - @ApiOperation(value = "【H5】获取用户信息") - public ResponseData getH5UserInfo(@RequestParam(value = "code", required = true) String code, - HttpServletRequest request, HttpServletResponse response) { - try { - - // 获取access_token - Map params = new HashMap<>(); - params.put("appid", SysConst.getSysConfig().getWxH5AppId()); - params.put("secret", SysConst.getSysConfig().getWxH5AppSecret()); - params.put("code", code); - params.put("grant_type", "authorization_code"); - JSONObject jsonObject = HttpsUtils.doGet("https://api.weixin.qq.com/sns/oauth2/access_token", params); - String accessToken = jsonObject.getString("access_token"); - String openid = jsonObject.getString("openid"); - if (StringUtils.isBlank(accessToken) || StringUtils.isBlank(openid)) { - log.error("getH5UserInfo error!", "获取微信access_token失败" ); - throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "获取微信access_token失败"); - } - params.clear(); - params.put("access_token", accessToken); - params.put("openid", openid); - params.put("lang", "zh_CN"); - JSONObject userInfo = HttpsUtils.doGet("https://api.weixin.qq.com/sns/userinfo", params); - if (StringUtils.isNotBlank(userInfo.getString("errcode"))) { - log.error("getH5UserInfo error!", "获取微信用户信息失败"); - throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "获取微信用户信息失败"); - } - if (StringUtils.isBlank(userInfo.getString("unionid"))) { - throw ErrorHelp.genException(SysCode.MiniProgram, ErrorCode.WECHAT_DECRYPT_ERROR, "获取UnionId失败"); - } - - HighUser user = highUserService.findByUnionId(userInfo.getString("unionid")); - if (user == null) { - user = new HighUser(); - user.setUnionId(userInfo.getString("unionid")); - user.setOpenIdH5(openid); - user.setRegTime(new Date()); - user.setStatus(1); - user.setGold(0); - user.setHeaderImg(userInfo.getString("headimgurl")); - user.setName(userInfo.getString("nickname")); - user.setInfoCompleteStatus(2); - highUserService.insertUser(user); - } else { - // 用户信息完整状态 - // 0:完整 - // 1:需要填写用户信息 - // 2:需要填写手机号 - if (user.getInfoCompleteStatus() == 1) { - if (StringUtils.isNotBlank(user.getPhone())) { - user.setInfoCompleteStatus(0); - } else { - user.setInfoCompleteStatus(2); - } - } - user.setOpenIdH5(openid); - user.setHeaderImg(userInfo.getString("headimgurl")); - user.setName(userInfo.getString("nickname")); - highUserService.updateUser(user); - } - - // 定义个人所有数据 - HighUserModel highUserModel = new HighUserModel(); - HighUser detailData = highUserService.getDetailDataByUser(user.getId()); - detailData.setPassword(null); - highUserModel.setHighUser(detailData); - SessionObject so = new SessionObject(user.getUnionId(), 1 , highUserModel); - userCenter.save(request, response, so); - return ResponseMsgUtil.success(so); - - } catch (Exception e) { - return ResponseMsgUtil.exception(e); - } - } - - - @RequestMapping(value = "/getUnionId", method = RequestMethod.GET) - @ResponseBody - @ApiOperation(value = "刷取unionid") - public ResponseData getUnionId(HttpServletRequest request, HttpServletResponse response) { - try { - // 获取access_token - Map params = new HashMap<>(); - params.put("appid", SysConst.getSysConfig().getWxAppId()); - params.put("secret", SysConst.getSysConfig().getWxAppSecret()); - params.put("grant_type", "client_credential"); - JSONObject jsonObject = HttpsUtils.doGet("https://api.weixin.qq.com/cgi-bin/token", params); - System.out.println(jsonObject.toJSONString()); - String accessToken = jsonObject.getString("access_token"); - if (StringUtils.isBlank(accessToken)) { - log.error("getH5UserInfo error!", "获取微信access_token失败" ); - throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "获取微信access_token失败"); - } - params.clear(); - List> userMapList = new ArrayList<>(); - Map userMap; - int pageNum = 1; - while (true) { - userMapList.clear();; - PageHelper.startPage(pageNum, 100); - PageInfo pageInfo = new PageInfo<>(highUserService.getListUser(new HashMap<>())); - if (pageInfo.getList().size() > 0) { - for (HighUser user : pageInfo.getList()) { - userMap = new HashMap<>(); - userMap.put("openid", user.getOpenId()); - userMap.put("lang", "zh_CN"); - userMapList.add(userMap); - } - - params.put("user_list", userMapList); - System.out.println(JSONObject.toJSONString(params)); - JSONObject userInfo = HttpsUtils.doPost("https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token=" + accessToken, JSONObject.toJSONString(params)); - System.out.println(userInfo.toJSONString()); - } - pageNum++; - break; - } - - return ResponseMsgUtil.success("处理成功"); - - } catch (Exception e) { - return ResponseMsgUtil.exception(e); - } - } - - }