1
0
mirror of https://gitee.com/coder-xiaomo/flashsale synced 2025-09-13 07:21:38 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee

添加用户注册功能;Encrpt修改为Encrypt;添加填充测试数据

This commit is contained in:
2022-03-02 11:28:19 +08:00
parent 54144d49c1
commit a02d930f11
10 changed files with 204 additions and 59 deletions

View File

@@ -6,19 +6,22 @@ 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;
import org.springframework.web.bind.annotation.*;
import sun.misc.BASE64Encoder;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
@Controller("user") // 允许被SpringBoot扫描到
@RequestMapping("/user") // 通过 "/user" 访问到
@CrossOrigin // 允许跨域
@CrossOrigin(allowCredentials = "true", allowedHeaders = "*") // 允许跨域
public class UserController extends BaseController {
@Autowired
@@ -35,26 +38,35 @@ public class UserController extends BaseController {
@RequestParam(name = "name") String name,
@RequestParam(name = "gender") Integer gender,
@RequestParam(name = "age") Integer age,
@RequestParam(name = "password") String password) throws BusinessException {
@RequestParam(name = "password") String password) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException {
// 验证手机号和对应的otpcode相符合
String inSessionOtpCode = (String) this.httpServletRequest.getSession().getAttribute(telephone);
if (com.alibaba.druid.util.StringUtils.equals(otpCode, inSessionOtpCode)) {
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.setGender(new Byte(String.valueOf(gender.intValue())));
userModel.setAge(age);
userModel.setTelephone(telephone);
userModel.setRegisterMode("phone");
userModel.setEncryptPassword(MD5Encoder.encode(password.getBytes()));
userModel.setEncryptPassword(this.EncodeByMD5(password));
userService.register(userModel);
return CommonReturnType.create(null);
}
public String EncodeByMD5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {
// 确定一个计算方法
MessageDigest md5 = MessageDigest.getInstance("MD5");
BASE64Encoder base64Encoder = new BASE64Encoder();
// 加密字符串
String newstr = base64Encoder.encode(md5.digest(str.getBytes("utf-8")));
return newstr;
}
// 用户获取OTP短信接口
@RequestMapping(value = "/getotp", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
@ResponseBody

View File

@@ -13,11 +13,11 @@ public class UserPasswordDO {
/**
*
* This field was generated by MyBatis Generator.
* This field corresponds to the database column user_password.encrpt_password
* This field corresponds to the database column user_password.encrypt_password
*
* @mbg.generated Tue Mar 01 14:58:29 CST 2022
*/
private String encrptPassword;
private String encryptPassword;
/**
*
@@ -54,26 +54,26 @@ public class UserPasswordDO {
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column user_password.encrpt_password
* This method returns the value of the database column user_password.encrypt_password
*
* @return the value of user_password.encrpt_password
* @return the value of user_password.encrypt_password
*
* @mbg.generated Tue Mar 01 14:58:29 CST 2022
*/
public String getEncrptPassword() {
return encrptPassword;
public String getEncryptPassword() {
return encryptPassword;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column user_password.encrpt_password
* This method sets the value of the database column user_password.encrypt_password
*
* @param encrptPassword the value for user_password.encrpt_password
* @param encryptPassword the value for user_password.encrypt_password
*
* @mbg.generated Tue Mar 01 14:58:29 CST 2022
*/
public void setEncrptPassword(String encrptPassword) {
this.encrptPassword = encrptPassword == null ? null : encrptPassword.trim();
public void setEncryptPassword(String encryptPassword) {
this.encryptPassword = encryptPassword == null ? null : encryptPassword.trim();
}
/**

View File

@@ -47,9 +47,12 @@ public class UserServiceImpl implements UserService {
|| StringUtils.isEmpty(userModel.getTelephone())) {
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
}
UserDO userDO = new UserDO();
userDOMapper.insertSelective(userDO);
// 实现 model -> dataobject 方法
UserDO userDO = convertFormModel(userModel);
userDOMapper.insertSelective(userDO);
userModel.setId(userDO.getId());
UserPasswordDO userPasswordDO = convertPasswordFormModel(userModel);
userPasswordDOMapper.insertSelective(userPasswordDO);
@@ -67,7 +70,7 @@ public class UserServiceImpl implements UserService {
BeanUtils.copyProperties(userDO, userModel);
if (userDO != null) {
// 不可再使用copyProperties因为里面id字段是重复的
userModel.setEncryptPassword(userPasswordDO.getEncrptPassword());
userModel.setEncryptPassword(userPasswordDO.getEncryptPassword());
}
return userModel;
@@ -88,7 +91,7 @@ public class UserServiceImpl implements UserService {
return null;
}
UserPasswordDO userPasswordDO = new UserPasswordDO();
userPasswordDO.setEncrptPassword(userModel.getEncryptPassword());
userPasswordDO.setEncryptPassword(userModel.getEncryptPassword());
userPasswordDO.setUserId(userModel.getId());
return userPasswordDO;

View File

@@ -4,7 +4,7 @@ server.port=8090
mybatis.mapperLocations=classpath:mapping/*.xml
spring.datasource.name=flashsale
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/flashsale
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/flashsale?useSSL=false
spring.datasource.username=root
spring.datasource.password=111111

View File

@@ -56,7 +56,7 @@
#{age,jdbcType=INTEGER}, #{telephone,jdbcType=VARCHAR}, #{registerMode,jdbcType=VARCHAR},
#{thirdPartyId,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.cxyxiaomo.flashsale.dataobject.UserDO">
<insert id="insertSelective" parameterType="com.cxyxiaomo.flashsale.dataobject.UserDO" keyProperty="id" useGeneratedKeys="true">
<!--
WARNING - @mbg.generated
This element is automatically generated by MyBatis Generator, do not modify.

View File

@@ -8,7 +8,7 @@
This element was generated on Tue Mar 01 14:58:29 CST 2022.
-->
<id column="id" jdbcType="INTEGER" property="id" />
<result column="encrpt_password" jdbcType="VARCHAR" property="encrptPassword" />
<result column="encrypt_password" jdbcType="VARCHAR" property="encryptPassword" />
<result column="user_id" jdbcType="INTEGER" property="userId" />
</resultMap>
<sql id="Base_Column_List">
@@ -17,7 +17,7 @@
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Tue Mar 01 14:58:29 CST 2022.
-->
id, encrpt_password, user_id
id, encrypt_password, user_id
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
<!--
@@ -51,9 +51,9 @@
This element is automatically generated by MyBatis Generator, do not modify.
This element was generated on Tue Mar 01 14:58:29 CST 2022.
-->
insert into user_password (id, encrpt_password, user_id
insert into user_password (id, encrypt_password, user_id
)
values (#{id,jdbcType=INTEGER}, #{encrptPassword,jdbcType=VARCHAR}, #{userId,jdbcType=INTEGER}
values (#{id,jdbcType=INTEGER}, #{encryptPassword,jdbcType=VARCHAR}, #{userId,jdbcType=INTEGER}
)
</insert>
<insert id="insertSelective" parameterType="com.cxyxiaomo.flashsale.dataobject.UserPasswordDO">
@@ -67,8 +67,8 @@
<if test="id != null">
id,
</if>
<if test="encrptPassword != null">
encrpt_password,
<if test="encryptPassword != null">
encrypt_password,
</if>
<if test="userId != null">
user_id,
@@ -78,8 +78,8 @@
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="encrptPassword != null">
#{encrptPassword,jdbcType=VARCHAR},
<if test="encryptPassword != null">
#{encryptPassword,jdbcType=VARCHAR},
</if>
<if test="userId != null">
#{userId,jdbcType=INTEGER},
@@ -94,8 +94,8 @@
-->
update user_password
<set>
<if test="encrptPassword != null">
encrpt_password = #{encrptPassword,jdbcType=VARCHAR},
<if test="encryptPassword != null">
encrypt_password = #{encryptPassword,jdbcType=VARCHAR},
</if>
<if test="userId != null">
user_id = #{userId,jdbcType=INTEGER},
@@ -110,7 +110,7 @@
This element was generated on Tue Mar 01 14:58:29 CST 2022.
-->
update user_password
set encrpt_password = #{encrptPassword,jdbcType=VARCHAR},
set encrypt_password = #{encryptPassword,jdbcType=VARCHAR},
user_id = #{userId,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>