Django+DRF框架完成绑定和解绑手机号的业务

class UserView(GenericViewSet, mixins.RetrieveModelMixin):
    """ 用户相关的用户级集 """
    queryset = models.User.objects.all()
    serializer_class = UserSerializers
    # 设置认证用户才能访问
    permission_classes = [IsAuthenticated, UserPermissions]
    

    # 绑定手机号业务
    def bind_mobile(self, request: Request, *args, **kwargs):
        # 1. 获取参数code,mobile,codeID
        code = request.data.get('code')
        mobile = request.data.get('mobile')
        codeID = request.data.get('codeID')
        # 2. 校验参数
        # 2.1 code,mobile,codeID是否存在
        if not all([code, mobile, codeID]):
            return Response({'error': "code、mobile、codeID缺少!"}, status=status.HTTP_422_UNPROCESSABLE_ENTITY)
        # 2.2 code,是否存在数据库
        exists = VerifCode.objects.filter(id=codeID, code=code, mobile=mobile).exists()
        if not exists:
            return Response({'error': "该验证码无效!"}, status=status.HTTP_422_UNPROCESSABLE_ENTITY)
        else:
            # 2.3 code,是否过期,时间为三分钟
            # 2.3.1 获取此验证码的创建时间,并转成时间戳
            code_obj = VerifCode.objects.filter(id=codeID, code=code, mobile=mobile).first()
            code_time = code_obj.create_time.timestamp()
            # 2.3.2 获取当前时间,并转成时间戳
            now_time = time.time()
            # 删除验证码(避免出现用户在有效期内,使用同一个验证码重复请求)
            if code_time + 180 < now_time:
                return Response({'error': "验证码已过期,请重新获取!"}, status=status.HTTP_422_UNPROCESSABLE_ENTITY)
        # 3. 修改此用户的手机号
        # 3.1 判断该手机号是否已经被绑定
        exists_mobile = User.objects.filter(mobile=mobile).exists()
        if exists_mobile:
            return Response({'error': "该手机号已经被绑定!"}, status=status.HTTP_422_UNPROCESSABLE_ENTITY)
        # 3.2 将手机号绑定给该用户
        user = request.user
        user.mobile = mobile
        user.save()
        return Response({'message': "手机号绑定成功!"}, status=status.HTTP_200_OK)
#解绑手机号业务    
def unbind_mobile(self, request: Request, *args, **kwargs):
        # 1. 获取参数
        code = request.data.get('code')
        mobile = request.data.get('mobile')
        codeID = request.data.get('codeID')
        # 2. 校验参数
        # 2.1 code,mobile,codeID是否存在
        if not all([code, mobile, codeID]):
            return Response({'error': "code、mobile、codeID缺少!"}, status=status.HTTP_422_UNPROCESSABLE_ENTITY)
        # 2.2 code,是否存在数据库
        exists = VerifCode.objects.filter(id=codeID, code=code, mobile=mobile).exists()
        if not exists:
            return Response({'error': "该验证码无效!"}, status=status.HTTP_422_UNPROCESSABLE_ENTITY)
        else:
            # 2.3 code,是否过期,时间为三分钟
            # 2.3.1 获取此验证码的创建时间,并转成时间戳
            code_obj = VerifCode.objects.filter(id=codeID, code=code, mobile=mobile).first()
            code_time = code_obj.create_time.timestamp()
            # 2.3.2 获取当前时间,并转成时间戳
            now_time = time.time()
            # 删除验证码(避免出现用户在有效期内,使用同一个验证码重复请求)
            code_obj.delete()
            if code_time + 180 < now_time:
                return Response({'error': "验证码已过期,请重新获取!"}, status=status.HTTP_422_UNPROCESSABLE_ENTITY)
        # 3. 解绑手机
        # 3.1 先判断手机号是否绑定
        user_mobile = request.user.mobile
        if user_mobile != mobile:
            return Response({'error': "当前用户没有绑定该手机号!"}, status=status.HTTP_422_UNPROCESSABLE_ENTITY)
        # 3.2 将该用户手机号置为空
        user = request.user
        user.mobile = ''
        user.save()
        return Response({"messgae": "解绑手机号成功!"}, status=status.HTTP_200_OK)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值