修复生成请求的方法

main
RogerWork 4 months ago
parent 371909c9bf
commit 456a4af8d5
  1. 6
      dingxin_toolbox_drf/settings.py
  2. 22
      dspt_api/migrations/0006_rename_cinemaids_eccinemaids_ecapigroup_type.py
  3. 22
      dspt_api/migrations/0007_remove_ecapigroup_order_alter_ecapigroup_api_id.py
  4. 18
      dspt_api/migrations/0008_rename_desc_ecapi_comment.py
  5. 18
      dspt_api/migrations/0009_rename_comment_ecapi_description.py
  6. 18
      dspt_api/migrations/0010_rename_desc_ecapiparams_description.py
  7. 10
      dspt_api/models.py
  8. 1
      dspt_api/urls.py
  9. 41
      dspt_api/views.py

@ -27,8 +27,8 @@ print(BASE_DIR)
# # SERVER_IP = '' # # SERVER_IP = ''
CONFIG = { CONFIG = {
'dev': { 'dev': {
'SERVER_IP': '172.16.1.114', 'SERVER_IP': '172.16.1.168',
'WEB_SERVER_IP': '172.16.1.114', 'WEB_SERVER_IP': '172.16.1.168',
'DB': { 'DB': {
"HOST": 'home.rogersun.cn', "HOST": 'home.rogersun.cn',
"PORT": 3306, "PORT": 3306,
@ -36,7 +36,7 @@ CONFIG = {
"PASSWORD": "cine123456", "PASSWORD": "cine123456",
"NAME": "dingxin_toolbox", "NAME": "dingxin_toolbox",
}, },
'REDIS_IP': '172.16.1.114' 'REDIS_IP': '172.16.1.168'
}, },
'prd': { 'prd': {
'SERVER_IP': '172.16.1.63', 'SERVER_IP': '172.16.1.63',

@ -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='会员非会员'),
),
]

@ -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'),
),
]

@ -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',
),
]

@ -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',
),
]

@ -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',
),
]

@ -36,12 +36,12 @@ class EcEnv(models.Model):
class EcApi(models.Model): class EcApi(models.Model):
id = models.IntegerField(primary_key=True) 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) path = models.CharField(verbose_name='接口地址', max_length=50, null=False)
type = models.CharField(verbose_name='会员非会员', max_length=20, default='', null=False) # 会员 member 非会员 nonmember type = models.CharField(verbose_name='会员非会员', max_length=20, default='', null=False) # 会员 member 非会员 nonmember
def __str__(self): def __str__(self):
return self.desc return self.description
class Meta: class Meta:
verbose_name = '接口' verbose_name = '接口'
@ -53,7 +53,7 @@ class EcApiParams(models.Model):
id = models.IntegerField(primary_key=True) id = models.IntegerField(primary_key=True)
api_id = models.IntegerField(verbose_name='接口id', null=False) api_id = models.IntegerField(verbose_name='接口id', null=False)
param = models.CharField(verbose_name='参数', max_length=50, 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) value = models.CharField(verbose_name='参数值模版', max_length=500, null=True, blank=True)
is_checked = models.BooleanField(verbose_name='是否默认勾选', null=False, default=False) is_checked = models.BooleanField(verbose_name='是否默认勾选', null=False, default=False)
is_request = 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) id = models.IntegerField(primary_key=True)
group_id = models.CharField(verbose_name='接口分组id', max_length=20, null=False) group_id = models.CharField(verbose_name='接口分组id', max_length=20, null=False)
name = 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.CharField(verbose_name='接口id', max_length=2000, null=False)
api_id = models.IntegerField(verbose_name='接口id', null=False) type = models.CharField(verbose_name='会员非会员', max_length=20, default='', null=False) # 会员 member 非会员 nonmember
def __str__(self): def __str__(self):
return self.name return self.name

@ -9,6 +9,7 @@ urlpatterns = [
path('get_cinema_ids', views.EcCinemaIdsViewSet.as_view({'get': 'list'}), name='get_ec_cinema_ids'), 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_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_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('get_url', views.general_api_url),
path('send_request', views.send_request), path('send_request', views.send_request),
] ]

@ -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.models import EcChannel, EcEnv, EcApi, EcApiParams, EcCinemaIds, EcRequestLog, EcApiGroup
from dspt_api.serializers import EcChannelSerializer, EcEnvSerializer, EcApiSerializer, EcApiParamsSerializer, \ from dspt_api.serializers import EcChannelSerializer, EcEnvSerializer, EcApiSerializer, EcApiParamsSerializer, \
EcCinemaIdsSerializer, EcRequestLogSerializer, EcApiGroupSerializer EcCinemaIdsSerializer, EcRequestLogSerializer, EcApiGroupSerializer
from django.http.response import JsonResponse
from dspt_api.util.sign import Sign from dspt_api.util.sign import Sign
@ -28,12 +28,15 @@ class EcEnvViewSet(viewsets.ModelViewSet):
class EcApiViewSet(viewsets.ModelViewSet): class EcApiViewSet(viewsets.ModelViewSet):
queryset = EcApi.objects.all() queryset = EcApi.objects.all()
serializer_class = EcApiSerializer serializer_class = EcApiSerializer
filter_backends = (DjangoFilterBackend,)
# http://172.16.1.114:8000/ec/get_api?type=nonmember
filterset_fields = ('type',)
class EcApiParamsViewSet(viewsets.ModelViewSet): class EcApiParamsViewSet(viewsets.ModelViewSet):
queryset = EcApiParams.objects.all() queryset = EcApiParams.objects.all()
serializer_class = EcApiParamsSerializer serializer_class = EcApiParamsSerializer
filter_backends = (filters.SearchFilter, DjangoFilterBackend) filter_backends = (DjangoFilterBackend,)
# http://172.16.1.114:8000/ec/get_api_params?api_id=1 # http://172.16.1.114:8000/ec/get_api_params?api_id=1
filterset_fields = ('api_id',) filterset_fields = ('api_id',)
@ -51,6 +54,9 @@ class EcRequestLogViewSet(viewsets.ModelViewSet):
class EcApiGroupViewSet(viewsets.ModelViewSet): class EcApiGroupViewSet(viewsets.ModelViewSet):
queryset = EcApiGroup.objects.all() queryset = EcApiGroup.objects.all()
serializer_class = EcApiGroupSerializer serializer_class = EcApiGroupSerializer
filter_backends = (DjangoFilterBackend,)
# http://172.16.1.114:8000/ec/get_api_group?type=nonmember
filterset_fields = ('type',)
# 内部方法,用于生成requests请求对象 # 内部方法,用于生成requests请求对象
@ -62,14 +68,14 @@ def handle_request(_request):
api api地址 api api地址
params 请求参数 params 请求参数
""" """
req = _request.POST req = json.loads(_request.body)
env = req.get('env') env = req.get('env')
member_type = req.get('member_type') 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') api = req.get('api')
params = json.loads(req.get('params')) 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') 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) sign = Sign(key)
req_params = sign.add_sig(params) req_params = sign.add_sig(params)
req_obj = prepare_request(base_url, api, req_params) req_obj = prepare_request(base_url, api, req_params)
@ -85,6 +91,29 @@ def prepare_request(_url, _api, _params):
return req 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并返回前端 # 外部接口, 用于实时生成url并返回前端
@csrf_exempt @csrf_exempt
def general_api_url(request): def general_api_url(request):
@ -97,7 +126,7 @@ def general_api_url(request):
params 请求参数 params 请求参数
""" """
req_obj = handle_request(request) req_obj = handle_request(request)
return req_obj.url return JsonResponse({'url': req_obj.url})
# 对外接口,用于收集用户提交内容后发送请求 # 对外接口,用于收集用户提交内容后发送请求

Loading…
Cancel
Save