短信注册接口
注册需要的数据:手机号、验证码、密码(用户名自动生成)
前端传来的数据格式:{"mobile":"xxx", "code": "xxx", "password": "xxx"}
后端路由: http://127.0.0.1:8000/api/v1/user/userinfo/register/
views.py
class UserView(ViewSet):
@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='注册成功')
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):
mobile = attrs.get('mobile')
code = attrs.get('code')
old_code = cache.get('sms_code_%s' % mobile)
if not (old_code == code or code == '8888'):
raise APIException(detail='验证码错误')
attrs['username'] = mobile
attrs.pop('code')
return attrs
def create(self, validated_data):
user = UserInfo.objects.create_user(**validated_data)
return user
登录前台
<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="mul_login_click">登录</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="mobile_login_click">登录</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,
}
},
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 + 'user/userinfo/check_mobile?mobile=' + this.mobile).then(res => {
if (res.data.code != 100) {
this.sms = ''
this.$message({
message: '该手机号没注册,请先注册',
type: 'error'
});
}
})
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 + 'user/userinfo/send_sms/?mobile=' + this.mobile,).then(res => {
console.log(res.data.sms)
this.$message({
message: res.data.msg,
type: "success"
})
})
},
mul_login_click() {
if (this.username && this.password) {
this.$axios.post(this.$settings.BASE_URL + 'user/userinfo/mul_login/', {
username: this.username,
password: this.password
},).then(res => {
console.log(res)
if (res.data.code == 100) {
this.$cookies.set('username', res.data.username)
this.$cookies.set('token', res.data.token)
this.$cookies.set('icon', res.data.icon)
this.$emit('close')
} else {
this.$message({
message: res.data.msg,
type: "error"
})
}
})
} else {
this.$message({
message: '用户名密码为必填项',
type: "error"
})
}
},
mobile_login_click() {
if (this.mobile && this.sms) {
this.$axios.post(this.$settings.BASE_URL + 'user/userinfo/mobile_login/', {
mobile: this.mobile,
code: this.sms
},).then(res => {
if (res.data.code == 100) {
this.$cookies.set('username', res.data.username)
this.$cookies.set('token', res.data.token)
this.$cookies.set('icon', res.data.icon)
this.$emit('close')
} else {
this.$message({
message: res.data.msg,
type: "error"
})
}
})
} else {
this.$message({
message: '以上内容不能为空',
type: "error"
})
}
},
}
}
</script>
注册前台
<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="handle_register">注册</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 + 'user/userinfo/check_mobile/?mobile=' + this.mobile).then(res => {
if (res.data.code == 100) {
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 + 'user/userinfo/send_sms/?mobile=' + this.mobile).then(res => {
console.log(res.data.sms)
this.$message({
message: res.data.msg,
type: "success"
});
})
},
handle_register() {
if (this.mobile && this.sms && this.password) {
this.$axios.post(this.$settings.BASE_URL + 'user/userinfo/register/', {
mobile: this.mobile,
code: this.sms,
password: this.password
}).then(res => {
console.log(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>
Header.vue
<template>
<div class="header">
<div class="slogan">
<p>老男孩IT教育 | 帮助有志向的年轻人通过努力学习获得体面的工作和生活</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: '',
token: ''
}
},
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;
},
// 退出登录
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
this.username = this.$cookies.get('username')
},
components: {
Login,
Register
}
}
</script>
redis介绍
redis:非关系型数据库,(nosql),内存存储,速度非常快,可以持久化【数据从内存同步到硬盘】,数据类型丰富【5大数据类型:字符串、列表、哈希(字典)、集合、有序集合】,数据以key-value的形式存储【没有表结构,相当于python中的字典】
nosql:泛指非关系型数据库,可以理解为:1、不止sql 2、没有sql(不写sql语句)
1. 高性能的网络模型:IO多路复用的epoll模型,能承载住非常高的并发量
2. 纯内存操作,避免了很多IO
3. 单线程架构,避免了线程间切换的消耗
6.x之前:单线程、单进程
6.x以后:多线程架构,数据操作还是使用单线程,其他操作如数据持久化等交给别的线程
1. 当缓存数据库使用,请求打到从库(redis)上,减少主库(mysql)的压力。提高接口的响应速度
请求进到视图-->去数据库查询[多表查询,去硬盘读取数据],速度慢--->转成json格式
请求进到视图-->去redis查询[内存查询]速度快--->直接取出json格式字符串
2. 做计数器:单线程,不存在并发安全问题
统计网站访问量、文章阅读量。。。
3. 去重操作:集合
4. 排行榜:有序集合
游戏段位排行榜、阅读排行榜
5. 布隆过滤器
6. 抽奖
7. 消息队列
redis安装
redis是使用c语言写的开源软件
-版本没有最新
-下载完是源代码:c语言源码 :https://redis.io/download/
-最稳定:6.x
-最新7.x
-上面最新只到5.x
下载完一路下一步即可,具体可参照:https://www.cnblogs.com/liuqingzheng/p/9831331.html
-启动服务,手动停止
-客户端链接:redis-cli -h 127.0.0.1 -p 6379
-简单命令:
set name lqz
get name
ping
-停掉服务:
-去win服务点关闭
-客户端关闭:shutdown
-redis-cli
-图形化工具:redis-destop-management
-python操作
python操作redis
pip install install
from redis import Redis
conn = Redis(host='127.0.0.1', port=6379)
conn.set('name', 'jixing')
print(conn.get('name'))
conn.close()
redis连接池
from redis import Redis
from threading import Thread
import redis
from POOL import pool
def get_name_from_redis():
conn = redis.Redis(connection_pool=pool)
res=conn.get('name')
print(res)
conn.close()
for i in range(100):
t=Thread(target=get_name_from_redis)
t.start()
import time
time.sleep(10)