diff --git a/dingxin_toolbox_drf/http.py b/dingxin_toolbox_drf/http.py index 0387d23..dc44040 100644 --- a/dingxin_toolbox_drf/http.py +++ b/dingxin_toolbox_drf/http.py @@ -8,13 +8,13 @@ class SetRemoteAddrFromForwardedFor(MiddlewareMixin): """ def process_request(self, request): - print('process_request') - print("request.META") - print(request.META) + # print('process_request') + # print("request.META") + # print(request.META) try: - print("request.META") + # print("request.META") real_ip = request.META['HTTP_X_FORWARDED_FOR'] - print('real_ip', real_ip) + # print('real_ip', real_ip) except KeyError as err: print('HTTP_X_FORWARDED_FOR key error') print(err) diff --git a/dspt_api/util/api/ecard_refund.py b/dspt_api/util/api/ecard_refund.py index 075da0b..11849dc 100644 --- a/dspt_api/util/api/ecard_refund.py +++ b/dspt_api/util/api/ecard_refund.py @@ -2,6 +2,7 @@ import time from django_redis import get_redis_connection from dspt_api.util.general.get_order_num import general_order_num from dspt_api.util.general.handle_redis import get_data_from_redis +from dspt_api.util.api.ecard_order_detail import ApiEcardOrderDetail class ApiEcardRefund: @@ -9,57 +10,17 @@ class ApiEcardRefund: self.member_type = kwargs.get('member_type') self.api = kwargs.get('api') self.ip = kwargs.get('ip') + self.kwargs = kwargs def get_suggestion(self): + # source_order_no + source_order_no = ApiEcardOrderDetail(**self.kwargs).get_suggestion()[0] # partner_order_no order_num = general_order_num('REF', self.api, self.member_type, self.ip) - # source_order_no - _now = int(time.time() * 1000) - request_renew = {'name': '3.4.3 联名卡续卡', 'path': 'ecard/renew'} - redis_key_renew_order_num = f'dspt_api_{self.ip}_{self.member_type}_{request_renew["path"]}_order_num' - redis_key_renew = f'dspt_api_{self.ip}_{self.member_type}_{request_renew["path"]}' - - request_regist = {'name': '3.4.2 联名卡开卡', 'path': 'ecard/regist'} - redis_key_regist_order_num = f'dspt_api_{self.ip}_{self.member_type}_{request_regist["path"]}_order_num' - redis_key_regist = f'dspt_api_{self.ip}_{self.member_type}_{request_regist["path"]}' - - result_renew, _format__renew, redis_data_renew, _timestamp_renew = get_data_from_redis(redis_key_renew) - result_regist, _format_regist, redis_data_regist, _timestamp_regist = get_data_from_redis(redis_key_regist) - - redis_conn = get_redis_connection() - order_num_renew = redis_conn.get(redis_key_renew_order_num) - print('order_num_renew', order_num_renew) - order_num_regist = redis_conn.get(redis_key_regist_order_num) - print('order_num_regist', order_num_regist) - - order_num_result = [ - {'param': 'source_order_no', 'value': '请手动输入参数!', 'is_checked': True, 'result': True, - 'timestamp': _now}, - {'param': 'partner_order_no', 'value': order_num, 'is_checked': True, 'result': True, - 'timestamp': int(time.time() * 1000)}] - - if order_num_regist: - order_num_result_regist = [ - {'param': 'source_order_no', 'value': str(order_num_regist, encoding='utf-8'), 'is_checked': True, - 'result': True, 'timestamp': _now}, - {'param': 'partner_order_no', 'value': order_num, 'is_checked': True, 'result': True, - 'timestamp': int(time.time() * 1000)}] - if order_num_renew: - order_num_result_renew = [ - {'param': 'source_order_no', 'value': str(order_num_renew, encoding='utf-8'), 'is_checked': True, - 'result': True, - 'timestamp': _now}, + return [{'param': 'source_order_no', 'value': source_order_no['value'], 'is_checked': True, 'result': True, + 'timestamp': int(time.time() * 1000)}, {'param': 'partner_order_no', 'value': order_num, 'is_checked': True, 'result': True, 'timestamp': int(time.time() * 1000)}] - if order_num_renew: - order_num_result = order_num_result_renew - if order_num_regist: - order_num_result = order_num_result_regist - if order_num_renew and order_num_regist: - order_num_result = order_num_result_regist if _timestamp_regist > _timestamp_renew else order_num_result_renew - - return order_num_result - def get_timestamp(self): return int(time.time() * 1000) diff --git a/dspt_api/util/api/ecard_seat_price.py b/dspt_api/util/api/ecard_seat_price.py new file mode 100644 index 0000000..d836b17 --- /dev/null +++ b/dspt_api/util/api/ecard_seat_price.py @@ -0,0 +1,26 @@ +from dspt_api.util.general.handle_redis import get_data_from_redis +from dspt_api.util.api.seat_lock import ApiSeatLock +from dspt_api.util.api.ecard_detail import ApiEcardDetail +import random + + +class ApiEcardSeatPrice: + def __init__(self, **kwargs): + self.member_type = kwargs.get('member_type') + self.api = kwargs.get('api') + self.ip = kwargs.get('ip') + self.kwargs = kwargs + + def get_suggestion(self): + # 获取 play_id seat_idplay_update_time + params = ApiSeatLock(**self.kwargs).get_suggestion() + # 获取 ecard_number + ecard_number = ApiEcardDetail(**self.kwargs).get_suggestion() + return params + ecard_number + + def get_timestamp(self): + # 获取 play_id seat_idplay_update_time + params_ts = ApiSeatLock(**self.kwargs).get_timestamp() + # 获取 ecard_number + ecard_number_ts = ApiEcardDetail(**self.kwargs).get_timestamp() + return params_ts if params_ts > ecard_number_ts else ecard_number_ts diff --git a/dspt_api/util/api/order_buy_goods.py b/dspt_api/util/api/order_buy_goods.py new file mode 100644 index 0000000..89c4b84 --- /dev/null +++ b/dspt_api/util/api/order_buy_goods.py @@ -0,0 +1,61 @@ +from dspt_api.util.general.handle_redis import get_data_from_redis +from dspt_api.util.general.get_order_num import general_order_num +from dspt_api.util.handle_goods import general_goods_param +import time +import random +import json + + +class ApiOrderBuyGoods: + 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): + 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) + if result: + goods_data = random.choice(redis_data) + goods_info, cash = general_goods_param(goods_data) + print('ApiOrderBuyGoods', goods_info) + order_num = general_order_num('GS', self.api, self.member_type, self.ip) + delivery_appoint_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + 5 * 60)) + return [ + {'param': 'partner_buy_ticket_id', 'value': order_num, 'is_checked': True, 'result': True, + 'timestamp': _timestamp}, + {'param': 'goods', 'value': json.dumps(goods_info), 'is_checked': True, 'result': True, 'timestamp': _timestamp}, + {'param': 'cash', 'value': cash, 'is_checked': True, 'result': True, 'timestamp': _timestamp}, + {'param': 'mobile', 'value': '18688886666', 'is_checked': True, 'result': True, + 'timestamp': _timestamp}, + {'param': 'delivery_type', 'value': '2', 'is_checked': False, 'result': True, 'timestamp': _timestamp}, + {'param': 'delivery_location', 'value': '1号厅', 'is_checked': False, 'result': True, + 'timestamp': _timestamp}, + {'param': 'delivery_appoint_time', 'value': delivery_appoint_time, 'is_checked': False, 'result': True, + 'timestamp': _timestamp}, + {'param': 'contact_number', 'value': '18688886666', 'is_checked': False, 'result': True, + 'timestamp': _timestamp}] + else: + # 返回推荐参数应该包含参数名,参数值,和是否勾选的状态 + # redis_data + request_api["name"] + return [ + {'param': 'partner_buy_ticket_id', 'value': redis_data + request_api["name"], 'is_checked': True, + 'result': True, + 'timestamp': _timestamp}, + {'param': 'goods', 'value': redis_data + request_api["name"], 'is_checked': True, 'result': True, + 'timestamp': _timestamp}, + {'param': 'cash', 'value': '0', 'is_checked': True, 'result': True, 'timestamp': _timestamp}, + {'param': 'mobile', 'value': '18688886666', 'is_checked': True, 'result': True, + 'timestamp': _timestamp}, + {'param': 'delivery_type', 'value': '2', 'is_checked': False, 'result': True, 'timestamp': _timestamp}, + {'param': 'delivery_location', 'value': '1号厅', 'is_checked': False, 'result': True, + 'timestamp': _timestamp}, + {'param': 'delivery_appoint_time', 'value': '0000-00-00 00:00:00', 'is_checked': False, 'result': True, + 'timestamp': _timestamp}, + {'param': 'contact_number', 'value': '18688886666', 'is_checked': False, 'result': True, + 'timestamp': _timestamp} + ] + + def get_timestamp(self): + return int(time.time()*1000) diff --git a/dspt_api/util/handle_goods.py b/dspt_api/util/handle_goods.py index 6ed7d45..29d75de 100644 --- a/dspt_api/util/handle_goods.py +++ b/dspt_api/util/handle_goods.py @@ -1,6 +1,50 @@ +import json +import random + +from dspt_api.util.random_params import random_params + + +# 将接口请求的数据中卖品信息整理成列表结构 def format_goods(_data): goods_list = [] for cate in _data['res']['data']: for g in cate['goods']: goods_list.append(g) return {'res': {'status': _data['res']['status'], 'data': goods_list}} + + +# 将接口获取的卖品数据转化成请求参数格式 +def general_goods_param(_data): + params = [] + total_cash = 0 + for g in _data: + _num = g.get('buy_num', 1) + info = { + 'id': g['id'], + 'type': g['type'], + 'price': round(float(g.get('partnerPrice', g['onlinePrice'])) * _num, 2), + 'num': _num + } + if g['type'] == 'package' and g['packageType'] == '2': + add_price = 0 + optional_package = [] + for key, val in g['user_select'].items(): + member = [] + member_id = [] + for m in val['data']: + if m['id'] in member_id: + for item in member: + if item['id'] == m['id']: + item['num'] = item['num'] + 1 + add_price += round(float(m['addPrice']), 2) + else: + member.append({'id': m['id'], 'num': 1, 'add_price': m['addPrice']}) + member_id.append(m['id']) + add_price += round(float(m['addPrice']), 2) + optional_package.append({'index': key, 'members': member}) + info['optional_package'] = optional_package + info['price'] = round(info['price'] + add_price, 2) + total_cash = round(total_cash + info['price'] * _num, 2) + params.append(info) + print('general_goods_param---------params', params) + return params, total_cash diff --git a/dspt_api/util/random_params.py b/dspt_api/util/random_params.py index 707a400..81a95a6 100644 --- a/dspt_api/util/random_params.py +++ b/dspt_api/util/random_params.py @@ -1,14 +1,15 @@ import random import json import time - from django_redis import get_redis_connection RANDOM_DATA_API_LIST = ['cinema/plays', 'play/seat-status', 'cinema/goods', 'ecard/ecard-levels'] + def random_params(_user_info, _handle_data): if _user_info["api"] in RANDOM_DATA_API_LIST: - + if str(_handle_data['res']['status']) == '0': + return False 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): @@ -17,16 +18,19 @@ def random_params(_user_info, _handle_data): 'api': _user_info["api"], 'member_type': _user_info["member_type"], 'format': _user_info["format"], - 'timestamp': int(time.time()*1000), + 'timestamp': int(time.time() * 1000), } if _user_info["api"] == 'play/seat-status': _user_data = get_ok_status_seat_list(_handle_data['res']['data']) if _user_info["api"] == 'ecard/ecard-levels': _user_data = _handle_data['res']['data']['ecardLevelData'] + if _user_info["api"] == 'cinema/goods': + print('random_params', _handle_data['res']['data']) + _user_data = [random_goods(_handle_data['res']['data'])] else: _user_data = _handle_data['res']['data'] if len(_user_data) > 0: - if _user_info["api"] == 'play/seat-status': + if _user_info["api"] in ('play/seat-status', 'cinema/goods'): data['user_data'] = [[random.choice(_user_data)]] else: data['user_data'] = [random.choice(_user_data)] @@ -41,3 +45,12 @@ def get_ok_status_seat_list(_data): if s['seatStatus'] == 'ok' and s['type'] in ['danren', 'zhendong', 'vip']: seat_list.append(s) return seat_list + + +def random_goods(_data): + goods = random.choice(_data) + n = 1 + while goods['type'] == 'package' and goods['packageType'] == '2' and n < 10: + goods = random.choice(_data) + n += 1 + return goods \ No newline at end of file diff --git a/dspt_api/util/suggest_params.py b/dspt_api/util/suggest_params.py index fcc728a..2a39c80 100644 --- a/dspt_api/util/suggest_params.py +++ b/dspt_api/util/suggest_params.py @@ -9,8 +9,10 @@ from dspt_api.util.api.play_period_changed_seats import ApiPlayPeriodChangedSeat from dspt_api.util.api.play_seat_overview import ApiPlaySeatOverview from dspt_api.util.api.cinema_goods_pic import ApiCinemaGoodsPic from dspt_api.util.api.seat_lock import ApiSeatLock +from dspt_api.util.api.order_buy_goods import ApiOrderBuyGoods from dspt_api.util.api.ecard_detail import ApiEcardDetail from dspt_api.util.api.ecard_order_detail import ApiEcardOrderDetail +from dspt_api.util.api.ecard_seat_price import ApiEcardSeatPrice # 通过api来匹配不同的接口文件获取推荐 @@ -43,6 +45,9 @@ def suggest_params(member_type, api, ip): if api == 'seat/lock' and member_type == 'nonmember': print('seat/lock') params = ApiSeatLock(**data).get_suggestion() + if api == 'order/buy-goods' and member_type == 'nonmember': + print('order/buy-goods') + params = ApiOrderBuyGoods(**data).get_suggestion() if api == 'ecard/regist' and member_type == 'nonmember': print('ecard/regist') params = ApiEcardRegist(**data).get_suggestion() @@ -55,6 +60,9 @@ def suggest_params(member_type, api, ip): if api == 'ecard/order-detail' and member_type == 'nonmember': print('ecard/order-detail') params = ApiEcardOrderDetail(**data).get_suggestion() + if api == 'ecard/seat-price' and member_type == 'nonmember': + print('ecard/seat-price') + params = ApiEcardSeatPrice(**data).get_suggestion() if api == 'ecard/refund' and member_type == 'nonmember': print('ecard/refund') params = ApiEcardRefund(**data).get_suggestion() @@ -88,6 +96,9 @@ def suggest_params_timestamp(member_type, api, ip): if api == 'seat/lock' and member_type == 'nonmember': print('seat/lock') _timestamps = ApiSeatLock(**data).get_timestamp() + if api == 'order/buy-goods' and member_type == 'nonmember': + print('order/buy-goods') + _timestamps = ApiOrderBuyGoods(**data).get_timestamp() if api == 'ecard/regist' and member_type == 'nonmember': print('ecard/regist') _timestamps = ApiEcardRegist(**data).get_timestamp() @@ -100,6 +111,9 @@ def suggest_params_timestamp(member_type, api, ip): if api == 'ecard/order-detail' and member_type == 'nonmember': print('ecard/order-detail') _timestamps = ApiEcardOrderDetail(**data).get_timestamp() + if api == 'ecard/seat-price' and member_type == 'nonmember': + print('ecard/seat-price') + _timestamps = ApiEcardSeatPrice(**data).get_timestamp() if api == 'ecard/refund' and member_type == 'nonmember': print('ecard/refund') _timestamps = ApiEcardRefund(**data).get_timestamp() diff --git a/dspt_api/views.py b/dspt_api/views.py index dbcd5c5..45ddc30 100644 --- a/dspt_api/views.py +++ b/dspt_api/views.py @@ -4,6 +4,7 @@ import re import time import requests +import urllib import xml.etree.ElementTree from urllib.parse import urljoin from django.db.models import Q @@ -172,7 +173,7 @@ def general_api_url(request): # resp_format = params['format'] req_obj, sig = handle_request(request) failed_resp_data = {'url': '请检查foramt、pid参数', 'sig': ''} - result = {'url': req_obj.url, 'sig': sig} + result = {'url': urllib.parse.unquote(req_obj.url), 'sig': sig} if req_obj is False: return JsonResponse(failed_resp_data) return JsonResponse(result)