一、短信注册接口
需要手机号+验证码+密码——>{mobile:手机号,code:验证码,password:密码}
——>post请求,后端路由127.0.0.1:8080/api/v1/userinfo/user/register
- 写个视图
def register(self,request):
- 写个序列化类
UserRegisterSerializer
只用来做反序列化和数据校验,不做序列化 - 接收三个字段:mobile、code、password,表里没有code,重写code字段可设置最长、最短,都是4就行
- 全局校验:1、验证code是否正确、不正确直接返回 2、入库前的准备,把code用pop剔除,username设置为手机号,返回attrs 3、保存正常不用写,新增userinfo,密码是加密的重写create方法
- register视图获取、保存、返回注册成功
代码展示
serializer.py
class UserRegisterSerializer(serializers.ModelSerializer):
code = serializers.CharField(max_length=4, min_length=4)
class Meta:
model = UserInfo
fields = ['mobile', 'code', 'password']
def validate(self, attrs):
# 1、验证code是否正确
mobile = attrs.get('mobile')
code = attrs.get('code')
old_code = cache.get('sms_code_%s' % mobile)
if not (old_code == code or code == '1111'):
raise APIException('铁铁,验证码错误')
# 2、入库前的准备,code剔除,username设置为手机号
attrs['username'] = mobile
attrs.pop('code')
return attrs
# 3、保存正常不用写,新增userinfo,密码是加密的——>重写create方法
def create(self, validated_data): # 现在里面三个字段:mobile、username、password
user = UserInfo.objects.create_user(**validated_data)
return user
user/views
@action(methods=['POST'], detail=False)
def register(self,request):
ser = UserRegisterSerializer(data=request.data)
ser.is_valid(raise_exception=True)
ser.save()
return APIResponse(msg='铁铁,注册成功')
二、登录前台
多方式登录方法
用户名+密码登录
- 传用户名和密码,element整个弹窗“用户名或密码不能为空”
- 登录地址发送
$axios.post请求
- 登录成功将用户名、token、头像存到本地存储中,放cooks里,一般7天过期
- 销毁模态框
$emit('close')
触发Header.vue里的@close - 写右上角的登录|注销,取出token和username,退出功能只需要本地删除token即可,
$cookies.remove移除
手机号+验证码登录
@blur ="check_mobile"
如果手机号没有就啥也不动没问题,输入正则判断输入手机号不合法清空,is_send=True
正确的话允许发送短信,不是True不可以,点一次就变成Falsethis.sms_interval = "发送中...
启用下方定时任务- 发送短信:axios get请求 网址:
前面一串+send_sms+?mobile='this.mobile'
- 验证手机号是否注册了,if判断,写了手机号不存在直接清空
- 验证手机号+验证码都有的情况下登录,登录成功cookies存下来
Login.vue
<template>
<div class="login">
<div class="box">
<i class="el-icon-close" @click="close_login"></i>
<div class="content">
<div class="nav">
<span :class="{active: login_method === 'is_pwd'}"
@click="change_login_method('is_pwd')">密码登录</span>
<span :class="{active: login_method === 'is_sms'}"
@click="change_login_method('is_sms')">短信登录</span>
</div>
<el-form v-if="login_method === 'is_pwd'">
<el-input
placeholder="用户名/手机号/邮箱"
prefix-icon="el-icon-user"
v-model="username"
clearable>
</el-input>
<el-input
placeholder="密码"
prefix-icon="el-icon-key"
v-model="password"
clearable
show-password>
</el-input>
<el-button type="primary" @click="handleMulLogin">登录</el-button>
</el-form>
<el-form v-if="login_method === 'is_sms'">
<el-input
placeholder="手机号"
prefix-icon="el-icon-phone-outline"
v-model="mobile"
clearable
@blur="check_mobile">
</el-input>
<el-input
placeholder="验证码"
prefix-icon="el-icon-chat-line-round"
v-model="sms"
clearable>
<template slot="append">
<span class="sms" @click="send_sms">{{ sms_interval }}</span>
</template>
</el-input>
<el-button type="primary" @click="handleSmsLogin">登录</el-button>
</el-form>
<div class="foot">
<span @click="go_register">立即注册</span>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: "Login",
data() {
return {
username: '',
password: '',
mobile: '',
sms: '',
login_method: 'is_pwd',
sms_interval: '获取验证码',
is_send: false, // 是true才可以发送短信
}
},
methods: {
close_login() {
this.$emit('close')
},
go_register() {
this.$emit('go')
},
change_login_method(method) {
this.login_method = method;
},
check_mobile() {
// 手机号如果没填,就直接返回
if (!this.mobile) return;
if (!this.mobile.match(/^1[3-9][0-9]{9}$/)) {
this.$message({
message: '手机号有误',
type: 'warning',
duration: 1000,
onClose: () => {
this.mobile = '';
}
});
return false;
}
//后端校验一下是否注册了
this.$axios.get(this.$settings.BASE_URL + 'userinfo/user/mobile/?mobile=' + this.mobile).then(res => {
if (res.data.code != 100) {
this.mobile = ''
this.$message({
message: '该手机号没注册,请先注册',
type: 'error'
});
return // 函数结束掉
}
})
this.is_send = true; // 可以发送短信了
},
send_sms() {
//如果is_send不是true,是不能发短信的
if (!this.is_send) return;
this.is_send = false;
let sms_interval_time = 60;
this.sms_interval = "发送中...";
let timer = setInterval(() => {
if (sms_interval_time <= 1) {
clearInterval(timer);
this.sms_interval = "获取验证码";
this.is_send = true; // 重新回复点击发送功能的条件
} else {
sms_interval_time -= 1;
this.sms_interval = `${sms_interval_time}秒后再发`;
}
}, 1000);
// 发送短信
this.$axios.get(this.$settings.BASE_URL + 'userinfo/user/send_sms/?mobile=' + this.mobile).then(
res => {
this.$message({
message: res.data.msg,
type: 'success'
});
}
)
},
// 多方式登录方法
handleMulLogin() {
if (this.username && this.password) {
this.$axios.post(this.$settings.BASE_URL + 'userinfo/user/mul_login/', {
username: this.username,
password: this.password
}).then(res => {
console.log(res.data)
if (res.data.code == 100) {
// 用户名,token,头像,存到本地存储
this.$cookies.set('token', res.data.token)
this.$cookies.set('username', res.data.username)
this.$cookies.set('icon', res.data.icon)
// 销毁调登录模态框
this.$emit('close')
} else {
this.$message({
message: res.data.msg,
type: 'error'
});
}
})
} else {
this.$message({
message: '用户名或密码不能为空',
type: 'warning'
});
}
},
// 短信登录
handleSmsLogin() {
if (this.mobile && this.sms) {
this.$axios.post(this.$settings.BASE_URL + 'userinfo/user/mobile_login/', {
mobile: this.mobile,
code: this.sms
}).then(res => {
if (res.data.code == 100) {
// 用户名,token,头像,存到本地存储
this.$cookies.set('token', res.data.token)
this.$cookies.set('username', res.data.username)
this.$cookies.set('icon', res.data.icon)
// 销毁调登录模态框
this.$emit('close')
} else {
this.$message({
message: res.data.msg,
type: 'error'
});
}
})
}
}
}
}
</script>
<style scoped>
.login {
width: 100vw;
height: 100vh;
position: fixed;
top: 0;
left: 0;
z-index: 10;
background-color: rgba(0, 0, 0, 0.3);
}
.box {
width: 400px;
height: 420px;
background-color: white;
border-radius: 10px;
position: relative;
top: calc(50vh - 210px);
left: calc(50vw - 200px);
}
.el-icon-close {
position: absolute;
font-weight: bold;
font-size: 20px;
top: 10px;
right: 10px;
cursor: pointer;
}
.el-icon-close:hover {
color: darkred;
}
.content {
position: absolute;
top: 40px;
width: 280px;
left: 60px;
}
.nav {
font-size: 20px;
height: 38px;
border-bottom: 2px solid darkgrey;
}
.nav > span {
margin: 0 20px 0 35px;
color: darkgrey;
user-select: none;
cursor: pointer;
padding-bottom: 10px;
border-bottom: 2px solid darkgrey;
}
.nav > span.active {
color: black;
border-bottom: 3px solid black;
padding-bottom: 9px;
}
.el-input, .el-button {
margin-top: 40px;
}
.el-button {
width: 100%;
font-size: 18px;
}
.foot > span {
float: right;
margin-top: 20px;
color: orange;
cursor: pointer;
}
.sms {
color: orange;
cursor: pointer;
display: inline-block;
width: 70px;
text-align: center;
user-select: none;
}
</style>
Header.vue
<template>
<div class="header">
<div class="slogan">
<p>人终向前走 | 花自向阳开 </p>
</div>
<div class="nav">
<ul class="left-part">
<li class="logo">
<router-link to="/">
<img src="../assets/img/head-logo.svg" alt="">
</router-link>
</li>
<li class="ele">
<span @click="goPage('/free-course')" :class="{active: url_path === '/free-course'}">狼图腾</span>
</li>
<li class="ele">
<span @click="goPage('/actual-course')" :class="{active: url_path === '/actual-course'}">三毛流浪记</span>
</li>
<li class="ele">
<span @click="goPage('/light-course')" :class="{active: url_path === '/light-course'}">鲁滨逊漂流记</span>
</li>
</ul>
<div class="right-part">
<div v-if="!username">
<span @click="put_login">登录</span>
<span class="line">|</span>
<span @click="put_register">注销</span>
</div>
<div v-else>
<span>{{ username }}</span>
<span class="line">|</span>
<span @click="logout">注销</span>
</div>
<Login v-if="is_login" @close="close_login" @go="put_register"></Login>
<Register v-if="is_register" @close="close_register" @go="put_login"></Register>
</div>
</div>
</div>
</template>
<script>
import Login from "@/components/Login";
import Register from "@/components/Register";
export default {
name: "Header",
data() {
return {
url_path: sessionStorage.url_path || '/',
is_login: false,
is_register: false,
username: '',
}
},
methods: {
goPage(url_path) {
// 已经是当前路由就没有必要重新跳转
if (this.url_path !== url_path) {
// 传入的参数,如果不等于当前路径,就跳转
this.$router.push(url_path)
}
sessionStorage.url_path = url_path;
},
goLogin() {
this.loginShow = true
},
put_login() {
this.is_login = true;
this.is_register = false;
},
put_register() {
this.is_login = false;
this.is_register = true;
},
close_login() {
this.is_login = false;
this.username = this.$cookies.get('username')
},
close_register() {
this.is_register = false;
},
// 退出功能:正常只需要本地删除token即可,不需要跟后端交互,如果有需求,需要发请求,统计用户退出时间。。。
logout() {
this.$cookies.remove('token')
this.$cookies.remove('username')
this.$cookies.remove('icon')
this.username=''
}
},
created() {
sessionStorage.url_path = this.$route.path
this.url_path = this.$route.path
//取出cookie中得token和username
this.username = this.$cookies.get('username')
},
components: {
Login,
Register
}
}
</script>
<style scoped>
.header {
background-color: white;
box-shadow: 0 0 5px 0 #aaa;
}
.header:after {
content: "";
display: block;
clear: both;
}
.slogan {
background-color: #eee;
height: 40px;
}
.slogan p {
width: 1200px;
margin: 0 auto;
color: #aaa;
font-size: 13px;
line-height: 40px;
}
.nav {
background-color: white;
user-select: none;
width: 1200px;
margin: 0 auto;
}
.nav ul {
padding: 15px 0;
float: left;
}
.nav ul:after {
clear: both;
content: '';
display: block;
}
.nav ul li {
float: left;
}
.logo {
margin-right: 20px;
}
.ele {
margin: 0 20px;
}
.ele span {
display: block;
font: 15px/36px '微软雅黑';
border-bottom: 2px solid transparent;
cursor: pointer;
}
.ele span:hover {
border-bottom-color: orange;
}
.ele span.active {
color: orange;
border-bottom-color: orange;
}
.right-part {
float: right;
}
.right-part .line {
margin: 0 10px;
}
.right-part span {
line-height: 68px;
cursor: pointer;
}
</style>
三、注册前台
- 判断手机号是否存在,如果存在直接返回让去登录
- 不存在,则写手机号+密码+验证码不为空
- 注册成功后,跳转到登录页面
Register.vue
<template>
<div class="register">
<div class="box">
<i class="el-icon-close" @click="close_register"></i>
<div class="content">
<div class="nav">
<span class="active">新用户注册</span>
</div>
<el-form>
<el-input
placeholder="手机号"
prefix-icon="el-icon-phone-outline"
v-model="mobile"
clearable
@blur="check_mobile">
</el-input>
<el-input
placeholder="密码"
prefix-icon="el-icon-key"
v-model="password"
clearable
show-password>
</el-input>
<el-input
placeholder="验证码"
prefix-icon="el-icon-chat-line-round"
v-model="sms"
clearable>
<template slot="append">
<span class="sms" @click="send_sms">{{ sms_interval }}</span>
</template>
</el-input>
<el-button type="primary" @click="handleRegister">注册</el-button>
</el-form>
<div class="foot">
<span @click="go_login">立即登录</span>
</div>
</div>
</div>
</div>
</template>
<script>
export default {
name: "Register",
data() {
return {
mobile: '',
password: '',
sms: '',
sms_interval: '获取验证码',
is_send: false,
}
},
methods: {
close_register() {
this.$emit('close', false)
},
go_login() {
this.$emit('go')
},
check_mobile() {
if (!this.mobile) return;
if (!this.mobile.match(/^1[3-9][0-9]{9}$/)) {
this.$message({
message: '手机号有误',
type: 'warning',
duration: 1000,
onClose: () => {
this.mobile = '';
}
});
return false;
}
// 判断手机号是否存在
this.$axios.get(this.$settings.BASE_URL + 'userinfo/user/mobile/?mobile=' + this.mobile).then(res => {
if (res.data.code == 100) {
this.mobile = ''
this.$message({
message: '铁铁,该手机号已经注册,请直接登录',
type: 'error'
});
return // 函数结束掉
}
})
this.is_send = true;
},
send_sms() {
if (!this.is_send) return;
this.is_send = false;
let sms_interval_time = 60;
this.sms_interval = "发送中...";
let timer = setInterval(() => {
if (sms_interval_time <= 1) {
clearInterval(timer);
this.sms_interval = "获取验证码";
this.is_send = true; // 重新回复点击发送功能的条件
} else {
sms_interval_time -= 1;
this.sms_interval = `${sms_interval_time}秒后再发`;
}
}, 1000);
// 发送短信
this.$axios.get(this.$settings.BASE_URL + 'userinfo/user/send_sms/?mobile=' + this.mobile).then(
res => {
this.$message({
message: res.data.msg,
type: 'success'
});
}
)
},
handleRegister() {
if (this.mobile && this.sms && this.password) {
this.$axios.post(this.$settings.BASE_URL + 'userinfo/user/register/', {
mobile: this.mobile,
code: this.sms,
password: this.password
}).then(res => {
if (res.data.code == '100') {
// 跳转到登录
this.$emit('go')
} else {
this.$message({
message: res.data.msg,
type: 'error'
});
}
})
} else {
this.$message({
message: '铁铁,手机号、密码、验证码都不能为空',
type: 'error'
});
}
}
}
}
</script>
<style scoped>
.register {
width: 100vw;
height: 100vh;
position: fixed;
top: 0;
left: 0;
z-index: 10;
background-color: rgba(0, 0, 0, 0.3);
}
.box {
width: 400px;
height: 480px;
background-color: white;
border-radius: 10px;
position: relative;
top: calc(50vh - 240px);
left: calc(50vw - 200px);
}
.el-icon-close {
position: absolute;
font-weight: bold;
font-size: 20px;
top: 10px;
right: 10px;
cursor: pointer;
}
.el-icon-close:hover {
color: darkred;
}
.content {
position: absolute;
top: 40px;
width: 280px;
left: 60px;
}
.nav {
font-size: 20px;
height: 38px;
border-bottom: 2px solid darkgrey;
}
.nav > span {
margin-left: 90px;
color: darkgrey;
user-select: none;
cursor: pointer;
padding-bottom: 10px;
border-bottom: 2px solid darkgrey;
}
.nav > span.active {
color: black;
border-bottom: 3px solid black;
padding-bottom: 9px;
}
.el-input, .el-button {
margin-top: 40px;
}
.el-button {
width: 100%;
font-size: 18px;
}
.foot > span {
float: right;
margin-top: 20px;
color: orange;
cursor: pointer;
}
.sms {
color: orange;
cursor: pointer;
display: inline-block;
width: 70px;
text-align: center;
user-select: none;
}
</style>
Header.vue
<template>
<div class="header">
<div class="slogan">
<p>人终向前走 | 花自向阳开 </p>
</div>
<div class="nav">
<ul class="left-part">
<li class="logo">
<router-link to="/">
<img src="../assets/img/head-logo.svg" alt="">
</router-link>
</li>
<li class="ele">
<span @click="goPage('/free-course')" :class="{active: url_path === '/free-course'}">狼图腾</span>
</li>
<li class="ele">
<span @click="goPage('/actual-course')" :class="{active: url_path === '/actual-course'}">三毛流浪记</span>
</li>
<li class="ele">
<span @click="goPage('/light-course')" :class="{active: url_path === '/light-course'}">鲁滨逊漂流记</span>
</li>
</ul>
<div class="right-part">
<div v-if="!username">
<span @click="put_login">登录</span>
<span class="line">|</span>
<span @click="put_register">注册</span>
</div>
<div v-else>
<span>{{ username }}</span>
<span class="line">|</span>
<span @click="logout">注销</span>
</div>
<Login v-if="is_login" @close="close_login" @go="put_register"></Login>
<Register v-if="is_register" @close="close_register" @go="put_login"></Register>
</div>
</div>
</div>
</template>
<script>
import Login from "@/components/Login";
import Register from "@/components/Register";
export default {
name: "Header",
data() {
return {
url_path: sessionStorage.url_path || '/',
is_login: false,
is_register: false,
username: '',
}
},
methods: {
goPage(url_path) {
// 已经是当前路由就没有必要重新跳转
if (this.url_path !== url_path) {
// 传入的参数,如果不等于当前路径,就跳转
this.$router.push(url_path)
}
sessionStorage.url_path = url_path;
},
goLogin() {
this.loginShow = true
},
put_login() {
this.is_login = true;
this.is_register = false;
},
put_register() {
this.is_login = false;
this.is_register = true;
},
close_login() {
this.is_login = false;
this.username = this.$cookies.get('username')
},
close_register() {
this.is_register = false;
},
// 退出功能:正常只需要本地删除token即可,不需要跟后端交互,如果有需求,需要发请求,统计用户退出时间。。。
logout() {
this.$cookies.remove('token')
this.$cookies.remove('username')
this.$cookies.remove('icon')
this.username=''
}
},
created() {
sessionStorage.url_path = this.$route.path
this.url_path = this.$route.path
//取出cookie中得token和username
this.username = this.$cookies.get('username')
},
components: {
Login,
Register
}
}
</script>
<style scoped>
.header {
background-color: white;
box-shadow: 0 0 5px 0 #aaa;
}
.header:after {
content: "";
display: block;
clear: both;
}
.slogan {
background-color: #eee;
height: 40px;
}
.slogan p {
width: 1200px;
margin: 0 auto;
color: #aaa;
font-size: 13px;
line-height: 40px;
}
.nav {
background-color: white;
user-select: none;
width: 1200px;
margin: 0 auto;
}
.nav ul {
padding: 15px 0;
float: left;
}
.nav ul:after {
clear: both;
content: '';
display: block;
}
.nav ul li {
float: left;
}
.logo {
margin-right: 20px;
}
.ele {
margin: 0 20px;
}
.ele span {
display: block;
font: 15px/36px '微软雅黑';
border-bottom: 2px solid transparent;
cursor: pointer;
}
.ele span:hover {
border-bottom-color: orange;
}
.ele span.active {
color: orange;
border-bottom-color: orange;
}
.right-part {
float: right;
}
.right-part .line {
margin: 0 10px;
}
.right-part span {
line-height: 68px;
cursor: pointer;
}
</style>
四、redis介绍
1、介绍CS架构的软件
redis:非关系型数据库【存数据的地方】,内部存储,速度非常快,如果断电了,内存数据就没了
redis是nosql数据库:指非关系型数据库:1、不限于SQL 2、没有SQL(我不写sql)
可以持久化【数据从内存同步到硬盘】,
数据类型丰富【5大数据类型】
- 字符串
- 列表
- 哈希(字典)
- 集合
- 有序集合
key_value形式存储【根本没有表的结构,相当于咱们的字典】
2、redis 为什么这么快?
- 高性能的网络模型:IO多路复用的epoll模型,承载住非常高的并发量
- 纯内存操作,避免了很多IO
- 单线程架构,避免了线程之间切换的消耗
- 6.x以前:单线程,单进程
- 6.x以后:多线程架构,数据库操作还是使用单线程,别的线程做数据持久化,其他操作
3、redis应用场景(了解项)
- 当缓存数据库使用,接口缓存,提高接口响应速度
- 请求进到视图——>去数据查询【多表查询,去硬盘取数据:速度慢】——>转成json格式字符串——>返回给前端
- 请求到视图——>去redis【内存】——>取json格式字符串——>返回给前端
- 当计数器:单线程,不存在并发安全问题
- 统计网站访问量
- 个人站点浏览量
- 文章阅读量
- 去重操作:集合
- 排行榜:有序集合
- 阅读排行榜
- 游戏金币排行榜
- 布隆过滤器
- 抽奖
- 消息队列
五、redis安装
开源软件:使用c语言写的---【编译型语言,在操作系统运行,要编译成可执行文件,由于采用了IO多路复用的epoll模型,所以它不支持windows,只有linux操作系统支持epoll】
微软官方:改了,编译成可执行的安装包,下载一路下一步安装
-版本没有最新
官网:https://redis.io/
-下载完是源代码:c语言源码 :https://redis.io/download/#redis-stack-downloads
-最稳定:6.x
-最新7.x
中文网:http://redis.cn/download.html
-上面最新只到5.x
win版本下载地址
- 最新5.x版本
https://github.com/tporadowski/redis/releases/
- 最新3.x版本
https://github.com/microsoftarchive/redis/releases
- 下载完一路下一步即可,具体可参照:
https://www.cnblogs.com/liuqingzheng/p/9831331.html
win装完会有redis服务
- 启动服务,手动停止
- 客户端链接:redis-cli -h 127.0.0.1 -p 6379
- 简单命令:
- set name lqz
- get name
- ping 客户端和服务端连接
停掉服务:
- 去win服务点关闭
- 客户端关闭:shutdown
mysql服务端、mysql客户端 | redis服务端、redis客户端 |
Navicate | redis-cli |
命令窗口cmd | 图形化工具:redis-destop-management |
python操作 | python操作 |
标签:username,code,mobile,08,学习,color,luffy,path,data From: https://www.cnblogs.com/zzjjpp/p/16889145.html