From 456a4af8d55917debc5e499b76c6b67c67263e31 Mon Sep 17 00:00:00 2001 From: RogerWork Date: Thu, 25 Jul 2024 16:35:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=94=9F=E6=88=90=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dingxin_toolbox_drf/settings.py | 6 +-- ...e_cinemaids_eccinemaids_ecapigroup_type.py | 22 ++++++++++ ...capigroup_order_alter_ecapigroup_api_id.py | 22 ++++++++++ .../0008_rename_desc_ecapi_comment.py | 18 ++++++++ .../0009_rename_comment_ecapi_description.py | 18 ++++++++ ...010_rename_desc_ecapiparams_description.py | 18 ++++++++ dspt_api/models.py | 10 ++--- dspt_api/urls.py | 1 + dspt_api/views.py | 41 ++++++++++++++++--- 9 files changed, 142 insertions(+), 14 deletions(-) create mode 100644 dspt_api/migrations/0006_rename_cinemaids_eccinemaids_ecapigroup_type.py create mode 100644 dspt_api/migrations/0007_remove_ecapigroup_order_alter_ecapigroup_api_id.py create mode 100644 dspt_api/migrations/0008_rename_desc_ecapi_comment.py create mode 100644 dspt_api/migrations/0009_rename_comment_ecapi_description.py create mode 100644 dspt_api/migrations/0010_rename_desc_ecapiparams_description.py diff --git a/dingxin_toolbox_drf/settings.py b/dingxin_toolbox_drf/settings.py index 6ddb6e9..4b35bdb 100644 --- a/dingxin_toolbox_drf/settings.py +++ b/dingxin_toolbox_drf/settings.py @@ -27,8 +27,8 @@ print(BASE_DIR) # # SERVER_IP = '' CONFIG = { 'dev': { - 'SERVER_IP': '172.16.1.114', - 'WEB_SERVER_IP': '172.16.1.114', + 'SERVER_IP': '172.16.1.168', + 'WEB_SERVER_IP': '172.16.1.168', 'DB': { "HOST": 'home.rogersun.cn', "PORT": 3306, @@ -36,7 +36,7 @@ CONFIG = { "PASSWORD": "cine123456", "NAME": "dingxin_toolbox", }, - 'REDIS_IP': '172.16.1.114' + 'REDIS_IP': '172.16.1.168' }, 'prd': { 'SERVER_IP': '172.16.1.63', diff --git a/dspt_api/migrations/0006_rename_cinemaids_eccinemaids_ecapigroup_type.py b/dspt_api/migrations/0006_rename_cinemaids_eccinemaids_ecapigroup_type.py new file mode 100644 index 0000000..17de51b --- /dev/null +++ b/dspt_api/migrations/0006_rename_cinemaids_eccinemaids_ecapigroup_type.py @@ -0,0 +1,22 @@ +# Generated by Django 4.2.7 on 2024-06-07 03:15 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('dspt_api', '0005_alter_ecapiparams_value'), + ] + + operations = [ + migrations.RenameModel( + old_name='CinemaIds', + new_name='EcCinemaIds', + ), + migrations.AddField( + model_name='ecapigroup', + name='type', + field=models.CharField(default='', max_length=20, verbose_name='会员非会员'), + ), + ] diff --git a/dspt_api/migrations/0007_remove_ecapigroup_order_alter_ecapigroup_api_id.py b/dspt_api/migrations/0007_remove_ecapigroup_order_alter_ecapigroup_api_id.py new file mode 100644 index 0000000..2faa7ea --- /dev/null +++ b/dspt_api/migrations/0007_remove_ecapigroup_order_alter_ecapigroup_api_id.py @@ -0,0 +1,22 @@ +# Generated by Django 4.2.7 on 2024-06-07 03:18 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('dspt_api', '0006_rename_cinemaids_eccinemaids_ecapigroup_type'), + ] + + operations = [ + migrations.RemoveField( + model_name='ecapigroup', + name='order', + ), + migrations.AlterField( + model_name='ecapigroup', + name='api_id', + field=models.CharField(max_length=2000, verbose_name='接口id'), + ), + ] diff --git a/dspt_api/migrations/0008_rename_desc_ecapi_comment.py b/dspt_api/migrations/0008_rename_desc_ecapi_comment.py new file mode 100644 index 0000000..83e8c11 --- /dev/null +++ b/dspt_api/migrations/0008_rename_desc_ecapi_comment.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.7 on 2024-07-19 02:23 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('dspt_api', '0007_remove_ecapigroup_order_alter_ecapigroup_api_id'), + ] + + operations = [ + migrations.RenameField( + model_name='ecapi', + old_name='desc', + new_name='comment', + ), + ] diff --git a/dspt_api/migrations/0009_rename_comment_ecapi_description.py b/dspt_api/migrations/0009_rename_comment_ecapi_description.py new file mode 100644 index 0000000..5655c4c --- /dev/null +++ b/dspt_api/migrations/0009_rename_comment_ecapi_description.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.7 on 2024-07-19 02:24 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('dspt_api', '0008_rename_desc_ecapi_comment'), + ] + + operations = [ + migrations.RenameField( + model_name='ecapi', + old_name='comment', + new_name='description', + ), + ] diff --git a/dspt_api/migrations/0010_rename_desc_ecapiparams_description.py b/dspt_api/migrations/0010_rename_desc_ecapiparams_description.py new file mode 100644 index 0000000..7905828 --- /dev/null +++ b/dspt_api/migrations/0010_rename_desc_ecapiparams_description.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.7 on 2024-07-19 02:25 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('dspt_api', '0009_rename_comment_ecapi_description'), + ] + + operations = [ + migrations.RenameField( + model_name='ecapiparams', + old_name='desc', + new_name='description', + ), + ] diff --git a/dspt_api/models.py b/dspt_api/models.py index 04e66df..56677f1 100644 --- a/dspt_api/models.py +++ b/dspt_api/models.py @@ -36,12 +36,12 @@ class EcEnv(models.Model): class EcApi(models.Model): id = models.IntegerField(primary_key=True) - desc = models.CharField(verbose_name='接口描述', max_length=20, null=False) + description = models.CharField(verbose_name='接口描述', max_length=20, null=False) path = models.CharField(verbose_name='接口地址', max_length=50, null=False) type = models.CharField(verbose_name='会员非会员', max_length=20, default='', null=False) # 会员 member 非会员 nonmember def __str__(self): - return self.desc + return self.description class Meta: verbose_name = '接口' @@ -53,7 +53,7 @@ class EcApiParams(models.Model): id = models.IntegerField(primary_key=True) api_id = models.IntegerField(verbose_name='接口id', null=False) param = models.CharField(verbose_name='参数', max_length=50, null=False) - desc = models.CharField(verbose_name='参数描述', max_length=500, null=False) + description = models.CharField(verbose_name='参数描述', max_length=500, null=False) value = models.CharField(verbose_name='参数值模版', max_length=500, null=True, blank=True) is_checked = models.BooleanField(verbose_name='是否默认勾选', null=False, default=False) is_request = models.BooleanField(verbose_name='是否是必选项', null=False, default=False) @@ -104,8 +104,8 @@ class EcApiGroup(models.Model): id = models.IntegerField(primary_key=True) group_id = models.CharField(verbose_name='接口分组id', max_length=20, null=False) name = models.CharField(verbose_name='接口分组id', max_length=20, null=False) - order = models.IntegerField(verbose_name='子接口顺序', null=False) - api_id = models.IntegerField(verbose_name='接口id', null=False) + api_id = models.CharField(verbose_name='接口id', max_length=2000, null=False) + type = models.CharField(verbose_name='会员非会员', max_length=20, default='', null=False) # 会员 member 非会员 nonmember def __str__(self): return self.name diff --git a/dspt_api/urls.py b/dspt_api/urls.py index 5a4b87e..a6516fa 100644 --- a/dspt_api/urls.py +++ b/dspt_api/urls.py @@ -9,6 +9,7 @@ urlpatterns = [ path('get_cinema_ids', views.EcCinemaIdsViewSet.as_view({'get': 'list'}), name='get_ec_cinema_ids'), path('get_request_log', views.EcRequestLogViewSet.as_view({'get': 'list'}), name='get_ec_request_log'), path('get_api_group', views.EcApiGroupViewSet.as_view({'get': 'list'}), name='get_ec_api_group'), + path('get_params_by_type', views.get_api_params_by_api_type), path('get_url', views.general_api_url), path('send_request', views.send_request), ] diff --git a/dspt_api/views.py b/dspt_api/views.py index 555cbd9..9c2990c 100644 --- a/dspt_api/views.py +++ b/dspt_api/views.py @@ -10,7 +10,7 @@ from django.views.decorators.csrf import csrf_exempt from dspt_api.models import EcChannel, EcEnv, EcApi, EcApiParams, EcCinemaIds, EcRequestLog, EcApiGroup from dspt_api.serializers import EcChannelSerializer, EcEnvSerializer, EcApiSerializer, EcApiParamsSerializer, \ EcCinemaIdsSerializer, EcRequestLogSerializer, EcApiGroupSerializer - +from django.http.response import JsonResponse from dspt_api.util.sign import Sign @@ -28,12 +28,15 @@ class EcEnvViewSet(viewsets.ModelViewSet): class EcApiViewSet(viewsets.ModelViewSet): queryset = EcApi.objects.all() serializer_class = EcApiSerializer + filter_backends = (DjangoFilterBackend,) + # http://172.16.1.114:8000/ec/get_api?type=nonmember + filterset_fields = ('type',) class EcApiParamsViewSet(viewsets.ModelViewSet): queryset = EcApiParams.objects.all() serializer_class = EcApiParamsSerializer - filter_backends = (filters.SearchFilter, DjangoFilterBackend) + filter_backends = (DjangoFilterBackend,) # http://172.16.1.114:8000/ec/get_api_params?api_id=1 filterset_fields = ('api_id',) @@ -51,6 +54,9 @@ class EcRequestLogViewSet(viewsets.ModelViewSet): class EcApiGroupViewSet(viewsets.ModelViewSet): queryset = EcApiGroup.objects.all() serializer_class = EcApiGroupSerializer + filter_backends = (DjangoFilterBackend,) + # http://172.16.1.114:8000/ec/get_api_group?type=nonmember + filterset_fields = ('type',) # 内部方法,用于生成requests请求对象 @@ -62,14 +68,14 @@ def handle_request(_request): api api地址 params 请求参数 """ - req = _request.POST + req = json.loads(_request.body) env = req.get('env') member_type = req.get('member_type') - base_url = EcEnv.objects.filter(Q(code=env) & Q(type=member_type)).first().get('host') api = req.get('api') params = json.loads(req.get('params')) + base_url = EcEnv.objects.filter(Q(code=env) & Q(type=member_type)).values('host').first()['host'] pid = params.get('pid') - key = EcChannel.objects.filter(Q(pid=pid) & Q(env=env)).first().get('channel_key') + key = EcChannel.objects.filter(Q(pid=pid) & Q(env=env)).values('channel_key').first()['channel_key'] sign = Sign(key) req_params = sign.add_sig(params) req_obj = prepare_request(base_url, api, req_params) @@ -85,6 +91,29 @@ def prepare_request(_url, _api, _params): return req +@csrf_exempt +def get_api_params_by_api_type(request): + """ + 对比接口用于获取指定类型的接口参数 + http://172.16.1.168:8000/ec/get_params_by_type?type=nonmember + """ + # 通过Api model获取会员或非会员的api id + _type = request.GET.get('type') + api_list = EcApi.objects.filter(type=_type).values('id') + api_id = [api['id'] for api in api_list] + # 通过 api id 查询对应api的参数 + api_params = EcApiParams.objects.filter(api_id__in=api_id) + params_data = EcApiParamsSerializer(api_params, many=True).data + # 组织数据结构,根据api id分组 + api_params_dict = {} + for params in params_data: + if params['api_id'] in api_params_dict.keys(): + api_params_dict[params['api_id']].append(params) + else: + api_params_dict[params['api_id']] = [params] + return JsonResponse(api_params_dict, json_dumps_params={'ensure_ascii': False}) + + # 外部接口, 用于实时生成url并返回前端 @csrf_exempt def general_api_url(request): @@ -97,7 +126,7 @@ def general_api_url(request): params 请求参数 """ req_obj = handle_request(request) - return req_obj.url + return JsonResponse({'url': req_obj.url}) # 对外接口,用于收集用户提交内容后发送请求