def get(self, request): # 1.获取code值 # 2.判断前端是否传递code值 # 3.通过code值获取access_token值,需先建立qq对象 # 4.通过access_token获取openid值 code = request.GET.get('code', None) if not code: return Response({'error': ' 缺少code值'}) state = '/' qq = OAuthQQ(client_id=settings.QQ_CLIENT_ID, client_secret=settings.QQ_CLIENT_SECRET, redirect_uri=settings.QQ_REDIRECT_URI, state=state) try: access_token = qq.get_access_token(code) openid = qq.get_open_id(access_token) except Exception: return Response({'message': 'QQ服务异常'}, status=503) # 判断openid是否绑定 try: oauth_user = OAuthQQUser.objects.get(openid=openid) except Exception: # 捕获到异常说明openid不存在,用户没有绑定过,将openid返回,用于绑定用户身份并进入绑定界面 tjs = TJS(settings.SECRET_KEY, 300) # 加密之后为byte类型,要先解码 open_id = tjs.dumps({'openid': openid}).decode() return Response({'access_token': open_id}) else: user = oauth_user.user # 存在则用户登陆成功,跳转到首页,绑定token值 jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER payload = jwt_payload_handler(user) token = jwt_encode_handler(payload) response = Response({ 'token': token, 'username': user.username, 'user_id': user.id, }) merge_cart_cookie_to_redis(request, user, response) return response
def post(self, request, *args, **kwargs): response = super().post(request, *args, **kwargs) serializer = self.get_serializer(data=request.data) if serializer.is_valid(): user = serializer.object.get('user') or request.user # 普通传参.传参顺序必须一致 response = merge_cart_cookie_to_redis(request, user, response) # 结果返回 return response
def get(self, request): # 1.获取code值 code = request.GET.get('code', None) # 2.判断前端是否传递code值 if not code: return Response({'error': ' 缺少code值'}) # 3.通过code值获取access_token值,需先建立weibo对象 state = '/' weibo = OAuthWB(client_id=settings.WEIBO_APP_ID, client_secret=settings.WEIBO_APP_KEY, redirect_uri=settings.WEIBO_REDIRECT_URI, state=state) try: access_token = weibo.get_access_token(code) except Exception: return Response({'message': '微博服务异常'}, status=503) # 4.判断access_token否绑定 try: oauth_user = OAuthSinaUser.objects.get(access_token=access_token) except Exception: # 捕获到异常说明access_token不存在,用户没有绑定过,将access_token返回,用于绑定用户身份并进入绑定界面 tjs = TJS(settings.SECRET_KEY, 300) # 加密之后为byte类型,要先解码 accesstoken = tjs.dumps({'access_token': access_token}).decode() return Response({'access_token': accesstoken}) else: # 存在则用户登陆成功,跳转到首页,绑定token值 user = oauth_user.user jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER payload = jwt_payload_handler(user) token = jwt_encode_handler(payload) response = Response({ 'token': token, 'username': user.username, 'user_id': user.id, }) merge_cart_cookie_to_redis(request, user, response) return response
def post(self, request, *args, **kwargs): # 原有的登录功能不变,调用父类方法 response = super().post(request, *args, **kwargs) # 获取user serializer = self.get_serializer(data=request.data) if serializer.is_valid(): user = serializer.object.get('user') or request.user response = merge_cart_cookie_to_redis(request, response, user) # 返回数据 return response
def post(self, request, *args, **kwargs): response = super().post(request, *args, **kwargs) serializer = self.get_serializer(data=request.data) if serializer.is_valid(): user = serializer.object.get('user') response = merge_cart_cookie_to_redis(request, response, user) return response
def post(self, request, *args, **kwargs): # 调用父类的方法,获取drf jwt扩展默认的认证用户处理结果 response = super().post(request, *args, **kwargs) # 仿照drf jwt扩展对于用户登录的认证方式,判断用户是否认证登录成功 serializer = self.get_serializer(data=request.data) if serializer.is_valid(): user = serializer.object.get('user') or request.user # 如果用户登录认证成功,则合并购物车 response = merge_cart_cookie_to_redis(request, response, user) return response
def get(self, request): # 1、获取code值 code = request.query_params.get('code', None) # 2、判断是否真的前端传递有code值 if code is None: return Response({'errors': '缺少code值'}, status=400) # 3、通过code值获取access_token # 初始化OAuthQQ对象 qq = OAuthQQ(client_secret=settings.QQ_CLIENT_SECRET, client_id=settings.QQ_CLIENT_ID, redirect_uri=settings.QQ_REDIRECT_URI, state='/') access_token = qq.get_access_token(code) # 4、通过access_token值或openid openid = qq.get_open_id(access_token) # 5、判断openid是否绑定 try: # 6、查询openid所对应的数据是否存在 qq_user = OAuthQQUser.objects.get(openid=openid) except: # 7、 不存在则进入绑定页面进行保存绑定 tjs = TJS(settings.SECRET_KEY, 300) open_id = tjs.dumps({'openid': openid}).decode() return Response({'access_token': open_id}) else: # 8、存在则用户登录成功跳转到首页 # 9、生成jwt token值 user = qq_user.user jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER payload = jwt_payload_handler(user) token = jwt_encode_handler(payload) response = Response({ 'token': token, 'username': user.username, 'user_id': user.id }) response = merge_cart_cookie_to_redis(request, response, user) return response
def get(self, request): """ 获取openid 思路分析: 前端: 1. 用户扫码成功之后,qq服务器会引导用户跳转到美多页面 2. 前端通过js代码获取路径中的code(授权码)值, 并携带code值向后端发送请求 后端: 3. 获取code值,生成Access Token """ # 获取code值 AuthCode = request.query_params.get('code', None) # 判断AuthCode值是否存在 if not AuthCode: return Response({'message': '缺少code值'}, status=400) # 通过code值获取token, 实例QQ对象 qq = OAuthQQ(client_id=QQ_CLIENT_ID, client_secret=QQ_CLIENT_SECRET, redirect_uri=QQ_REDIRECT_URI, state='/') # 调用get_access_token方法获取token值 access_token = qq.get_access_token(code=AuthCode) # 通过access_token获取openid openid = qq.get_open_id(access_token=access_token) # 判断操作 try: # 查询表中是否有数据 qq_user = OAuthQQUser.objects.get(openid=openid) except Exception as e: # 将openid进行加密操作,密文返回给前端 tjs = TJS(settings.SECRET_KEY, 300) # 调用加密方法进行加密 open_id = tjs.dumps({'openid': openid}).decode() # 报错,说明没有,跳转到绑定页面(access_token前端接收openid的变量) # 因为在绑定的时候需要openid与user一起绑定所以需要传递 return Response({'access_token': open_id}) else: # 没有报错说明已经绑定过,跳转到首页 # 获取qq_user中的user对象 user = qq_user.user # 因为是登录操作,所以需要生成token数据发送给前端 # token加密 jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER payload = jwt_payload_handler(user) token = jwt_encode_handler(payload) # user对象添加属性 user.token = token response = Response({ 'token': token, 'username': user.username, 'user_id': user.id }) response = merge_cart_cookie_to_redis(request, response, user) return response