增加url处理的逻辑

main
RogerWork 6 months ago
parent 8940d23028
commit d3d930b001
  1. 2
      dspt_api/urls.py
  2. 0
      dspt_api/util/__init__.py
  3. 46
      dspt_api/util/sign.py
  4. 52
      dspt_api/views.py

@ -9,4 +9,6 @@ urlpatterns = [
path('get_cinema_ids', views.EcCinemaIdsViewSet.as_view({'get': 'list'}), name='get_ec_cinema_ids'), 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_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_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),
] ]

@ -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

@ -1,11 +1,16 @@
import json
import requests
from django.db.models import Q
from django_filters.rest_framework import DjangoFilterBackend from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import filters from rest_framework import filters
from rest_framework import viewsets 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.models import EcChannel, EcEnv, EcApi, EcApiParams, EcCinemaIds, EcRequestLog, EcApiGroup
from dspt_api.serializers import EcChannelSerializer, EcEnvSerializer, EcApiSerializer, EcApiParamsSerializer, \ from dspt_api.serializers import EcChannelSerializer, EcEnvSerializer, EcApiSerializer, EcApiParamsSerializer, \
EcCinemaIdsSerializer, EcRequestLogSerializer, EcApiGroupSerializer EcCinemaIdsSerializer, EcRequestLogSerializer, EcApiGroupSerializer
from util.sign import Sign
# Create your views here. # Create your views here.
class EcChannelViewSet(viewsets.ModelViewSet): class EcChannelViewSet(viewsets.ModelViewSet):
@ -44,3 +49,48 @@ class EcRequestLogViewSet(viewsets.ModelViewSet):
class EcApiGroupViewSet(viewsets.ModelViewSet): class EcApiGroupViewSet(viewsets.ModelViewSet):
queryset = EcApiGroup.objects.all() queryset = EcApiGroup.objects.all()
serializer_class = EcApiGroupSerializer 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()

Loading…
Cancel
Save