diff --git a/dspt_api/migrations/0011_alter_ecrequestlog_request_and_more.py b/dspt_api/migrations/0011_alter_ecrequestlog_request_and_more.py new file mode 100644 index 0000000..1d45050 --- /dev/null +++ b/dspt_api/migrations/0011_alter_ecrequestlog_request_and_more.py @@ -0,0 +1,23 @@ +# Generated by Django 4.2.7 on 2024-07-29 05:56 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('dspt_api', '0010_rename_desc_ecapiparams_description'), + ] + + operations = [ + migrations.AlterField( + model_name='ecrequestlog', + name='request', + field=models.CharField(max_length=5000, verbose_name='请求链接'), + ), + migrations.AlterField( + model_name='ecrequestlog', + name='response', + field=models.TextField(verbose_name='返回值'), + ), + ] diff --git a/dspt_api/models.py b/dspt_api/models.py index 56677f1..c6b1448 100644 --- a/dspt_api/models.py +++ b/dspt_api/models.py @@ -87,8 +87,8 @@ class EcRequestLog(models.Model): id = models.IntegerField(primary_key=True) ip = models.CharField(verbose_name='请求ip', max_length=20, null=False) # 接口测试人的ip env = models.CharField(verbose_name='环境', max_length=10, null=False) # 电商平台 dspt 自营电商 zyds - request = models.CharField(verbose_name='请求链接', max_length=500, null=False) - response = models.CharField(verbose_name='返回值', max_length=10000, null=False) + request = models.CharField(verbose_name='请求链接', max_length=5000, null=False) + response = models.TextField(verbose_name='返回值', null=False) request_datetime = models.DateTimeField(verbose_name='请求发送时间', null=False) def __str__(self): diff --git a/dspt_api/views.py b/dspt_api/views.py index 9c2990c..48b7d65 100644 --- a/dspt_api/views.py +++ b/dspt_api/views.py @@ -1,3 +1,4 @@ +import datetime import json import requests from urllib.parse import urljoin @@ -11,6 +12,7 @@ from dspt_api.models import EcChannel, EcEnv, EcApi, EcApiParams, EcCinemaIds, E from dspt_api.serializers import EcChannelSerializer, EcEnvSerializer, EcApiSerializer, EcApiParamsSerializer, \ EcCinemaIdsSerializer, EcRequestLogSerializer, EcApiGroupSerializer from django.http.response import JsonResponse +from django.utils import timezone from dspt_api.util.sign import Sign @@ -79,7 +81,7 @@ def handle_request(_request): sign = Sign(key) req_params = sign.add_sig(params) req_obj = prepare_request(base_url, api, req_params) - return req_obj + return req_obj, req_params['_sig'] # 内部方法,用于拼接请求 @@ -125,8 +127,8 @@ def general_api_url(request): api api地址 params 请求参数 """ - req_obj = handle_request(request) - return JsonResponse({'url': req_obj.url}) + req_obj, sig = handle_request(request) + return JsonResponse({'url': req_obj.url, 'sig': sig}) # 对外接口,用于收集用户提交内容后发送请求 @@ -142,31 +144,36 @@ def send_request(request): # 初始化redis redis_conn = get_redis_connection() user_ip = request.META.get('REMOTE_ADDR') - api = request.get('api') + web_req = json.loads(request.body) + api = web_req.get('api') redis_key_api = f'dspt_api_{user_ip}_{api}' redis_key_user_data = f'dspt_api_{user_ip}_{api}_data' # 发送请求 - req = handle_request(request) + req, sig = handle_request(request) response = requests.Session().send(req) + print('response', response) # 记录Redis if redis_conn.exists(redis_key_api): redis_conn.delete(redis_key_api) else: data = { - 'request_url': request.get('request_url'), + 'request_url': req.url, 'response_data': response.json() } + print(data) redis_conn.set(redis_key_api, json.dumps(data), 10 * 60 * 60) redis_conn.delete(redis_key_user_data) # 插入数据 db_data = { 'ip': user_ip, - 'env': request.get('env'), - 'request': request.get('request_url'), - 'response': response.json() + 'env': web_req.get('env'), + 'request': req.url, + 'response': response.json(), + 'request_datetime': timezone.now(), } - EcRequestLog.objects.create(db_data) - return response.json() + print(db_data) + EcRequestLog.objects.create(**db_data) + return JsonResponse(response.json()) # 对外接口,用于收集用户选择的数据,例如场次,卖品等,以便后续接口直接调用