From 0cbf984e9bfa697370864d1a4927970990b25eee Mon Sep 17 00:00:00 2001 From: = <=> Date: Thu, 27 May 2021 23:34:57 +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 --- .../cweb/controller/HighUserController.java | 38 +++++++++---------- .../com/cweb/controller/WechatController.java | 3 -- .../java/com/hai/service/HighUserService.java | 10 +++++ .../hai/service/impl/HighUserServiceImpl.java | 35 ++++++++++++++++- 4 files changed, 63 insertions(+), 23 deletions(-) diff --git a/hai-cweb/src/main/java/com/cweb/controller/HighUserController.java b/hai-cweb/src/main/java/com/cweb/controller/HighUserController.java index 0095c2bd..252a9c0a 100644 --- a/hai-cweb/src/main/java/com/cweb/controller/HighUserController.java +++ b/hai-cweb/src/main/java/com/cweb/controller/HighUserController.java @@ -18,6 +18,8 @@ import com.hai.service.HighUserCouponService; import com.hai.service.HighUserService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import org.apache.commons.lang3.StringUtils; +import org.apache.poi.util.StringUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -29,6 +31,7 @@ import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; /** * @Auther: 袁野 @@ -79,37 +82,34 @@ public class HighUserController { } - @RequestMapping(value = "/updateUserPhone", method = RequestMethod.GET) + @RequestMapping(value = "/bindUserPhone", method = RequestMethod.GET) @ResponseBody - @ApiOperation(value = "修改用户手机号") - public ResponseData updateUserPhone(@RequestParam(value = "phone", required = true) String phone, + @ApiOperation(value = "绑定用户手机号") + public ResponseData bindUserPhone(@RequestParam(value = "phone", required = true) String phone, @RequestParam(value = "code", required = true) String code, - HttpServletRequest request) { + HttpServletRequest request, HttpServletResponse response) { try { - VerifyCode verifyCode = VerifyCodeStorage.getDate(phone); if(verifyCode == null) { - log.error("HighUserController --> updateUserPhone() error!", "验证码错误"); + log.error("HighUserController --> bindUserPhone() error!", "验证码错误"); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "验证码错误"); } if(!verifyCode.getObject().equals(code)) { - log.error("HighUserController --> updateUserPhone() error!", "验证码错误"); + log.error("HighUserController --> bindUserPhone() error!", "验证码错误"); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "验证码错误"); } - // 用户 - SessionObject sessionObject = userCenter.getSessionObject(request); - HighUserModel userInfoModel = (HighUserModel) sessionObject.getObject(); - HighUser highUser = highUserService.findByUserId(userInfoModel.getHighUser().getId()); - if (highUser == null) { - log.error("HighUserController --> updateUserPhone() error!", "未找到用户"); - throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到用户"); - } - highUser.setPhone(phone); - highUserService.updateUser(highUser); - return ResponseMsgUtil.success("修改成功"); + + HighUser highUser = highUserService.bindUserPhone(phone, request); + + HighUserModel highUserModel = new HighUserModel(); + highUser.setPassword(null); + highUserModel.setHighUser(highUser); + SessionObject so = new SessionObject(highUser.getOpenId(), 1 , highUserModel); + userCenter.save(request, response, so); + return ResponseMsgUtil.success(so); } catch (Exception e) { - log.error("HighUserController --> updateUserPhone() error!", e); + log.error("HighUserController --> bindUserPhone() error!", e); return ResponseMsgUtil.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 07e17cf3..6e5bee61 100644 --- a/hai-cweb/src/main/java/com/cweb/controller/WechatController.java +++ b/hai-cweb/src/main/java/com/cweb/controller/WechatController.java @@ -146,7 +146,6 @@ public class WechatController { highUserService.insertUser(user); } else { user.setPhone(phoneNoInfo.getPurePhoneNumber()); - user.setOpenId(openId);; highUserService.updateUser(user); } @@ -203,7 +202,6 @@ public class WechatController { user.setName(userInfo.getNickName()); highUserService.insertUser(user); } else { - user.setOpenId(openId); user.setHeaderImg(userInfo.getAvatarUrl()); user.setName(userInfo.getNickName()); highUserService.updateUser(user); @@ -314,7 +312,6 @@ public class WechatController { user.setName(userInfo.getString("nickname")); highUserService.insertUser(user); } else { - user.setOpenId(openid); user.setHeaderImg(userInfo.getString("headimgurl")); user.setName(userInfo.getString("nickname")); highUserService.updateUser(user); diff --git a/hai-service/src/main/java/com/hai/service/HighUserService.java b/hai-service/src/main/java/com/hai/service/HighUserService.java index e9d7b737..97593df7 100644 --- a/hai-service/src/main/java/com/hai/service/HighUserService.java +++ b/hai-service/src/main/java/com/hai/service/HighUserService.java @@ -2,6 +2,7 @@ package com.hai.service; import com.hai.entity.HighUser; +import javax.servlet.http.HttpServletRequest; import java.util.List; import java.util.Map; @@ -53,6 +54,15 @@ public interface HighUserService { **/ HighUser findByPhone(String phone); + /** + * 绑定手机号 + * @param phone + * @param request + * @return + * @throws Exception + */ + HighUser bindUserPhone(String phone, HttpServletRequest request) throws Exception; + /** * * @Title: updateUser diff --git a/hai-service/src/main/java/com/hai/service/impl/HighUserServiceImpl.java b/hai-service/src/main/java/com/hai/service/impl/HighUserServiceImpl.java index 7c3a531a..a51a6115 100644 --- a/hai-service/src/main/java/com/hai/service/impl/HighUserServiceImpl.java +++ b/hai-service/src/main/java/com/hai/service/impl/HighUserServiceImpl.java @@ -3,11 +3,14 @@ package com.hai.service.impl; import com.hai.common.exception.ErrorCode; import com.hai.common.exception.ErrorHelp; import com.hai.common.exception.SysCode; +import com.hai.common.security.SessionObject; +import com.hai.common.security.UserCenter; import com.hai.common.utils.DateUtil; import com.hai.dao.HighUserMapper; import com.hai.entity.HighGoldRec; import com.hai.entity.HighUser; import com.hai.entity.HighUserExample; +import com.hai.model.HighUserModel; import com.hai.service.HighGoldRecService; import com.hai.service.HighUserService; import org.apache.commons.lang3.StringUtils; @@ -16,6 +19,7 @@ import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; import java.util.Date; import java.util.List; import java.util.Map; @@ -32,6 +36,9 @@ public class HighUserServiceImpl implements HighUserService { @Resource private HighGoldRecService highGoldRecService; + @Resource + private UserCenter userCenter; + @Override public List getListUser(Map map) { @@ -76,7 +83,7 @@ public class HighUserServiceImpl implements HighUserService { @Override public HighUser findByPhone(String phone) { HighUserExample example = new HighUserExample(); - example.createCriteria().andPhoneEqualTo(phone); + example.createCriteria().andPhoneEqualTo(phone).andStatusEqualTo(1); List list = highUserMapper.selectByExample(example); if (list != null && list.size() > 0) { return list.get(0); @@ -84,6 +91,32 @@ public class HighUserServiceImpl implements HighUserService { return null; } + @Override + @Transactional(propagation= Propagation.REQUIRES_NEW) + public HighUser bindUserPhone(String phone, HttpServletRequest request) throws Exception { + // 查询手机号是否被绑定 + HighUser user = findByPhone(phone); + if (user != null && StringUtils.isNotBlank(user.getOpenId())) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "手机号已被绑定"); + } + + // 用户 + SessionObject sessionObject = userCenter.getSessionObject(request); + HighUserModel userInfoModel = (HighUserModel) sessionObject.getObject(); + HighUser highUser = findByUserId(userInfoModel.getHighUser().getId()); + if (highUser == null) { + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到用户"); + } + highUser.setStatus(0); + highUserMapper.updateByPrimaryKey(highUser); + + user.setHeaderImg(highUser.getHeaderImg()); + user.setName(highUser.getName()); + user.setOpenId(highUser.getOpenId()); + highUserMapper.updateByPrimaryKey(user); + return user; + } + @Override public void updateUser(HighUser highUser) { highUserMapper.updateByPrimaryKeySelective(highUser);