def generate_verify_url(self): # 构造有效数据 data = {'user_id': self.id} # 加密 token = tjws.dumps(data, constants.VERIFY_EMAIL_EXPIRES) # 构造激活链接 return 'http://www.meiduo.site:8080/success_verify_email.html?token=' + token
def generate_verify_email_token(self): # 用id加密 data = {'user_id': self.id} # 加密 token = tjws.dumps(data, constants.USER_EMAIL_TOKEN_EXPIRES) # 拼接 url return 'http://www.meiduo.site:8080/success_verify_email.html?token=' + token
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): """先获取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') #根据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 generate_verify_email_url(self): """生成验证邮箱的url""" data = {"user_id": self.id, "email": self.email} token = tjws.dumps(data, constants.VERIFY_EMAIL_TOKEN_EXPIRES) # 验证网址链接 verify_url = 'http://www.meiduo.site:8080/success_verify_email.html?token=' + token return verify_url
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 generate_email_verify(self): """生产激活邮箱地址""" data = {"user_id": self.id} token = tjws.dumps(data, 60 * 60) verify_url = 'http://www.meiduo.site:8080/success_verify_email.html?token=' + token return verify_url