From 888452e7a1bc6478e5fa1f3468e260505ce9f158 Mon Sep 17 00:00:00 2001 From: RogerWork Date: Mon, 26 Aug 2024 20:20:45 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E5=A2=9E=E5=8A=A0=E9=9A=8F=E6=9C=BA?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E7=9A=84=E9=80=BB=E8=BE=91=202.=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=97=B6=E9=97=B4=E6=88=B3=E7=9A=84=E6=A6=82=E5=BF=B5?= =?UTF-8?q?=EF=BC=8C=20=E7=94=A8=E6=9D=A5=E5=88=A4=E6=96=AD=E6=8E=A8?= =?UTF-8?q?=E8=8D=90=E6=95=B0=E6=8D=AE=E7=9A=84=E6=96=B0=E6=97=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dspt_api/urls.py | 6 +++- dspt_api/util/api/cinema_goods_pic.py | 20 +++++++++---- dspt_api/util/api/cinema_hall_seats.py | 14 ++++++--- dspt_api/util/api/cinema_plays_increment.py | 11 +++++-- dspt_api/util/api/play_info.py | 14 ++++++--- .../util/api/play_period_changed_seats.py | 19 ++++++++---- dspt_api/util/api/play_seat_overview.py | 14 +++++++-- dspt_api/util/api/play_seat_status.py | 20 +++++++++---- dspt_api/util/api/seat_lock.py | 29 +++++++++++++++++++ dspt_api/util/general/handle_redis.py | 12 ++++++-- dspt_api/util/random_params.py | 21 ++++++++++++++ dspt_api/util/suggest_params.py | 27 +++++++++++++++++ dspt_api/views.py | 27 ++++++++++++++++- 13 files changed, 201 insertions(+), 33 deletions(-) create mode 100644 dspt_api/util/api/seat_lock.py create mode 100644 dspt_api/util/random_params.py diff --git a/dspt_api/urls.py b/dspt_api/urls.py index 761a65b..1078e75 100644 --- a/dspt_api/urls.py +++ b/dspt_api/urls.py @@ -1,6 +1,9 @@ from dspt_api import views from django.urls import path + + + urlpatterns = [ path('get_channel', views.EcChannelViewSet.as_view({'get': 'list'}), name='get_ec_channel'), path('get_env', views.EcEnvViewSet.as_view({'get': 'list'}), name='get_ec_env'), @@ -10,9 +13,10 @@ urlpatterns = [ 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_params_by_type', views.get_api_params_by_api_type), - path('get_url', views.general_api_url), + path('get_url', views.general_api_url), path('send_request', views.send_request), path('get_suggest_params', views.get_suggest_params_by_api), + path('get_suggest_params_timestamp', views.get_suggest_params_timestamp_by_api), path('set_user_data', views.set_user_select_data), path('clear_user_data', views.clear_user_select_data), ] diff --git a/dspt_api/util/api/cinema_goods_pic.py b/dspt_api/util/api/cinema_goods_pic.py index 2817626..ad4d545 100644 --- a/dspt_api/util/api/cinema_goods_pic.py +++ b/dspt_api/util/api/cinema_goods_pic.py @@ -13,16 +13,26 @@ class ApiCinemaGoodsPic(object): # play_id play_update_time request_api = {'name': '3.2.1 获取卖品列表', 'path': 'cinema/goods'} redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}' - result, _format, redis_data = get_data_from_redis(redis_key_api) + result, _format, redis_data, _timestamp = get_data_from_redis(redis_key_api) if result: goods_list = get_goods_list(redis_data, _format) goods = random.choice(goods_list) goods_id = goods['id'] goods_type = goods['type'] - return [{'param': 'goods_id', 'value': goods_id, 'is_checked': True, 'result': True}, - {'param': 'type', 'value': goods_type, 'is_checked': True, 'result': True}] + return [ + {'param': 'goods_id', 'value': goods_id, 'is_checked': True, 'result': True, 'timestamp': _timestamp}, + {'param': 'type', 'value': goods_type, 'is_checked': True, 'result': True, 'timestamp': _timestamp}] else: # 返回推荐参数应该包含参数名,参数值,和是否勾选的状态 return [ - {'param': 'goods_id', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': False}, - {'param': 'type', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': False}] + {'param': 'goods_id', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': False, + 'timestamp': _timestamp}, + {'param': 'type', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': False, + 'timestamp': _timestamp}] + + def get_timestamp(self): + # play_id play_update_time + request_api = {'name': '3.2.1 获取卖品列表', 'path': 'cinema/goods'} + redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}' + result, _format, redis_data, _timestamp = get_data_from_redis(redis_key_api) + return _timestamp if result else 0 diff --git a/dspt_api/util/api/cinema_hall_seats.py b/dspt_api/util/api/cinema_hall_seats.py index 45d52c0..00400cb 100644 --- a/dspt_api/util/api/cinema_hall_seats.py +++ b/dspt_api/util/api/cinema_hall_seats.py @@ -12,14 +12,20 @@ class ApiCinemaHallSeats: # 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, _format, redis_data = get_data_from_redis(redis_key_api) + result, _format, redis_data, _timestamp = 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, 'result': True}] + return [{'param': 'hall_id', 'value': hall_id, 'is_checked': True, 'result': True, 'timestamp': _timestamp}] else: # 返回推荐参数应该包含参数名,参数值,和是否勾选的状态 return [ - {'param': 'hall_id', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': False}] - + {'param': 'hall_id', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': False, + 'timestamp': _timestamp}] + def get_timestamp(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, _format, redis_data, _timestamp = get_data_from_redis(redis_key_api) + return _timestamp if result else 0 diff --git a/dspt_api/util/api/cinema_plays_increment.py b/dspt_api/util/api/cinema_plays_increment.py index c991c20..7d2596c 100644 --- a/dspt_api/util/api/cinema_plays_increment.py +++ b/dspt_api/util/api/cinema_plays_increment.py @@ -12,8 +12,15 @@ class ApiCinemaPlaysIncrement: # start request_api = {'name': '3.1.6 获取放映计划列表(增量)', 'path': 'cinema/plays-increment'} redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}' - result, _format, redis_data = get_data_from_redis(redis_key_api) + result, _format, redis_data, _timestamp = get_data_from_redis(redis_key_api) # 生成默认值 start = redis_data['nextSyncStartTime'] if result else datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d') + ' 06:00:00' - return [{'param': 'start', 'value': start, 'is_checked': True, 'result': True}] + return [{'param': 'start', 'value': start, 'is_checked': True, 'result': True, 'timestamp': _timestamp}] + + def get_timestamp(self): + # start + request_api = {'name': '3.1.6 获取放映计划列表(增量)', 'path': 'cinema/plays-increment'} + redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}' + result, _format, redis_data, _timestamp = get_data_from_redis(redis_key_api) + return _timestamp if result else 0 diff --git a/dspt_api/util/api/play_info.py b/dspt_api/util/api/play_info.py index 551b7ff..a8b626a 100644 --- a/dspt_api/util/api/play_info.py +++ b/dspt_api/util/api/play_info.py @@ -13,14 +13,20 @@ class ApiPlayInfo: # play_id request_api = {'name': '3.1.5 获取放映计划列表', 'path': 'cinema/plays'} redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}' - result, _format, redis_data = get_data_from_redis(redis_key_api) + result, _format, redis_data, _timestamp = get_data_from_redis(redis_key_api) if result: play_data = random.choice(redis_data) play_id = play_data['id'] - return [{'param': 'play_id', 'value': play_id, 'is_checked': True, 'result': True}] + return [{'param': 'play_id', 'value': play_id, 'is_checked': True, 'result': True, 'timestamp': _timestamp}] else: # 返回推荐参数应该包含参数名,参数值,和是否勾选的状态 return [ - {'param': 'play_id', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': True}] - + {'param': 'play_id', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': True, + 'timestamp': _timestamp}] + def get_timestamp(self): + # play_id + request_api = {'name': '3.1.5 获取放映计划列表', 'path': 'cinema/plays'} + redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}' + result, _format, redis_data, _timestamp = get_data_from_redis(redis_key_api) + return _timestamp if result else 0 diff --git a/dspt_api/util/api/play_period_changed_seats.py b/dspt_api/util/api/play_period_changed_seats.py index e294bed..708e7cd 100644 --- a/dspt_api/util/api/play_period_changed_seats.py +++ b/dspt_api/util/api/play_period_changed_seats.py @@ -13,15 +13,24 @@ class ApiPlayPeriodChangedSeats: # play_id play_update_time request_api = {'name': '3.1.5 获取放映计划列表', 'path': 'cinema/plays'} redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}' - result, _format, redis_data = get_data_from_redis(redis_key_api) + result, _format, redis_data, _timestamp = get_data_from_redis(redis_key_api) if result: play_data = random.choice(redis_data) play_id = play_data['id'] start = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d') + ' 06:00:00' - return [{'param': 'play_id', 'value': play_id, 'is_checked': True, 'result': True}, - {'param': 'start', 'value': start, 'is_checked': True, 'result': True}] + return [{'param': 'play_id', 'value': play_id, 'is_checked': True, 'result': True, 'timestamp': _timestamp}, + {'param': 'start', 'value': start, 'is_checked': True, 'result': True, 'timestamp': _timestamp}] else: # 返回推荐参数应该包含参数名,参数值,和是否勾选的状态 return [ - {'param': 'play_id', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': True}, - {'param': 'start', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': True}] + {'param': 'play_id', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': True, + 'timestamp': _timestamp}, + {'param': 'start', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': True, + 'timestamp': _timestamp}] + + def get_timestamp(self): + # play_id play_update_time + request_api = {'name': '3.1.5 获取放映计划列表', 'path': 'cinema/plays'} + redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}' + result, _format, redis_data, _timestamp = get_data_from_redis(redis_key_api) + return _timestamp if result else 0 diff --git a/dspt_api/util/api/play_seat_overview.py b/dspt_api/util/api/play_seat_overview.py index 68da602..11fed09 100644 --- a/dspt_api/util/api/play_seat_overview.py +++ b/dspt_api/util/api/play_seat_overview.py @@ -12,12 +12,20 @@ class ApiPlaySeatOverview: # play_id play_update_time request_api = {'name': '3.1.5 获取放映计划列表', 'path': 'cinema/plays'} redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}' - result, _format, redis_data = get_data_from_redis(redis_key_api) + result, _format, redis_data, _timestamp = get_data_from_redis(redis_key_api) if result: play_data = random.choice(redis_data) play_id = play_data['id'] - return [{'param': 'play_id', 'value': play_id, 'is_checked': True}] + return [{'param': 'play_id', 'value': play_id, 'is_checked': True, 'timestamp': _timestamp}] else: # 返回推荐参数应该包含参数名,参数值,和是否勾选的状态 return [ - {'param': 'play_id', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': True}] + {'param': 'play_id', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': True, + 'timestamp': _timestamp}] + + def get_timestamp(self): + # play_id play_update_time + request_api = {'name': '3.1.5 获取放映计划列表', 'path': 'cinema/plays'} + redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}' + result, _format, redis_data, _timestamp = get_data_from_redis(redis_key_api) + return _timestamp if result else 0 diff --git a/dspt_api/util/api/play_seat_status.py b/dspt_api/util/api/play_seat_status.py index cd70e3e..e895ed4 100644 --- a/dspt_api/util/api/play_seat_status.py +++ b/dspt_api/util/api/play_seat_status.py @@ -12,15 +12,25 @@ class ApiPlaySeatStatus: # play_id play_update_time request_api = {'name': '3.1.5 获取放映计划列表', 'path': 'cinema/plays'} redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}' - result, _format, redis_data = get_data_from_redis(redis_key_api) + result, _format, redis_data, _timestamp = get_data_from_redis(redis_key_api) if result: play_data = random.choice(redis_data) play_id = play_data['id'] play_update_time = play_data['cineUpdateTime'] - return [{'param': 'play_id', 'value': play_id, 'is_checked': True, 'result': True}, - {'param': 'play_update_time', 'value': play_update_time, 'is_checked': True, 'result': True}] + return [{'param': 'play_id', 'value': play_id, 'is_checked': True, 'result': True, 'timestamp': _timestamp}, + {'param': 'play_update_time', 'value': play_update_time, 'is_checked': True, 'result': True, + 'timestamp': _timestamp}] else: # 返回推荐参数应该包含参数名,参数值,和是否勾选的状态 return [ - {'param': 'play_id', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': True}, - {'param': 'play_update_time', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': True}] + {'param': 'play_id', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': True, + 'timestamp': _timestamp}, + {'param': 'play_update_time', 'value': redis_data + request_api["name"], 'is_checked': True, + 'result': True, 'timestamp': _timestamp}] + + def get_timestamp(self): + # play_id play_update_time + request_api = {'name': '3.1.5 获取放映计划列表', 'path': 'cinema/plays'} + redis_key_api = f'dspt_api_{self.ip}_{self.member_type}_{request_api["path"]}' + result, _format, redis_data, _timestamp = get_data_from_redis(redis_key_api) + return _timestamp if result else 0 diff --git a/dspt_api/util/api/seat_lock.py b/dspt_api/util/api/seat_lock.py new file mode 100644 index 0000000..ab66c73 --- /dev/null +++ b/dspt_api/util/api/seat_lock.py @@ -0,0 +1,29 @@ +from dspt_api.util.general.handle_redis import get_data_from_redis +import random + + +class ApiSeatLock: + 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): + # # play_id + # request_api_play = {'name': '3.1.5 获取放映计划列表', 'path': 'cinema/plays'} + # redis_key_api_play = f'dspt_api_{self.ip}_{self.member_type}_{request_api_play["path"]}' + # result_play, _format_play, redis_data_play = get_data_from_redis(redis_key_api_play) + # # seat_id + # request_api_seat = {'name': '3.1.8 获取某场次座位状态', 'path': 'play/seat-status'} + # redis_key_api_seat = f'dspt_api_{self.ip}_{self.member_type}_{request_api_seat["path"]}' + # result_seat, _format_seat, redis_data_seat = get_data_from_redis(redis_key_api_seat) + # if result: + # play_data = random.choice(redis_data) + # play_id = play_data['id'] + # return [{'param': 'play_id', 'value': play_id, 'is_checked': True, 'result': True}] + # else: + # # 返回推荐参数应该包含参数名,参数值,和是否勾选的状态 + # return [ + # {'param': 'play_id', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': True}] + + diff --git a/dspt_api/util/general/handle_redis.py b/dspt_api/util/general/handle_redis.py index 21876c4..254099f 100644 --- a/dspt_api/util/general/handle_redis.py +++ b/dspt_api/util/general/handle_redis.py @@ -12,7 +12,13 @@ def get_data_from_redis(redis_key): redis_data = json.loads(redis_conn.get(redis_key + '_user_data')) user_data = redis_data['user_data'] print('user_data', json.dumps(user_data)) - return True, redis_data['format'], user_data + return True, redis_data['format'], user_data, redis_data['timestamp'] + # 如果没用户数据时优先获取随机数据 + if redis_conn.exists(redis_key + '_random'): + redis_data = json.loads(redis_conn.get(redis_key + '_random')) + user_data = redis_data['user_data'] + print('user_data', json.dumps(user_data)) + return True, redis_data['format'], user_data, redis_data['timestamp'] # 没有用户数据时,随机选择数据 if redis_conn.exists(redis_key): redis_data = json.loads(redis_conn.get(redis_key)) @@ -26,5 +32,5 @@ def get_data_from_redis(redis_key): return False, redis_data['format'], '请检查接口返回值或手动输入参数:' if len(resp_data) == 0: return False, redis_data['format'], '接口返回数据为空,请手动输入参数:' - return True, redis_data['format'], resp_data - return False, 'json', '请手动输入参数,或先请求接口:' + return True, redis_data['format'], resp_data, redis_data['timestamp'] + return False, 'json', '请手动输入参数,或先请求接口:', 0 diff --git a/dspt_api/util/random_params.py b/dspt_api/util/random_params.py new file mode 100644 index 0000000..da7bdf8 --- /dev/null +++ b/dspt_api/util/random_params.py @@ -0,0 +1,21 @@ +import random +import json +import time + +from django_redis import get_redis_connection + +def random_params(_user_info, _handle_data): + print('_user_info', _user_info) + print('_handle_data', _handle_data) + redis_key_api = f'dspt_api_{_user_info["user_ip"]}_{_user_info["member_type"]}_{_user_info["api"]}_random' + redis_conn = get_redis_connection() + if redis_conn.get(redis_key_api): + redis_conn.delete(redis_key_api) + data = { + 'api': _user_info["api"], + 'member_type': _user_info["member_type"], + 'format': _user_info["format"], + 'timestamp': int(time.time()*1000), + 'user_data': [random.choice(_handle_data['res']['data'])], + } + redis_conn.set(redis_key_api, json.dumps(data)) diff --git a/dspt_api/util/suggest_params.py b/dspt_api/util/suggest_params.py index 42b3c44..353c41b 100644 --- a/dspt_api/util/suggest_params.py +++ b/dspt_api/util/suggest_params.py @@ -35,3 +35,30 @@ def suggest_params(member_type, api, ip): print('cinema/goods-pic') params = ApiCinemaGoodsPic(**data).get_suggestion() return params + +def suggest_params_timestamp(member_type, api, ip): + data = {'member_type': member_type, 'api': api, 'ip': ip} + _timestamps = 0 + # 非会员 + if api == 'cinema/hall-seats' and member_type == 'nonmember': + print('cinema/hall-seats') + _timestamps = ApiCinemaHallSeats(**data).get_timestamp() + if api == 'cinema/plays-increment' and member_type == 'nonmember': + print('cinema/plays-increment') + _timestamps = ApiCinemaPlaysIncrement(**data).get_timestamp() + if api == 'play/info' and member_type == 'nonmember': + print('play/info') + _timestamps = ApiPlayInfo(**data).get_timestamp() + if api == 'play/seat-status' and member_type == 'nonmember': + print('play/seat-status') + _timestamps = ApiPlaySeatStatus(**data).get_timestamp() + if api == 'play/period-changed-seats' and member_type == 'nonmember': + print('play/period-changed-seats') + _timestamps = ApiPlayPeriodChangedSeats(**data).get_timestamp() + if api == 'play/seat-overview' and member_type == 'nonmember': + print('play/seat-overview') + _timestamps = ApiPlaySeatOverview(**data).get_timestamp() + if api == 'cinema/goods-pic' and member_type == 'nonmember': + print('cinema/goods-pic') + _timestamps = ApiCinemaGoodsPic(**data).get_timestamp() + return _timestamps \ No newline at end of file diff --git a/dspt_api/views.py b/dspt_api/views.py index ceb0f7e..bbab863 100644 --- a/dspt_api/views.py +++ b/dspt_api/views.py @@ -1,6 +1,7 @@ import datetime import json import re +import time import requests import xml.etree.ElementTree @@ -14,14 +15,17 @@ from django.views.decorators.csrf import csrf_exempt from django.http.response import JsonResponse, HttpResponse # from rest_framework.response import Response from django.utils import timezone + +from dspt_api.util.random_params import random_params 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.suggest_params import suggest_params +from dspt_api.util.suggest_params import suggest_params, suggest_params_timestamp from dspt_api.util.general.handle_xml_resp import HandleXmlResp from dspt_api.util.general.format_xml import format_xml + # Create your views here. class EcChannelViewSet(viewsets.ModelViewSet): queryset = EcChannel.objects.all() @@ -140,6 +144,17 @@ def get_suggest_params_by_api(request): return JsonResponse(params, safe=False) +@csrf_exempt +def get_suggest_params_timestamp_by_api(request): + # 获取基础数据 + member_type = request.GET.get('member_type') + api = request.GET.get('api') + user_ip = request.META.get('REMOTE_ADDR') + _ts = suggest_params_timestamp(member_type, api, user_ip) + print('timestamp', _ts) + return JsonResponse({'timestamp': _ts}) + + # 外部接口, 用于实时生成url并返回前端 @csrf_exempt def general_api_url(request): @@ -210,6 +225,7 @@ def send_request(request): 'request_url': req.url, 'member_type': member_type, 'format': resp_format, + 'timestamp': int(time.time() * 1000), 'params': params, 'response_data': response.text, 'handled_data': handled_data, @@ -226,6 +242,14 @@ def send_request(request): } print(db_data) EcRequestLog.objects.create(**db_data) + # 写入随机值 + user_data = { + 'user_ip': user_ip, + 'member_type': member_type, + 'api': api, + 'format': resp_format, + } + random_params(user_data, handled_data) return JsonResponse({'format': resp_format, 'data': response_data, 'handled': json.dumps(handled_data)}) @@ -256,6 +280,7 @@ def set_user_select_data(request): 'api': api, 'member_type': member_type, 'format': resp_format, + 'timestamp': int(time.time() * 1000), 'user_data': [json.loads(user_data)], } print('set_user_select_data', data)