APNConfigController.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import json
  2. from django.http import QueryDict
  3. from django.views import View
  4. from Ansjer.config import LOGGER
  5. from Model.models import DeviceVersionInfo, CountryAPN, LanguageModel, CountryLanguageModel, CountryModel
  6. from Object.Enums.RedisKeyConstant import RedisKeyConstant
  7. from Object.RedisObject import RedisObject
  8. from Object.ResponseObject import ResponseObject
  9. from Object.TokenObject import TokenObject
  10. from Service.CommonService import CommonService
  11. class APNConfigView(View):
  12. def get(self, request, *args, **kwargs):
  13. request.encoding = 'utf-8'
  14. operation = kwargs.get('operation')
  15. return self.validation(request.GET, request, operation)
  16. def post(self, request, *args, **kwargs):
  17. request.encoding = 'utf-8'
  18. operation = kwargs.get('operation')
  19. return self.validation(request.POST, request, operation)
  20. def validation(self, request_dict, request, operation):
  21. response = ResponseObject('en')
  22. tko = TokenObject(request.META.get('HTTP_AUTHORIZATION'))
  23. if tko.code != 0:
  24. return response.json(tko.code)
  25. response.lang = tko.lang
  26. userID = tko.userID
  27. if operation == 'APNConfigList':
  28. return self.get_apn_config(request_dict, response)
  29. elif operation == 'CountryCodeList':
  30. return self.get_country_code_list(request_dict, response)
  31. else:
  32. return response.json(414)
  33. @staticmethod
  34. def get_apn_config(request_dict, response):
  35. # 从请求字典中获取uid和version
  36. country_code = request_dict.get('countryCode', None)
  37. if not country_code:
  38. return response.json(444)
  39. country_code = country_code.upper()
  40. country_apn_qs = CountryAPN.objects.filter(iso=country_code).values('apn_name', 'apn_address', 'username', 'password',
  41. 'auth_type')
  42. country_apn_list = []
  43. for country_apn in country_apn_qs:
  44. country_apn_list.append({
  45. 'apnName': country_apn['apn_name'] if country_apn['apn_name'] else '',
  46. 'apnAddress': country_apn['apn_address'] if country_apn['apn_address'] else '',
  47. 'username': country_apn['username'] if country_apn['username'] else '',
  48. 'password': country_apn['password'] if country_apn['password'] else '',
  49. 'authType': country_apn['auth_type'] if country_apn['auth_type'] else '',
  50. })
  51. return response.json(0, country_apn_list)
  52. @staticmethod
  53. def get_country_code_list(request_dict, response):
  54. lang = request_dict.get('lang', 'en')
  55. # 获取语言 ID
  56. language = LanguageModel.objects.filter(lang=lang).first()
  57. if not language:
  58. language = LanguageModel.objects.filter(lang='en').first()
  59. # 查询tb_country_language和tb_country,获取国家名称和国家iso2代码
  60. countries = CountryLanguageModel.objects.filter(language=language).select_related('country')
  61. # 返回国家名和对应的 country_code
  62. country_iso_list = [{
  63. 'countryName': country.country_name,
  64. 'countryCode': country.country.country_code
  65. } for country in countries]
  66. return response.json(0, country_iso_list)