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. P2257 YY的GCD--洛谷luogu

    传送门 题目描述 神犇YY虐完数论后给傻×kAc出了一题 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对 kAc这种傻×必然不 ...

  2. Python这么强大, 怎样才能快速入坑?

    作为一种年轻的编程语言,Python为何能在短短几年的时间内就以迅雷不及掩耳之势驰骋编程界?答案很简单,在人工智能时代,AlphaGo 都在使用的 Python语言,是最接近 AI 的编程语言. 随着 ...

  3. Java多线程核心技术(五)单例模式与多线程

    本文只需要考虑一件事:如何使单例模式遇到多线程是安全的.正确的 1.立即加载 / "饿汉模式" 什么是立即加载?立即加载就是使用类的时候已经将对象创建完毕,常见的实现办法就是直接 ...

  4. Ubuntu: 软件库(software repositories)

    Linux 生态下,几乎每个发行版都有自己的软件库(software repositories),Ubuntu 当然也不例外.Ubuntu 提供了四个不同的软件库,分别是 main.restricte ...

  5. 出题人的RP值(牛客练习赛38--A题)(排序)

    链接:https://ac.nowcoder.com/acm/contest/358/A来源:牛客网 题目描述 众所周知,每个人都有自己的rp值(是个非负实数),膜别人可以从别人身上吸取rp值. 然而 ...

  6. C#使用OneNote的图片文字识别功能(OCR)

    http://www.cnblogs.com/Charltsing/p/OneNoteOCR.html 有需要技术咨询的,联系QQ564955427 前段时间有人问我能不能通过OneNote扫描图片, ...

  7. 为什么HashMap初始大小为16,为什么加载因子大小为0.75,这两个值的选取有什么特点?

    先看HashMap的定义: public class HashMap<K,V>extends AbstractMap<K,V>implements Map<K,V> ...

  8. 数据快速批量添加到Elasticsearch

    如何把数据快速批量添加到Elasticsearch中 问题来源 最近新做一个项目,有部分搜索比较频繁的数据,而且量级比较大,预计一两年时间很可能达到100G,项目要求不要存在数据库中,最终出来有两个方 ...

  9. JMeter Exception: java.net.BindException: Address already in use: connect(转)

    转自:http://twit88.com/blog/2008/07/28/jmeter-exception-javanetbindexception-address-already-in-use-co ...

  10. React-Native之轮播组件looped-carousel的介绍与使用

    React-Native之轮播组件looped-carousel的介绍与使用 一,关于react-native轮播组件的介绍与对比 1,react-native-swiper在动态使用网页图片,多张图 ...