Refactor mini program environment configuration and remove deprecated debug features

- Simplified the environment configuration in app.js by removing the debug environment options and related functions.
- Set a default API base URL for production while allowing commented options for local and testing environments.
- Cleaned up settings.js by removing the debug environment label and switching functionality, streamlining the user interface.
- Updated documentation to reflect the removal of debug features and added a summary of synchronization requirements in the planning document.
This commit is contained in:
Alex-larget
2026-03-16 13:30:05 +08:00
parent 0e7b81eaa8
commit e75092eaad
15 changed files with 915 additions and 209 deletions

View File

@@ -0,0 +1,122 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
目录接口诊断脚本 - 检查正式环境 soulapi 的 /api/miniprogram/book/* 是否正常
用法:
python scripts/test/check-catalog-api.py
SOUL_TEST_ENV=soulapi python scripts/test/check-catalog-api.py
"""
import json
import sys
from pathlib import Path
# 加载测试配置
sys.path.insert(0, str(Path(__file__).resolve().parent))
from config import API_BASE, ENV_LABEL, get_env_banner
try:
import urllib.request
import urllib.error
_urlopen = urllib.request.urlopen
_Request = urllib.request.Request
_HTTPError = urllib.error.HTTPError
_URLError = urllib.error.URLError
except ImportError:
import urllib2
_urlopen = urllib2.urlopen
_Request = urllib2.Request
_HTTPError = urllib2.HTTPError
_URLError = urllib2.URLError
def fetch(url, timeout=10):
"""GET 请求,返回 (parsed_json, status_code, error_msg)"""
try:
req = _Request(url)
req.get_method = lambda: "GET"
req.add_header("Content-Type", "application/json")
resp = _urlopen(req, timeout=timeout)
body = resp.read().decode("utf-8", errors="replace")
code = getattr(resp, "status", resp.getcode() if hasattr(resp, "getcode") else 200)
try:
data = json.loads(body)
except json.JSONDecodeError:
return None, code, "非 JSON 响应: " + body[:200]
return data, code, None
except _HTTPError as e:
try:
body = e.read().decode("utf-8", errors="replace")
data = json.loads(body) if body else {}
except Exception:
data = {}
return data, e.code, str(e)
except _URLError as e:
return None, None, str(e.reason) if hasattr(e, "reason") else str(e)
except Exception as e:
return None, None, str(e)
def main():
print(get_env_banner())
base = API_BASE.rstrip("/")
endpoints = [
("/api/miniprogram/book/parts", "目录-篇章列表(目录页主接口)"),
("/api/miniprogram/book/all-chapters", "全书章节app.loadBookData"),
("/health", "健康检查"),
]
all_ok = True
for path, desc in endpoints:
url = base + path
print(f"\n--- {desc} ---")
print(f"URL: {url}")
data, code, err = fetch(url)
if err:
print("[FAIL] 请求失败:", err)
all_ok = False
continue
if code and code != 200:
print("[FAIL] HTTP", code)
if data:
print(f" 响应: {json.dumps(data, ensure_ascii=False)[:300]}")
all_ok = False
continue
if not data:
print("[FAIL] 无响应体")
all_ok = False
continue
success = data.get("success")
if path == "/health":
status = data.get("status", "?")
print("[OK] status=%s, version=%s" % (status, data.get("version", "?")))
elif path == "/api/miniprogram/book/parts":
parts = data.get("parts") or []
total = data.get("totalSections", 0)
fixed = data.get("fixedSections") or []
print("[OK] success=%s, parts=%d, totalSections=%d, fixedSections=%d" % (success, len(parts), total, len(fixed)))
if not parts and total == 0:
print(" [WARN] 篇章为空! 请检查正式环境数据库 chapters 表")
elif parts:
print(" 首篇章: id=%s, title=%s" % (parts[0].get("id"), parts[0].get("title")))
elif path == "/api/miniprogram/book/all-chapters":
arr = data.get("data") or data.get("chapters") or []
print("[OK] success=%s, data=%d" % (success, len(arr)))
if not arr:
print(" [WARN] 章节列表为空! 请检查 chapters 表")
elif arr:
first = arr[0] if isinstance(arr[0], dict) else {}
print(" 首条: id=%s, partTitle=%s" % (first.get("id"), first.get("partTitle")))
print("\n" + "=" * 50)
if all_ok:
print("[OK] 所有接口正常,若小程序仍无法加载,请检查:")
print(" 1. 微信公众平台 → 服务器域名 → request 合法域名 是否包含 soulapi.quwanzhi.com")
print(" 2. 正式版小程序 baseUrl 是否为 https://soulapi.quwanzhi.com")
else:
print("[FAIL] 存在异常,请根据上述输出排查后端或数据库")
print("=" * 50)
if __name__ == "__main__":
main()

View File

@@ -0,0 +1,75 @@
# -*- coding: utf-8 -*-
"""
清理流程测试产生的数据persons测试自动创建_、测试新人物_、chapterst 开头的 6 位数字 id
用法cd scripts/test && python process/cleanup_test_data.py
"""
import os
import re
import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import requests
API_BASE = os.environ.get("SOUL_API_BASE", "http://localhost:8080").rstrip("/")
ADMIN_USER = os.environ.get("SOUL_ADMIN_USERNAME", "admin")
ADMIN_PASS = os.environ.get("SOUL_ADMIN_PASSWORD", "admin123")
def main():
r = requests.post(
f"{API_BASE}/api/admin",
json={"username": ADMIN_USER, "password": ADMIN_PASS},
timeout=10,
)
if not r.json().get("success") or not r.json().get("token"):
print("登录失败")
return 1
token = r.json()["token"]
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
# 1. 删除测试 Person
rp = requests.get(f"{API_BASE}/api/db/persons", headers=headers, timeout=10)
if rp.json().get("success"):
persons = rp.json().get("persons") or []
for p in persons:
name = p.get("name") or ""
if name.startswith("测试自动创建_") or name.startswith("测试新人物_"):
pid = p.get("personId")
rd = requests.delete(
f"{API_BASE}/api/db/persons?personId={pid}",
headers=headers,
timeout=10,
)
if rd.json().get("success"):
print(f" 已删除 Person: {name} ({pid})")
else:
print(f" 删除 Person 失败: {name} - {rd.json()}")
# 2. 删除测试 Chapterid 形如 t123456
rl = requests.get(
f"{API_BASE}/api/db/book?action=list",
headers=headers,
timeout=10,
)
if rl.json().get("success"):
sections = rl.json().get("sections") or []
for s in sections:
sid = s.get("id") or ""
if re.match(r"^t\d{6}$", sid):
rd = requests.delete(
f"{API_BASE}/api/db/book?id={sid}",
headers=headers,
timeout=10,
)
if rd.json().get("success"):
print(f" 已删除 Chapter: {sid} ({s.get('title', '')})")
else:
print(f" 删除 Chapter 失败: {sid} - {rd.json()}")
print("清理完成")
return 0
if __name__ == "__main__":
sys.exit(main())