后端注册接口完善

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')
        password = request.data.get('password')

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

        # rand_name = self.randomUsername()
        redis_client = get_redis_connection('img_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(password)
        user.save()

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

user/urls.py中添加

urlpatterns = [
      path('register/',views.RegisterView.as_view())
]

注册信息

posted @ 2020-10-08 17:17  BeginnerY  阅读(157)  评论(0编辑  收藏  举报