def post(self, request): """ 绑用户接口 :param request: mobile手机号,password密码,sms_code短信验证码,access_token凭据 都在request.data中,传入了序列化器 :return: { 'token': user.token, 'user_id': user.id, 'username': user.username } """ # 接收数据 serializer = OAuthQQUserSerializer(data=request.data) # 验证 if not serializer.is_valid(): return Response({'message': serializer.errors}) # 保存 qquser = serializer.save() # 生成token user = qquser.user token = jwt_token.generate(user) # 响应 response = Response({ 'token': token, 'user_id': qquser.id, 'username': qquser.user.username }) # 合并购物车 response = merge_cart_cookie_to_redis(request, user, response) return response
def get(self, request): """先获取openid, 判断是否已经存在openid,存在就是授权登录过了,直接就可以返回状态保持了""" # 获取code code = request.query_params.get('code') # 获取access_token oauth = OAuthQQ() access_token = oauth.get_access_token(code) # 获取openid openid = oauth.get_openid(access_token) # 接下来就可以判断是否已经授权过了,就用这openid来判断的 try: # 在qquser这边查询 qquser = QQUser.objects.get(openid=openid) except: # 这里是不存在的情况。 返回access_token包含openid的,以便用这个openid来授权用户 # 用itsdangerous里面的TimedJSONWebSignatureSerializer来加密这个openid access_token = tjws.dumps({"openid": openid}, constants.QQ_AUTH_TOKEN_EXPIRES) return Response({"access_token": access_token}) else: # 这里是存在的情况, 直接返回状态保持就可以登录了 return Response({ "username": qquser.user.username, "user_id": qquser.user.id, "token": generate(qquser.user) })
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) })
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
def post(self, request): #接收 serializer = QQBindSerializer(data=request.data) if not serializer.is_valid(): return Response({'message': serializer.errors}) qquser = serializer.save() return Response({ 'user_id': qquser.user.id, 'username': qquser.user.username, 'token': generate(qquser.user) })
def get(self, request): """ 获取qq登陆的用户数据 :param request: 包含数据的请求 :return: response响应 """ # 获取QQ返回的授权凭证 # code = oauth.get_code(request.query_params) code = request.query_params.get('code') if not code: return Response({'message': '缺少code'}, status=status.HTTP_400_BAD_REQUEST) oauth = OAuthQQ() # 获取用户的access_token, openid try: access_token = oauth.get_access_token(code) openid = oauth.get_open_id(access_token) except QQAPIError: return Response({'message': 'QQ服务异常'}, status=status.HTTP_503_SERVICE_UNAVAILABLE) # 通过openid判断用户是否存在 try: qquser = OAuthQQUser.objects.get(openid=openid) except OAuthQQUser.DoesNotExist: # 不报错退出 # 如果不存在,则通知客户端转到绑定页面 # 用户第一次使用QQ登录 # 将openid加密存入token中 token = tjws.dumps({'openid': openid}, constants.SAVE_QQ_USER_TOKEN_EXPIRES) return Response({ 'access_token': token # 由序列化器接收,用于解密后获取openid }) else: # 找到用户,生成token user = qquser.user token = jwt_token.generate(user) response = Response({ 'token': token, 'user_id': qquser.id, 'username': qquser.user.username }) # 合并购物车 response = merge_cart_cookie_to_redis(request, user, response) return response
def post(self, request): """绑定视图, 来到这里就表示这个QQ之前没有授权和一个用户绑定过""" # 获取数据 data = request.data # 验证数据 serializer = QQUserAuthSerializer(data=data) if not serializer.is_valid(): return Response({"message": serializer.errors}) # 保存用户, 序列化器返回的可能是创建出来的用户,也可能是原有的用户,如果手机已经注册过了就会返回注册过的用户信息 qquser = serializer.save() # 返回响应 return Response({ "username": qquser.user.username, "id": qquser.user.id, "token": generate(qquser.user) })
def post(self, request): #接收 serializer = QQBindSerializer(data=request.data) #验证 if not serializer.is_valid(): return Response({"message": serializer.errors}) # 绑定:在qquser表中创建一条数据 qquser = serializer.save() # 响应:绑定完成,登录成功,状态保持 response = Response({ "user_id": qquser.user.id, "username": qquser.user.username, "token": generate(qquser.user) }) # 合并 response = merge_cookie_to_redis(request, qquser.user.id, response) # 响应 return response
def post(self, request): """登录成功后的绑定视图""" # 接收(获取到请求体里数据赋给data,创建一个序列化器对象) serializer = QQBindSerializer(data=request.data) # 验证 if not serializer.is_valid(): return Response({'message': serializer.errors}) # 绑定: 在qquser表中创建一条数据 qquser = serializer.save() # 响应: 绑定完成,表示登录成功,状态保存 response = Response({ 'user_id': qquser.user.id, 'username': qquser.user.username, 'token': generate(qquser.user) }) # 合并 response = merge_cookie_to_redis(request, qquser.user.id, response) # 响应 return response
def create(self, validated_data): # validated_data 上方验证通过后,数据保存在这里 """新建数据""" user = User() user.username = validated_data.get('username') user.mobile = validated_data.get('mobile') # user.password=validated_data.get('password') # 密码需要加密 user.set_password(validated_data.get('password')) user.save() # 生成记录登录状态的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) token = jwt_token.generate(user) user.token = token return user