From 25313c6d813bf31fdcad4db0a038552fe86f68f2 Mon Sep 17 00:00:00 2001 From: RogerWork Date: Wed, 7 Aug 2024 14:07:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E7=AC=AC=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E6=8E=A8=E8=8D=90=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 | 1 + dspt_api/util/api/__init__.py | 0 dspt_api/util/api/cinema_hall_seats.py | 25 ++++++++++++++++++ dspt_api/util/general/__init__.py | 0 dspt_api/util/general/handle_redis.py | 31 ++++++++++++++++++++++ dspt_api/util/mapi/__init__.py | 0 dspt_api/util/suggest_params.py | 12 +++++++++ dspt_api/views.py | 36 +++++++++++++------------- 8 files changed, 87 insertions(+), 18 deletions(-) create mode 100644 dspt_api/util/api/__init__.py create mode 100644 dspt_api/util/api/cinema_hall_seats.py create mode 100644 dspt_api/util/general/__init__.py create mode 100644 dspt_api/util/general/handle_redis.py create mode 100644 dspt_api/util/mapi/__init__.py create mode 100644 dspt_api/util/suggest_params.py diff --git a/dspt_api/urls.py b/dspt_api/urls.py index a6516fa..3934fa5 100644 --- a/dspt_api/urls.py +++ b/dspt_api/urls.py @@ -12,4 +12,5 @@ urlpatterns = [ path('get_params_by_type', views.get_api_params_by_api_type), path('get_url', views.general_api_url), path('send_request', views.send_request), + path('get_suggest_params', views.get_suggest_params_by_api), ] diff --git a/dspt_api/util/api/__init__.py b/dspt_api/util/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/dspt_api/util/api/cinema_hall_seats.py b/dspt_api/util/api/cinema_hall_seats.py new file mode 100644 index 0000000..1c27995 --- /dev/null +++ b/dspt_api/util/api/cinema_hall_seats.py @@ -0,0 +1,25 @@ +from dspt_api.util.general.handle_redis import get_data_from_redis +import random + + +class ApiCinemaHallSeats: + def __init__(self, **kwargs): + self.member_type = kwargs.get('member_type') + self.api = kwargs.get('api') + self.ip = kwargs.get('ip') + + def get_suggestion(self): + # hall_id + request_api = {'name': '3.1.3 获取影厅列表', 'path': 'cinema/halls'} + redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}' + result, redis_data = get_data_from_redis(redis_key_api) + if result: + hall_data = random.choice(redis_data) + hall_id = hall_data['id'] + return [{'param': 'hall_id', 'value': hall_id, 'is_checked': True}] + else: + # 返回推荐参数应该包含参数名,参数值,和是否勾选的状态 + return [ + {'param': 'hall_id', 'value': redis_data + request_api["name"], 'is_checked': True}] + + diff --git a/dspt_api/util/general/__init__.py b/dspt_api/util/general/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/dspt_api/util/general/handle_redis.py b/dspt_api/util/general/handle_redis.py new file mode 100644 index 0000000..eb7b5f8 --- /dev/null +++ b/dspt_api/util/general/handle_redis.py @@ -0,0 +1,31 @@ +from django_redis import get_redis_connection +import json +import xmltodict + + +# 封装方法处理从redis中获取接口数据 +def get_data_from_redis(redis_key): + # 初始化redis + redis_conn = get_redis_connection() + if redis_conn.exists(redis_key): + cinema_halls_data = json.loads(redis_conn.get(redis_key)) + resp_data = [] + resp_result = 0 + if cinema_halls_data['format'] == 'json': + resp = json.loads(cinema_halls_data['response_data']) + print('json-response', resp) + resp_result = resp['res']['status'] + if str(resp_result) == '1': + resp_data = resp['res']['data'] + if cinema_halls_data['format'] == 'xml': + resp = xmltodict.parse(cinema_halls_data['response_data']) + print('xml-response', resp) + resp_result = resp['root']['status'] + if str(resp_result) == '1': + resp_data = resp['root']['data']['item'] + if str(resp_result) != '1': + return False, '请检查接口返回值或手动输入参数:' + if len(resp_data) == 0: + return False, '接口返回数据为空,请手动输入参数:' + return True, resp_data + return False, '请手动输入参数,或先请求接口:' diff --git a/dspt_api/util/mapi/__init__.py b/dspt_api/util/mapi/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/dspt_api/util/suggest_params.py b/dspt_api/util/suggest_params.py new file mode 100644 index 0000000..f259ac0 --- /dev/null +++ b/dspt_api/util/suggest_params.py @@ -0,0 +1,12 @@ +from dspt_api.util.api.cinema_hall_seats import ApiCinemaHallSeats + + +# 通过api来匹配不同的接口文件获取推荐 +# 返回推荐参数应该包含参数名,参数值,和是否勾选的状态 +def suggest_params(member_type, api, ip): + data = {'member_type': member_type, 'api': api, 'ip': ip} + params = [] + if api == 'cinema/hall-seats' and member_type == 'nonmember': + print('cinema/hall-seats') + params = ApiCinemaHallSeats(**data).get_suggestion() + return params diff --git a/dspt_api/views.py b/dspt_api/views.py index c7f548f..8962f7a 100644 --- a/dspt_api/views.py +++ b/dspt_api/views.py @@ -16,7 +16,7 @@ from dspt_api.util.sign import Sign 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 dspt_api.util.xml_util import get_xml +from dspt_api.util.suggest_params import suggest_params # Create your views here. @@ -126,6 +126,17 @@ def get_api_params_by_api_type(request): return JsonResponse(api_params_dict, json_dumps_params={'ensure_ascii': False}) +# 通过接口获取推荐参数的入口函数 +@csrf_exempt +def get_suggest_params_by_api(request): + # 获取基础数据 + member_type = request.GET.get('member_type') + api = request.GET.get('api') + user_ip = request.META.get('REMOTE_ADDR') + params = suggest_params(member_type, api, user_ip) + return JsonResponse(params, safe=False) + + # 外部接口, 用于实时生成url并返回前端 @csrf_exempt def general_api_url(request): @@ -146,14 +157,6 @@ def general_api_url(request): if req_obj is False: return JsonResponse(failed_resp_data) return JsonResponse(result) - # if resp_format == 'xml': - # if req_obj is False: - # return HttpResponse(get_xml(failed_resp_data)) - # return HttpResponse(get_xml(result)) - # if resp_format == 'json': - # if req_obj is False: - # return JsonResponse(failed_resp_data) - # return JsonResponse(result) # 对外接口,用于收集用户提交内容后发送请求 @@ -170,31 +173,32 @@ def send_request(request): req = json.loads(request.body) params = json.loads(req.get('params')) resp_format = params['format'] + member_type = req.get('member_type') # 初始化redis redis_conn = get_redis_connection() user_ip = request.META.get('REMOTE_ADDR') web_req = json.loads(request.body) api = web_req.get('api') - redis_key_api = f'dspt_api_{user_ip}_{api}' + redis_key_api = f'dspt_api_{user_ip}_{member_type}_{api}' # redis_key_user_data = f'dspt_api_{user_ip}_{api}_data' # 发送请求 req, sig = handle_request(request) failed_resp_data = {'url': '请检查foramt、pid参数', 'sig': ''} - # if resp_format == 'xml' and req is False: - # return HttpResponse(get_xml(failed_resp_data)) - # if resp_format == 'json' and req is False: - # return JsonResponse(failed_resp_data) + if req is False: return JsonResponse(failed_resp_data) response = requests.Session().send(req) print('response', response.content) + # 记录Redis if redis_conn.exists(redis_key_api): redis_conn.delete(redis_key_api) data = { 'request_url': req.url, + 'member_type': member_type, 'format': resp_format, + 'params': params, 'response_data': response.text } print(data) @@ -211,10 +215,6 @@ def send_request(request): print(db_data) EcRequestLog.objects.create(**db_data) return JsonResponse({'format': resp_format, 'data': response.text}) - # if resp_format == 'xml': - # return HttpResponse(response.content) - # if resp_format == 'json': - # return JsonResponse(response.json()) # 对外接口,用于收集用户选择的数据,例如场次,卖品等,以便后续接口直接调用