Files
CKB-Interface/application/store/create_vendor_folder.py
2026-03-24 10:39:16 +08:00

115 lines
3.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
import sys
import requests
import json
import os
sys.stdout.reconfigure(encoding='utf-8')
# 配置
TOKEN = "afxp_fa4137GfIzJtlOCeQHjpCXc83SFWmeAOrZnq"
PROJECT_ID = "6037107"
PARENT_FOLDER_ID = "78015216" # 门店端-新版目录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"
}
folder_name = "供应链采购管理"
description = "供应链采购管理相关接口"
print("=" * 60)
print(f"创建目录: {folder_name}")
print("=" * 60)
# 通过导入OpenAPI创建目录
openapi_spec = {
"openapi": "3.0.0",
"info": {
"title": "供应链采购管理目录创建",
"version": "1.0.0"
},
"tags": [
{
"name": folder_name,
"description": description
}
],
"paths": {
f"/api/__placeholder__/supply-chain": {
"get": {
"summary": f"[占位] {folder_name} 目录占位接口",
"description": "这是一个占位接口,用于创建目录。可以在 Apifox 中手动删除。",
"tags": [folder_name],
"responses": {
"200": {
"description": "占位响应"
}
}
}
}
}
}
payload = {
"input": json.dumps(openapi_spec, ensure_ascii=False),
"options": {
"targetEndpointFolderId": int(PARENT_FOLDER_ID), # 指定父目录
"endpointOverwriteBehavior": "CREATE_NEW"
}
}
try:
response = requests.post(
f"{BASE_URL}/projects/{PROJECT_ID}/import-openapi",
headers=headers,
json=payload,
params={"locale": "zh-CN"},
timeout=30
)
print(f"\nHTTP状态码: {response.status_code}")
print(f"响应内容: {response.text[:1000]}")
if response.status_code == 200:
try:
result = response.json()
except:
print("⚠️ 响应不是JSON格式可能是重定向或HTML")
print(" 目录可能已创建请在Apifox Web UI中查看")
sys.exit(0)
print(f"响应: {json.dumps(result, ensure_ascii=False, indent=2)[:500]}")
if result.get('success'):
counters = result.get('data', {}).get('counters', {})
endpoint_folder_created = counters.get('endpointFolderCreated', 0)
endpoint_created = counters.get('endpointCreated', 0)
if endpoint_folder_created > 0:
print(f"\n✅ 目录创建成功!")
print(f" 已创建 {endpoint_folder_created} 个目录")
print(f" 已创建 {endpoint_created} 个占位接口")
print(f"\n💡 提示: 占位接口可以在 Apifox Web UI 中手动删除")
elif endpoint_created > 0:
print(f"\n⚠️ 目录可能已存在")
print(f" 已创建 {endpoint_created} 个占位接口")
print(f"\n💡 提示: 请在 Apifox Web UI 中查看目录是否已创建")
else:
print(f"\n⚠️ 未创建目录或接口")
else:
print(f"\n❌ 创建失败: {result.get('errorMessage', 'Unknown error')}")
else:
print(f"\n❌ HTTP {response.status_code}")
print(f"响应内容: {response.text[:500]}")
except Exception as e:
print(f"\n❌ 异常: {str(e)}")
import traceback
traceback.print_exc()
print("\n" + "=" * 60)