diff --git a/hai-cweb/src/main/java/com/cweb/controller/HighUserCommonController.java b/hai-cweb/src/main/java/com/cweb/controller/HighUserCommonController.java new file mode 100644 index 00000000..15447231 --- /dev/null +++ b/hai-cweb/src/main/java/com/cweb/controller/HighUserCommonController.java @@ -0,0 +1,87 @@ +package com.cweb.controller; + +import cn.binarywang.wx.miniapp.api.WxMaService; +import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; +import com.alibaba.fastjson.JSONObject; +import com.cweb.config.SessionKeyCache; +import com.cweb.config.WxMaConfiguration; +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.security.VerifyCode; +import com.hai.common.security.VerifyCodeStorage; +import com.hai.common.utils.ResponseMsgUtil; +import com.hai.entity.HighUser; +import com.hai.model.HighUserModel; +import com.hai.model.ResponseData; +import com.hai.service.HighUserService; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.util.Date; + +@Controller +@RequestMapping(value = "/user") +@Api(value = "用户共用接口") +public class HighUserCommonController { + + private static final Logger log = LoggerFactory.getLogger(WechatController.class); + + @Resource + private HighUserService highUserService; + + @Resource + private UserCenter userCenter; + + @RequestMapping(value = "/login", method = RequestMethod.GET) + @ResponseBody + @ApiOperation(value = "登录接口【用户不存在会自动注册】") + public ResponseData login(@RequestParam(value = "phone", required = true) String phone, + @RequestParam(value = "code", required = true) String code, + HttpServletRequest request, HttpServletResponse response) { + try { + VerifyCode verifyCode = VerifyCodeStorage.getDate(phone); + if(verifyCode == null) { + log.error("HighUserController --> bindUserPhone() error!", "验证码错误"); + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "验证码错误"); + } + if(!verifyCode.getObject().equals(code)) { + log.error("HighUserController --> bindUserPhone() error!", "验证码错误"); + throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "验证码错误"); + } + + HighUser user = highUserService.findByPhone(phone); + if(user == null) { + user = new HighUser(); + user.setName("用户" + System.currentTimeMillis()); + user.setPhone(phone); + user.setGold(0); + user.setRegTime(new Date()); + user.setStatus(1); + highUserService.insertUser(user); + } + // 定义个人所有数据 + HighUserModel highUserModel = new HighUserModel(); + user.setPassword(null); + highUserModel.setHighUser(user); + SessionObject so = new SessionObject(user.getOpenId(), 1 , highUserModel); + userCenter.save(request, response, so); + return ResponseMsgUtil.success(so); + } catch (Exception e) { + return ResponseMsgUtil.exception(e); + } + } +} 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 a51a6115..04ceede4 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 @@ -94,12 +94,6 @@ public class HighUserServiceImpl implements HighUserService { @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(); @@ -107,14 +101,25 @@ public class HighUserServiceImpl implements HighUserService { 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; + // 查询手机号是否被绑定 + HighUser user = findByPhone(phone); + if (user != null) { + if (StringUtils.isNotBlank(user.getOpenId())) { + 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; + } + highUser.setPhone(phone); + highUserMapper.updateByPrimaryKey(highUser); + return highUser; } @Override