|
@@ -1527,3 +1527,322 @@ class wxAuthSignView(TemplateView):
|
|
|
print('---')
|
|
|
print(user_qs)
|
|
|
return self.do_login(user_qs, response)
|
|
|
+
|
|
|
+# 获取验证码
|
|
|
+class OauthAuthCodeView(TemplateView):
|
|
|
+
|
|
|
+ @method_decorator(csrf_exempt)
|
|
|
+ def dispatch(self, *args, **kwargs):
|
|
|
+ return super(OauthAuthCodeView, self).dispatch(*args, **kwargs)
|
|
|
+
|
|
|
+ @ratelimit(key='ip', rate='2/m')
|
|
|
+ def post(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ lang = request.POST.get('lang', None)
|
|
|
+ if not lang:
|
|
|
+ lang = request.POST.get('language', None)
|
|
|
+ response = ResponseObject(lang)
|
|
|
+ request_dict = request.POST
|
|
|
+ phone = request_dict.get('phone', None)
|
|
|
+ if phone is not None:
|
|
|
+ was_limited = getattr(request, 'limited', False)
|
|
|
+ if was_limited is True:
|
|
|
+ return response.json(5)
|
|
|
+ return self.ValidationError(request_dict, response)
|
|
|
+
|
|
|
+ @ratelimit(key='ip', rate='2/m')
|
|
|
+ def get(self, request, *args, **kwargs):
|
|
|
+ # Device_User.objects.filter(userEmail='chanjunkai@163.com').delete()
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ lang = request.GET.get('lang', None)
|
|
|
+ if not lang:
|
|
|
+ lang = request.GET.get('language', None)
|
|
|
+ response = ResponseObject(lang)
|
|
|
+ was_limited = getattr(request, 'limited', False)
|
|
|
+ if was_limited is True:
|
|
|
+ return response.json(5)
|
|
|
+ request_dict = request.GET
|
|
|
+ return self.ValidationError(request_dict, response)
|
|
|
+
|
|
|
+ def ValidationError(self, request_dict, response):
|
|
|
+ email = request_dict.get('email', None)
|
|
|
+ phone = request_dict.get('phone', None)
|
|
|
+ country_code = request_dict.get('country_code', None)
|
|
|
+ sign_name = request_dict.get('sign_name', None)
|
|
|
+ token = request_dict.get('token', None)
|
|
|
+ print (token)
|
|
|
+ if email is not None:
|
|
|
+ email = email.strip()
|
|
|
+ # 阿里云的发送邮箱的调用方法
|
|
|
+ return self.aliyun_emailCode(email, response)
|
|
|
+
|
|
|
+ # return self.emailCode(email, response)
|
|
|
+ elif phone is not None:
|
|
|
+ phone = phone.strip()
|
|
|
+ if country_code is None:
|
|
|
+ return self.phoneCode(phone, response, sign_name)
|
|
|
+ else:
|
|
|
+ country_code = str(country_code.strip())
|
|
|
+ return self.phoneCodeV2(country_code, phone, response, sign_name)
|
|
|
+ else:
|
|
|
+ return response.json(444)
|
|
|
+
|
|
|
+ def emailCode(self, email, response):
|
|
|
+
|
|
|
+ dataValid = DataValid()
|
|
|
+ # 邮箱匹配
|
|
|
+ if dataValid.email_validate(email) is False:
|
|
|
+ return response.json(107)
|
|
|
+ reds = RedisObject()
|
|
|
+ identifyingCode = reds.get_data(key=email + '_OauthPerfect')
|
|
|
+ # 是否以获取邮箱验证码
|
|
|
+ if identifyingCode:
|
|
|
+ return response.json(89)
|
|
|
+ user_qs = Device_User.objects.filter(username=email)
|
|
|
+ email_qs = Device_User.objects.filter(userEmail=email)
|
|
|
+ # 邮箱用户是否已存在
|
|
|
+ if user_qs.exists():
|
|
|
+ return response.json(103)
|
|
|
+ elif email_qs.exists():
|
|
|
+ return response.json(103)
|
|
|
+ # 生成随机6位数
|
|
|
+ identifyingCode = RandomStr(6, True)
|
|
|
+ # 设置随机数缓存生命周期
|
|
|
+ send_data = TemplateService.email_message(type='register_code', language=response.lang)
|
|
|
+ ses = SesClassObject()
|
|
|
+ # 发送邮件
|
|
|
+ send_res = ses.send_email(
|
|
|
+ send_address_list=[email],
|
|
|
+ subject=send_data['title'],
|
|
|
+ body=send_data['body'].replace("{username}", email).replace("{captcha}",
|
|
|
+ str(identifyingCode))
|
|
|
+ )
|
|
|
+ if send_res is not True:
|
|
|
+ return response.json(44)
|
|
|
+ if reds.set_data(key=email + '_OauthPerfect', val=identifyingCode, expire=600) is not True:
|
|
|
+ return response.json(10, 'error')
|
|
|
+ return response.json(0)
|
|
|
+ # return response.json(0, {'identifyingCode': identifyingCode})
|
|
|
+
|
|
|
+ # 阿里云获取邮箱验证码
|
|
|
+ def aliyun_emailCode(self, email, response):
|
|
|
+ print('阿里云开始')
|
|
|
+ dataValid = DataValid()
|
|
|
+ # 邮箱匹配
|
|
|
+ if dataValid.email_validate(email) is False:
|
|
|
+ return response.json(107)
|
|
|
+ reds = RedisObject()
|
|
|
+ identifyingCode = reds.get_data(key=email + '_OauthPerfect')
|
|
|
+ # 是否以获取邮箱验证码
|
|
|
+ if identifyingCode:
|
|
|
+ return response.json(89)
|
|
|
+ user_qs = Device_User.objects.filter(username=email)
|
|
|
+ email_qs = Device_User.objects.filter(userEmail=email)
|
|
|
+ # 邮箱用户是否已存在
|
|
|
+ if user_qs.exists():
|
|
|
+ return response.json(103)
|
|
|
+ elif email_qs.exists():
|
|
|
+ return response.json(103)
|
|
|
+ # 生成随机6位数
|
|
|
+ identifyingCode = RandomStr(6, True)
|
|
|
+ # 设置随机数缓存生命周期
|
|
|
+ send_data = TemplateService.email_message(type='register_code', language=response.lang)
|
|
|
+ ses = SesClassObject()
|
|
|
+ # 发送邮件
|
|
|
+ send_res = ses.alyEmailCode(
|
|
|
+ send_address_list=[email],
|
|
|
+ subject=send_data['title'],
|
|
|
+ body=send_data['body'].replace("{username}", email).replace("{captcha}", str(identifyingCode))
|
|
|
+ )
|
|
|
+ if send_res is not True:
|
|
|
+ return response.json(44)
|
|
|
+ if reds.set_data(key=email + '_OauthPerfect', val=identifyingCode, expire=600) is not True:
|
|
|
+ return response.json(10, 'error')
|
|
|
+ return response.json(0)
|
|
|
+
|
|
|
+ def phoneCode(self, phone, response, sign_name):
|
|
|
+ dataValid = DataValid()
|
|
|
+ if dataValid.mobile_validate(phone) is not True:
|
|
|
+ return response.json(107)
|
|
|
+ reds = RedisObject()
|
|
|
+ reds_key = str(phone) + '_OauthPerfect'
|
|
|
+ identifyingCode = reds.get_data(key=reds_key)
|
|
|
+ reds_key_ttl = reds.get_ttl(key=reds_key)
|
|
|
+ if reds_key_ttl > 240 and identifyingCode:
|
|
|
+ # if identifyingCode :
|
|
|
+ return response.json(90)
|
|
|
+ user_qs = Device_User.objects.filter(username=phone)
|
|
|
+ phone_qs = Device_User.objects.filter(phone=phone)
|
|
|
+ if user_qs.exists() or phone_qs.exists():
|
|
|
+ return response.json(101)
|
|
|
+ identifyingCode = RandomStr(6, True)
|
|
|
+ # 发送手机验证码
|
|
|
+ aliSms = AliSmsObject()
|
|
|
+ if sign_name == 'zosi':
|
|
|
+ sign_ms = '周视'
|
|
|
+ else:
|
|
|
+ sign_ms = 'Ansjer'
|
|
|
+ res = aliSms.send_code_sms(phone=phone, code=identifyingCode, sign_name=sign_ms,
|
|
|
+ temp_msg='SMS_151600991')
|
|
|
+ print(res)
|
|
|
+ if res["Code"] == "OK":
|
|
|
+ if reds.set_data(key=reds_key, val=identifyingCode, expire=300) is not True:
|
|
|
+ # if reds.set_data(key=phone + '_identifyingCode', val=identifyingCode, expire=60) is not True:
|
|
|
+
|
|
|
+ return response.json(10, '生成缓存系统错误')
|
|
|
+ return response.json(0)
|
|
|
+ else:
|
|
|
+ return response.json(10, res["Message"])
|
|
|
+
|
|
|
+ def phoneCodeV2(self, country_code, phone, response, sign_name):
|
|
|
+ dataValid = DataValid()
|
|
|
+ if dataValid.mobile_validate(phone) is not True:
|
|
|
+ return response.json(107)
|
|
|
+ reds = RedisObject()
|
|
|
+ reds_key = str(phone) + '_OauthPerfect'
|
|
|
+ identifyingCode = reds.get_data(key=reds_key)
|
|
|
+ reds_key_ttl = reds.get_ttl(key=reds_key)
|
|
|
+ if reds_key_ttl > 240 and identifyingCode:
|
|
|
+ # if identifyingCode :
|
|
|
+ return response.json(90)
|
|
|
+ user_qs = Device_User.objects.filter(username=phone)
|
|
|
+ phone_qs = Device_User.objects.filter(phone=phone)
|
|
|
+ if user_qs.exists() or phone_qs.exists():
|
|
|
+ return response.json(101)
|
|
|
+ identifyingCode = RandomStr(6, True)
|
|
|
+ # 短信签名
|
|
|
+ # sign_name_dict = {
|
|
|
+ # 'ansjer':'Ansjer',
|
|
|
+ # 'zosi':'周视'
|
|
|
+ # }
|
|
|
+ sign_ms = ''
|
|
|
+ if country_code == '86':
|
|
|
+ # 国内短信推送模板
|
|
|
+ temp_msg = 'SMS_151600991'
|
|
|
+ rec_phone = phone
|
|
|
+ if sign_name == 'zosi':
|
|
|
+ sign_ms = '周视'
|
|
|
+ else:
|
|
|
+ sign_ms = 'Ansjer'
|
|
|
+ else:
|
|
|
+ # 国际短信推送模板
|
|
|
+ temp_msg = 'SMS_172165867'
|
|
|
+ rec_phone = country_code + phone
|
|
|
+ sign_ms = 'Ansjer'
|
|
|
+
|
|
|
+ # 发送手机验证码
|
|
|
+ aliSms = AliSmsObject()
|
|
|
+ res = aliSms.send_code_sms(phone=rec_phone, code=identifyingCode, sign_name=sign_ms,
|
|
|
+ temp_msg=temp_msg)
|
|
|
+ print(res)
|
|
|
+ if res["Code"] == "OK":
|
|
|
+ # if reds.set_data(key=reds_key, val=identifyingCode, expire=60) is not True:
|
|
|
+ if reds.set_data(key=reds_key, val=identifyingCode, expire=300) is not True:
|
|
|
+ return response.json(10, '生成缓存系统错误')
|
|
|
+ return response.json(0)
|
|
|
+ else:
|
|
|
+ return response.json(10, res["Message"])
|
|
|
+
|
|
|
+class OauthPerfectView(TemplateView):
|
|
|
+ @method_decorator(csrf_exempt)
|
|
|
+ def dispatch(self, *args, **kwargs):
|
|
|
+ return super(OauthPerfectView, self).dispatch(*args, **kwargs)
|
|
|
+
|
|
|
+ def get(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ request_dict = request.GET
|
|
|
+ lang = request_dict.get('lang')
|
|
|
+ if not lang:
|
|
|
+ lang = request_dict.get('language', None)
|
|
|
+ response = ResponseObject(lang)
|
|
|
+ was_limited = getattr(request, 'limited', False)
|
|
|
+ if was_limited is True:
|
|
|
+ return response.json(5)
|
|
|
+ return self.ValidationError(request_dict, response)
|
|
|
+
|
|
|
+ def post(self, request):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ request_dict = request.POST
|
|
|
+ lang = request_dict.get('lang')
|
|
|
+ if not lang:
|
|
|
+ lang = request_dict.get('language', None)
|
|
|
+ response = ResponseObject(lang)
|
|
|
+ was_limited = getattr(request, 'limited', False)
|
|
|
+ if was_limited is True:
|
|
|
+ return response.json(5)
|
|
|
+ return self.ValidationError(request_dict, response)
|
|
|
+
|
|
|
+ def ValidationError(self, request_dict, response):
|
|
|
+ phone = request_dict.get('phone', None)
|
|
|
+ email = request_dict.get('email', None)
|
|
|
+ password = request_dict.get('password', None)
|
|
|
+ authcode = request_dict.get('authcode', None)
|
|
|
+ token = request_dict.get('token', None)
|
|
|
+ print (token)
|
|
|
+ token = request_dict.get('token')
|
|
|
+ tko = TokenObject(token)
|
|
|
+ if password is None or authcode is None:
|
|
|
+ return response.json(444, 'password,authcode')
|
|
|
+ authcode = authcode.strip()
|
|
|
+ password = password.strip()
|
|
|
+ if phone is not None:
|
|
|
+ phone = phone.strip()
|
|
|
+ return self.do_phone(tko,phone, authcode, password, response)
|
|
|
+ elif email is not None:
|
|
|
+ email = email.strip()
|
|
|
+ return self.do_email(tko,email, authcode, password, response)
|
|
|
+ else:
|
|
|
+ return response.json(444, 'phone')
|
|
|
+
|
|
|
+ def do_email(self,tko, email, authcode, password, response):
|
|
|
+ data_valid = DataValid()
|
|
|
+ if data_valid.email_validate(email) is not True:
|
|
|
+ return response.json(105)
|
|
|
+ if data_valid.password_validate(password) is not True:
|
|
|
+ return response.json(109)
|
|
|
+
|
|
|
+ if tko.code == 0:
|
|
|
+ user_qs = Device_User.objects.filter(userID=tko.userID)
|
|
|
+ else:
|
|
|
+ return response.json(tko.code)
|
|
|
+ if not user_qs.exists():
|
|
|
+ return response.json(104)
|
|
|
+ reds = RedisObject()
|
|
|
+ resetCode = reds.get_data(key=email + '_OauthPerfect')
|
|
|
+ if resetCode is False:
|
|
|
+ return response.json(90)
|
|
|
+ if authcode != resetCode:
|
|
|
+ return response.json(121)
|
|
|
+ # if not reds.set_data(key=email + '_forgetPwdResetCode', val=resetCode, expire=300):
|
|
|
+ # return response.json(10, '生成缓存错误')
|
|
|
+ user_qs.update(userEmail=email,password=make_password(password))
|
|
|
+ if not reds.del_data(email + '_OauthPerfect'):
|
|
|
+ return response.json(10, '删除缓存失败')
|
|
|
+ return response.json(0)
|
|
|
+
|
|
|
+ def do_phone(self,tko, phone, authcode, password, response):
|
|
|
+ data_valid = DataValid()
|
|
|
+ if data_valid.mobile_validate(phone) is not True:
|
|
|
+ return response.json(100)
|
|
|
+ if data_valid.password_validate(password) is not True:
|
|
|
+ return response.json(109)
|
|
|
+
|
|
|
+ if tko.code == 0:
|
|
|
+ user_qs = Device_User.objects.filter(userID=tko.userID)
|
|
|
+ else:
|
|
|
+ return response.json(tko.code)
|
|
|
+ if not user_qs.exists():
|
|
|
+ return response.json(102)
|
|
|
+ reds = RedisObject()
|
|
|
+ resetCode = reds.get_data(key=str(phone) + '_OauthPerfect')
|
|
|
+ print (resetCode)
|
|
|
+ if resetCode is False:
|
|
|
+ return response.json(90)
|
|
|
+ if authcode != resetCode:
|
|
|
+ return response.json(121)
|
|
|
+ # if not reds.set_data(key=phone + '_forgetPwdResetCode', val=resetCode, expire=300):
|
|
|
+ # return response.json(10, '生成缓存错误')
|
|
|
+ user_qs.update(phone=phone,password=make_password(password))
|
|
|
+ if not reds.del_data(str(phone) + '_OauthPerfect'):
|
|
|
+ return response.json(10, '删除缓存失败')
|
|
|
+ return response.json(0)
|