Ejemplo n.º 1
0
    def get(self, request):
        #获取code
        code = request.query_params.get('code')
        #根据code获取token
        oauthqq = OAuthQQ()
        token = oauthqq.get_access_token(code)
        #根据token获取openid
        openid = oauthqq.get_openid(token)

        #查询openid是否存在

        try:
            qquser = OAuthQQUser.objects.get(openid=openid)
        except:
            #如果不存在,则通知用户绑定页面
            #将openid加密进行输出
            data = tjws.dumps({'openid': openid}, constants.BIND_TOKEN_EXPIRES)
            # 响应
            return Response({'access_token': data})
        else:
            # 如果存在则状态保持,登录成功
            response = Response({
                "user_id": qquser.user_id,
                "username": qquser.user.username,
                "token": generate(qquser.user)
            })
            #合并
            response = merge_cookie_to_redis(request, qquser.user.id, request)
            #响应
            return response
Ejemplo n.º 2
0
    def get(self, request):
        # 获取code
        code = request.query_params.get('code')
        print(code)
        # 根据code获取token
        oauthqq = OAuthQQ()
        token = oauthqq.get_access_token(code)

        # 根据token 获取openid
        openid = oauthqq.get_openid(token)

        # 查询openid 是否存在
        try:
            qquser = QQUser.objects.get(openid=openid)
        except:
            # 如果不存在,则通知客户端转到绑定页面
            # 将openid加密进行输出
            data = tjws.dumps({'openid': openid}, constants.BIND_TOKEN_EXPIRES)
            # 响应
            return Response({'access_token': data})
        else:
            # 如果存在就状态保存,登录成功
            return Response({
                'user_id': qquser.user.id,
                'username': qquser.user.username,
                'token': generate(qquser.user)
            })
Ejemplo n.º 3
0
    def get(self, request):
        """
        GET请求
        :param request:
        :return:
        """
        # 获取code
        code = request.query_params.get('code')

        # 根据code获取token
        oauthqq = OAuthQQ()
        token = oauthqq.get_access_token(code)

        # 根据token获取openid
        openid = oauthqq.get_openid(token)

        # 查询openid是否存在,即之前是否已经用qq号绑定了一个注册账号
        try:
            qquser = QQUser.objects.get(openid=openid)
        except Exception as e:
            logger.error(e)
            # 如果不存在,则通知客户端的转到绑定页面
            # 将openid加密进行输出
            data = tjws.dumps({'openid': openid}, constants.BIND_TOKEN_EXPIRES)
            # 响应
            return Response({
                'access_token': data
            })
        else:
            # 如果存在则进行状态保持,登录成功
            return Response({
                'user_id': qquser.user.id,
                'username': qquser.user.username,
                'token': generate(qquser.user)  # 生成token值
            })
Ejemplo n.º 4
0
 def get(self, request):
     # 提供用于qq登录的url
     next = request.query_params.get("next")
     #用户QQ登录成功后进入美多商城的哪个网址
     oauth = OAuthQQ(state=next)
     login_url = oauth.get_qq_login_url()
     return Response({'login_url': login_url})
Ejemplo n.º 5
0
 def get(self, request):
     # 接受登录后的地址
     state = request.query_params.get('next')
     # 创建工具类对象
     oauthqq = OAuthQQ(state=state)
     # 获取授权地址
     url = oauthqq.get_qq_login_url()
     return Response({'login_url': url})
Ejemplo n.º 6
0
 def get(self, request):
     """
     提供用于QQ登录的url
     """
     next = request.query_params.get('next')
     oauth = OAuthQQ(state=next)
     login_url = oauth.get_qq_login_url()
     return Response({'login_url': login_url})
Ejemplo n.º 7
0
 def get(self, request):
     """
     GET请求
     :param request:
     :return:
     """
     # 接收登录后的地址(请求字符串中携带的参数)
     state = request.query_params.get('next')
     # 创建工具类对象
     oauthqq = OAuthQQ(state=state)
     # 获取授权地址
     url = oauthqq.get_qq_login_url()
     # 响应
     return Response({
         'login_url': url
     })
Ejemplo n.º 8
0
    def validate(self, attrs):
        # 检验access_token
        access_token = attrs['access_token']

        openid = OAuthQQ.check_save_user_token(access_token)
        if not openid:
            raise serializers.ValidationError('无效的的access_token')

        attrs['openid'] = openid
        # 检验短信验证码
        mobile = attrs['mobile']
        sms_code = attrs['sms_code']
        redis_conn = get_redis_connection('sms_code')
        real_sms_code = redis_conn.get('sms_code_' + mobile)
        if real_sms_code.decode() != sms_code:
            raise serializers.ValidationError('短信验证码错误')

        # 如果用户存在,检查用户密码
        try:
            user = User.objects.get(mobile=mobile)
        except User.DoesNotExist:
            pass
        else:
            password = attrs['password']
            if not user.check_password(password):
                raise serializers.ValidationError('密码错误')
            attrs['user'] = user
        return attrs
Ejemplo n.º 9
0
    def get(self, request):
        """
        获取qq登录的用户数据
        :param request:
        :return:
        """
        code = request.query_params.get('code')
        if not code:
            return Response({'message': '缺少code'},
                            status=status.HTTP_400_BAD_REQUEST)

        oauth = OAuthQQ()
        # 获取用户openid
        try:
            access_token = oauth.get_access_token(code)
            openid = oauth.get_openid(access_token)
        except QQAPIError:
            return Response({'message': 'QQ服务异常'},
                            status=status.HTTP_503_SERVICE_UNAVAILABLE)

        # 判断用户是否存在
        try:
            qq_user = OAuthQQUser.objects.get(openid=openid)
        except OAuthQQUser.DoesNotExist:
            # 用户第一次使用QQ登录
            token = oauth.generate_save_user_token(openid)
            return Response({'access_token': token})
        else:
            # 找到用户,生成token
            user = qq_user.user
            token = generate_jwt_token(user)

            response = Response({
                'token': token,
                'user_id': user.id,
                'username': user.username
            })
            response = merge_cart_cookie_to_redis(request, user, response)
            return response