基於驗證碼的使用者註冊
阿新 • • 發佈:2020-12-30
一後端介面
1引入工具類MD5
public final class MD5 { public static String encrypt(String strSrc) { try { char hexChars[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; byte[] bytes = strSrc.getBytes(); MessageDigest md = MessageDigest.getInstance("MD5"); md.update(bytes); bytes = md.digest(); int j = bytes.length; char[] chars = new char[j * 2]; int k = 0; for (int i = 0; i < bytes.length; i++) { byte b = bytes[i]; chars[k++] = hexChars[b >>> 4 & 0xf]; chars[k++] = hexChars[b & 0xf]; } return new String(chars); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); throw new RuntimeException("MD5加密出錯!!+" + e); } } public static void main(String[] args) { System.out.println(MD5.encrypt("111111")); } }
2定義RegisterVo
@Data public class RegisterVo implements Serializable { private static final long serialVersionUID = 1L; /** * 暱稱 */ private String nickname; /** * 手機 */ private String mobile; /** * 密碼 */ private String password; /** * 驗證碼 */ private String code; }
3web層
@Api(description = "會員管理") @CrossOrigin @RestController @RequestMapping("/api/ucenter/member") @Slf4j public class ApiMemberController { @Autowired private MemberService memberService; @ApiOperation(value = "會員註冊") @PostMapping("register") public R register(@RequestBody RegisterVo registerVo) { memberService.register(registerVo); return R.ok().message("註冊成功"); } }
4service層
介面
/**
* 功能描述:註冊會員
*
* @author cakin
* @date 2020/12/25
* @param registerVo 註冊資訊
*/
void register(RegisterVo registerVo);
5實現
/**
* @className: MemberServiceImpl
* @description: 會員表 服務實現類
* @date: 2020/12/25
* @author: cakin
*/
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {
@Autowired
private RedisTemplate redisTemplate;
/**
* 功能描述:註冊會員
*
* @param registerVo 註冊資訊
* @author cakin
* @date 2020/12/25
*/
@Override
public void register(RegisterVo registerVo) {
// 校驗引數
String nickname = registerVo.getNickname();
String mobile = registerVo.getMobile();
String password = registerVo.getPassword();
String code = registerVo.getCode();
// 校驗手機是否為空以及校驗手機是否非法
if (StringUtils.isEmpty(mobile)
|| !FormUtils.isMobile(mobile)) {
throw new GuliException(ResultCodeEnum.LOGIN_MOBILE_ERROR);
}
// 校驗其他欄位是否為空
if (StringUtils.isEmpty(nickname)
|| StringUtils.isEmpty(password)
|| StringUtils.isEmpty(code)) {
throw new GuliException(ResultCodeEnum.PARAM_ERROR);
}
// 通過redis校驗驗證碼
String checkCode = (String) redisTemplate.opsForValue().get(mobile);
if (!code.equals(checkCode)) {
throw new GuliException(ResultCodeEnum.CODE_ERROR);
}
// 校驗使用者是否註冊:mysql
QueryWrapper<Member> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("mobile", mobile);
Integer count = baseMapper.selectCount(queryWrapper);
if (count > 0) {
throw new GuliException(ResultCodeEnum.REGISTER_MOBLE_ERROR);
}
// 註冊
Member member = new Member();
member.setNickname(nickname);
member.setMobile(mobile);
member.setPassword(MD5.encrypt(password));
member.setAvatar("https://guli-file-191125.oss-cn-beijing.aliyuncs.com/avatar/default.jpg");
member.setDisabled(false);
baseMapper.insert(member);
}
}
二前端
1api
import request from '@/utils/request'
export default {
// 傳送驗證碼
sendMessage(mobile) {
return request({
baseURL: 'http://localhost:8150',
url: `/api/sms/send/${mobile}`,
method: 'get'
})
},
// 註冊
register(member) {
return request({
baseURL: 'http://localhost:8160',
url: '/api/ucenter/member/register',
method: 'post',
data: member
})
}
}
2頁面部分
<template>
<div class="main">
<div class="title">
<a href="/login">登入</a>
<span>·</span>
<a class="active" href="/register">註冊</a>
</div>
<div class="sign-up-container">
<form action="register">
<div class="input-prepend restyle">
<input
v-model="member.nickname"
type="text"
placeholder="你的暱稱">
<i class="iconfont icon-user"/>
</div>
<div class="input-prepend restyle no-radius">
<input
v-model="member.mobile"
type="text"
placeholder="手機號">
<i class="iconfont icon-phone"/>
</div>
<div class="input-prepend restyle no-radius" style="position:relative">
<input
v-model="member.code"
type="text"
placeholder="驗證碼">
<i class="iconfont icon-yanzhengma"/>
<a
href="javascript:"
type="button"
style="position:absolute;right: 10px;top: 15px;"
@click="getCodeFun()">{{ codeText }}</a>
</div>
<div class="input-prepend">
<input
v-model="member.password"
type="password"
placeholder="設定密碼">
<i class="iconfont icon-password"/>
</div>
<div class="btn">
<input
type="button"
class="sign-up-button"
value="註冊"
@click="submitRegister()">
</div>
<p class="sign-up-msg">
點選 “註冊” 即表示您同意並願意遵守簡書
<a target="_blank" href="http://www.jianshu.com/p/c44d171298ce">使用者協議</a>
和
<a target="_blank" href="http://www.jianshu.com/p/2ov8x3">隱私政策</a> 。
</p>
</form>
<!-- 更多註冊方式 -->
<div class="more-sign">
<h6>社交帳號直接註冊</h6>
<ul>
<li><a id="weixin" class="weixin" href="http://localhost:8150/api/ucenter/wx/login"><i class="iconfont icon-weixin"/></a></li>
<li><a id="qq" class="qq" target="_blank" href="#"><i class="iconfont icon-qq"/></a></li>
</ul>
</div>
</div>
</div>
</template>
<script>
import '~/assets/css/sign.css'
import '~/assets/css/iconfont.css'
import registerApi from '~/api/register'
export default {
layout: 'sign',
data() {
return {
member: {
mobile: '',
code: '',
nickname: '',
password: ''
},
sending: false, // 是否傳送驗證碼
second: 60, // 倒計時間
codeText: '獲取驗證碼'
}
},
methods: {
// 獲取驗證碼
getCodeFun() {
if (this.sending) return // 如果已點選則退出,防止多次重複提交
this.sending = true // 使用者已點選
registerApi.sendMessage(this.member.mobile).then(response => {
// 倒計時
this.timeDown()
// 提示傳送成功
this.$message.success(response.message)
})
},
// 倒計時
timeDown() {
this.codeText = this.second
// 定義計時器
const timer = setInterval(() => {
this.codeText--
if (this.codeText < 1) {
clearInterval(timer)
this.codeText = '獲取驗證碼'
this.sending = false
this.second = 60
}
// console.log(new Date())
}, 1000)
},
// 註冊
submitRegister() {
registerApi.register(this.member).then(response => {
// 提示註冊成功
this.$message.success(response.message)
this.$router.push({ path: 'login' })
})
}
}
}
</script>