def check_sms(cls, data): ''' check sms code with mobile and code ''' mobile = data.get('mobile', None) if not mobile: raise ValidationError({'mobile': ['This field is required.']}) code = data.get('code', None) if not code: raise ValidationError({'code': ['This field is required.']}) res = redis_conn.get(cls.gen_sms_code_key(mobile)) if res: send_code = res if send_code and code == send_code: sms_token = "".join( random.choice(string.ascii_letters + string.digits) for _ in range(24)) redis_conn.set(cls.gen_sms_token_key(sms_token), mobile, ex=settings.SMS_LIFESPAN.seconds * 10) redis_conn.delete(cls.gen_sms_code_key(mobile)) return sms_token, int( time.time()) + settings.SMS_LIFESPAN.seconds * 10 raise ValidationError({'code': ['invalid']})
def create(self, validated_data): ''' send sms ''' code = gen_code() mobile = validated_data['mobile'] send_sms(mobile, code, self.get_template_id()) key = self.gen_sms_code_key(mobile) value = code redis_conn.set(key, value, ex=settings.SMS_LIFESPAN.seconds) return '_'
def gen_email(self, *args, **kwargs): ''' 生成重置密码邮件 ''' subject = '[ArkID] 您正在重置ArkID登录密码' email_token = self.gen_email_token() link = settings.BASE_URL + settings.FE_EMAIL_RESET_PWD_URL + f'?email_token={email_token}' key = self.gen_email_token_key(email_token) redis_conn.set(key, self.validated_data['email'], ex=60 * 60 * 24 * 3) content = f'点击以下链接完成验证,3天之内有效:</br><a href="{link}">{link}</a>' html = render_to_string('email/common.html', { 'company': CompanyConfig.get_current().name_cn, 'content': content }) return { 'subject': subject, 'content': html, }