首页 > 其他分享 >路飞项目的中间部分样式,登录接口;腾讯云发短信

路飞项目的中间部分样式,登录接口;腾讯云发短信

时间:2022-11-10 22:35:20浏览次数:75  
标签:username get self 路飞 token user 腾讯 发短信 import

目录

路飞项目

一、首页中间部分样式

<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: #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
}

.course_detail {
  padding: 50px;
}
</style>
    
    

二、多方式登录接口

1.登录注册

  • 多方式登录

    • 用户名/邮箱/手机号+密码

    • 校验手机号是否存在

  • 验证码登录

    • 发送验证码

    • 手机号注册接口

2.代码详情

  • UserView

    from django.shortcuts import render
    from rest_framework.viewsets import ViewSet, GenericViewSet, ViewSetMixin
    from rest_framework.decorators import action
    from .serializer import UserMulSerializer
    from utils.response import APIResponse
    
    
    class UserView(ViewSet):
        @action(methods=['POST'], detail=False)
        def mul_login(self, request):
            # 使用序列化类
            ser = UserMulSerializer(data=request.data)
            # jwt模块的登录就是这么写的
            ser.is_valid(raise_exception=True)
            # 会执行:序列化类字段自己的校验规则,局部钩子,全局钩子
            # 用户名密码校验通过,在序列化类中签发token
            token = ser.context.get('token')
            username = ser.context.get('username')
            icon = ser.context.get('icon')  # icon是个对象
            return APIResponse(token=token, username=username, icon=icon)
    
    
  • UserMulSerializer

    from rest_framework import serializers
    from .models import UserInfo
    from django.contrib.auth import authenticate
    import re
    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 UserMulSerializer(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 = UserInfo.objects.filter(mobile=username).first()
            elif re.match(r'^.+@.+$', username):  # adsa@adsf  会有bug,用户名中如果有@,登录不了了
                user = UserInfo.objects.filter(email=username).first()
            else:
                user = UserInfo.objects.filter(username=username).first()
            if user:
                flag = user.check_password(password)
                if flag:
                    return user
            else:
                raise APIException('用户名或密码错误')
    
        def _get_token(self, user):
            try:
                payload = jwt_payload_handler(user)
                token = jwt_encode_handler(payload)
            except Exception as e:
                raise ValidationError(str(e))
    
        def validate(self, attrs):
            # 校验用户名密码
            user = self._get_user(attrs)
            # 获取token
            token = self._get_token(user)
            # 把token,username,icon
            self.context['token'] = token
            self.context['username'] = user.username
            self.context['icon'] = 'http://127.0.0.1:8000/media/' + str(user.icon)
            return attrs
    
    
  • urls

    from rest_framework.routers import SimpleRouter
    from . import views
    
    router = SimpleRouter()
    router.register('user', views.UserView, 'user')
    urlpatterns = [
    ]
    urlpatterns += router.urls
    
    

3.手机号是否存在接口

  • 通过get请求

    # get请求:  127.0.0.1:8080/api/v1/userinfo/user/mobile/?mobile=132222222
    
    ### 视图类
    @action(methods=['GET'], detail=False)
    def mobile(self, request):
        try:
            mobile = request.data.get('mobile')
            UserInfo.objects.get(mobile=mobile)
            return APIResponse(msg='该手机号已被注册')
        except Exception as e:
            raise APIException('手机号不存在')
    

腾讯云

我们将尝试写发短信的接口,借助于腾讯云开放平台

找到短信接口:https://console.cloud.tencent.com/smsv2

一、申请使用腾讯云短信

  1. 创建签名:使用公众号申请
  2. 申请模板:发送短信的模板,{1}---可以后期用代码填上内容
  3. 免费可发的有100条
  4. 代码发送短信,参考:https://cloud.tencent.com/document/product/382/43196

二、API 和 SDK

# API文档
    -之前学的接口文档的概念
    -使用api调用,比较麻烦,固定输入,接受固定的返回
    -使用postman都可以测试,携带你的认证的秘钥。
    
# SDK:Software Development Kit 软件开发工具包
    -分语言的
    -基于API,使用某个编程语言封装的包
    -例如python:pip install 包
    -包.发短信(参数)

    -一般厂商都会提供各大主流语言的sdk


# 腾讯短信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/Zhang614/p/16879008.html

相关文章