1
0
mirror of https://gitee.com/coder-xiaomo/flashsale synced 2025-01-10 11:48:14 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee

用户注册功能实现(后端)

This commit is contained in:
程序员小墨 2022-03-01 22:03:09 +08:00
parent 293cb0248b
commit 54144d49c1
4 changed files with 86 additions and 0 deletions

View File

@ -60,6 +60,11 @@
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
</dependencies>
<build>

View File

@ -2,9 +2,11 @@ package com.cxyxiaomo.flashsale.controller;
import com.cxyxiaomo.flashsale.controller.viewobject.UserVO;
import com.cxyxiaomo.flashsale.error.BusinessException;
import com.cxyxiaomo.flashsale.error.EmBusinessError;
import com.cxyxiaomo.flashsale.response.CommonReturnType;
import com.cxyxiaomo.flashsale.service.UserService;
import com.cxyxiaomo.flashsale.service.model.UserModel;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
@ -25,6 +27,34 @@ public class UserController extends BaseController {
@Autowired
private HttpServletRequest httpServletRequest;
// 用户注册接口
@RequestMapping(value = "/register", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
@ResponseBody
public CommonReturnType register(@RequestParam(name = "telephone") String telephone,
@RequestParam(name = "optCode") String otpCode,
@RequestParam(name = "name") String name,
@RequestParam(name = "gender") Integer gender,
@RequestParam(name = "age") Integer age,
@RequestParam(name = "password") String password) throws BusinessException {
// 验证手机号和对应的otpcode相符合
String inSessionOtpCode = (String) this.httpServletRequest.getSession().getAttribute(telephone);
if (com.alibaba.druid.util.StringUtils.equals(otpCode, inSessionOtpCode)) {
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "短信验证码不符合");
}
// 用户的注册流程
UserModel userModel = new UserModel();
userModel.setName(name);
userModel.setGender(gender);
userModel.setAge(age);
userModel.setTelephone(telephone);
userModel.setRegisterMode("phone");
userModel.setEncryptPassword(MD5Encoder.encode(password.getBytes()));
userService.register(userModel);
return CommonReturnType.create(null);
}
// 用户获取OTP短信接口
@RequestMapping(value = "/getotp", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
@ResponseBody

View File

@ -1,8 +1,12 @@
package com.cxyxiaomo.flashsale.service;
import com.cxyxiaomo.flashsale.error.BusinessException;
import com.cxyxiaomo.flashsale.service.model.UserModel;
public interface UserService {
// 通过用户id获取用户对象的方法
UserModel getUserById(Integer id);
// 用户注册
void register(UserModel userModel) throws BusinessException;
}

View File

@ -4,11 +4,15 @@ import com.cxyxiaomo.flashsale.dao.UserDOMapper;
import com.cxyxiaomo.flashsale.dao.UserPasswordDOMapper;
import com.cxyxiaomo.flashsale.dataobject.UserDO;
import com.cxyxiaomo.flashsale.dataobject.UserPasswordDO;
import com.cxyxiaomo.flashsale.error.BusinessException;
import com.cxyxiaomo.flashsale.error.EmBusinessError;
import com.cxyxiaomo.flashsale.service.UserService;
import com.cxyxiaomo.flashsale.service.model.UserModel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class UserServiceImpl implements UserService {
@ -31,6 +35,28 @@ public class UserServiceImpl implements UserService {
}
}
@Override
@Transactional
public void register(UserModel userModel) throws BusinessException {
if (userModel == null) {
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
}
if (StringUtils.isEmpty(userModel.getName())
|| userModel.getGender() == null
|| userModel.getAge() == null
|| StringUtils.isEmpty(userModel.getTelephone())) {
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
}
UserDO userDO = new UserDO();
userDOMapper.insertSelective(userDO);
// 实现 model -> dataobject 方法
UserPasswordDO userPasswordDO = convertPasswordFormModel(userModel);
userPasswordDOMapper.insertSelective(userPasswordDO);
return;
}
private UserModel convertFromDataObject(UserDO userDO, UserPasswordDO userPasswordDO) {
if (userDO == null) {
@ -46,4 +72,25 @@ public class UserServiceImpl implements UserService {
return userModel;
}
private UserDO convertFormModel(UserModel userModel) {
if (userModel == null) {
return null;
}
UserDO userDO = new UserDO();
BeanUtils.copyProperties(userModel, userDO);
return userDO;
}
private UserPasswordDO convertPasswordFormModel(UserModel userModel) {
if (userModel == null) {
return null;
}
UserPasswordDO userPasswordDO = new UserPasswordDO();
userPasswordDO.setEncrptPassword(userModel.getEncryptPassword());
userPasswordDO.setUserId(userModel.getId());
return userPasswordDO;
}
}