内容概要
- 1.首页中间部分样式
- 2.多方式登录接口
- 3.手机号是否存在接口
- 4.腾讯云短信介绍和申请
1 首页中间部分样式
<div class="course">
<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">价格:999</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%" height="500px">
<style scoped>
.time {
font-size: 13px;
color:
}
.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
}
.course_detail {
padding: 50px;
}
</style>
2 多方式登录接口
1 多方式登录 【用户名、邮箱、手机号+密码】
4 校验手机号是否存在
2 验证码登录
3 发送验证码
5 手机号注册接口
-{username:用户名/邮箱/手机号,password:123}---->post请求
-之前写的逻辑,校验用户,是写在视图类的方法中
-今天写:咱们把逻辑写在序列化类中
视图类
from rest_framework.viewsets import ViewSet, GenericViewSet, ViewSetMixin
from rest_framework.decorators import action
from .models import UserInfo
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 = ser.context.get('token')
username = ser.context.get('username')
icon = ser.context.get('icon')
return APIResponse(token=token, username=username,
icon=icon)
序列化类
from rest_framework import serializers
from .models import UserInfo
import re
from django.contrib.auth import authenticate
from rest_framework.exceptions import ValidationError
from rest_framework_jwt.settings import api_settings
jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER
jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER
class UserMulLoginSerializer(serializers.ModelSerializer):
username = serializers.CharField()
class Meta:
model = UserInfo
fields = ['username', 'password']
def _get_user(self, attrs):
username = attrs.get('username')
password = attrs.get('password')
if re.match(r'^1[3-9][0-9]{9}$', username):
user = authenticate(mobile=username, password=password)
elif re.match(r'^.+@.+$', username):
user = authenticate(email=username, password=password)
else:
user = authenticate(username=username, password=password)
if user:
return user
else:
raise ValidationError('用户名或密码错误')
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 = self._get_token(user)
self.context['token'] = token
self.context['username'] = user.username
self.context['icon'] = 'http://127.0.0.1:8000/media/'+str(user.icon)
return attrs
路由
from django.contrib import admin
from django.urls import path, re_path
from home import views
from django.views.static import serve
from django.conf import settings
from . import views
from rest_framework.routers import SimpleRouter
router = SimpleRouter()
router.register('user', views.UserView, 'user')
urlpatterns = [
]
urlpatterns += router.urls
3 手机号是否存在接口
3.1 视图类
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('手机号不存在')
4 腾讯云短信介绍和申请
-注册平台---》找到短信
-https://console.cloud.tencent.com/smsv2
1 创建签名:使用公众号申请
-网站:备案:工信部备案
-申请个人一个公众号:
-https://mp.weixin.qq.com/
-等审核通过
2 申请模板:发送短信的模板 {1} {2} 后期用代码填上
3 免费赠送100条
4 代码发送短信:参照文档写代码:https://cloud.tencent.com/document/product/382/13444
-v2 老一些
-v3 最新


4.1 什么是api,什么是sdk
-之前学的接口文档的概念
-使用api调用,比较麻烦,固定输入,接受固定的返回
-使用postman都可以测试,携带你的认证的秘钥。
-分语言的
-基于API,使用某个编程语言封装的包
-例如python:pip install 包
-包.发短信(参数)
-一般厂商都会提供各大主流语言的sdk
1 已开通短信服务,创建签名和模板并通过审核
2 如需发送国内短信,需要先 购买国内短信套餐包。
3 已准备依赖环境:Python 2.7 - 3.6 版本。
4 已在访问管理控制台 >API密钥管理页面获取 SecretID 和 SecretKey。
SecretID 用于标识 API 调用者的身份。
SecretKey 用于加密签名字符串和服务器端验证签名字符串的密钥,SecretKey 需妥善保管
5 短信的调用地址为sms.tencentcloudapi.com。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)