diff --git a/apps/third/dahua.py b/apps/third/dahua.py index bea5c6c3..f56bc659 100644 --- a/apps/third/dahua.py +++ b/apps/third/dahua.py @@ -16,6 +16,7 @@ from apps.utils.tools import convert_to_base64, print_roundtrip from django.utils.timezone import now from apps.third.tapis import dhapis from apps.utils.tools import singleton +from django.core.cache import cache requests.packages.urllib3.disable_warnings() myLogger = logging.getLogger('log') @@ -34,7 +35,6 @@ class DhClient: self.client_secret = client_secret self.headers = {"Connection": "close"} self.isGetingToken = False - self.token = None self.log = {} self._get_token() @@ -47,15 +47,15 @@ class DhClient: } is_ok, res = self.request(**dhapis['token_login'], params=params, raise_exception=False) if is_ok == 'success': - self.token = res['access_token'] - self.headers['Authorization'] = 'bearer ' + res['access_token'] - self.headers['User-Id'] = '1' + cache.set('dh_token', res['access_token']) self.isGetingToken = False def request(self, url: str, method: str, params=dict(), json=dict(), timeout=120, file_path_rela=None, raise_exception=True): if not settings.DAHUA_ENABLED: raise ParseError('大华对接未启用') + self.headers['Authorization'] = 'bearer ' + cache.get('dh_token', '') + self.headers['User-Id'] = '1' self.log = {"requested_at": now(), "id": uuid.uuid4(), "path": url, "method": method, "params": params, "body": json, "target": "dahua", "result": 10, "headers": self.headers} files = None @@ -111,7 +111,7 @@ class DhClient: def get_full_pic(self, path: str): """返回完整访问地址 """ - return '{}/evo-apigw/evo-oss/{}?token={}'.format(settings.DAHUA_BASE_URL, path, self.token) + return '{}/evo-apigw/evo-oss/{}?token={}'.format(settings.DAHUA_BASE_URL, path, cache.get('dh_token', '')) def snap(self, code: str): """摄像头实时截图 diff --git a/apps/third/speaker.py b/apps/third/speaker.py index 4ce03d1a..81061ba8 100644 --- a/apps/third/speaker.py +++ b/apps/third/speaker.py @@ -14,7 +14,7 @@ from apps.utils.tools import print_roundtrip, ranstr from apps.third.tapis import spapis from django.utils.timezone import now from apps.utils.tools import singleton - +from django.core.cache import cache requests.packages.urllib3.disable_warnings() myLogger = logging.getLogger('log') @@ -46,13 +46,14 @@ class SpClient: } is_ok, res = self.request(**spapis['user_login'], json=json_data, raise_exception=False) if is_ok == 'success': - self.headers['Authorization'] = 'Bearer ' + res['user']['token'] + cache.set('sp_token', res['user']['token']) self.isGetingToken = False def request(self, url: str, method: str, params=dict(), json=dict(), timeout=120, file_path_rela=None, raise_exception=True): if not settings.SP_ENABLED: raise ParseError('音响对接未启用') + self.headers['Authorization'] = 'Bearer ' + cache.get('sp_token', '') self.log = {"requested_at": now(), "id": uuid.uuid4(), "path": url, "method": method, "params": params, "body": json, "target": "speaker", "result": 10, "headers": self.headers} files = None diff --git a/apps/third/views.py b/apps/third/views.py index 0666ef7e..7862d442 100755 --- a/apps/third/views.py +++ b/apps/third/views.py @@ -21,6 +21,7 @@ from rest_framework import serializers from django.conf import settings import stomp from django.conf import settings +from django.core.cache import cache myLogger = logging.getLogger('log') # Create your views here. @@ -160,7 +161,7 @@ class XxCommonViewSet(CreateModelMixin, CustomGenericViewSet): 获取token """ - return Response({'token': xxClient.token}) + return Response({'token': cache.get('xx_token', '')}) @action(methods=['post'], detail=False, permission_classes=[IsAdminUser], @@ -266,7 +267,7 @@ class DhCommonViewSet(CreateModelMixin, CustomGenericViewSet): 获取token """ - return Response({'token': dhClient.token, 'header': dhClient.headers}) + return Response({'token': cache.get('dh_token', '')}) @action(methods=['post'], detail=False, permission_classes=[IsAuthenticated], diff --git a/apps/third/xunxi.py b/apps/third/xunxi.py index e0d6dd5c..75e75d14 100644 --- a/apps/third/xunxi.py +++ b/apps/third/xunxi.py @@ -13,6 +13,7 @@ from apps.utils.tools import print_roundtrip from django.utils.timezone import now from apps.third.tapis import xxapis from apps.utils.tools import singleton +from django.core.cache import cache requests.packages.urllib3.disable_warnings() myLogger = logging.getLogger('log') @@ -29,7 +30,6 @@ class XxClient: self.username = username self.isGetingToken = False self.headers = {"Connection": "close"} - self.token = '' self.log = {} self._get_token() @@ -40,13 +40,13 @@ class XxClient: } is_ok, res = self.request(**xxapis['token_login'], json=json, raise_exception=False) if is_ok == 'success': - self.token = res['token'] + cache.set('xx_token', res['token']) self.isGetingToken = False def request(self, url: str, method: str = 'post', params=dict(), json=dict(), timeout=20, raise_exception=True): if not settings.XX_ENABLED: raise ParseError('寻息对接未启用') - params['accessToken'] = self.token + params['accessToken'] = cache.get('xx_token', '') json['username'] = self.username json['buildId'] = settings.XX_BUILDID json['licence'] = settings.XX_LICENCE