From 007d1b53c2d2791ad821f9ac559649a5a75db522 Mon Sep 17 00:00:00 2001 From: hu177768073 <177768073@qq.com> Date: Fri, 27 Sep 2024 14:35:21 +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 --- .../com/hfkj/controller/LoginController.java | 3 +- .../hfkj/controller/UserGradeController.java | 218 ++++++++++++++++++ .../hfkj/common/security/SecUserCenter.java | 2 +- .../com/hfkj/common/security/UserCenter.java | 2 +- 4 files changed, 222 insertions(+), 3 deletions(-) create mode 100644 cweb/src/main/java/com/hfkj/controller/UserGradeController.java diff --git a/bweb/src/main/java/com/hfkj/controller/LoginController.java b/bweb/src/main/java/com/hfkj/controller/LoginController.java index 71097b9..35bce8b 100644 --- a/bweb/src/main/java/com/hfkj/controller/LoginController.java +++ b/bweb/src/main/java/com/hfkj/controller/LoginController.java @@ -14,6 +14,7 @@ import io.swagger.annotations.ApiOperation; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -32,7 +33,7 @@ public class LoginController { @Resource private SecUserService secUserService; - @Resource + @Autowired private SecUserCenter userCenter; @RequestMapping(value="/login",method = RequestMethod.POST) diff --git a/cweb/src/main/java/com/hfkj/controller/UserGradeController.java b/cweb/src/main/java/com/hfkj/controller/UserGradeController.java new file mode 100644 index 0000000..b85bc4e --- /dev/null +++ b/cweb/src/main/java/com/hfkj/controller/UserGradeController.java @@ -0,0 +1,218 @@ +package com.hfkj.controller; + +import com.alibaba.fastjson.JSONObject; +import com.hfkj.common.exception.ErrorCode; +import com.hfkj.common.exception.ErrorHelp; +import com.hfkj.common.exception.SysCode; +import com.hfkj.common.security.UserCenter; +import com.hfkj.common.utils.MemberValidateUtil; +import com.hfkj.common.utils.RedisUtil; +import com.hfkj.common.utils.ResponseMsgUtil; +import com.hfkj.model.ResponseData; +import com.hfkj.model.UserSessionObject; +import com.hfkj.service.user.BsUserService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +import javax.annotation.Resource; +import java.util.HashMap; +import java.util.Map; + +/** + * @className: ClientController + * @author: HuRui + * @date: 2024/9/6 + **/ +@Controller +@RequestMapping(value="/userGrade") +@Api(value="客户端业务") +public class UserGradeController { + @Resource + private BsUserService userService; + @Resource + private RedisUtil redisUtil; + @Autowired + private UserCenter userCenter; + + @RequestMapping(value = "/getUser", method = RequestMethod.GET) + @ResponseBody + @ApiOperation(value = "查询用户信息") + public ResponseData getUser() { + try { + Map param = new HashMap<>(); + // 用户信息 + param.put("user", userCenter.getSessionModel(UserSessionObject.class)); + + return ResponseMsgUtil.success(param); + + } catch (Exception e) { + return ResponseMsgUtil.exception(e); + } + } + + @RequestMapping(value = "/updateWechat", method = RequestMethod.POST) + @ResponseBody + @ApiOperation(value = "修改用户微信号") + public ResponseData updateWechat(@RequestBody JSONObject body) { + try { + if (body == null || StringUtils.isBlank(body.getString("wechatNum"))) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); + } + UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class); + userService.updateWechatNum(userSession.getUser().getId(), body.getString("wechatNum")); + return ResponseMsgUtil.success("操作成功"); + + } catch (Exception e) { + return ResponseMsgUtil.exception(e); + } + } + + @RequestMapping(value = "/bindPhone", method = RequestMethod.POST) + @ResponseBody + @ApiOperation(value = "绑定手机号") + public ResponseData bindPhone(@RequestBody JSONObject body) { + try { + if (body == null + || StringUtils.isBlank(body.getString("phone")) + || StringUtils.isBlank(body.getString("smsCode"))) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); + } + UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class); + if (StringUtils.isNotBlank(userSession.getUser().getPhone())) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "手机号已绑定"); + } + String phone = body.getString("phone"); + // 校验手机号格式 + if (!MemberValidateUtil.validatePhone(phone)) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请输入正确的手机号"); + } + if (StringUtils.isBlank(body.getString("smsCode"))) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请输入短信验证码"); + } + if (userService.getUser(phone) != null) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "手机号已被绑定"); + } + // 手机号的验证码 + Object phoneCodeObject = redisUtil.get("SMS_BIND_PHONE_CODE:" + phone); + if (phoneCodeObject == null) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "短信验证码错误"); + } + if (!body.getString("smsCode").equals(phoneCodeObject.toString())) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "短信验证码错误"); + } + redisUtil.del("SMS_BIND_PHONE_CODE:" + phone); + + // 更新手机号 + userService.updatePhone(userSession.getUser().getId(), body.getString("phone")); + + return ResponseMsgUtil.success(userCenter.getSessionModel(UserSessionObject.class)); + + } catch (Exception e) { + return ResponseMsgUtil.exception(e); + } + } + + @RequestMapping(value = "/bindInviteUser", method = RequestMethod.POST) + @ResponseBody + @ApiOperation(value = "绑定邀请人Id") + public ResponseData bindInviteUser(@RequestBody JSONObject body) { + try { + if (body == null || body.getLong("inviteUseId") == null) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); + } + UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class); + + // 绑定邀请人 + userService.bindInviteUser(userSession.getUser().getId(), body.getLong("inviteUseId")); + + return ResponseMsgUtil.success(userCenter.getSessionModel(UserSessionObject.class)); + + } catch (Exception e) { + return ResponseMsgUtil.exception(e); + } + } + + @RequestMapping(value = "/verifyUpdPhoneSmsCode", method = RequestMethod.POST) + @ResponseBody + @ApiOperation(value = "验证修改手机号验证码") + public ResponseData verifyUpdPhoneSmsCode(@RequestBody JSONObject body) { + try { + if (body == null + || StringUtils.isBlank(body.getString("phone")) + || StringUtils.isBlank(body.getString("smsCode"))) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); + } + String phone = body.getString("phone"); + // 手机号的验证码 + Object phoneCodeObject = redisUtil.get("SMS_UPDATE_PHONE_CODE:" + phone); + if (phoneCodeObject == null) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "短信验证码错误或已失效"); + } + if (!body.getString("smsCode").equals(phoneCodeObject.toString())) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "短信验证码错误或已失效"); + } + redisUtil.del("SMS_UPDATE_PHONE_CODE:" + phone); + + return ResponseMsgUtil.success(true); + + } catch (Exception e) { + return ResponseMsgUtil.exception(e); + } + } + + @RequestMapping(value = "/updatePhone", method = RequestMethod.POST) + @ResponseBody + @ApiOperation(value = "修改手机号") + public ResponseData updatePhone(@RequestBody JSONObject body) { + try { + if (body == null + || StringUtils.isBlank(body.getString("newPhone")) + || StringUtils.isBlank(body.getString("smsCode"))) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); + } + UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class); + if (StringUtils.isBlank(userSession.getUser().getPhone())) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "用户未绑定手机号"); + } + String phone = body.getString("newPhone"); + if (userSession.getUser().getPhone().equals(phone)) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "新手机号与现绑定手机号相同"); + } + if (userService.getUser(phone) != null) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "手机号已被绑定"); + } + // 校验手机号格式 + if (!MemberValidateUtil.validatePhone(phone)) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请输入正确的手机号"); + } + if (StringUtils.isBlank(body.getString("smsCode"))) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请输入短信验证码"); + } + // 手机号的验证码 + Object phoneCodeObject = redisUtil.get("SMS_UPDATE_PHONE_CODE:" + phone); + if (phoneCodeObject == null) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "短信验证码错误或已失效"); + } + if (!body.getString("smsCode").equals(phoneCodeObject.toString())) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "短信验证码错误或已失效"); + } + redisUtil.del("SMS_UPDATE_PHONE_CODE:" + phone); + + // 更新手机号 + userService.updatePhone(userSession.getUser().getId(), body.getString("newPhone")); + + return ResponseMsgUtil.success(userCenter.getSessionModel(UserSessionObject.class)); + + } catch (Exception e) { + return ResponseMsgUtil.exception(e); + } + } + +} diff --git a/service/src/main/java/com/hfkj/common/security/SecUserCenter.java b/service/src/main/java/com/hfkj/common/security/SecUserCenter.java index 6e06a08..7e75330 100644 --- a/service/src/main/java/com/hfkj/common/security/SecUserCenter.java +++ b/service/src/main/java/com/hfkj/common/security/SecUserCenter.java @@ -15,7 +15,7 @@ import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; -@Component() +@Component public class SecUserCenter { private static Logger log = LoggerFactory.getLogger(SecUserCenter.class); diff --git a/service/src/main/java/com/hfkj/common/security/UserCenter.java b/service/src/main/java/com/hfkj/common/security/UserCenter.java index 303eb64..01295d9 100644 --- a/service/src/main/java/com/hfkj/common/security/UserCenter.java +++ b/service/src/main/java/com/hfkj/common/security/UserCenter.java @@ -14,7 +14,7 @@ import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; -@Component() +@Component public class UserCenter { private static Logger log = LoggerFactory.getLogger(UserCenter.class);