268 lines
11 KiB
Python
268 lines
11 KiB
Python
# -*- coding: utf-8 -*-
|
||
import sys
|
||
import requests
|
||
import json
|
||
|
||
sys.stdout.reconfigure(encoding='utf-8')
|
||
|
||
# 配置
|
||
TOKEN = "afxp_fa4137GfIzJtlOCeQHjpCXc83SFWmeAOrZnq"
|
||
PROJECT_ID = "6037107"
|
||
FOLDER_ID = "78121195" # 流量采购管理目录ID
|
||
BASE_URL = "https://api.apifox.com/api/v1"
|
||
|
||
headers = {
|
||
"X-Apifox-Api-Version": "2024-03-28",
|
||
"Authorization": f"Bearer {TOKEN}",
|
||
"Content-Type": "application/json; charset=utf-8"
|
||
}
|
||
|
||
# 流量采购接口列表
|
||
apis = [
|
||
{
|
||
"name": "获取流量套餐列表",
|
||
"method": "GET",
|
||
"path": "/v2/store/flow-packages",
|
||
"folderId": FOLDER_ID,
|
||
"description": "获取所有可购买的流量套餐列表\n\n**功能特性**:\n- 只返回启用状态的套餐\n- 按排序字段排序\n- 包含套餐价格、月流量、时长等信息",
|
||
"tags": ["流量采购"],
|
||
"responses": [{
|
||
"code": 200,
|
||
"contentType": "application/json",
|
||
"jsonSchema": {
|
||
"type": "object",
|
||
"properties": {
|
||
"code": {"type": "integer"},
|
||
"msg": {"type": "string"},
|
||
"data": {
|
||
"type": "array",
|
||
"items": {
|
||
"type": "object",
|
||
"properties": {
|
||
"id": {"type": "integer", "description": "套餐ID"},
|
||
"name": {"type": "string", "description": "套餐名称"},
|
||
"tag": {"type": "string", "description": "套餐标签"},
|
||
"originalPrice": {"type": "number", "description": "原价"},
|
||
"price": {"type": "number", "description": "售价"},
|
||
"monthlyFlow": {"type": "integer", "description": "每月流量(人/月)"},
|
||
"duration": {"type": "integer", "description": "套餐时长(月)"},
|
||
"discount": {"type": "string", "description": "折扣"},
|
||
"totalFlow": {"type": "integer", "description": "总流量(人)"},
|
||
"privileges": {"type": "array", "description": "套餐特权"}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}]
|
||
},
|
||
{
|
||
"name": "获取流量套餐详情",
|
||
"method": "GET",
|
||
"path": "/v2/store/flow-packages/{id}",
|
||
"folderId": FOLDER_ID,
|
||
"description": "获取指定流量套餐的详细信息\n\n**功能特性**:\n- 包含套餐完整信息\n- 包含计算字段(折扣、总流量等)",
|
||
"tags": ["流量采购"],
|
||
"parameters": {
|
||
"path": [{
|
||
"name": "id",
|
||
"required": True,
|
||
"type": "integer",
|
||
"description": "套餐ID"
|
||
}]
|
||
},
|
||
"responses": [{
|
||
"code": 200,
|
||
"contentType": "application/json",
|
||
"jsonSchema": {
|
||
"type": "object",
|
||
"properties": {
|
||
"code": {"type": "integer"},
|
||
"msg": {"type": "string"},
|
||
"data": {
|
||
"type": "object",
|
||
"properties": {
|
||
"id": {"type": "integer"},
|
||
"name": {"type": "string"},
|
||
"tag": {"type": "string"},
|
||
"originalPrice": {"type": "number"},
|
||
"price": {"type": "number"},
|
||
"monthlyFlow": {"type": "integer"},
|
||
"duration": {"type": "integer"},
|
||
"discount": {"type": "string"},
|
||
"totalFlow": {"type": "integer"},
|
||
"privileges": {"type": "array"}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}]
|
||
},
|
||
{
|
||
"name": "获取剩余流量",
|
||
"method": "GET",
|
||
"path": "/v2/store/flow-packages/remaining-flow",
|
||
"folderId": FOLDER_ID,
|
||
"description": "获取当前用户的有效流量套餐剩余流量信息\n\n**功能特性**:\n- 自动从JWT Token获取用户ID\n- 返回剩余流量、剩余天数、百分比等信息\n- 如果用户没有有效套餐,返回404",
|
||
"tags": ["流量采购"],
|
||
"responses": [{
|
||
"code": 200,
|
||
"contentType": "application/json",
|
||
"jsonSchema": {
|
||
"type": "object",
|
||
"properties": {
|
||
"code": {"type": "integer"},
|
||
"msg": {"type": "string"},
|
||
"data": {
|
||
"type": "object",
|
||
"properties": {
|
||
"packageName": {"type": "string", "description": "套餐名称"},
|
||
"remainingFlow": {"type": "integer", "description": "剩余流量(人)"},
|
||
"totalFlow": {"type": "integer", "description": "总流量(人)"},
|
||
"flowPercentage": {"type": "number", "description": "剩余流量百分比"},
|
||
"remainingDays": {"type": "integer", "description": "剩余天数"},
|
||
"totalDays": {"type": "integer", "description": "总天数"},
|
||
"timePercentage": {"type": "number", "description": "剩余时间百分比"},
|
||
"expireTime": {"type": "string", "description": "到期日期"},
|
||
"startTime": {"type": "string", "description": "开始日期"}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}]
|
||
},
|
||
{
|
||
"name": "创建流量采购订单",
|
||
"method": "POST",
|
||
"path": "/v2/store/flow-packages/order",
|
||
"folderId": FOLDER_ID,
|
||
"description": "创建流量套餐购买订单\n\n**功能特性**:\n- 自动从JWT Token获取用户ID\n- 支持金额为0的免费套餐(自动完成)\n- 返回订单信息供前端跳转支付",
|
||
"tags": ["流量采购"],
|
||
"requestBody": {
|
||
"type": "application/json",
|
||
"jsonSchema": {
|
||
"type": "object",
|
||
"required": ["packageId"],
|
||
"properties": {
|
||
"packageId": {"type": "integer", "description": "套餐ID"},
|
||
"payType": {"type": "string", "description": "支付方式", "enum": ["wechat", "alipay"], "default": "wechat"},
|
||
"remark": {"type": "string", "description": "备注"}
|
||
}
|
||
}
|
||
},
|
||
"responses": [{
|
||
"code": 200,
|
||
"contentType": "application/json",
|
||
"jsonSchema": {
|
||
"type": "object",
|
||
"properties": {
|
||
"code": {"type": "integer"},
|
||
"msg": {"type": "string"},
|
||
"data": {
|
||
"type": "object",
|
||
"properties": {
|
||
"orderNo": {"type": "string", "description": "订单号"},
|
||
"amount": {"type": "number", "description": "订单金额(仅待支付订单)"},
|
||
"payType": {"type": "string", "description": "支付方式(仅待支付订单)"},
|
||
"status": {"type": "string", "description": "订单状态:success=购买成功(免费套餐),pending=待支付"}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}]
|
||
},
|
||
{
|
||
"name": "获取订单列表",
|
||
"method": "GET",
|
||
"path": "/v2/store/flow-packages/orders",
|
||
"folderId": FOLDER_ID,
|
||
"description": "获取当前用户的流量套餐订单列表\n\n**功能特性**:\n- 自动从JWT Token获取用户ID\n- 支持分页查询\n- 支持按订单状态筛选",
|
||
"tags": ["流量采购"],
|
||
"parameters": {
|
||
"query": [
|
||
{"name": "page", "type": "integer", "description": "页码", "default": 1},
|
||
{"name": "limit", "type": "integer", "description": "每页数量", "default": 10},
|
||
{"name": "status", "type": "integer", "description": "订单状态:0=待支付, 1=已完成, 2=已取消, 3=已退款"}
|
||
]
|
||
},
|
||
"responses": [{
|
||
"code": 200,
|
||
"contentType": "application/json",
|
||
"jsonSchema": {
|
||
"type": "object",
|
||
"properties": {
|
||
"code": {"type": "integer"},
|
||
"msg": {"type": "string"},
|
||
"data": {
|
||
"type": "object",
|
||
"properties": {
|
||
"list": {"type": "array", "description": "订单列表"},
|
||
"total": {"type": "integer", "description": "总数量"},
|
||
"page": {"type": "integer", "description": "当前页码"},
|
||
"limit": {"type": "integer", "description": "每页数量"}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}]
|
||
}
|
||
]
|
||
|
||
# 创建接口
|
||
print("=" * 60)
|
||
print("开始上传流量采购接口到Apifox...")
|
||
print(f"项目ID: {PROJECT_ID}")
|
||
print(f"目录ID: {FOLDER_ID}")
|
||
print("=" * 60)
|
||
|
||
success_count = 0
|
||
fail_count = 0
|
||
api_ids = []
|
||
|
||
for i, api in enumerate(apis, 1):
|
||
print(f"\n[{i}/{len(apis)}] 创建接口: {api['name']}")
|
||
print(f" {api['method']} {api['path']}")
|
||
|
||
try:
|
||
response = requests.post(
|
||
f"{BASE_URL}/projects/{PROJECT_ID}/http-apis",
|
||
headers=headers,
|
||
json=api,
|
||
timeout=30
|
||
)
|
||
|
||
if response.status_code == 200:
|
||
result = response.json()
|
||
if result.get('success'):
|
||
api_id = result.get('data', {}).get('id')
|
||
print(f" ✅ 创建成功 (ID: {api_id})")
|
||
success_count += 1
|
||
api_ids.append(api_id)
|
||
else:
|
||
print(f" ❌ 创建失败: {result.get('errorMessage', 'Unknown error')}")
|
||
fail_count += 1
|
||
else:
|
||
print(f" ❌ HTTP {response.status_code}: {response.text[:200]}")
|
||
fail_count += 1
|
||
except Exception as e:
|
||
print(f" ❌ 异常: {str(e)}")
|
||
fail_count += 1
|
||
|
||
# 输出结果
|
||
print("\n" + "=" * 60)
|
||
print("✅ 完成!")
|
||
print(f"\n📊 统计:")
|
||
print(f" - 成功: {success_count}/{len(apis)}")
|
||
print(f" - 失败: {fail_count}")
|
||
|
||
if api_ids:
|
||
print(f"\n📝 接口ID列表:")
|
||
for i, api_id in enumerate(api_ids, 1):
|
||
print(f" {i}. {api_id}")
|
||
|
||
print(f"\n🔗 访问链接:")
|
||
print(f" https://app.apifox.com/project/{PROJECT_ID}")
|
||
print(f" 流量采购目录: https://app.apifox.com/project/{PROJECT_ID}/apis/folder/{FOLDER_ID}")
|
||
print("=" * 60)
|
||
|