首页 > 其他分享 >为开源代码做贡献 pycharm使用git 登录注册功能分析 手机号是否存在的接口 多方式登录接口 腾讯云短信申请

为开源代码做贡献 pycharm使用git 登录注册功能分析 手机号是否存在的接口 多方式登录接口 腾讯云短信申请

时间:2023-03-03 22:14:07浏览次数:36  
标签:username 源代码 短信 登录 接口 token user 序列化

目录

为开源代码贡献

github,gitee 看到好的开源项目,发现有bug,为他增加新功能 --->>> 你加入了代码 --->>> 想合并进开源项目,如何做?

步骤:

1.先fork开源项目 --->>> 复制这个项目到我的仓库中

2.clone下来,改代码,一路提交到远端(我的)

3.提交pr,等作者同意

pycharm使用git

只要用命令操作的,都可以点击完成。

1.先配置pycharm使用git

image-20230303100202661

git add . 工作区拉到暂存区

image-20230303095408897

git commit -m '解释' 暂存区提交到版本库

image-20230303095542916

从远程仓库拉取代码

image-20230303095614690

本地推送代码至远程仓库

image-20230303095712512

在改动的文件右击有:add、commit、push

image-20230303095927399

右下角 branch,分支操作

image-20230303155628444

远程仓库操作

image-20230303100439931

跟历史代码比较

image-20230303100555402

提交记录

image-20230303100649080

登录注册功能分析

接口分析

  • 校验手机号是否存在的接口
  • 多方式登录接口:用户名/手机号/邮箱 + 密码都可以登录
  • 发送手机验证码接口(借助于第三方短信平台)
  • 短信登录接口
  • 注册接口

手机号是否存在接口

urls.py

from rest_frame.router import SimpleRouter
from . import views

router = SimplRouter()
# http://127.0.0.1:8000/api/v1/user/userinfo/send_sms/	get 发送短信
router.register('userinfo',views.UserView,'userinfo')

urlpatterns = []

urlpatterns += router.urls

views.py

class UserView(GenericViewSet):
    @action(methods=['GET'], detail=False)  # 保证这个接口的安全(短信轰炸机--》解析出了好多网站的发送短信接口,用多线程)
    def send_sms(self, request, *args, **kwargs):
        try:
            # 从地址栏中取出手机号  query_params :queryDict
            mobile = request.query_params['mobile']
            User.objects.get(mobile=mobile)
        except Exception as e:
            raise e
            # return APIResponse(code=777,msg='手机号不存在')
        return APIResponse(msg='手机号存在')

视图函数模板

   def send_sms(self, request, *args, **kwargs):
        try:
            # 放心大胆写
        except Exception as e:
            raise e
        return APIResponse()

多方式登录接口

使用:用户名,手机号,邮箱+密码登录

发post请求,--->>> {username:[email protected], password:123}

视图类

class UserView(ViewSetMixin, GenericAPIView):
  serializer_calss = UserLoginSerializer
  queryset = User,objects.all().filter(is_active=True)
  @action(methods=['POST'], detail=False)
  def login_mul(self, reqeust, *args, **kwargs):
    '''
    把这个逻辑放在序列化类中
    1.取出前端传入的用户名和密码
    2.通过用户名和密码去数据库查询用户
    3.如果能查到,签发token
    4.返回给前端登录成功
    '''
    # 实例化 序列化类对象时,可以传入context字典	context 是 视图类和序列化类沟通的桥梁
    # 序列化类全局钩子,放入的
    # 有了序列化类对象,通过 对象.context 就可以拿到值
    ser = self.get_serializer(data=request.data)
    ser.is_valid(raise_exception=True)  # 执行这话,会走字段自己的校验,局部钩子,全局钩子
    token = ser.context.get('token')  # ser.context什么呢?
    username = ser.context.get('username')
    return APIResponse(token=token, username=username) # {code:100,msg:成功,token:assafdaf,username:lqz}

序列化类

from .models import User
from rest_framework import serializers
import res
from rest_framework.exceptions import APIException, 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 UserLoginSerializer(serializers.ModelSerializer):
    # 坑
    # 重写一下username,把原来的校验规则去掉
    username = serializers.CharField()

    class Meta:
        model = User
        # username 映射过来,是唯一的,字段自己的校验就过不了,所有要重写这个字段
        fields = ['username', 'password']  # 这个序列化类用来校验字段---不做序列化,也不做反序列化

    # 全局钩子
    def validate(self, attrs):
        '''
         把这个逻辑放在序列化类中
         1 取出前端传入的用户名和密码
         2 通过用户名和密码去数据库查询用户
         3 如果能查到,签发token
         4 返回给前端登录成功
        '''
        # attrs 是前端传入的数据,经过 字段自己校验和局部钩子校验过后的数据   {username:lqz,password:123}
        user = self._get_user(attrs)
        token = self._get_token(user)
        # 把用户名,和token放到ser的 context中
        self.context['token'] = token
        self.context['username'] = user.username
        return attrs

    # 在类内部,隐藏属性和方法, __ 开头
    # 公司里约定俗成,不用 __ ,使用 _ ,表示不想给外部用,但是实在想用,根据名字直接用
    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 = User.objects.filter(mobile=username).first()
        elif re.match(r'^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$', username):
            user = User.objects.filter(email=username).first()
        else:
            user = User.objects.filter(username=username).first()
        if user and user.check_password(password):
            return user
        else:
            # 用户不存在或密码错误   这里的代码,还是在全局钩子中执行,全局钩子校验失败,要抛异常,所以在这抛异常
            raise APIException('用户不存在或密码错误')

    def _get_token(self, user):
        payload = jwt_payload_handler(user)
         token = jwt_encode_handler(payload)
        return token

腾讯云短信申请

发送短信接口,借助于第三方短信平台,收费的:有腾讯云短信,阿里

申请微信公众号:自己搜 实名认证

使用腾讯短信

-https://cloud.tencent.com,微信扫码登录
    -搜索短信:https://console.cloud.tencent.com/smsv2
    - 创建短信签名:公众号注册,提交等待审核
	- 创建短信正文模版
	-等待审核
	-发送短信
    	python 代码发送短信

补充

# https://gitee.com/aeasringnar/django-RESTfulAPI/tree/master
# https://gitee.com/aeasringnar

标签:username,源代码,短信,登录,接口,token,user,序列化
From: https://www.cnblogs.com/xiao-fu-zi/p/17177143.html

相关文章