def post(self, request): mobile = request.POST.get('mobile') pwd = request.POST.get('pwd') sms_code = request.POST.get('sms_code') secret_openid = request.POST.get('openid') openid = check_openid(secret_openid) if openid is None: return HttpResponseBadRequest('openid错误') try: user = User.objects.get(mobile=mobile) except User.DoesNotExist: user = User.objects.create_user(username=mobile, password=pwd, mobile=mobile) else: if not user.check_password(pwd): return HttpResponseBadRequest('密码错误') OAuthQQUser.objects.create(user=user, openid=openid) login(request, user) response = redirect(reverse('contents:index')) response.set_cookie('username', user.username, max_age=24 * 3600) return response
def post(self, request): # 1. 接收参数 data = json.loads(request.body.decode()) # 2. 提取参数 mobile = data.get('mobile') password = data.get('password') sms_code = data.get('sms_code') access_token = data.get('access_token') openid = check_openid(access_token) # 3. 验证参数 # 4. 根据手机号判断用户信息 try: user = User.objects.get(mobile=mobile) except: user = User.objects.create(username=mobile, password=password, mobile=mobile) else: if not user.check_password(password): return JsonResponse({"code": 400, 'errmsg': '绑定失败'}) # 5. 绑定用户信息 OAuthQQUser.objects.create(openid=openid, user=user) login(request, user) response = JsonResponse({'code': 0, 'errmsg': 'ok'}) response.set_cookie('username', user.username, max_age=14 * 24 * 3600) return response
def post(self, request): mobile = request.POST.get('mobile') pwd = request.POST.get('pwd') sms_code = request.POST.get('sms_code') secret_openid = request.POST.get('openid') if not all([mobile, pwd, sms_code, secret_openid]): return HttpResponseBadRequest('参数不全') # 4.判断手机号是否符合规则 if not re.match(r'^1[3-9]\d{9}$', mobile): return HttpResponseBadRequest('请输入正确的手机号码') # 5.判断密码是否符合规则 if not re.match(r'^[0-9A-Za-z]{8,20}$', pwd): return HttpResponseBadRequest('请输入8-20位的密码') redis_conn = get_redis_connection('code') sms_code_server = redis_conn.get('sms_%s' % mobile) if sms_code_server is None: return render(request, 'oauth_callback.html', {'sms_code_errmsg': '无效的短信验证码'}) if sms_code != sms_code_server.decode(): return render(request, 'oauth_callback.html', {'sms_code_errmsg': '输入短信验证码有误'}) openid = check_openid(secret_openid) if openid is None: return HttpResponseBadRequest('openid错误') try: user = User.objects.get(mobile=mobile) except User.DoesNotExist: user = User.objects.create_user(username=mobile, password=pwd, mobile=mobile) else: if not user.check_password(pwd): return HttpResponseBadRequest('密码错误') OAuthQQUser.objects.create(user=user, openid=openid) login(request, user) response = redirect(reverse('contents:index')) response.set_cookie('username', user.username, max_age=24 * 3600) return response
def post(self, request): # ①接收数据 mobile = request.POST.get('mobile') pwd = request.POST.get('pwd') sms_code = request.POST.get('sms_code') secret_openid = request.POST.get('openid') #添加解密 openid = check_openid(secret_openid) if openid is None: return HttpResponseBadRequest('openid错误') # ②验证数据 openid (此处课上省略) # 参数是否齐全 # 手机号是否符合规则 # 密码是否符合规则 # 短信验证码是否一致 # ③根据手机号进行用户信息的查询 user try: user = User.objects.get(mobile=mobile) except User.DoesNotExist: # 如果不存在,说明用户手机号没有注册过,我们就以这个手机号注册一个用户 user = User.objects.create_user(username=mobile, password=pwd, mobile=mobile) else: # 如果存在,则需要验证密码 if not user.check_password(pwd): return HttpResponseBadRequest('密码错误') # ④ 绑定openid 和 user OAuthQQUser.objects.create(user=user, openid=openid) # ⑤ 登陆(设置登陆状态,设置cookie,跳转到首页) login(request, user) response = redirect(reverse('contents:index')) response.set_cookie('username', user.username, max_age=24 * 3600) return response
def post(self, request): """美多商城用户绑定到openid""" # ①接收数据 mobile = request.POST.get('mobile') password = request.POST.get('pwd') # pic_code = request.POST.get('pic_code') sms_code = request.POST.get('sms_code') secret_openid = request.POST.get('openid') # ②验证数据 # # 参数是否齐全 # if not all([mobile, password, sms_code]): # return HttpResponseBadRequest('参数不全') # 手机号是否符合规则 # if not re.match(r'^1[3-9]\d{9}$', mobile): # return HttpResponseBadRequest('请输入正确的手机号') # 密码是否符合规则 # if not re.match(r'^[0-9A-Za-z]{8,20}$', password): # return HttpResponseBadRequest('请输入8-20位的密码') # 短信验证码是否一致 # from django_redis import get_redis_connection # redis_conn = get_redis_connection('code') # sms_code_server = redis_conn.get('sms_%s' % mobile) # 没有短信验证码 # if sms_code_server is None: # return render(request, 'oauth_callback.html', {'sms_code_errmsg': '无效的短信验证码'}) # # 获取到的密码与数据库密码不相等 # if sms_code != sms_code_server: # return render(request, 'oauth_callback.html', {'sms_code_errmsg': '输入短信验证码有误'}) # # openid解密 openid = check_openid(secret_openid) if openid is None: return HttpResponseBadRequest('openid错误') # ③根据手机号进行用户信息的查询 user try: from apps.users.models import User user = User.objects.get(mobile=mobile) except User.DoesNotExist: # 如果不存在,说明用户手机号没有注册过,我们就以这个手机号注册一个用户 user = User.objects.create(username=mobile, password=password, mobile=mobile) # 创建完并加密 new_openid = serect_openid(openid) # 运行登录检查里会看到加密后的openid # value = "eluqnHjkip6zNjyXf3L2QWXTfcoGhpaf3Nhw71tWZoIiNQeT1zYtEr0DPkcck8mY" else: # 如果存在,则需要验证密码 from apps.users.models import User # 状态保持 login(request, user) if user.check_password(password): return HttpResponseBadRequest('密码错误') # ④ 绑定openid 和 user #user新创的=user OAuthQQUser.objects.create(user=user, openid=secret_openid) # ⑤ 登陆(设置登陆状态,设置cookie,跳转到首页) login(request, user) response = redirect(reverse('contents:index')) response.set_cookie('username', user.username, max_age=24 * 3600) return response