|
@@ -3366,93 +3366,144 @@ class SingleLoginView(TemplateView):
|
|
|
phone = request_dict.get('phone', None)
|
|
|
sign_name = request_dict.get('sign_name', 'zosi')
|
|
|
country_code = request_dict.get('country_code', None)
|
|
|
- # code_type = 1 表示修改绑定手机号
|
|
|
code_type = request_dict.get('code_type', None)
|
|
|
- # 短信签名
|
|
|
- sign_name = CommonService.confirm_msg_sign_name(sign_name)
|
|
|
- if email is not None:
|
|
|
- email = email.strip()
|
|
|
- # 阿里云的发送邮箱的调用方法
|
|
|
- return self.get_aliyun_email_code(email, response)
|
|
|
- elif phone is not None and code_type == "1":
|
|
|
- phone = phone.strip()
|
|
|
- country_code = str(country_code.strip())
|
|
|
- return self.get_change_phone_code(country_code, phone, response, sign_name)
|
|
|
- elif phone is not None:
|
|
|
- phone = phone.strip()
|
|
|
- country_code = str(country_code.strip())
|
|
|
- return self.get_phone_code(country_code, phone, response, sign_name)
|
|
|
- else:
|
|
|
+ uid = request_dict.get('uid', None)
|
|
|
+ if not any([email, phone]):
|
|
|
return response.json(444)
|
|
|
|
|
|
+ try:
|
|
|
+ # 查看设备密码,传uid和检验主用户
|
|
|
+ if code_type == '2':
|
|
|
+ if uid is None:
|
|
|
+ return response.json(444)
|
|
|
+ user_info = email if email is not None else phone
|
|
|
+ # 查询主用户信息
|
|
|
+ device_info_qs = Device_Info.objects.filter(UID=uid).values('vodPrimaryMaster')
|
|
|
+ if not device_info_qs.exists():
|
|
|
+ return response.json(173)
|
|
|
+ if user_info != device_info_qs[0]['vodPrimaryMaster']:
|
|
|
+ return response.json(12)
|
|
|
+ # 邮箱验证码
|
|
|
+ if email is not None:
|
|
|
+ email = email.strip()
|
|
|
+ return self.get_aliyun_email_code(email, code_type, uid, response)
|
|
|
+ # 手机验证码
|
|
|
+ else:
|
|
|
+ phone = phone.strip()
|
|
|
+ country_code = str(country_code.strip())
|
|
|
+ # 短信签名
|
|
|
+ sign_name = CommonService.confirm_msg_sign_name(sign_name)
|
|
|
+ return self.get_phone_code(country_code, phone, sign_name, code_type, uid, response)
|
|
|
+ except Exception as e:
|
|
|
+ return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
+
|
|
|
@staticmethod
|
|
|
- def get_aliyun_email_code(email, response):
|
|
|
- print('阿里云开始')
|
|
|
+ def get_aliyun_email_code(email, code_type, uid, response):
|
|
|
+ """
|
|
|
+ 获取邮箱验证码
|
|
|
+ @param email: 邮箱
|
|
|
+ @param code_type: 不传: 单点登录, 2: 查看设备密码
|
|
|
+ @param uid:
|
|
|
+ @param response:
|
|
|
+ @return:
|
|
|
+ """
|
|
|
data_valid = DataValid()
|
|
|
# 邮箱匹配
|
|
|
if data_valid.email_validate(email) is False:
|
|
|
return response.json(107)
|
|
|
- reds = RedisObject()
|
|
|
- key = email + '_SingleLogin'
|
|
|
- identifying_code = reds.get_data(key=key)
|
|
|
- # 是否以获取邮箱验证码
|
|
|
- if identifying_code:
|
|
|
- return response.json(89)
|
|
|
+ # 查询用户是否存在
|
|
|
user_qs = Device_User.objects.filter(Q(username=email) | Q(userEmail=email))
|
|
|
- # 邮箱用户是否已存在
|
|
|
if not user_qs.exists():
|
|
|
return response.json(99)
|
|
|
+
|
|
|
+ # 确定缓存key
|
|
|
+ if code_type is None:
|
|
|
+ redis_key = '{}_SingleLogin'.format(email)
|
|
|
+ email_type = 'register_code'
|
|
|
+ elif code_type == '2':
|
|
|
+ redis_key = '{}_{}_GetDevicePassword'.format(email, uid)
|
|
|
+ email_type = 'get_device_password'
|
|
|
+ else:
|
|
|
+ return response.json(444)
|
|
|
+
|
|
|
+ redis_obj = RedisObject()
|
|
|
+ # 检查是否已获取邮箱验证码
|
|
|
+ identifying_code = redis_obj.get_data(key=redis_key)
|
|
|
+ if identifying_code:
|
|
|
+ return response.json(89)
|
|
|
+
|
|
|
# 生成随机6位数
|
|
|
identifying_code = RandomStr(6, True)
|
|
|
# 设置随机数缓存生命周期
|
|
|
- send_data = TemplateService.email_message(type='register_code', language=response.lang)
|
|
|
+ send_data = TemplateService.email_message(type=email_type, 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(identifying_code))
|
|
|
+ body=send_data['body'].replace('{username}', email).replace('{captcha}', str(identifying_code))
|
|
|
)
|
|
|
if send_res is not True:
|
|
|
return response.json(44)
|
|
|
- if reds.set_data(key=key, val=identifying_code, expire=600) is not True:
|
|
|
+ if redis_obj.set_data(key=redis_key, val=identifying_code, expire=600) is not True:
|
|
|
return response.json(10, 'error')
|
|
|
return response.json(0)
|
|
|
|
|
|
@staticmethod
|
|
|
- def get_phone_code(country_code, phone, response, sign_name):
|
|
|
+ def get_phone_code(country_code, phone, sign_name, code_type, uid, response):
|
|
|
+ """
|
|
|
+ 获取手机验证码
|
|
|
+ @param country_code: 国家编码
|
|
|
+ @param phone: 手机号码
|
|
|
+ @param sign_name: 短信签名
|
|
|
+ @param code_type: 验证码类型, None: 单点登录, 1: 修改手机号码, 2: 查看设备密码
|
|
|
+ @param uid:
|
|
|
+ @param response:
|
|
|
+ @return:
|
|
|
+ """
|
|
|
data_valid = DataValid()
|
|
|
if data_valid.mobile_validate(phone) is not True:
|
|
|
return response.json(107)
|
|
|
- reds = RedisObject()
|
|
|
- reds_key = str(phone) + '_SingleLogin'
|
|
|
- identifying_code = reds.get_data(key=reds_key)
|
|
|
- reds_key_ttl = reds.get_ttl(key=reds_key)
|
|
|
- if reds_key_ttl > 240 and identifying_code:
|
|
|
- return response.json(90)
|
|
|
- user_qs = Device_User.objects.filter(Q(username=phone) | Q(phone=phone))
|
|
|
- if not user_qs.exists():
|
|
|
- return response.json(102)
|
|
|
- identifying_code = RandomStr(6, True)
|
|
|
+ # 根据验证码类型确定缓存key和短信模板
|
|
|
+ if code_type is None:
|
|
|
+ user_qs = Device_User.objects.filter(Q(username=phone) | Q(phone=phone))
|
|
|
+ if not user_qs.exists():
|
|
|
+ return response.json(102)
|
|
|
+ redis_key = '{}_SingleLogin'.format(phone)
|
|
|
+ # 短信模板
|
|
|
+ temp_msg = 'SMS_151675019' if country_code == '86' else 'SMS_172200051'
|
|
|
+ elif code_type == '1':
|
|
|
+ redis_key = '{}_ChangePhone'.format(phone)
|
|
|
+ temp_msg = 'SMS_151675018' if country_code == '86' else 'SMS_172165867'
|
|
|
+ elif code_type == '2':
|
|
|
+ redis_key = '{}_{}_GetDevicePassword'.format(phone, uid)
|
|
|
+ temp_msg = 'SMS_479855154' if country_code == '86' else 'SMS_479785146'
|
|
|
+ else:
|
|
|
+ return response.json(444)
|
|
|
+
|
|
|
+ # 根据手机区号处理发送手机号码和签名
|
|
|
if country_code == '86':
|
|
|
- # 国内短信推送模板
|
|
|
- temp_msg = 'SMS_151675019'
|
|
|
rec_phone = phone
|
|
|
else:
|
|
|
- # 国际短信推送模板
|
|
|
- temp_msg = 'SMS_172200051'
|
|
|
rec_phone = country_code + phone
|
|
|
sign_name = 'Ansjer'
|
|
|
|
|
|
+ redis_obj = RedisObject()
|
|
|
+ identifying_code = redis_obj.get_data(key=redis_key)
|
|
|
+ reds_key_ttl = redis_obj.get_ttl(key=redis_key)
|
|
|
+ if reds_key_ttl > 240 and identifying_code:
|
|
|
+ return response.json(90)
|
|
|
+ identifying_code = RandomStr(6, True)
|
|
|
+
|
|
|
# 发送手机验证码
|
|
|
alisms = AliSmsObject()
|
|
|
res = alisms.send_code_sms(phone=rec_phone, code=identifying_code, sign_name=sign_name, temp_msg=temp_msg)
|
|
|
- if res["Code"] == "OK":
|
|
|
- if reds.set_data(key=reds_key, val=identifying_code, expire=300) is not True:
|
|
|
+ if res['Code'] == 'OK':
|
|
|
+ if redis_obj.set_data(key=redis_key, val=identifying_code, expire=300) is not True:
|
|
|
return response.json(10, '生成缓存系统错误')
|
|
|
return response.json(0)
|
|
|
else:
|
|
|
- return response.json(10, res["Message"])
|
|
|
+ return response.json(10, res['Message'])
|
|
|
|
|
|
def change_password(self, request_dict, response):
|
|
|
phone = request_dict.get('phone', None)
|