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

添加用户注册功能;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

@ -11,7 +11,7 @@
Target Server Version : 50726
File Encoding : 65001
Date: 01/03/2022 17:01:05
Date: 02/03/2022 10:30:12
*/
SET NAMES utf8mb4;
@ -42,13 +42,13 @@ INSERT INTO `user_info` VALUES (1, 'admin', 2, 18, '110', 'phone', '');
-- ----------------------------
DROP TABLE IF EXISTS `user_password`;
CREATE TABLE `user_password` (
`id` int(11) NOT NULL,
`encrpt_password` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`id` int(11) NOT NULL AUTO_INCREMENT,
`encrypt_password` varchar(128) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`user_id` int(11) NOT NULL DEFAULT 0,
PRIMARY KEY (`id`) USING BTREE,
INDEX `user_id`(`user_id`) USING BTREE,
CONSTRAINT `user_password_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `user_info` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user_password

View File

@ -12,28 +12,6 @@
> 用户名密码均为 `admin`
### 本次修改通过以下方式查看👇
> http://localhost:8090/user/getotp?telephone=1
预期效果:
```json
{"data":null,"status":"success"}
```
控制台:
```sql
telephone = 1 & otpCode = 90824
```
> http://localhost:8090/user/get?id=2
预期效果:
```json
{"data":{"errCode":10002,"errMsg":"未知错误"},"status":"fail"}
```
### 层次结构以User为例自上向下
| | 目录 | Java对象类型 | 说明 | 举例 |

View File

@ -21,6 +21,12 @@
<button class="btn blue" id="getotp" type="submit">
获取OTP短信
</button>
<a href="register.html">用户注册</a>
<br>
<button class="btn blue" id="fillData">
快速测试
</button>
</div>
</div>
@ -39,9 +45,11 @@
data: {
"telephone": telephone,
},
xhrFields: {withCredentials: true},
success: function (data) {
if (data.status == "success") {
alert("OTP已经发送到了您的手机上请注意查收");
window.location.href = "./register.html";
} else {
alert("OTP发送失败原因为" + data.data.errMsg);
}
@ -51,6 +59,18 @@
}
})
})
$("#fillData").on("click", function () {
$("#telephone").val("18900000001");
$("#telephone").attr("disabled", true);
// 屏蔽弹窗
alert = function () {};
$("#getotp").click();
})
if(location.search=="?quickDebug") {
$("#fillData").click();
}
})
</script>
</body>

132
frontend/register.html Normal file
View File

@ -0,0 +1,132 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link href="static/assets/global/plugins/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css"/>
<link href="static/assets/global/css/components.css" rel="stylesheet" type="text/css"/>
<link href="static/assets/admin/pages/css/login.css" rel="stylesheet" type="text/css"/>
<script src="./static/assets/global/plugins/jquery-1.11.0.min.js" type="text/javascript"></script>
</head>
<body class="login">
<div class="content">
<h3 class="form-title">用户注册</h3>
<div class="from-group">
<label class="control-label">手机号</label>
<div>
<input class="form-control" type="text" placeholder="手机号" name="telephone" id="telephone">
</div>
</div>
<div class="from-group">
<label class="control-label">验证码</label>
<div>
<input class="form-control" type="text" placeholder="验证码" name="optCode" id="optCode">
</div>
</div>
<div class="from-group">
<label class="control-label">用户昵称</label>
<div>
<input class="form-control" type="text" placeholder="用户昵称" name="name" id="name">
</div>
</div>
<div class="from-group">
<label class="control-label">性别</label>
<div>
<input class="form-control" type="text" placeholder="性别" name="gender" id="gender">
</div>
</div>
<div class="from-group">
<label class="control-label">年龄</label>
<div>
<input class="form-control" type="text" placeholder="年龄" name="age" id="age">
</div>
</div>
<div class="from-group">
<label class="control-label">密码</label>
<div>
<input class="form-control" type="password" placeholder="密码" name="password" id="password">
</div>
</div>
<div class="form-actions">
<button class="btn blue" id="register" type="submit">
提交注册
</button>
<a href="getotp.html?quickDebug">获取验证码</a>
</div>
</div>
<script>
jQuery(document).ready(function () {
$("#register").on("click", function () {
var telephone = $("#telephone").val();
var optCode = $("#optCode").val();
var name = $("#name").val();
var gender = $("#gender").val();
var age = $("#age").val();
var password = $("#password").val();
if (telephone == null || telephone == "") {
alert("手机号不能为空");
return false; // 捕获onclick事件不让他传递到上一层
}
if (optCode == null || optCode == "") {
alert("验证码不能为空");
return false; // 捕获onclick事件不让他传递到上一层
}
if (name == null || name == "") {
alert("姓名不能为空");
return false; // 捕获onclick事件不让他传递到上一层
}
if (gender == null || gender == "") {
alert("性别不能为空");
return false; // 捕获onclick事件不让他传递到上一层
}
if (age == null || age == "") {
alert("年龄不能为空");
return false; // 捕获onclick事件不让他传递到上一层
}
if (password == null || password == "") {
alert("密码不能为空");
return false; // 捕获onclick事件不让他传递到上一层
}
$.ajax({
type: "POST",
contentType: "application/x-www-form-urlencoded",
url: "http://localhost:8090/user/register",
data: {
"telephone": telephone,
"optCode": optCode,
"name": name,
"gender": gender,
"age": age,
"password": password,
},
xhrFields: {withCredentials: true},
success: function (data) {
if (data.status == "success") {
alert("注册成功");
} else {
alert("注册失败,原因为" + data.data.errMsg);
}
},
error: function (data) {
alert("注册失败,原因为" + data.responseText);
}
})
})
function filldata() {
$("#telephone").val("18900000001");
var date = new Date();
$("#name").val("user-" + Math.random().toString(36).slice(-6) + "-" + date.getSeconds() + date.getMilliseconds());
$("#gender").val(Math.round(Math.random() * (2 - 1) + 1));
$("#age").val(Math.round(Math.random() * (100 - 1) + 1));
$("#password").val(Math.random().toString(36).slice(-6)); // 生成随机数转成36进制再截取部分
$("#optCode").focus();
$("#getotp").click();
}
filldata();
})
</script>
</body>
</html>

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>