首页中间部分样式
<template>
<div class="home">
<Header></Header>
<Banner></Banner>
<div class="fa-discourse">
<el-row>
<el-col :span="6" v-for="(o, index) in 8" :key="o" class="course_detail">
<el-card :body-style="{ padding: '0px' }">
<img src="https://tva1.sinaimg.cn/large/e6c9d24egy1h1g0zd133mj20l20a875i.jpg"
class="image">
<div style="padding: 14px;">
<span>推荐课程</span>
<div class="bottom clearfix">
<time class="time">价格:998</time>
<el-button type="text" class="button">查看详情</el-button>
</div>
</div>
</el-card>
</el-col>
</el-row>
</div>
<img src="https://tva1.sinaimg.cn/large/e6c9d24egy1h1g112oiclj224l0u0jxl.jpg" alt="" width="100%px" height="500px">
<Footer></Footer>
</div>
</template>
<style scoped>
.time {
font-size: 13px;
color: #999;
}
.bottom {
margin-top: 13px;
line-height: 12px;
}
.button {
padding: 0;
float: right;
}
.image {
width: 100%;
display: block;
}
.clearfix:before,
.clearfix:after {
display: table;
content: "";
}
.clearfix:after {
clear: both
}
</style>
多方式登录接口
视图类
from rest_framework.viewsets import ViewSet
from rest_framework.decorators import action
from .serializer import UserMulLoginSerializer
from utils.response import APIResponse
class UserView(ViewSet):
@action(methods=['POST'], detail=False)
def mul_login(self, request):
ser = UserMulLoginSerializer(data=request.data)
# 执行序列化类的校验,如果校验通过则继续往下走,如果不通过则抛异常
ser.is_valid(raise_exception=True)
# 用户名密码校验通过后,在序列化类中签发token
token = ser.context.get('token')
username = ser.context.get('username')
icon = ser.context.get('icon')
# 前端看到的{code: 100, msg: ''}
return APIResponse(token=token, username=username, icon=icon)
序列化类
from rest_framework import serializers
from .models import UserInfo
from django.contrib.auth import authenticate
from rest_framework.exceptions import ValidationError
from rest_framework_jwt.settings import api_settings
import re
from rest_framework.exceptions import APIException
jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
class UserMulLoginSerializer(serializers.ModelSerializer):
# 重写字段的校验,登录时就没有unique的限制
username = serializers.CharField()
class Meta:
model = UserInfo
fields = ['username', 'password']
# attrs是校验过的数据,字段自己的规则和局部钩子
def _get_user(self, attrs):
username = attrs.get('username')
password = attrs.get('password')
# username可能是用户名、邮箱、手机号,使用正则来判断
if re.match(r'^1[3-9][0-9]{9}$', username): # 手机号登录
user = UserInfo.objects.get(mobile=username)
elif re.match(r'^.+@.+$', username): # 邮箱登录
user = UserInfo.objects.get(email=username)
else: # 用户名登录
user = UserInfo.objects.get(username=username)
if user:
return user
else:
raise APIException(detail='用户名或密码错误')
def _get_token(self, user):
try:
payload = jwt_payload_handler(user)
token = jwt_encode_handler(payload)
return token
except Exception as e:
raise ValidationError(str(e))
def validate(self, attrs):
# 取出用户名和密码,校验用户是否存在
user = self._get_user(attrs)
# 签发token
token = self._get_token(user)
# 将token放到序列化类中
self.context['token'] = token
self.context['username'] = user.username
# user.icon是个对象,要转换为字符串在拼接成完整的地址
self.context['icon'] = 'http://127.0.0.1:8000/media/'+str(user.icon)
# 有问题则抛异常,没问题则返回attrs
return attrs
路由
from . import views
from rest_framework.routers import SimpleRouter
router = SimpleRouter()
router.register('user', views.UserView, 'user')
urlpatterns = []
urlpatterns += router.urls
手机号是否存在接口
class UserView(ViewSet):
@action(methods=['GET'], detail=False)
def mobile(self, request):
try:
mobile = request.query_params.get('mobile')
# 有且只有一个是才不报错
UserInfo.objects.get(mobile=mobile)
return APIResponse(msg='手机号存在')
except Exception as e:
raise APIException('手机号不存在')
腾讯云短信介绍和申请
1.要写发送短信接口,借助于短信运营商发短信
-腾讯云开放平台,有很多开放的接口供人使用,我们用的是短信
https://console.cloud.tencent.com/smsv2
2.申请使用短信
-签名类型
网站:要去工信部备案
公众号:申请个人一个公众号
https://mp.weixin.qq.com/
-
3.申请模板
发送短信的模板 {1} {2} 后期用代码填上
'申请后免费赠送100条,之后收费'
-代码发送短信参照文档
https://cloud.tencent.com/document/product/382/13444
api文档,sdk
1.API文档
-使用api调用,比较麻烦,需要固定输入,接收固定的返回
-使用postman都可以测试,但是要携带认证的秘钥
2.SDK(Software Development 软件开发工具包)
-SDK是分语言,它是基于API,使用某个编程语言的包
-例如python:pip install 包
包.发短信(参数)
3.腾讯短信SDK使用
1 已开通短信服务,创建签名和模板并通过审核。 # 开了
2 如需发送国内短信,需要先 购买国内短信套餐包。 #赠送了
3 已准备依赖环境:Python 2.7 - 3.6 版本。 # 我们有
4 已在访问管理控制台 >API密钥管理页面获取 SecretID 和 SecretKey。
SecretID 用于标识 API 调用者的身份。
SecretKey 用于加密签名字符串和服务器端验证签名字符串的密钥,SecretKey 需妥善保管
5 短信的调用地址为sms.tencentcloudapi.com。
标签:username,登录,方式,get,self,接口,token,user,import
From: https://www.cnblogs.com/riuqi/p/16879038.html