|
@@ -58,8 +58,8 @@ class ConfirmRegion(TemplateView):
|
|
|
'push_api_url': 'https://push.dvema.com'})
|
|
|
|
|
|
|
|
|
-# 根据设备的ip返回域名和地区id
|
|
|
class ConfirmRegionV2(TemplateView):
|
|
|
+ # 设备根据ip获取域名V2接口
|
|
|
@method_decorator(csrf_exempt)
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
return super(ConfirmRegionV2, self).dispatch(*args, **kwargs)
|
|
@@ -73,46 +73,68 @@ class ConfirmRegionV2(TemplateView):
|
|
|
data_dict = {'serial_number': serial_number}
|
|
|
device_domain_region_qs = DeviceDomainRegionModel.objects.filter(serial_number=serial_number)
|
|
|
|
|
|
- # 根据请求ip确认地区
|
|
|
+ # 获取ip
|
|
|
request.encoding = 'utf-8'
|
|
|
ip = CommonService.get_ip_address(request)
|
|
|
data_dict['ip'] = ip
|
|
|
- ipInfo = CommonService.getIpIpInfo(ip, 'CN')
|
|
|
- country_code = ipInfo['country_code']
|
|
|
+
|
|
|
+ # 获取国家编码
|
|
|
+ ip_addr_qs = IPAddr.objects.filter(ip=ip).values('country_code')
|
|
|
+ if ip_addr_qs.exists():
|
|
|
+ country_code = ip_addr_qs[0]['country_code']
|
|
|
+ else:
|
|
|
+ ip_qs = IPQuery(ip)
|
|
|
+ country_code = ip_qs.country_id
|
|
|
|
|
|
if country_code:
|
|
|
- data_dict['country_name'] = ipInfo['country_name']
|
|
|
+ data_dict['country_code'] = country_code
|
|
|
country_qs = CountryModel.objects.filter(country_code=country_code).\
|
|
|
- values('region__api', 'region__id')
|
|
|
+ values('region__api', 'region__push_api', 'region__id')
|
|
|
if country_qs.exists():
|
|
|
api = country_qs[0]['region__api']
|
|
|
+ push_api = country_qs[0]['region__push_api']
|
|
|
region_id = country_qs[0]['region__id']
|
|
|
- else: # 默认返回美洲地区api
|
|
|
- api, region_id = self.get_default_api()
|
|
|
+ else:
|
|
|
+ # 返回美洲域名
|
|
|
+ data_dict['country_code'] = 'NA'
|
|
|
+ api, push_api, region_id = self.get_default_api()
|
|
|
else:
|
|
|
- # 默认返回美洲地区api
|
|
|
- api, region_id = self.get_default_api()
|
|
|
+ # 返回美洲域名
|
|
|
+ data_dict['country_code'] = 'NA'
|
|
|
+ api, push_api, region_id = self.get_default_api()
|
|
|
|
|
|
# 更新或创建设备域名数据
|
|
|
- data_dict['api'] = api
|
|
|
data_dict['region_id'] = region_id
|
|
|
if device_domain_region_qs.exists():
|
|
|
device_domain_region_qs.update(**data_dict)
|
|
|
else:
|
|
|
device_domain_region_qs.create(**data_dict)
|
|
|
- res = {'request_api_url': api, 'region_id': region_id}
|
|
|
+
|
|
|
+ # 响应数据
|
|
|
+ res = {
|
|
|
+ 'request_api_url': api,
|
|
|
+ 'push_api_url': push_api,
|
|
|
+ 'region_id': region_id
|
|
|
+ }
|
|
|
return response.json(0, res)
|
|
|
except Exception as e:
|
|
|
- print(e)
|
|
|
- return response.json(500, 'error_line:{}, error_msg:{}'.format(e.__traceback__.tb_lineno, repr(e)))
|
|
|
+ print(repr(e))
|
|
|
+ api, push_api, region_id = self.get_default_api()
|
|
|
+ res = {
|
|
|
+ 'request_api_url': api,
|
|
|
+ 'push_api_url': push_api,
|
|
|
+ 'region_id': region_id
|
|
|
+ }
|
|
|
+ return response.json(0, res)
|
|
|
|
|
|
# 获取区域表美洲的相关数据
|
|
|
@staticmethod
|
|
|
def get_default_api():
|
|
|
- region_qs = RegionModel.objects.filter(continent_code='NA').values('api', 'id')
|
|
|
+ region_qs = RegionModel.objects.filter(continent_code='NA').values('api', 'push_api', 'id')
|
|
|
api = region_qs[0]['api']
|
|
|
+ push_api = region_qs[0]['push_api']
|
|
|
region_id = region_qs[0]['id']
|
|
|
- return api, region_id
|
|
|
+ return api, push_api, region_id
|
|
|
|
|
|
|
|
|
class Device_Region(object):
|