瀏覽代碼

查看设备密码获取验证码-邮件

locky 5 月之前
父節點
當前提交
c99282d890
共有 2 個文件被更改,包括 73 次插入14 次删除
  1. 30 14
      Controller/UserController.py
  2. 43 0
      Service/TemplateService.py

+ 30 - 14
Controller/UserController.py

@@ -3398,8 +3398,7 @@ class SingleLoginView(TemplateView):
             # 邮箱验证码
             if email is not None:
                 email = email.strip()
-                # 阿里云的发送邮箱的调用方法
-                return self.get_aliyun_email_code(email, response)
+                return self.get_aliyun_email_code(email, code_type, response)
             # 手机验证码
             else:
                 phone = phone.strip()
@@ -3411,36 +3410,53 @@ class SingleLoginView(TemplateView):
             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, response):
+        """
+        获取邮箱验证码
+        @param email: 邮箱
+        @param code_type:  不传: 单点登录, 2: 查看设备密码
+        @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 = email + '_SingleLogin'
+            email_type = 'register_code'
+        elif code_type == '2':
+            redis_key = email + '_GetDevicePassword'
+            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)
 

+ 43 - 0
Service/TemplateService.py

@@ -130,4 +130,47 @@ class TemplateService:
 """
                 }
             }
+        elif type == 'get_device_password':
+                data = {
+                    'cn': {
+                        'title': '查看设备密码',
+                        'body': """
+                            <!DOCTYPE html>
+        <html lang="en">
+        <head>
+            <meta charset="UTF-8">
+            <title>Title</title>
+        </head>
+        <body>
+        <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;">您好,{username}</h2>
+            <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>
+        </html>
+                            """,
+                    },
+                    'en': {
+                        'title': 'View Device Password',
+                        'body': """
+                            <!DOCTYPE html>
+        <html lang="en">
+        <head>
+            <meta charset="UTF-8">
+            <title>Title</title>
+        </head>
+        <body>
+        <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 view device password. 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>
+        </html>
+        """
+                    }
+                }
         return data[language]