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. 1、c++对c语言的扩展

    1.类型增强 检查更加严格 比如,把一个 const 类型的指针赋给非 const 类型的指针.c 语言中可以通的过,但是在 c++中则编不过去 ; int b = a; const int *pa ...

  2. Android so注入(inject)和Hook技术学习(一)

    以前对Android so的注入只是通过现有的框架,并没有去研究so注入原理,趁现在有时间正好拿出来研究一下. 首先来看注入流程.Android so的注入流程如下: attach到远程进程 -> ...

  3. VS2017设置主题和代码字体

    从Visual Studio Marketplace下载主题插件 VS2017带有一个软件超市(Visual Studio Marketplace),可以和Eclipse一样从软件超市中下载各种插件, ...

  4. 开源工具 DotnetRSA 快速生成和转换RSA秘钥

    一.简介 DotnetRSA 是一个利用 .NET Core 2.1 开发的 .NET Global Tool,是可以想npm全局安装一样,安装在你的系统中,只需敲一行命令便可以快速生成RSA加密算法 ...

  5. ASP.NET Core 与支付宝开发文档

    一.目录 ASP.NET Core 2.0 使用支付宝PC网站支付 ASP.NET Core 2.0 支付宝当面付之扫码支付 常见使用问题解答 已有多个公司数个项目用本组件并上线,稳定使用. 二.项目 ...

  6. 617. Merge Two Binary Trees(Easy)

    Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...

  7. python3 pip 安装Scrapy在win10 安装报错error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools

    问题描述 当前环境win10,python_3.6.1,64位. 在windows下,在dos中运行pip install Scrapy报错: building 'twisted.test.raise ...

  8. amd,cmd规范

    AMD 和 CMD 的区别有哪些? AMD规范与CMD规范的区别 回顾:前端模块化和AMD.CMD规范(全) 浅析JS模块规范:AMD,CMD,CommonJS 理解AMD ,CMD,CommonJS ...

  9. 牛客练习赛38 D 题 出题人的手环 (离散化+树状数组求逆序对+前缀和)

    链接:https://ac.nowcoder.com/acm/contest/358/D来源:牛客网 出题人的手环 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K,其他 ...

  10. 福州大学软件工程1816 | W班 第5次作业成绩排名

    写在前面 汇总成绩排名链接 1.作业链接 第五次作业--项目选题报告(团队) 2.评分准则 本次作业映射总分为100分+贡献度得分,由以下部分组成: 选题报告内容(10分) 本组评审表设计(5分) 现 ...