1 2 3 4

后端注册接口完善

1.完善注册接口

1.1 修改user/views.py中完善视图函数

# 注册接口
class RegisterView(APIView):
    """
    用户注册, 权限是: 匿名用户可访问
    """
    # 自定义权限类
    permission_classes = (AllowAny,)

    def post(self, request):
        """
        接收用户名,密码,手机号和验证码, 前端校验两遍一致性, 注册成功后返回成功, 然后用户自行登录获取token
        1. 用户名
        2. 密码
        3. 手机号
        4. 验证码
        :param request:
        :return:  {'code':0,'msg':'注册成功'}
        code: "260361"
        password: "123123"
        phone: "13303479527"
        username: "liangxuepeng"
        """
        username = request.data.get('username')
        phone = request.data.get('phone')
        code = request.data.get('code')
        passwrod = request.data.get('password')

        if all([username, passwrod, phone, code]):
            pass
        else:
            return Response({'code': 999, 'msg': '参数不全'})

        # rand_name = self.randomUsername()
        # 验证手机验证码
        redis_client = get_redis_connection('verify_code')
        code_redis = redis_client.get(phone)
        if code_redis:
            code_redis = code_redis.decode()

        if not code == code_redis:
            return Response({'code': 999, 'msg': '手机验证码错误'})

        user = User(username=username, phone=phone)
        user.set_password(passwrod)
        user.save()

        return Response({'code': 0, 'msg': '注册成功'})

 

posted @ 2020-10-08 17:15  发哥奇妙IT之旅  阅读(127)  评论(0编辑  收藏  举报