Selaa lähdekoodia

注册邮箱验证码改为5分钟

zhangdongming 1 vuosi sitten
vanhempi
commit
e3ea3a5002
2 muutettua tiedostoa jossa 45 lisäystä ja 34 poistoa
  1. 43 32
      Controller/UserController.py
  2. 2 2
      Service/TemplateService.py

+ 43 - 32
Controller/UserController.py

@@ -848,39 +848,50 @@ class v2authCodeView(TemplateView):
 
     # 阿里云获取邮箱验证码
     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 + '_identifyingCode')
-        # 是否以获取邮箱验证码
-        if identifyingCode:
+        try:
+            print('阿里云开始')
+            dataValid = DataValid()
+            # 邮箱匹配
+            if dataValid.email_validate(email) is False:
+                return response.json(107)
+            reds = RedisObject()
+            code_key = f'ZOSI:BASIC:USER:EMAIL:{email}'
+            code = reds.get_data(key=code_key)
+            # 是否以获取邮箱验证码
+            if code:
+                return response.json(0)
+            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)
+            start_time = time.time()
+            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))
+            )
+            end_time = time.time()
+            LOGGER.info('阿里云邮箱发送验证码执行时间: {:.3f} 秒,email:{}'.format((end_time - start_time), email))
+            if send_res is not True:
+                return response.json(44)
+            # 单个邮箱验证码有效期5分钟
+            if reds.set_data(key=email + '_identifyingCode', val=identifyingCode, expire=300) is not True:
+                return response.json(10, 'error')
+            # 单个邮箱一分钟发送一次验证码
+            reds.set_data(key=code_key, val=identifyingCode, expire=60)
+            return response.json(0)
+        except Exception as e:
+            LOGGER.info('aliyun_emailCode error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
             return response.json(0)
-        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 + '_identifyingCode', val=identifyingCode, expire=60) is not True:
-            return response.json(10, 'error')
-        return response.json(0)
 
     def phoneCode(self, phone, response, sign_name):
         dataValid = DataValid()

+ 2 - 2
Service/TemplateService.py

@@ -31,7 +31,7 @@ class TemplateService:
 <div class="content"
      style="overflow: hidden;padding:30px 10% 70px 10%;margin:0 10%;background-color: #fff;box-shadow:0 4px 20px rgba(0,0,0,0.1);word-break: break-all;">
     <h2 style="margin: 30px 0;">Hello, {username}</h2>
-    <p style="margin-bottom: 40px;">请输入验证码完成注册,有效期1分钟:</p>
+    <p style="margin-bottom: 40px;">请输入验证码完成注册,有效期5分钟:</p>
     <span style="padding: 10px 20px; font-size: 24px;background-color: #EB6F5A;border-radius:4px;color:#fff;">{captcha}</span>
 </div>
 </body>
@@ -51,7 +51,7 @@ class TemplateService:
 <div class="content"
      style="overflow: hidden;padding:30px 10% 70px 10%;margin:0 10%;background-color: #fff;box-shadow:0 4px 20px rgba(0,0,0,0.1);word-break: break-all;">
     <h2 style="margin: 30px 0;">Hello, {username}</h2>
-    <p style="margin-bottom: 40px;">Please input code to reg. only valid in 1 minutes!</p>
+    <p style="margin-bottom: 40px;">Please input code to reg. only valid in 5 minutes!</p>
     <span style="padding: 10px 20px; font-size: 24px;background-color: #EB6F5A;border-radius:4px;color:#fff;">{captcha}</span>
 </div>
 </body>