瀏覽代碼

完善保存序列号地区信息接口,优化响应对象

locky 2 年之前
父節點
當前提交
fc666905d5
共有 3 個文件被更改,包括 80 次插入58 次删除
  1. 28 10
      Controller/SerialNumberController.py
  2. 23 23
      Object/ResponseObject.py
  3. 29 25
      Object/uidManageResponseObject.py

+ 28 - 10
Controller/SerialNumberController.py

@@ -2,11 +2,12 @@ import json
 import logging
 import time
 
+import requests
 from django.db import transaction
 from django.views import View
 
 from Ansjer.config import CRCKey, CONFIG_INFO, CONFIG_TEST, CONFIG_US, \
-    CONFIG_CN, USED_SERIAL_REDIS_LIST, UNUSED_SERIAL_REDIS_LIST
+    CONFIG_CN, USED_SERIAL_REDIS_LIST, UNUSED_SERIAL_REDIS_LIST, SERVER_DOMAIN_US
 from Model.models import SerialNumberModel, CompanySerialModel, UIDCompanySerialModel, UIDModel, Device_Info, \
     iotdeviceInfoModel, LogModel, UidSetModel, UID_Bucket, \
     Unused_Uid_Meal, Order_Model, StsCrdModel, VodHlsModel, ExperienceContextModel, UidUserModel, ExperienceAiModel, \
@@ -378,17 +379,34 @@ class SerialNumberView(View):
 
         try:
             serial_number = serial_number[:9]
-            region_data = {
-                'ip': CommonService.get_ip_address(request),
-                'region_id': CommonService.confirm_region_id()
-            }
 
-            device_domain_region_qs = DeviceDomainRegionModel.objects.filter(serial_number=serial_number)
-            if device_domain_region_qs.exists():
-                device_domain_region_qs.update(region_data)
+            # 不是美洲服,请求美洲域名保存数据
+            if CONFIG_INFO != CONFIG_US:
+                url = SERVER_DOMAIN_US + 'serialNumber/saveRegion'
+                data = request_dict.dict()
+                data['ip'] = CommonService.get_ip_address(request)
+                data['region_id'] = CommonService.confirm_region_id()
+                try:
+                    r = requests.post(url=url, data=data, timeout=3)
+                    assert r.status_code == 200
+                    res = r.json()
+                    assert res['result_code'] == 0
+                except (TimeoutError, AssertionError):
+                    return response.json(5)
             else:
-                region_data['serial_number'] = serial_number
-                DeviceDomainRegionModel.objects.create(serial_number=serial_number)
+                data = {}
+                # 处理其他服务器发起请求的情况
+                region_id = request_dict.get('region_id', None)
+                if region_id is not None:
+                    data['ip'] = request_dict.get('ip', '')
+                    data['region_id'] = int(region_id)
+                # 更新或写入数据
+                device_domain_region_qs = DeviceDomainRegionModel.objects.filter(serial_number=serial_number)
+                if device_domain_region_qs.exists():
+                    device_domain_region_qs.update(**data)
+                else:
+                    data['serial_number'] = serial_number
+                    DeviceDomainRegionModel.objects.create(**data)
             return response.json(0)
         except Exception as e:
             return response.json(500, repr(e))

+ 23 - 23
Object/ResponseObject.py

@@ -7,10 +7,13 @@ class ResponseObject(object):
         self.lang = lang
         self.returntype = returntype
 
-    def data(self, code, res={}):
+    def data(self, code, res=None):
+        if res is None:
+            res = {}
+
         data_en = {
             0: 'Success',
-            5: 'Please try again one minute later!',
+            5: 'Please try again one minute later!',
             10: res,
             12: 'You are not the primary user of the device!',
             13: 'Timestamp token verification failed',
@@ -23,12 +26,12 @@ class ResponseObject(object):
             89: 'Already send the code, please check it or get it again after 10m',
             90: 'please check code or get it again after 1m',
             99: 'Mail doesn\'t exist!',
-            100: 'Phone format error',
+            100: 'Phone format error!',
             101: 'Phone already existed!',
             102: 'Phone doesn\'t exist!',
             103: 'Mail already existed!',
             104: 'Account doesn\'t exist!',
-            105: 'Email format error',
+            105: 'Email format error!',
             107: 'The username not conform to the rules!',
             109: 'The password not conform to the rules!',
             110: 'user doesn\'t activated',
@@ -52,7 +55,7 @@ class ResponseObject(object):
             414: 'Please confirm the request url!',
             424: 'Database Error !',
             444: 'Wrong parameters!',
-            474: 'System Maintaining',
+            474: 'System Maintaining!',
             475: 'App Version too low, please upgrade!',
             500: 'Internal error!',
             700: 'Upload file error',
@@ -228,28 +231,25 @@ class ResponseObject(object):
             10057: '不能删除',
             10058: '默认家庭不能删除'
         }
-        if self.lang == 'cn':
-            msg = data_cn
-        elif self.lang == 'zh-Hans':
-            msg = data_cn
-        elif self.lang == 'zh-Hant':
-            msg = data_cn
-        else:
-            msg = data_en
-        try:
-            message = msg[code]
-        except Exception as e:
-            message = '系统错误,code不存在'
-        print(self.lang == 'cn')
-        print(msg)
+
+        msg = data_cn if self.lang == 'cn' or self.lang == 'zh-Hans' or self.lang == 'zh-Hant' else data_en
+
+        reason = msg.get(code)
+        if reason is None:
+            reason = 'code不存在'
+
         if self.returntype == 'pc':
-            return {'code': code, 'msg': message, 'data': res, 'error_code': code}
-        return {'result_code': code, 'reason': message, 'result': res, 'error_code': code}
+            return {'code': code, 'msg': reason, 'data': res, 'error_code': code}
+        return {'result_code': code, 'reason': reason, 'result': res, 'error_code': code}
 
-    def formal(self, code, res={}):
+    def formal(self, code, res=None):
+        if res is None:
+            res = {}
         formal_data = self.data(code, res)
         return json.dumps(formal_data, ensure_ascii=False)
 
-    def json(self, code, res={}):
+    def json(self, code, res=None):
+        if res is None:
+            res = {}
         result = self.formal(code, res)
         return HttpResponse(result)

+ 29 - 25
Object/uidManageResponseObject.py

@@ -7,7 +7,10 @@ class uidManageResponseObject(object):
     def __init__(self, lang='cn'):
         self.lang = lang
 
-    def data(self, code, res={}):
+    def data(self, code, res=None):
+        if res is None:
+            res = {}
+
         data_cn = {
             0: '成功',
             5: '请一分钟后再尝试',
@@ -18,10 +21,10 @@ class uidManageResponseObject(object):
             42: '两次输入的新密码错误',
             43: '客户端服务器已关闭,请下载新版本使用',
             44: '系统错误,发送邮件失败',
-            45: '系统错误,生成令牌出错!',
-            46: '系统错误,发送短信失败!',
+            45: '系统错误,生成令牌出错',
+            46: '系统错误,发送短信失败',
             47: '旧密码不正确',
-            74: '关联旧用户失败!',
+            74: '关联旧用户失败',
             79: '您已经申请过重置密码,请到邮箱进行确认!',
             89: '您已经获得了验证码,请在10分钟后检查或再次确认。',
             99: '账户或密码错误',
@@ -41,7 +44,7 @@ class uidManageResponseObject(object):
             175: 'mac地址已用完',
             176: '数据库异常',
             305: '令牌格式是错误的,相关参数是不存在的!',
-            307: '令牌已过期!',
+            307: '令牌已过期',
             308: '此次下载已失效',
             309: '你没有权限访问',
             373: '没有相应的公司',
@@ -51,7 +54,7 @@ class uidManageResponseObject(object):
             377: 'uid已使用',
             378: '更新序列号状态失败',
             379: '序列号不存在',
-            404: '没有访问权限!',
+            404: '没有访问权限',
             414: '请确认请求url!',
             444: '请确认参数的正确性!',
             500: '内部错误!',
@@ -61,9 +64,10 @@ class uidManageResponseObject(object):
             10042: '序列号已被占用',
             10043: '无法解绑,序列号的状态为被占用',
         }
+
         data_en = {
             0: 'Success',
-            5: 'Please try again one minute later',
+            5: 'Please try again one minute later!',
             8: 'User accounts already exist',
             9: 'User accounts is not exist',
             10: res,
@@ -71,10 +75,10 @@ class uidManageResponseObject(object):
             42: 'The new password entered twice is incorrect',
             43: 'The client server is closed. Please download the new version for use',
             44: 'System error,send email fail!',
-            45: 'System error,generate token fail',
-            46: 'System error, sending SMS failed',
+            45: 'System error,generate token fail!',
+            46: 'System error, sending SMS failed!',
             47: 'Old password is incorrect',
-            74: 'Failed to connect old users',
+            74: 'Failed to connect old users!',
             79: 'You have applied for reset password, please go to email for confirmation!',
             89: 'You have already obtained the verification code, please check it or get it again after 10 minutes.',
             99: ' ERROR Incorrect account or password',
@@ -85,7 +89,7 @@ class uidManageResponseObject(object):
             107: 'The username format does not conform to the rules!',
             108: 'The mailbox format does not conform to the rules! ',
             110: 'Because the user is not activated, the user is an invalid user!',
-            111: 'The password you entered is incorrect',
+            111: 'The password you entered is incorrect!',
             120: 'The captcha has expired or does not exist, please obtain the captcha again!',
             121: 'The verification code is wrong!',
             138: 'The phone format does not conform to the rules! ',
@@ -108,26 +112,26 @@ class uidManageResponseObject(object):
             414: 'Please confirm the request url!',
             444: 'Please confirm the correctness of the parameters!',
             500: 'Internal error!',
-            1112: 'The two passwords you entered do not match',
+            1112: 'The two passwords you entered do not match!',
             10041: 'The remaining number of serial numbers that can be bound has exceeded',
             10042: 'Serial number is already occupied',
             10043: 'Unable to unbind, the status of the serial number is occupied',
         }
 
-        if self.lang == 'cn':
-            msg = data_cn
-        else:
-            msg = data_en
-        try:
-            message = msg[code]
-        except Exception as e:
-            message = '系统错误,code不存在'
-        return {'result_code': code, 'reason': message, 'result': res, 'error_code': code}
+        msg = data_cn if self.lang == 'cn' else data_en
+        reason = msg.get(code)
+        if reason is None:
+            reason = 'code不存在'
+        return {'result_code': code, 'reason': reason, 'result': res, 'error_code': code}
 
-    def formal(self, code, res={}):
+    def formal(self, code, res=None):
+        if res is None:
+            res = {}
         formal_data = self.data(code, res)
-        return json.dumps(formal_data,ensure_ascii=False)
+        return json.dumps(formal_data, ensure_ascii=False)
 
-    def json(self, code, res={}):
+    def json(self, code, res=None):
+        if res is None:
+            res = {}
         result = self.formal(code, res)
-        return HttpResponse(result)
+        return HttpResponse(result)