Browse Source

验证昵称是否合规

locky 2 years ago
parent
commit
734d7f5d80
5 changed files with 85 additions and 0 deletions
  1. 4 0
      Ansjer/config.py
  2. 10 0
      Controller/TestApi.py
  3. 12 0
      Controller/UserManger.py
  4. 57 0
      Object/ContentSecurityObject.py
  5. 2 0
      Object/ResponseObject.py

+ 4 - 0
Ansjer/config.py

@@ -27,6 +27,10 @@ RESET_REGION_ID_SERIAL_REDIS_LIST = 'reset_region_id_serial_redis_list'
 # 地区id列表
 REGION_ID_LIST = [1, 2, 3, 4, 5]
 
+# 阿里云AccessKey和AccessKeySecret
+ALI_ACCESS_KEY_ID = 'LTAI5t9BXQWTPfBEh2Qu8YNA'
+ALI_ACCESS_KEY_SECRET = '8TcEjHkHGsJaknbHlHzZP4HXQ7GuvT'
+
 # 亚马逊的数据库DynamoDB的密钥
 AWS_DynamoDB_REGION = 'us-west-1'
 AWS_DynamoDB_ACCESS_KEY = 'AKIA2E67UIMD6X23FEBG'

+ 10 - 0
Controller/TestApi.py

@@ -30,6 +30,7 @@ from Model.models import Order_Model, Store_Meal, DeviceLogModel, VodBucketModel
     TestSerialRepetition, TestDeviceFindSerial, UIDCompanySerialModel, CompanySerialModel, LogModel, Device_User
 from Object.AWS.AmazonS3Util import AmazonS3Util
 from Object.AliPayObject import AliPayObject
+from Object.ContentSecurityObject import ContentSecurity
 from Object.IPWeatherObject import IPQuery
 from Object.RedisObject import RedisObject
 from Object.ResponseObject import ResponseObject
@@ -123,6 +124,8 @@ class testView(View):
             return self.get_serial_details(request_dict, response, request)
         elif operation == 'find_device_serial':  # 查找设备序列号接口:306低功耗无Wi-Fi产品
             return self.find_device_serial(request_dict, response)
+        elif operation == 'ali_text_review':  # 阿里云文本审核
+            return self.ali_text_review(request_dict, response)
         # elif operation == 'funboost':  # funboost测试
         #     return self.funboost_test(request_dict, response)
         else:
@@ -905,6 +908,13 @@ class testView(View):
             password = make_password(password)
         return response.json(0)
 
+    @staticmethod
+    def ali_text_review(request_dict, response):
+        service = 'nickname_detection'
+        service_parameters = request_dict.get('service_parameters', None)
+        review_result = ContentSecurity().text_review(service, service_parameters)
+        return response.json(0, review_result)
+
     # @staticmethod
     # @boost("task_queue_name1", qps=0.5, broker_kind=BrokerEnum.REDIS_ACK_ABLE)  # 入参包括20种,运行控制方式非常多,想得到的控制都会有。
     # def task_fun(x, y):

+ 12 - 0
Controller/UserManger.py

@@ -14,6 +14,7 @@ from Ansjer.config import BASE_DIR, ACCESS_KEY_ID, SECRET_ACCESS_KEY, REGION_NAM
 from Ansjer.config import SERVER_DOMAIN
 from Model.models import Role, Device_User, UserOauth2Model, UserExModel, CountryLanguageModel, LanguageModel, App_Info, \
     IcloudUseDetails
+from Object.ContentSecurityObject import ContentSecurity
 from Object.RedisObject import RedisObject
 from Object.ResponseObject import ResponseObject
 from Object.TokenObject import TokenObject
@@ -203,6 +204,17 @@ class perfectUserInfoView(TemplateView):
                 UserData = json.loads(userContent)
             except Exception as e:
                 return response.json(444, repr(e))
+
+            # 验证昵称是否合规
+            nickname = UserData.get('NickName')
+            if nickname:
+                service = 'nickname_detection'
+                service_dict = {'content': nickname}
+                service_parameters = json.dumps(service_dict)
+                legal = ContentSecurity().text_review(service, service_parameters)
+                if not legal:
+                    return response.json(108)
+
         try:
             if userIconPath and userContent:
                 User.update(userIconPath=userIconPath, userIconUrl=userIconUrl, **UserData)

+ 57 - 0
Object/ContentSecurityObject.py

@@ -0,0 +1,57 @@
+# @Author    : Rocky
+# @File      : ContentSecurityObject.py
+# @Time      : 2023/8/18 11:02
+from alibabacloud_green20220302.client import Client as Green20220302Client
+from alibabacloud_tea_openapi import models as open_api_models
+from alibabacloud_green20220302 import models as green_20220302_models
+from alibabacloud_tea_util import models as util_models
+from Ansjer.config import ALI_ACCESS_KEY_ID, ALI_ACCESS_KEY_SECRET
+
+ILLEGAL_LABEL_LIST = ['political_content', 'profanity', 'contraband', 'sexual_content', 'violence', 'negative_content',
+                      'religion', 'cyberbullying']
+
+
+class ContentSecurity:
+    """
+    阿里云内容安全服务类
+    """
+    def __init__(self):
+        pass
+
+    @staticmethod
+    def create_client() -> Green20220302Client:
+        """
+        使用AK&SK初始化账号Client
+        @return: Client
+        @throws Exception
+        """
+        config = open_api_models.Config(
+            # 必填,您的 AccessKey ID,
+            access_key_id=ALI_ACCESS_KEY_ID,
+            # 必填,您的 AccessKey Secret,
+            access_key_secret=ALI_ACCESS_KEY_SECRET
+        )
+        # Endpoint 请参考 https://api.aliyun.com/product/Green
+        config.endpoint = f'green-cip.cn-shanghai.aliyuncs.com'
+        return Green20220302Client(config)
+
+    def text_review(self, service, service_parameters):
+        """
+
+        @param service: 审核服务类型
+        @param service_parameters: 审核服务参数集
+        @return: bool, True: 审核通过, False: 审核不通过
+        """
+        client = self.create_client()
+        text_moderation_request = green_20220302_models.TextModerationRequest(service, service_parameters)
+        runtime = util_models.RuntimeOptions()
+        res = client.text_moderation_with_options(text_moderation_request, runtime)
+        print(res)
+        if res.status_code != 200:
+            return False
+        labels = res.body.data.labels
+        labels_list = labels.split(',')
+        for label in labels_list:
+            if label in ILLEGAL_LABEL_LIST:
+                return False
+            return True

+ 2 - 0
Object/ResponseObject.py

@@ -34,6 +34,7 @@ class ResponseObject(object):
             104: 'Account doesn\'t exist!',
             105: 'Email format error!',
             107: 'The username not conform to the rules!',
+            108: 'Illegal nickname!',
             109: 'The password not conform to the rules!',
             110: 'user doesn\'t activated',
             111: 'Error password',
@@ -153,6 +154,7 @@ class ResponseObject(object):
             104: '账户不存在!',
             105: '邮箱格式错误!',
             107: '用户名格式不符合!',
+            108: '用户昵称违规!',
             109: '密码格式不符合!',
             110: '用户未激活!',
             111: '密码不正确!',