|
|
|
@ -1,5 +1,7 @@ |
|
|
|
|
import datetime |
|
|
|
|
import json |
|
|
|
|
import re |
|
|
|
|
|
|
|
|
|
import requests |
|
|
|
|
import xml.etree.ElementTree |
|
|
|
|
from urllib.parse import urljoin |
|
|
|
@ -17,7 +19,9 @@ from dspt_api.models import EcChannel, EcEnv, EcApi, EcApiParams, EcCinemaIds, E |
|
|
|
|
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.general.handle_xml_resp import HandleXmlResp |
|
|
|
|
from dspt_api.util.general.format_xml import format_xml |
|
|
|
|
import chardet |
|
|
|
|
|
|
|
|
|
# Create your views here. |
|
|
|
|
class EcChannelViewSet(viewsets.ModelViewSet): |
|
|
|
@ -214,25 +218,72 @@ def send_request(request): |
|
|
|
|
} |
|
|
|
|
print(db_data) |
|
|
|
|
EcRequestLog.objects.create(**db_data) |
|
|
|
|
return JsonResponse({'format': resp_format, 'data': response.text}) |
|
|
|
|
# 处理xml数据 |
|
|
|
|
handled_data = '' |
|
|
|
|
_data = '' |
|
|
|
|
if resp_format == 'json': |
|
|
|
|
_data = response.text |
|
|
|
|
handled_data = json.loads(response.text) |
|
|
|
|
if resp_format == 'xml': |
|
|
|
|
_data = format_xml(re.sub(r'>\n?\s+?<', r'>\n<', response.text)) |
|
|
|
|
print('---format_xml-----', _data) |
|
|
|
|
handled_data = HandleXmlResp(response.text).format_xml() |
|
|
|
|
return JsonResponse({'format': resp_format, 'data': _data, 'handled': json.dumps(handled_data)}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 对外接口,用于收集用户选择的数据,例如场次,卖品等,以便后续接口直接调用 |
|
|
|
|
@csrf_exempt |
|
|
|
|
def user_select_data(request): |
|
|
|
|
def set_user_select_data(request): |
|
|
|
|
""" |
|
|
|
|
参数 包含 |
|
|
|
|
1. api地址 |
|
|
|
|
2. 接口类型 会员 非会员 |
|
|
|
|
3. 接口数据类型 json xml |
|
|
|
|
4. 用户选择数据 |
|
|
|
|
""" |
|
|
|
|
# 获取返回类型结果 |
|
|
|
|
req = json.loads(request.body) |
|
|
|
|
api = req.get('api') |
|
|
|
|
member_type = req.get('member_type') |
|
|
|
|
resp_format = req.get('format') |
|
|
|
|
user_data = req.get('user_data') |
|
|
|
|
user_ip = request.META.get('REMOTE_ADDR') |
|
|
|
|
# 初始化redis |
|
|
|
|
redis_conn = get_redis_connection() |
|
|
|
|
user_ip = request.META.get('REMOTE_ADDR') |
|
|
|
|
api = request.get('api') |
|
|
|
|
redis_key_user_data = f'dspt_api_{user_ip}_{api}_data' |
|
|
|
|
redis_key_user_data = f'dspt_api_{user_ip}_{member_type}_{api}_user_data' |
|
|
|
|
# 记录用户选择 |
|
|
|
|
data = { |
|
|
|
|
'user_data': request.get('user_data') |
|
|
|
|
'api': api, |
|
|
|
|
'member_type': member_type, |
|
|
|
|
'format': resp_format, |
|
|
|
|
'user_data': user_data, |
|
|
|
|
} |
|
|
|
|
if redis_conn.exists(redis_key_user_data): |
|
|
|
|
redis_conn.delete(redis_key_user_data) |
|
|
|
|
result = redis_conn.set(redis_key_user_data, json.dumps(data), 10 * 60 * 60) |
|
|
|
|
return result |
|
|
|
|
if redis_conn.set(redis_key_user_data, json.dumps(data), 10 * 60 * 60): |
|
|
|
|
return JsonResponse({'result': 'success'}) |
|
|
|
|
return JsonResponse({'result': 'fail'}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@csrf_exempt |
|
|
|
|
def clear_user_select_data(request): |
|
|
|
|
""" |
|
|
|
|
参数 包含 |
|
|
|
|
1. api地址 |
|
|
|
|
2. 接口类型 会员 非会员 |
|
|
|
|
""" |
|
|
|
|
# 获取返回类型结果 |
|
|
|
|
req = json.loads(request.body) |
|
|
|
|
api = req.get('api') |
|
|
|
|
member_type = req.get('member_type') |
|
|
|
|
user_ip = request.META.get('REMOTE_ADDR') |
|
|
|
|
# 初始化redis |
|
|
|
|
redis_conn = get_redis_connection() |
|
|
|
|
redis_key_user_data = f'dspt_api_{user_ip}_{member_type}_{api}_user_data' |
|
|
|
|
if redis_conn.exists(redis_key_user_data): |
|
|
|
|
if redis_conn.delete(redis_key_user_data): |
|
|
|
|
return JsonResponse({'result': 'success'}) |
|
|
|
|
return JsonResponse({'result': 'fail'}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 对外接口,用于给前端提供上次请求的数据,如果没有则返回数据库中的默认值 |
|
|
|
|