VERIFY-BAIJIAHAO-API.md 6.2 KB

验证百家号 API 功能

代码修改确认

1. AccountService.ts 修改

位置server/src/services/AccountService.ts 第 385 行

修改内容

// 修改前
const platformsSkipAI: PlatformType[] = ['douyin', 'xiaohongshu'];

// 修改后
const platformsSkipAI: PlatformType[] = ['douyin', 'xiaohongshu', 'baijiahao'];

验证命令

grep -n "platformsSkipAI" server/src/services/AccountService.ts

预期输出

385:          const platformsSkipAI: PlatformType[] = ['douyin', 'xiaohongshu', 'baijiahao'];

2. HeadlessBrowserService.ts 修改

位置server/src/services/HeadlessBrowserService.ts 第 580-585 行

修改内容

async fetchAccountInfo(platform: PlatformType, cookies: CookieData[]): Promise<AccountInfo> {
  logger.info(`[fetchAccountInfo] Starting for platform: ${platform}`);
  
  // 百家号使用直接 API 获取账号信息和作品列表
  if (platform === 'baijiahao') {
    logger.info(`[fetchAccountInfo] Using direct API for baijiahao`);
    return this.fetchBaijiahaoAccountInfoDirectApi(cookies);
  }
  // ...
}

验证命令

grep -A 3 "if (platform === 'baijiahao')" server/src/services/HeadlessBrowserService.ts | head -5

预期输出

if (platform === 'baijiahao') {
  logger.info(`[fetchAccountInfo] Using direct API for baijiahao`);
  return this.fetchBaijiahaoAccountInfoDirectApi(cookies);
}

重新编译和启动

步骤 1:停止当前服务

Ctrl+C 停止正在运行的服务

步骤 2:清理旧的编译文件

cd server
rm -rf dist
# 或者 Windows 下
# rmdir /s /q dist

步骤 3:重新安装依赖(可选)

npm install

步骤 4:重新编译

npm run build

预期输出:应该看到编译成功的消息,没有错误

步骤 5:启动服务

npm run dev
# 或者
npm start

测试刷新功能

步骤 1:打开账号管理页面

在浏览器中打开账号管理页面

步骤 2:点击百家号账号的"刷新"按钮

步骤 3:查看终端日志

应该看到的日志

[refreshAccount] Platform: baijiahao, shouldUseAI: false, aiAvailable: true
[checkCookieValid] Checking cookie for baijiahao, cookie count: X
[checkCookieValid] API check result for baijiahao: true
[fetchAccountInfo] Starting for platform: baijiahao
[fetchAccountInfo] Using direct API for baijiahao
[Baijiahao API] Fetching account info via direct API...
[Baijiahao API] Step 1: Fetching appinfo...
[Baijiahao API] appinfo response: errno=0, errmsg=success
[Baijiahao API] Got account info: name=xxx, id=bjh_xxx
[Baijiahao API] Step 2: Fetching growth info...
[Baijiahao API] Got fans count: 123
[Baijiahao API] Step 3: Fetching works list...
[Baijiahao API] Successfully fetched account info

不应该看到的日志

[AI Refresh] Starting AI-assisted refresh for account
[Playwright] Fetching account info for baijiahao
Launching browser...

故障排除

问题 1:仍然看到 AI 日志

症状

[AI Refresh] Starting AI-assisted refresh for account XXX (baijiahao)

原因platformsSkipAI 没有包含 baijiahao

解决

  1. 检查 server/src/services/AccountService.ts 第 385 行
  2. 确认包含 'baijiahao'
  3. 重新编译:npm run build
  4. 重启服务

问题 2:仍然使用 Playwright

症状

[Playwright] Fetching account info for baijiahao

原因fetchAccountInfo 方法没有正确处理百家号

解决

  1. 检查 server/src/services/HeadlessBrowserService.ts
  2. 确认有 if (platform === 'baijiahao') 判断
  3. 重新编译:npm run build
  4. 重启服务

问题 3:编译后仍然使用旧代码

症状:修改了代码,编译成功,但日志显示仍在使用旧逻辑

原因

  • 可能有多个 Node 进程在运行
  • 可能使用了缓存的代码

解决

# 1. 杀死所有 Node 进程
# Windows:
taskkill /F /IM node.exe
# Linux/Mac:
killall node

# 2. 清理编译输出
rm -rf server/dist

# 3. 重新编译
cd server
npm run build

# 4. 启动服务
npm run dev

问题 4:找不到 fetchBaijiahaoAccountInfoDirectApi

症状

TypeError: this.fetchBaijiahaoAccountInfoDirectApi is not a function

原因:方法名拼写错误或方法不存在

解决

  1. 检查 server/src/services/HeadlessBrowserService.ts
  2. 搜索 fetchBaijiahaoAccountInfoDirectApi 方法
  3. 确认方法存在且拼写正确

验证成功的标志

编译成功:没有错误 ✅ 日志正确:看到 [Baijiahao API] 而不是 [AI Refresh][Playwright]速度快:刷新时间从 10-30 秒降低到 1-3 秒 ✅ 数据准确:账号信息、粉丝数、作品数都正确

完整的验证流程

# 1. 停止服务
Ctrl+C

# 2. 清理
cd server
rm -rf dist

# 3. 验证代码
grep "platformsSkipAI" src/services/AccountService.ts
grep "if (platform === 'baijiahao')" src/services/HeadlessBrowserService.ts

# 4. 编译
npm run build

# 5. 启动
npm run dev

# 6. 测试
# 在浏览器中刷新百家号账号

# 7. 查看日志
# 应该看到 [Baijiahao API] 开头的日志

日志对比

❌ 错误的日志(使用 AI/浏览器)

[AI Refresh] Starting AI-assisted refresh for account 123 (baijiahao)
[Playwright] Launching browser...
[Playwright] Navigating to https://baijiahao.baidu.com/...

✅ 正确的日志(使用 API)

[refreshAccount] Platform: baijiahao, shouldUseAI: false
[fetchAccountInfo] Starting for platform: baijiahao
[fetchAccountInfo] Using direct API for baijiahao
[Baijiahao API] Fetching account info via direct API...
[Baijiahao API] Step 1: Fetching appinfo...
[Baijiahao API] appinfo response: errno=0
[Baijiahao API] Got account info: name=xxx
[Baijiahao API] Step 2: Fetching growth info...
[Baijiahao API] Got fans count: 123
[Baijiahao API] Step 3: Fetching works list...
[Baijiahao API] Successfully fetched account info

性能对比

方式 时间 日志特征
AI/浏览器 10-30秒 [AI Refresh][Playwright]
API 接口 1-3秒 [Baijiahao API]

如果刷新时间仍然很长(超过 5 秒),说明没有使用 API。