From d3d930b001cf6730fcf7c733e3846dc1951f671d Mon Sep 17 00:00:00 2001 From: RogerWork Date: Tue, 28 May 2024 18:11:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0url=E5=A4=84=E7=90=86?= =?UTF-8?q?=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dspt_api/urls.py | 2 ++ dspt_api/util/__init__.py | 0 dspt_api/util/sign.py | 46 ++++++++++++++++++++++++++++++++++ dspt_api/views.py | 52 ++++++++++++++++++++++++++++++++++++++- 4 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 dspt_api/util/__init__.py create mode 100644 dspt_api/util/sign.py diff --git a/dspt_api/urls.py b/dspt_api/urls.py index 52bdbcc..5a4b87e 100644 --- a/dspt_api/urls.py +++ b/dspt_api/urls.py @@ -9,4 +9,6 @@ urlpatterns = [ path('get_cinema_ids', views.EcCinemaIdsViewSet.as_view({'get': 'list'}), name='get_ec_cinema_ids'), path('get_request_log', views.EcRequestLogViewSet.as_view({'get': 'list'}), name='get_ec_request_log'), path('get_api_group', views.EcApiGroupViewSet.as_view({'get': 'list'}), name='get_ec_api_group'), + path('get_url', views.general_api_url), + path('send_request', views.send_request), ] diff --git a/dspt_api/util/__init__.py b/dspt_api/util/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/dspt_api/util/sign.py b/dspt_api/util/sign.py new file mode 100644 index 0000000..17145f0 --- /dev/null +++ b/dspt_api/util/sign.py @@ -0,0 +1,46 @@ +import re +from hashlib import md5 + +""" +用于生成网售签名 +""" + + +class Sign: + + def __init__(self, salt='ly725xw701'): + self.salt = salt + + def __ksort(self, params): + """ + 请求参数排序 + :params: 未排序的参数字典 + :return: 排列后的请求参数字符串 + """ + sorted_params = sorted(params.items(), key=lambda k: k[0]) + p = [f'{k}={v}' for k, v in sorted_params] + params_str = '&'.join(p) + return params_str + + def __generate_sign(self, params): + """ + 生成签名方法 + :param params: 未排序的参数字典 + :param salt: 盐值(授权码) + :return: 生成签名 + """ + str0 = self.salt + self.__ksort(params) + m1 = md5(str0.encode()).hexdigest() + str1 = m1 + self.salt + sign = md5(str1.encode()).hexdigest() + return sign + + def add_sig(self, params): + """ + 添加sig到params + :param params: + :return: + """ + sig = self.__generate_sign(params) + params.update({'_sig': sig}) + return params \ No newline at end of file diff --git a/dspt_api/views.py b/dspt_api/views.py index a04d11b..5b205d1 100644 --- a/dspt_api/views.py +++ b/dspt_api/views.py @@ -1,11 +1,16 @@ +import json +import requests +from django.db.models import Q from django_filters.rest_framework import DjangoFilterBackend from rest_framework import filters from rest_framework import viewsets - +from django.views.decorators.csrf import csrf_exempt from dspt_api.models import EcChannel, EcEnv, EcApi, EcApiParams, EcCinemaIds, EcRequestLog, EcApiGroup from dspt_api.serializers import EcChannelSerializer, EcEnvSerializer, EcApiSerializer, EcApiParamsSerializer, \ EcCinemaIdsSerializer, EcRequestLogSerializer, EcApiGroupSerializer +from util.sign import Sign + # Create your views here. class EcChannelViewSet(viewsets.ModelViewSet): @@ -44,3 +49,48 @@ class EcRequestLogViewSet(viewsets.ModelViewSet): class EcApiGroupViewSet(viewsets.ModelViewSet): queryset = EcApiGroup.objects.all() serializer_class = EcApiGroupSerializer + + +@csrf_exempt +def general_api_url(request): + req_obj = handle_request(request) + return req_obj.url + + +def handle_request(_request): + """ + POST请求参数 + env dspt或zyds + member_type member或nonmember + api api地址 + params 请求参数 + """ + req = _request.POST + env = req.get('env') + member_type = req.get('member_type') + base_url = EcEnv.objects.filter(Q(code=env) & Q(type=member_type)).first().get('host') + api = req.get('api') + params = json.loads(req.get('params')) + pid = params.get('pid') + key = EcChannel.objects.filter(Q(pid=pid) & Q(env=env)).first().get('channel_key') + sign = Sign(key) + req_params = sign.add_sig(params) + req_obj = prepare_request(base_url, api, req_params) + return req_obj + + +def prepare_request(_url, _api, _params): + req = requests.PreparedRequest() + req.prepare_method('GET') + req.prepare_url(url=_url + '/' + _api, params=_params) + req.prepare_headers({'host': _url[7:]}) + return req + + +@csrf_exempt +def send_request(request): + req = handle_request(request) + response = requests.Session().send(req) + # 插入数据 + + return response.json()