Ejemplo n.º 1
0
    def post(self, request):
        response = BaseResponse()

        receive = request.data
        if request.method == 'POST':
            print(receive)
            is_valid = verify(receive)
            print("is_valid", is_valid)
            if is_valid:

                username = receive.get("username")
                password = receive.get("password")
                user = auth.authenticate(username=username, password=password)
                # 用户名+密码验证成功
                if user is not None:
                    # update the token
                    key = self.generate_key()
                    from django.utils.timezone import utc
                    import datetime
                    now = datetime.datetime.now()
                    Token.objects.update_or_create(user=user,
                                                   defaults={
                                                       "key": key,
                                                       "created": now
                                                   })
                    user_info = UserInfo.objects.get(pk=user.pk)
                    serializer = UserInfoSerializer(user_info)
                    data = serializer.data

                    response.msg = "验证成功!"
                    response.userinfo = data
                    response.token = key

                # 用户名+密码匹配失败
                else:
                    # 只匹配用户名
                    try:
                        UserInfo.objects.get(username=username)
                        response.msg = "密码错误!"
                        response.code = 1002
                    except UserInfo.DoesNotExist:
                        response.msg = "用户不存在!"
                        response.code = 1003
            else:

                response.code = 1001
                response.msg = "请完成滑动验证!"

            return Response(response.dict)
Ejemplo n.º 2
0
    def post(self,request):
        response =BaseResponse()
        try:
            print(request.data)
            user = request.data.get("user")
            pwd = request.data.get("pwd")

            user = User.objects.filter(user=user, pwd=pwd).first()
            import uuid
            random_str = uuid.uuid4()
            if user:

                UserToken.objects.update_or_create(user=user, defaults={"token": random_str})
                response.user = user.user
                response.token = random_str
            else:
                response.code = 1001
                response.error = "用户名或者密码错误"

        except Exception as e:
            response.code = 1002
            response.error = str(e)

        return Response(response.dict)