From 522c904fdb1351cb7a8d397bc4eb7a9e9b85295c Mon Sep 17 00:00:00 2001 From: RogerWork Date: Mon, 19 Aug 2024 15:27:16 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E6=94=AF=E6=8C=81xml=E7=9A=84=E7=BE=8E?= =?UTF-8?q?=E5=8C=96=E8=BE=93=E5=87=BA=202.=20xml=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E4=B8=BAjson=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E7=9A=84=E9=80=BB=E8=BE=91=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dspt_api/urls.py | 2 + dspt_api/util/general/format_xml.py | 8 + dspt_api/util/general/handle_redis.py | 4 +- dspt_api/util/general/handle_xml_resp.py | 1002 ++++++++++++++++++++++ dspt_api/views.py | 69 +- 5 files changed, 1074 insertions(+), 11 deletions(-) create mode 100644 dspt_api/util/general/format_xml.py create mode 100644 dspt_api/util/general/handle_xml_resp.py diff --git a/dspt_api/urls.py b/dspt_api/urls.py index 3934fa5..761a65b 100644 --- a/dspt_api/urls.py +++ b/dspt_api/urls.py @@ -13,4 +13,6 @@ urlpatterns = [ path('get_url', views.general_api_url), path('send_request', views.send_request), path('get_suggest_params', views.get_suggest_params_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/general/format_xml.py b/dspt_api/util/general/format_xml.py new file mode 100644 index 0000000..674db27 --- /dev/null +++ b/dspt_api/util/general/format_xml.py @@ -0,0 +1,8 @@ +from xml.dom import minidom +import xml.etree.ElementTree as ET + +def format_xml(_xml): + root = ET.fromstring(_xml) + dom = minidom.parseString(ET.tostring(root)) + pretty_xml = dom.toprettyxml(indent="\t", newl="") + return pretty_xml \ No newline at end of file diff --git a/dspt_api/util/general/handle_redis.py b/dspt_api/util/general/handle_redis.py index f6178bd..b34f4e4 100644 --- a/dspt_api/util/general/handle_redis.py +++ b/dspt_api/util/general/handle_redis.py @@ -13,13 +13,13 @@ def get_data_from_redis(redis_key): resp_result = 0 if redis_data['format'] == 'json': resp = json.loads(redis_data['response_data']) - print('json-response', resp) + print('json-response', json.dumps(resp)) resp_result = resp['res']['status'] if str(resp_result) == '1': resp_data = resp['res']['data'] if redis_data['format'] == 'xml': resp = xmltodict.parse(redis_data['response_data']) - print('xml-response', resp) + print('xml-response', json.dumps(resp)) resp_result = resp['root']['status'] if str(resp_result) == '1': resp_data = resp['root']['data'].get('item', resp['root']['data']) diff --git a/dspt_api/util/general/handle_xml_resp.py b/dspt_api/util/general/handle_xml_resp.py new file mode 100644 index 0000000..80f6c5d --- /dev/null +++ b/dspt_api/util/general/handle_xml_resp.py @@ -0,0 +1,1002 @@ +import json +import xmltodict + + +class HandleXmlResp: + def __init__(self, xml_data): + self.xml_dict = {'res': xmltodict.parse(xml_data)['root']} + + def format_xml(self): + return self.format_data(self.xml_dict) + + def format_data(self, data): + if isinstance(data, dict): + new_data = {} + for key, value in data.items(): + if isinstance(value, dict): + if len(value.keys()) == 1: + _key = list(value.keys())[0] + if _key == 'item' and isinstance(value[_key], list): + new_data[key] = self.format_data(value[_key]) + if _key == 'item' and isinstance(value[_key], dict): + new_data[key] = [self.format_data(value[_key])] + else: + new_data[key] = self.format_data(value) + else: + new_data[key] = value + return new_data + if isinstance(data, list): + new_data = [] + for item in data: + if isinstance(item, dict): + new_data.append(self.format_data(item)) + else: + new_data.append(item) + return new_data + + +if __name__ == '__main__': + j = '''{ + "res": + { + "status": "1", + "errorMessage": null, + "errorCode": null, + "data": + { + "item": + [ + { + "id": "7114", + "movieInfo": + { + "item": + { + "cineMovieId": "2630", + "cineMovieNum": "051600112024", + "movieName": "蜘蛛夫人:超感觉醒(杜比视界)", + "movieSubtitle": "中文", + "movieLanguage": "英语", + "movieFormat": "数字", + "movieDimensional": "杜比", + "movieSize": "普通", + "joinStartTime": null, + "joinEndTime": null + } + }, + "cinePlayId": "260347", + "hallId": "530", + "hallName": "1号厅(冠名招商)", + "businessDate": "2024-08-19", + "startTime": "2024-08-19 18:38:00", + "endTime": "2024-08-19 20:35:00", + "priceType": "1", + "price": "90.00", + "marketPrice": "90.00", + "lowestPrice": "25.00", + "seatTotalNum": "118", + "seatAvailableNum": "89", + "allowBook": "1", + "cineUpdateTime": "2024-08-19 09:27:34", + "partnerPrice": "91.00", + "areaInfo": + { + "item": + [ + { + "areaId": "25", + "areaPrice": "91.00", + "areaServiceFee": "1.00", + "ecardPrices": + { + "item": + [ + { + "ecardLevelNo": "1043", + "ecardLevelPrice": "91.00", + "ecardLevelServiceFee": "1.00" + }, + { + "ecardLevelNo": "1047", + "ecardLevelPrice": "91.00", + "ecardLevelServiceFee": "1.00" + }, + { + "ecardLevelNo": "1062", + "ecardLevelPrice": "91.00", + "ecardLevelServiceFee": "1.00" + }, + { + "ecardLevelNo": "1069", + "ecardLevelPrice": "91.00", + "ecardLevelServiceFee": "1.00" + }, + { + "ecardLevelNo": "1079", + "ecardLevelPrice": "91.00", + "ecardLevelServiceFee": "1.00" + }, + { + "ecardLevelNo": "1080", + "ecardLevelPrice": "89.00", + "ecardLevelServiceFee": "1.00" + } + ] + } + }, + { + "areaId": "34", + "areaPrice": "91.00", + "areaServiceFee": "1.00", + "ecardPrices": + { + "item": + [ + { + "ecardLevelNo": "1043", + "ecardLevelPrice": "91.00", + "ecardLevelServiceFee": "1.00" + }, + { + "ecardLevelNo": "1047", + "ecardLevelPrice": "91.00", + "ecardLevelServiceFee": "1.00" + }, + { + "ecardLevelNo": "1062", + "ecardLevelPrice": "91.00", + "ecardLevelServiceFee": "1.00" + }, + { + "ecardLevelNo": "1069", + "ecardLevelPrice": "91.00", + "ecardLevelServiceFee": "1.00" + }, + { + "ecardLevelNo": "1079", + "ecardLevelPrice": "91.00", + "ecardLevelServiceFee": "1.00" + }, + { + "ecardLevelNo": "1080", + "ecardLevelPrice": "89.00", + "ecardLevelServiceFee": "1.00" + } + ] + } + }, + { + "areaId": "35", + "areaPrice": "91.00", + "areaServiceFee": "1.00", + "ecardPrices": + { + "item": + [ + { + "ecardLevelNo": "1043", + "ecardLevelPrice": "91.00", + "ecardLevelServiceFee": "1.00" + }, + { + "ecardLevelNo": "1047", + "ecardLevelPrice": "91.00", + "ecardLevelServiceFee": "1.00" + }, + { + "ecardLevelNo": "1062", + "ecardLevelPrice": "91.00", + "ecardLevelServiceFee": "1.00" + }, + { + "ecardLevelNo": "1069", + "ecardLevelPrice": "91.00", + "ecardLevelServiceFee": "1.00" + }, + { + "ecardLevelNo": "1079", + "ecardLevelPrice": "91.00", + "ecardLevelServiceFee": "1.00" + }, + { + "ecardLevelNo": "1080", + "ecardLevelPrice": "89.00", + "ecardLevelServiceFee": "1.00" + } + ] + } + }, + { + "areaId": "36", + "areaPrice": "91.00", + "areaServiceFee": "1.00", + "ecardPrices": + { + "item": + [ + { + "ecardLevelNo": "1043", + "ecardLevelPrice": "91.00", + "ecardLevelServiceFee": "1.00" + }, + { + "ecardLevelNo": "1047", + "ecardLevelPrice": "91.00", + "ecardLevelServiceFee": "1.00" + }, + { + "ecardLevelNo": "1062", + "ecardLevelPrice": "91.00", + "ecardLevelServiceFee": "1.00" + }, + { + "ecardLevelNo": "1069", + "ecardLevelPrice": "91.00", + "ecardLevelServiceFee": "1.00" + }, + { + "ecardLevelNo": "1079", + "ecardLevelPrice": "91.00", + "ecardLevelServiceFee": "1.00" + }, + { + "ecardLevelNo": "1080", + "ecardLevelPrice": "89.00", + "ecardLevelServiceFee": "1.00" + } + ] + } + } + ] + } + }, + { + "id": "7115", + "movieInfo": + { + "item": + { + "cineMovieId": "2620", + "cineMovieNum": "001110012018", + "movieName": "公主的战俘-27.77", + "movieSubtitle": "中文", + "movieLanguage": "国语", + "movieFormat": "数字", + "movieDimensional": "2D", + "movieSize": "普通", + "joinStartTime": null, + "joinEndTime": null + } + }, + "cinePlayId": "260348", + "hallId": "533", + "hallName": "4号厅(冠名招商)", + "businessDate": "2024-08-19", + "startTime": "2024-08-19 19:30:00", + "endTime": "2024-08-19 21:10:00", + "priceType": "1", + "price": "90.00", + "marketPrice": "90.00", + "lowestPrice": "27.77", + "seatTotalNum": "118", + "seatAvailableNum": "88", + "allowBook": "1", + "cineUpdateTime": "2024-08-19 09:27:34", + "partnerPrice": "94.00", + "areaInfo": + { + "item": + [ + { + "areaId": "1", + "areaPrice": "94.00", + "areaServiceFee": "4.00", + "ecardPrices": + { + "item": + [ + { + "ecardLevelNo": "1043", + "ecardLevelPrice": "94.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1047", + "ecardLevelPrice": "94.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1053", + "ecardLevelPrice": "75.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1057", + "ecardLevelPrice": "93.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1062", + "ecardLevelPrice": "94.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1069", + "ecardLevelPrice": "94.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1079", + "ecardLevelPrice": "94.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1080", + "ecardLevelPrice": "91.00", + "ecardLevelServiceFee": "4.00" + } + ] + } + }, + { + "areaId": "25", + "areaPrice": "94.00", + "areaServiceFee": "4.00", + "ecardPrices": + { + "item": + [ + { + "ecardLevelNo": "1043", + "ecardLevelPrice": "94.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1047", + "ecardLevelPrice": "94.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1053", + "ecardLevelPrice": "75.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1057", + "ecardLevelPrice": "93.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1062", + "ecardLevelPrice": "94.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1069", + "ecardLevelPrice": "94.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1079", + "ecardLevelPrice": "94.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1080", + "ecardLevelPrice": "91.00", + "ecardLevelServiceFee": "4.00" + } + ] + } + }, + { + "areaId": "34", + "areaPrice": "94.00", + "areaServiceFee": "4.00", + "ecardPrices": + { + "item": + [ + { + "ecardLevelNo": "1043", + "ecardLevelPrice": "94.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1047", + "ecardLevelPrice": "94.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1053", + "ecardLevelPrice": "75.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1057", + "ecardLevelPrice": "93.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1062", + "ecardLevelPrice": "94.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1069", + "ecardLevelPrice": "94.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1079", + "ecardLevelPrice": "94.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1080", + "ecardLevelPrice": "91.00", + "ecardLevelServiceFee": "4.00" + } + ] + } + }, + { + "areaId": "35", + "areaPrice": "94.00", + "areaServiceFee": "4.00", + "ecardPrices": + { + "item": + [ + { + "ecardLevelNo": "1043", + "ecardLevelPrice": "94.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1047", + "ecardLevelPrice": "94.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1053", + "ecardLevelPrice": "75.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1057", + "ecardLevelPrice": "93.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1062", + "ecardLevelPrice": "94.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1069", + "ecardLevelPrice": "94.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1079", + "ecardLevelPrice": "94.00", + "ecardLevelServiceFee": "4.00" + }, + { + "ecardLevelNo": "1080", + "ecardLevelPrice": "91.00", + "ecardLevelServiceFee": "4.00" + } + ] + } + } + ] + } + }, + { + "id": "7116", + "movieInfo": + { + "item": + { + "cineMovieId": "2615", + "cineMovieNum": "051101162022", + "movieName": "忍者神龟:崛起-20", + "movieSubtitle": "中文", + "movieLanguage": "英语", + "movieFormat": "数字", + "movieDimensional": "2D", + "movieSize": "普通", + "joinStartTime": null, + "joinEndTime": null + } + }, + "cinePlayId": "260349", + "hallId": "534", + "hallName": "5号厅(冠名招商)", + "businessDate": "2024-08-19", + "startTime": "2024-08-19 19:59:00", + "endTime": "2024-08-19 21:21:00", + "priceType": "1", + "price": "50.00", + "marketPrice": "50.00", + "lowestPrice": "20.00", + "seatTotalNum": "118", + "seatAvailableNum": "90", + "allowBook": "1", + "cineUpdateTime": "2024-08-19 09:27:34", + "partnerPrice": "55.00", + "areaInfo": + { + "item": + [ + { + "areaId": "1", + "areaPrice": "55.00", + "areaServiceFee": "5.00", + "ecardPrices": + { + "item": + [ + { + "ecardLevelNo": "1043", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1047", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1053", + "ecardLevelPrice": "44.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1057", + "ecardLevelPrice": "54.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1062", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1069", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1079", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1080", + "ecardLevelPrice": "52.00", + "ecardLevelServiceFee": "5.00" + } + ] + } + }, + { + "areaId": "14", + "areaPrice": "55.00", + "areaServiceFee": "5.00", + "ecardPrices": + { + "item": + [ + { + "ecardLevelNo": "1043", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1047", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1053", + "ecardLevelPrice": "44.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1057", + "ecardLevelPrice": "54.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1062", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1069", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1079", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1080", + "ecardLevelPrice": "52.00", + "ecardLevelServiceFee": "5.00" + } + ] + } + }, + { + "areaId": "15", + "areaPrice": "55.00", + "areaServiceFee": "5.00", + "ecardPrices": + { + "item": + [ + { + "ecardLevelNo": "1043", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1047", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1053", + "ecardLevelPrice": "44.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1057", + "ecardLevelPrice": "54.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1062", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1069", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1079", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1080", + "ecardLevelPrice": "52.00", + "ecardLevelServiceFee": "5.00" + } + ] + } + }, + { + "areaId": "16", + "areaPrice": "55.00", + "areaServiceFee": "5.00", + "ecardPrices": + { + "item": + [ + { + "ecardLevelNo": "1043", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1047", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1053", + "ecardLevelPrice": "44.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1057", + "ecardLevelPrice": "54.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1062", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1069", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1079", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1080", + "ecardLevelPrice": "52.00", + "ecardLevelServiceFee": "5.00" + } + ] + } + }, + { + "areaId": "18", + "areaPrice": "55.00", + "areaServiceFee": "5.00", + "ecardPrices": + { + "item": + [ + { + "ecardLevelNo": "1043", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1047", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1053", + "ecardLevelPrice": "44.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1057", + "ecardLevelPrice": "54.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1062", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1069", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1079", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1080", + "ecardLevelPrice": "52.00", + "ecardLevelServiceFee": "5.00" + } + ] + } + }, + { + "areaId": "19", + "areaPrice": "55.00", + "areaServiceFee": "5.00", + "ecardPrices": + { + "item": + [ + { + "ecardLevelNo": "1043", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1047", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1053", + "ecardLevelPrice": "44.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1057", + "ecardLevelPrice": "54.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1062", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1069", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1079", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1080", + "ecardLevelPrice": "52.00", + "ecardLevelServiceFee": "5.00" + } + ] + } + }, + { + "areaId": "20", + "areaPrice": "55.00", + "areaServiceFee": "5.00", + "ecardPrices": + { + "item": + [ + { + "ecardLevelNo": "1043", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1047", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1053", + "ecardLevelPrice": "44.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1057", + "ecardLevelPrice": "54.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1062", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1069", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1079", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1080", + "ecardLevelPrice": "52.00", + "ecardLevelServiceFee": "5.00" + } + ] + } + }, + { + "areaId": "21", + "areaPrice": "55.00", + "areaServiceFee": "5.00", + "ecardPrices": + { + "item": + [ + { + "ecardLevelNo": "1043", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1047", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1053", + "ecardLevelPrice": "44.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1057", + "ecardLevelPrice": "54.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1062", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1069", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1079", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1080", + "ecardLevelPrice": "52.00", + "ecardLevelServiceFee": "5.00" + } + ] + } + }, + { + "areaId": "22", + "areaPrice": "55.00", + "areaServiceFee": "5.00", + "ecardPrices": + { + "item": + [ + { + "ecardLevelNo": "1043", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1047", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1053", + "ecardLevelPrice": "44.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1057", + "ecardLevelPrice": "54.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1062", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1069", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1079", + "ecardLevelPrice": "55.00", + "ecardLevelServiceFee": "5.00" + }, + { + "ecardLevelNo": "1080", + "ecardLevelPrice": "52.00", + "ecardLevelServiceFee": "5.00" + } + ] + } + } + ] + } + } + ] + } + } +}''' + + new = HandleXmlResp('').format_data(json.loads(j)) + print(json.dumps(new, indent=4)) diff --git a/dspt_api/views.py b/dspt_api/views.py index 8962f7a..320cbf1 100644 --- a/dspt_api/views.py +++ b/dspt_api/views.py @@ -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'}) # 对外接口,用于给前端提供上次请求的数据,如果没有则返回数据库中的默认值