|
@@ -7,6 +7,7 @@ import time
|
|
|
import traceback
|
|
|
|
|
|
from django.core import serializers
|
|
|
+from django.db import models
|
|
|
from django.http import HttpResponse
|
|
|
from django.utils.decorators import method_decorator
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
@@ -14,12 +15,13 @@ from django.views.generic.base import View
|
|
|
|
|
|
import Ansjer
|
|
|
from Ansjer.config import BASE_DIR, SERVER_TYPE, SERVER_DOMAIN, SERVER_DOMAIN_SSL
|
|
|
-from Model.models import FAQModel
|
|
|
+from Model.models import FAQModel, HelpLink
|
|
|
+from Object.Enums.RedisKeyConstant import RedisKeyConstant
|
|
|
from Object.RedisObject import RedisObject
|
|
|
from Object.ResponseObject import ResponseObject
|
|
|
from Object.TokenObject import TokenObject
|
|
|
from var_dump import var_dump
|
|
|
-
|
|
|
+from Ansjer.config import LOGGER
|
|
|
from Service.CommonService import CommonService
|
|
|
from Service.ModelService import ModelService, ZositechHelpModel
|
|
|
|
|
@@ -389,3 +391,84 @@ class FAQView(View):
|
|
|
'content': json.dumps(data).replace("\'", "\"").replace("XX??????XX", "\'")
|
|
|
})
|
|
|
return response.json(0)
|
|
|
+
|
|
|
+
|
|
|
+class HelpLinkView(View):
|
|
|
+ def get(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ operation = kwargs.get('operation')
|
|
|
+ return self.validation(request.GET, request, operation)
|
|
|
+
|
|
|
+ def post(self, request, *args, **kwargs):
|
|
|
+ request.encoding = 'utf-8'
|
|
|
+ operation = kwargs.get('operation')
|
|
|
+ return self.validation(request.POST, request, operation)
|
|
|
+
|
|
|
+ def validation(self, request_dict, request, operation):
|
|
|
+ response = ResponseObject('en')
|
|
|
+ tko = TokenObject(request.META.get('HTTP_AUTHORIZATION'))
|
|
|
+ if tko.code != 0:
|
|
|
+ return response.json(tko.code)
|
|
|
+ response.lang = tko.lang
|
|
|
+ userID = tko.userID
|
|
|
+ if operation == 'queryFAQByDeviceType': # 获取电池电量列表
|
|
|
+ return HelpLinkView.query_faq_by_device_type(request, request_dict, response)
|
|
|
+ else:
|
|
|
+ return response.json(414)
|
|
|
+
|
|
|
+ @staticmethod
|
|
|
+ def query_faq_by_device_type(request, request_dict, response):
|
|
|
+ """根据设备类型和语言查询帮助链接"""
|
|
|
+ device_type = request_dict.get('device_type', None)
|
|
|
+ # lang = request_dict.get('lang', 'en')
|
|
|
+ # app_bundle_id = request_dict.get('app_bundle_id', None)
|
|
|
+ lang = 'en'
|
|
|
+
|
|
|
+ if not device_type:
|
|
|
+ return response.json(444, {'message': 'device_type和lang参数不能为空'})
|
|
|
+
|
|
|
+ try:
|
|
|
+ device_type = int(device_type)
|
|
|
+ redis = RedisObject()
|
|
|
+ cache_key = RedisKeyConstant.HELP_LINK_TYPE.value + f'{device_type}:{lang}'
|
|
|
+
|
|
|
+ # 先尝试从缓存获取
|
|
|
+ cached_data = redis.get_data(cache_key)
|
|
|
+ if cached_data:
|
|
|
+ cached_data = json.loads(cached_data)
|
|
|
+ return response.json(0, cached_data)
|
|
|
+
|
|
|
+ # 优化数据库查询 - 单次查询获取结果
|
|
|
+ help_link = HelpLink.objects.filter(
|
|
|
+ models.Q(device_type=device_type) | models.Q(device_type=-1),
|
|
|
+ lang=lang,
|
|
|
+ is_active=True
|
|
|
+ ).order_by(
|
|
|
+ models.Case(
|
|
|
+ models.When(device_type=device_type, then=0),
|
|
|
+ default=1
|
|
|
+ )
|
|
|
+ ).first()
|
|
|
+
|
|
|
+ if not help_link:
|
|
|
+ return response.json(173)
|
|
|
+
|
|
|
+ # 构建返回数据
|
|
|
+ data = {
|
|
|
+ 'url': help_link.url,
|
|
|
+ 'title': help_link.title,
|
|
|
+ 'description': help_link.description
|
|
|
+ }
|
|
|
+
|
|
|
+ # 设置缓存,过期时间30天
|
|
|
+ try:
|
|
|
+ redis.set_data(cache_key, json.dumps(data), RedisKeyConstant.EXPIRE_TIME_30_DAYS.value)
|
|
|
+ except Exception as e:
|
|
|
+ LOGGER.error(f"设置Redis缓存出错: {repr(e)}")
|
|
|
+ # 缓存失败不影响主流程
|
|
|
+
|
|
|
+ return response.json(0, data)
|
|
|
+
|
|
|
+ except Exception as e:
|
|
|
+ LOGGER.error(f"查询帮助链接出错: {repr(e)}")
|
|
|
+ return response.json(500)
|