完成第一个参数推荐逻辑

main
RogerWork 3 months ago
parent ebe7e0b6e0
commit 25313c6d81
  1. 1
      dspt_api/urls.py
  2. 0
      dspt_api/util/api/__init__.py
  3. 25
      dspt_api/util/api/cinema_hall_seats.py
  4. 0
      dspt_api/util/general/__init__.py
  5. 31
      dspt_api/util/general/handle_redis.py
  6. 0
      dspt_api/util/mapi/__init__.py
  7. 12
      dspt_api/util/suggest_params.py
  8. 36
      dspt_api/views.py

@ -12,4 +12,5 @@ urlpatterns = [
path('get_params_by_type', views.get_api_params_by_api_type), 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('send_request', views.send_request),
path('get_suggest_params', views.get_suggest_params_by_api),
] ]

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

@ -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, '请手动输入参数,或先请求接口:'

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

@ -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.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 dspt_api.util.xml_util import get_xml from dspt_api.util.suggest_params import suggest_params
# Create your views here. # 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}) 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并返回前端 # 外部接口, 用于实时生成url并返回前端
@csrf_exempt @csrf_exempt
def general_api_url(request): def general_api_url(request):
@ -146,14 +157,6 @@ def general_api_url(request):
if req_obj is False: if req_obj is False:
return JsonResponse(failed_resp_data) return JsonResponse(failed_resp_data)
return JsonResponse(result) 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) req = json.loads(request.body)
params = json.loads(req.get('params')) params = json.loads(req.get('params'))
resp_format = params['format'] resp_format = params['format']
member_type = req.get('member_type')
# 初始化redis # 初始化redis
redis_conn = get_redis_connection() redis_conn = get_redis_connection()
user_ip = request.META.get('REMOTE_ADDR') user_ip = request.META.get('REMOTE_ADDR')
web_req = json.loads(request.body) web_req = json.loads(request.body)
api = web_req.get('api') 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' # redis_key_user_data = f'dspt_api_{user_ip}_{api}_data'
# 发送请求 # 发送请求
req, sig = handle_request(request) req, sig = handle_request(request)
failed_resp_data = {'url': '请检查foramt、pid参数', 'sig': ''} 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: if req is False:
return JsonResponse(failed_resp_data) return JsonResponse(failed_resp_data)
response = requests.Session().send(req) response = requests.Session().send(req)
print('response', response.content) print('response', response.content)
# 记录Redis # 记录Redis
if redis_conn.exists(redis_key_api): if redis_conn.exists(redis_key_api):
redis_conn.delete(redis_key_api) redis_conn.delete(redis_key_api)
data = { data = {
'request_url': req.url, 'request_url': req.url,
'member_type': member_type,
'format': resp_format, 'format': resp_format,
'params': params,
'response_data': response.text 'response_data': response.text
} }
print(data) print(data)
@ -211,10 +215,6 @@ def send_request(request):
print(db_data) print(db_data)
EcRequestLog.objects.create(**db_data) EcRequestLog.objects.create(**db_data)
return JsonResponse({'format': resp_format, 'data': response.text}) return JsonResponse({'format': resp_format, 'data': response.text})
# if resp_format == 'xml':
# return HttpResponse(response.content)
# if resp_format == 'json':
# return JsonResponse(response.json())
# 对外接口,用于收集用户选择的数据,例如场次,卖品等,以便后续接口直接调用 # 对外接口,用于收集用户选择的数据,例如场次,卖品等,以便后续接口直接调用

Loading…
Cancel
Save