def put(self, request): # 接收 dict1 = json.loads(request.body.decode()) email = dict1.get('email') # 验证 if not all([email]): return http.JsonResponse({ 'code': RETCODE.PARAMERR, 'errmsg': '没有邮箱数据' }) if not re.match('^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return http.JsonResponse({ 'code': RETCODE.PARAMERR, 'errmsg': '邮箱格式错误' }) # 处理 user = request.user user.email = email user.save() # 调用发邮件的任务 token = meiduo_signature.dumps({'user_id': user.id}, contants.EMAIL_ACTIVE_EXPIRES) url = settings.EMAIL_ACTIVE_URL + '?token=%s' % token send_user_email.delay(email, url) # 响应 return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'ok'})
def get(self, request): code = request.GET.get('code') state = request.GET.get('state', '/') oauthqq = OAuthQQ(settings.QQ_CLIENT_ID, settings.QQ_CLIENT_SECRET, settings.QQ_REDIRECT_URI, state) # 1.根据code获取token token = oauthqq.get_access_token(code) # 2.根据token获取openid openid = oauthqq.get_open_id(token) # 判断是否初次授权 try: qquser = OAuthQQUser.objects.get(openid=openid) except: # 未查到数据,则为初次授权,显示绑定页面 # 将openi加密 json_str = meiduo_signature.dumps({"openid": openid}, constants.OPENID_EXPIRES) # 显示绑定页面 context = {'token': json_str} return render(request, 'oauth_callback.html', context) else: # 查询到授权对象,则状态保持,转到相关页面 user = qquser.user login(request, user) response = redirect(state) response.set_cookie('username', user.username) return response
def put(self, request): # 接收 json_dict = json.loads(request.body.decode()) email = json_dict.get('email') # 验证 if not all([email]): return http.JsonResponse({ 'code': RETCODE.EMAILERR, 'errmsg': "邮箱无效" }) if not re.match(r'^[a-z0-9][\w\.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return http.JsonResponse({ 'code': RETCODE.EMAILERR, 'errmsg': "邮箱格式错误" }) # 处理:修改当前登录用户的邮箱属性 user = request.user user.email = email user.save() # 发邮件:耗时代码,使用celery异步 # send_mail('美多商城-邮箱激活','',settings.EMAIL_FROM,[email],html_message='') # 将用户编号加密 token = meiduo_signature.dumps({'user_id': user.id}, constants.EMAIL_ACTIVE_EXPIRES) # 拼接激活的链接地址 verify_url = settings.EMAIL_VERIFY_URL + '?token=' + token # 异步发邮件 send_active_mail.delay(email, verify_url) # 响应 return http.JsonResponse({'code': RETCODE.OK, 'errmsg': "OK"})
def get(self, request): # 获取openid # 1.接收code code = request.GET.get('code') next_url = request.GET.get('state') # 2.创建工具对象 qq_tool = OAuthQQ(settings.QQ_CLIENT_ID, settings.QQ_CLIENT_SECRET, settings.QQ_REDIRECT_URI, next_url) try: # 3.根据code获取token token = qq_tool.get_access_token(code) # 4.根据token获取openid openid = qq_tool.get_open_id(token) # 绑定:与本网站的账号关联 try: qquser = OAuthQQUser.objects.get(openid=openid) except: # 未找到绑定对象,说明是初次授权,展示绑定页面 # 加密 token = meiduo_signature.dumps({'openid': openid}, constants.OPENID_EXPIRES) # 展示页面 context = {'token': token} return render(request, 'oauth_callback.html', context) else: # 查找到绑定对象,则状态保持 login(request, qquser.user) response = redirect(next_url) response.set_cookie('username', qquser.user.username) # 合并购物车 response = merge_cart_cookie_to_redis(request, response) return response except: openid = 0 return http.HttpResponse(openid)