Browse Source

校验验证码接口

zhangdongming 2 months ago
parent
commit
973a2a2354
2 changed files with 69 additions and 0 deletions
  1. 2 0
      Ansjer/urls.py
  2. 67 0
      Controller/UserController.py

+ 2 - 0
Ansjer/urls.py

@@ -298,6 +298,8 @@ urlpatterns = [
     re_path('APNConfig/(?P<operation>.*)', APNConfigController.APNConfigView.as_view()),
     re_path('APNConfig/(?P<operation>.*)', APNConfigController.APNConfigView.as_view()),
     re_path(r'^api/device/custom/(?P<operation>.*)$', DeviceCustomUIDController.DeviceCustomUIDView.as_view()),
     re_path(r'^api/device/custom/(?P<operation>.*)$', DeviceCustomUIDController.DeviceCustomUIDView.as_view()),
     re_path(r'^productInformation/(?P<operation>.*)$', IncomeProductsController.IncomeProductsView.as_view()),
     re_path(r'^productInformation/(?P<operation>.*)$', IncomeProductsController.IncomeProductsView.as_view()),
+    # 校验验证码
+    re_path(r'^api/open/authCode/(?P<operation>.*)$', UserController.VerifyCodeView.as_view()),
 
 
     # 后台界面接口 -------------------------------------------------------------------------------------------------------
     # 后台界面接口 -------------------------------------------------------------------------------------------------------
     # 登录,用户信息,权限
     # 登录,用户信息,权限

+ 67 - 0
Controller/UserController.py

@@ -5228,3 +5228,70 @@ class ChangeAccountInfoView(View):
         else:
         else:
             ord_user_qs.update(phone=new_phone)
             ord_user_qs.update(phone=new_phone)
         return response.json(0)
         return response.json(0)
+
+
+class VerifyCodeView(View):
+    def get(self, request, *args, **kwargs):
+        request.encoding = 'utf-8'
+        operation = kwargs.get('operation')
+        request_dict = request.GET
+        return self.validation(request_dict, request, operation)
+
+    def post(self, request, *args, **kwargs):
+        request.encoding = 'utf-8'
+        operation = kwargs.get('operation')
+        request_dict = request.POST
+        return self.validation(request_dict, request, operation)
+
+    def validation(self, request_dict, request, operation):
+        language = request_dict.get('lang', 'en')
+        response = ResponseObject(language)
+        if operation == 'verifyCode':
+            return self.verify_code(request_dict, response)
+        else:
+            return response.json(444)
+
+    @staticmethod
+    def verify_code(request_dict, response):
+        """
+        验证邮箱验证码
+        @param username: 账户名
+        @param code: 用户输入的验证码
+        @param response: 响应对象
+        @return: 验证结果
+        """
+        try:
+            username = request_dict.get('username')
+            code = request_dict.get('code')
+
+            # 基础参数验证
+            if not username or not code:
+                return response.json(444)
+
+            # 生成缓存Key
+            redis_key = f'{username}_GetBackendVerificationCode'
+            reds = RedisObject()
+
+            # 获取并验证验证码
+            cached_code = reds.get_data(key=redis_key)
+            if cached_code is False:
+                return response.json(92)
+
+            # 验证失败次数控制
+            if cached_code != code:
+                failure_key = f"{redis_key}_failures"
+                failures_raw = reds.get_data(key=failure_key)
+                failures = int(failures_raw or 0) + 1
+                reds.set_data(key=failure_key, val=failures, expire=60)
+
+                if failures >= 5:
+                    return response.json(5)
+
+                return response.json(121)
+
+            # 验证成功,清理缓存
+            reds.del_data(redis_key)
+            reds.del_data(f"{redis_key}_failures")
+            return response.json(0)
+        except Exception as e:
+            return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))