SpringBoot构建电商基础秒杀项目 学习笔记

UserService 添加

void register(UserModel userModel) throws BusinessException;

UserServiceImpl 添加

    @Override
@Transactional // 事务操作
public void register(UserModel userModel) throws BusinessException {
if(userModel == null){
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
} if(StringUtils.isEmpty(userModel.getTelphone())
|| StringUtils.isEmpty(userModel.getName())
|| userModel.getGender() == null
|| userModel.getAge() == null
|| StringUtils.isEmpty(userModel.getEncrptPassword())){
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
} UserDO userDO = convertFromModel(userModel);
try{
userDOMapper.insertSelective(userDO);
}catch (DuplicateKeyException ex){
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "手机号码已注册");
} userModel.setId(userDO.getId()); // 获取自增 id UserPasswordDO userPasswordDO = convertPasswordFromModel(userModel);
userPasswordDOMapper.insertSelective(userPasswordDO); return;
} private UserDO convertFromModel(UserModel userModel){
if(userModel == null){
return null;
} UserDO userDO = new UserDO();
BeanUtils.copyProperties(userModel, userDO); return userDO;
} private UserPasswordDO convertPasswordFromModel(UserModel userModel){
if(userModel == null){
return null;
} UserPasswordDO userPasswordDO = new UserPasswordDO();
userPasswordDO.setEncrptPassword(userModel.getEncrptPassword());
userPasswordDO.setUserId(userModel.getId()); return userPasswordDO;
}

userDOMapper.xml 修改

返回自增 id

<insert id="insertSelective" parameterType="com.karonda.dataobject.UserDO" keyProperty="id" useGeneratedKeys="true">

UserController 修改

@CrossOrigin(allowCredentials = "true", allowedHeaders = "*") // 解决跨域问题

    @RequestMapping(value = "/register", method = {RequestMethod.POST}, consumes = {CONTENT_TYPE_FORMED})
@ResponseBody
public CommonReturnType register(@RequestParam(name="telphone") String telphone,
@RequestParam(name="otpCode") String otpCode,
@RequestParam(name="name") String name,
@RequestParam(name="gender") Integer gender,
@RequestParam(name="age") Integer age,
@RequestParam(name="password") String password)
throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException { String inSessionOtpCode = (String)this.httpServletRequest.getSession().getAttribute(telphone);
if(!StringUtils.equals(otpCode, inSessionOtpCode)){
throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR, "短信验证码不正确");
} UserModel userModel = new UserModel();
userModel.setTelphone(telphone);
userModel.setName(name);
userModel.setGender(new Byte(String.valueOf(gender)));
userModel.setAge(age);
userModel.setEncrptPassword(EncodeByMd5(password));
userModel.setRegisterMode("byphone"); userService.register(userModel); return CommonReturnType.create(null);
} private String EncodeByMd5(String str) throws UnsupportedEncodingException, NoSuchAlgorithmException {
MessageDigest md5 = MessageDigest.getInstance("MD5");
BASE64Encoder base64Encoder = new BASE64Encoder(); String newstr = base64Encoder.encode(md5.digest(str.getBytes("utf-8"))); return newstr;
}

新建 register.html

<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head> <body>
<div id="app">
<el-row>
<el-col :span="8" :offset="8">
<h3>用户注册</h3>
<el-form ref="form" :model="form" label-width="80px">
<el-form-item label="手机号">
<el-input v-model="form.telphone"></el-input>
</el-form-item>
<el-form-item label="验证码">
<el-input v-model="form.otpCode"></el-input>
</el-form-item>
<el-form-item label="昵称">
<el-input v-model="form.name"></el-input>
</el-form-item>
<el-form-item label="性别">
<el-switch
v-model="form.gender"
active-color="#ff4949"
inactive-color="#13ce66"
active-value="2"
inactive-value="1"
active-text="女"
inactive-text="男"></el-switch>
</el-form-item>
<el-form-item label="年龄">
<el-input v-model="form.age"></el-input>
</el-form-item>
<el-form-item label="密码">
<el-input v-model="form.password" show-password></el-input>
</el-form-item> <el-form-item>
<el-button type="primary" @click="onSubmit">注册</el-button>
</el-form-item>
</el-form>
</el-col>
</el-row>
</div>
</body> <script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://cdn.bootcss.com/axios/0.18.0/axios.min.js"></script>
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>
var app = new Vue({
el: '#app',
data: {
form: {
telphone: '',
otpCode: '',
name: '',
gender: 1,
age: 0,
password: '',
}
},
methods: {
onSubmit(){ if(this.form.telphone == null || this.form.telphone == ''){
this.$message({
message: '手机号不能为空',
type: 'warning'
});
return;
} // https://www.cnblogs.com/yesyes/p/8432101.html
axios({
method: 'post',
url: 'http://localhost:8080/user/register',
data: this.form,
params: this.form,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
withCredentials: true,
})
.then(resp=>{
if(resp.data.status == 'success'){
this.$message({
message: '注册成功',
type: 'success'
});
}else{
this.$message.error('注册失败,原因为:' + resp.data.data.errMsg);
}
})
.catch(err =>{
this.$message.error('注册失败,原因为:' + err.status + ', ' + err.statusText);
});
},
}, });
</script> </html>

withCredentials 要设置为 true (getotp.html 同样需要设置)

源码:spring-boot-seckill

Spring Boot 构建电商基础秒杀项目 (五) 用户注册的更多相关文章

  1. Spring Boot 构建电商基础秒杀项目 (一) 项目搭建

    SpringBoot构建电商基础秒杀项目 学习笔记 Spring Boot 其实不是什么新的框架,它默认配置了很多框架的使用方式,就像 maven 整合了所有的 jar 包, Spring Boot ...

  2. Spring Boot 构建电商基础秒杀项目 (十二) 总结 (完结)

    SpringBoot构建电商基础秒杀项目 学习笔记 系统架构 存在问题 如何发现容量问题 如何使得系统水平扩展 查询效率低下 活动开始前页面被疯狂刷新 库存行锁问题 下单操作步骤多,缓慢 浪涌流量如何 ...

  3. Spring Boot 构建电商基础秒杀项目 (十一) 秒杀

    SpringBoot构建电商基础秒杀项目 学习笔记 新建表 create table if not exists promo ( id int not null auto_increment, pro ...

  4. Spring Boot 构建电商基础秒杀项目 (十) 交易下单

    SpringBoot构建电商基础秒杀项目 学习笔记 新建表 create table if not exists order_info ( id varchar(32) not null defaul ...

  5. Spring Boot 构建电商基础秒杀项目 (九) 商品列表 & 详情

    SpringBoot构建电商基础秒杀项目 学习笔记 ItemDOMapper.xml 添加 <select id="listItem" resultMap="Bas ...

  6. Spring Boot 构建电商基础秒杀项目 (八) 商品创建

    SpringBoot构建电商基础秒杀项目 学习笔记 新建数据表 create table if not exists item ( id int not null auto_increment, ti ...

  7. Spring Boot 构建电商基础秒杀项目 (七) 自动校验

    SpringBoot构建电商基础秒杀项目 学习笔记 修改 UserModel 添加注解 public class UserModel { private Integer id; @NotBlank(m ...

  8. Spring Boot 构建电商基础秒杀项目 (六) 用户登陆

    SpringBoot构建电商基础秒杀项目 学习笔记 userDOMapper.xml 添加 <select id="selectByTelphone" resultMap=& ...

  9. Spring Boot 构建电商基础秒杀项目 (四) getotp 页面

    SpringBoot构建电商基础秒杀项目 学习笔记 BaseController 添加 public static final String CONTENT_TYPE_FORMED = "a ...

随机推荐

  1. JavaScript模块化思想requireJS的使用

    1. 使用require.js的意义   (1)实现JS文件的异步加载,避免网页因为加载JS文件缓慢造成网页未响应 (2)管理模块之间的依赖性,便于代码的编写和维护.页面中只需要引入require.j ...

  2. 工具篇-Java中一些utils

    下边是整理的一些Java开发的utils,顺便吐槽下新浪博客的编辑器排版跟我写的博客一样 烂,所以改用博客园 一.字符串 1. Java中String与其他类型之间的转换 String与日期对象 pu ...

  3. [MicroPython]STM32F407开发板DIY声光控开关

    1.实验目的 1. 学习在PC机系统中扩展简单I/O 接口的方法. 2. 进一步学习编制数据输出程序的设计方法. 3. 学习光敏模块的工作原理. 4. 学习声音的工作原理. 5. 学习F40 7Mic ...

  4. Ambari 使用 Hive View 异常处理

    异常:进入Hive View提示user home check fail 详细日志:Service 'userhome' check failed: java.io.FileNotFoundExcep ...

  5. JAVA验证身份证格式及合法性

    旅游电子商务中,预订酒店或订购门票时会以身份证作为消费凭证,为了防止客户误填身份证带来不必要麻烦,需要验证码格式及合法性,代码如下: /** * 判断身份证格式 * * @param idNum * ...

  6. 语法设计——基于LL(1)文法的预测分析表法

    实验二.语法设计--基于LL(1)文法的预测分析表法 一.实验目的 通过实验教学,加深学生对所学的关于编译的理论知识的理解,增强学生对所学知识的综合应用能力,并通过实践达到对所学的知识进行验证.通过对 ...

  7. C#中委托,匿名函数,lamda表达式复习

    一.委托 1.就给类比较,类用class声明,委托用delegate声明. 2.委托要指向一个真正的方法. 3.委托的签名,要和指向的方法一样. //1.声明一个委托 public delegate ...

  8. new、getInstance()、newInstance()、Class.forName()

    1.对象使用之前通过getinstance()得到而不需要自己定义,用完之后不需要delete: 2.new 一定要生成一个新对象,分配内存:getInstance() 则不一定要再次创建,它可以把一 ...

  9. Minimal string CodeForces – 797C

    题目链接 题目难度: 1700rating 题目类型:string+贪心+STL 题目思路: 由于题目要求的最终结果是字典序最小的那个字符串,那么我们从贪心的从’a’开始查找字符串里是否存在,如果存在 ...

  10. UIAutomatorViewer 出现错误:Unable to connect to adb

    最近升级了AndroidSDK,打开UIAutomatorViewer.bat,结果发现获取不了Android设备界面上的UI信息.经过一番努力,终于把这个问题解决了,详细过程如下: 1. Unabl ...