def post(self, request): response = super().post(request) serializer = self.get_serializer(data=request.data) if serializer.is_valid(): user = serializer.validated_data.get("user") response = merge_cookie_to_redis(request, user, response) return response
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): '''实现登录功能''' # 1.接收json参数, 获取每一个 dict = json.loads(request.body.decode()) username = dict.get('username') password = dict.get('password') remembered = dict.get('remembered') # 2.总体检验, 查看是否为空 if not all([username, password]): return JsonResponse({'code': 400, 'errmsg': '缺少必传参数'}) # 3.username检验 # if not re.match(r'^[a-zA-Z0-9_-]{5,20}$', username): # return JsonResponse({'code': 400, # 'errmsg': 'username格式有误'}) # 4.password检验 if not re.match(r'^[a-zA-Z0-9]{8,20}$', password): return JsonResponse({'code': 400, 'errmsg': 'password格式有误'}) # 5.remembered检验是否为bool类型: if remembered: if not isinstance(remembered, bool): return JsonResponse({ 'code': 400, 'errmsg': 'remembered不是bool类型' }) # 6.登录认证(authenticate), 获取用户 user = authenticate(request=request, username=username, password=password) # 7.判断该用户是否存在 if not user: return JsonResponse({'code': 400, 'errmsg': '用户名或者密码错误'}) # 8.状态保持 login(request, user) # 9.判断是否需要记住用户 if remembered != True: # 11.如果不需要: 设置seesion有效期: 关闭浏览器立刻过期 request.session.set_expiry(0) else: # 10.如果需要: 设置sesion有效期: 两周 request.session.set_expiry(None) response = JsonResponse({'code': 0, 'errmsg': 'ok'}) # response.set_cookie(key, value, max_age) response.set_cookie('username', user.username, max_age=3600 * 24 * 14) # 增加合并购物车功能 response = merge_cookie_to_redis(request, response) # 12.返回状态 return response
def post(self, request, *args, **kwargs): response = super().post(request, *args, **kwargs) # 登录逻辑还是使用jwt中的视图实现,此处在登录后添加自己的逻辑 # 判断是否登录成功 if response.status_code == 200: #获取用户编号 user_id = response.data.get('user_id') #当前添加逻辑,合并购物车 response = merge_cookie_to_redis(request, user_id, response) return response
def post(self, request, *args, **kwargs): #调用jwt扩展的方法,对用户登录的数据进行验证 response = super().post(request) #如果用户登陆成功,进行购物车数据合并 serializer = self.get_serializer(data=request.data) if serializer.is_valid(): #表示用户登陆成功 user = serializer.validated_data.get('user') #合并购物车 response = merge_cookie_to_redis(request, user, response) return response
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 post(self, request): '''接收参数, 保存到数据库''' # 1.接收参数 dict = json.loads(request.body.decode()) username = dict.get('username') password = dict.get('password') password2 = dict.get('password2') mobile = dict.get('mobile') allow = dict.get('allow') sms_code_client = dict.get('sms_code') # 2.校验(整体) if not all( [username, password, password2, mobile, allow, sms_code_client]): return http.JsonResponse({'code': 400, 'errmsg': '缺少必传参数'}) # 3.username检验 if not re.match(r'^[a-zA-Z0-9_-]{5,20}$', username): return http.JsonResponse({'code': 400, 'errmsg': 'username格式有误'}) # 4.password检验 if not re.match(r'^[a-zA-Z0-9]{8,20}$', password): return http.JsonResponse({'code': 400, 'errmsg': 'password格式有误'}) # 5.password2 和 password if password != password2: return http.JsonResponse({'code': 400, 'errmsg': '两次输入不对'}) # 6.mobile检验 if not re.match(r'^1[3-9]\d{9}$', mobile): return http.JsonResponse({'code': 400, 'errmsg': 'mobile格式有误'}) # 7.allow检验 if allow != True: return http.JsonResponse({'code': 400, 'errmsg': 'allow格式有误'}) # 8.sms_code检验 (链接redis数据库) redis_conn = get_redis_connection('verify_code') # 9.从redis中取值 sms_code_server = redis_conn.get('sms_%s' % mobile) # 10.判断该值是否存在 if not sms_code_server: return http.JsonResponse({'code': 400, 'errmsg': '短信验证码过期'}) # 11.把redis中取得值和前端发的值对比 if sms_code_client != sms_code_server.decode(): return http.JsonResponse({'code': 400, 'errmsg': '验证码有误'}) # 12.保存到数据库 (username password mobile) try: user = User.objects.create_user(username=username, password=password, mobile=mobile) except Exception as e: return JsonResponse({'code': 400, 'errmsg': '保存到数据库出错'}) login(request, user) # 13.拼接json返回 # 生成响应对象 response = JsonResponse({'code': 0, 'errmsg': 'ok'}) # 在响应对象中设置用户名信息. # 将用户名写入到 cookie,有效期 14 天 # response.set_cookie('username', # user.username, # max_age=3600 * 24 * 14) # 增加合并购物车功能 response = merge_cookie_to_redis(request, response) # 返回响应结果 return response
def post(self, request): '''qq登录的第三个接口''' # 1.接收参数(json) dict = json.loads(request.body.decode()) mobile = dict.get('mobile') password = dict.get('password') sms_code_client = dict.get('sms_code') access_token = dict.get('access_token') # 2.总体检验,查看是否为空 if not all([mobile, password, sms_code_client, access_token]): return JsonResponse({'code': 400, 'errmsg': '缺少必传参数'}) # 3.mobile单个检验 if not re.match(r'^1[3-9]\d{9}$', mobile): return JsonResponse({'code': 400, 'errmsg': 'mobile格式有误'}) # 4.password单个检验 if not re.match(r'^[a-zA-Z0-9]{8,20}$', password): return JsonResponse({'code': 400, 'errmsg': 'password格式有误'}) # 5.链接redis, 获取redis的链接对象 redis_conn = get_redis_connection('verify_code') # 6.从redis中获取服务端的短信验证码 sms_code_server = redis_conn.get('sms_%s' % mobile) # 7.判断服务端的短信验证码是否过期 if not sms_code_server: return JsonResponse({'code': 400, 'errmsg': '短信验证码过期'}) # 8.对比前后端的短信验证码 if sms_code_client != sms_code_server.decode(): return JsonResponse({'code': 400, 'errmsg': '输入的短信验证码有误'}) # 9.自定义一个函数,把access_token解密:openid openid = check_access_token(access_token) # 10.判断openid是否存在,如果存在没问题 if openid is None: return JsonResponse({'code': 400, 'errmsg': 'openid为空'}) try: # 11.从User表中获取一个该手机号对应的用户 user = User.objects.get(mobile=mobile) except Exception as e: # 12.如果该用户不存在, 给User增加一个新的记录 user = User.objects.create_user(username=mobile, password=password, mobile=mobile) else: # 13.如果该用户存在, 比较密码是否一致 if not user.check_password(password): return JsonResponse({'code': 400, 'errmsg': '密码输入的不对'}) # 14.把openid和user保存到QQ表中 try: OAuthQQUser.objects.create(openid=openid, user=user) except Exception as e: return JsonResponse({'code': 400, 'errmsg': '保存到qq表中出错'}) # 15.状态保持 login(request, user) response = JsonResponse({'code': 0, 'errmsg': 'ok'}) # 16.设置cookie:username response.set_cookie('username', user.username, max_age=3600 * 24 * 14) #增加合并购物车功能: response = merge_cookie_to_redis(request, response) # 17.返回json return response