From 2c8e7a54356d1f6289be41b1546f0ef4d51568ae Mon Sep 17 00:00:00 2001 From: Caoweijie <594100253@qq.com> Date: Fri, 11 Sep 2026 18:00:29 +0800 Subject: [PATCH] feat: add extension test environment configuration --- README.md | 14 ++++++++++++++ extension/auth.js | 11 +++++++++-- extension/background.js | 4 ++-- extension/manifest.json | 2 ++ 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 732281c..0d3900d 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,20 @@ V9.3.0 将采集、七角色分析、证据追溯和最终报告统一为一条 ## 本地运行 +### 浏览器扩展环境 + +在 `extension/auth.js` 中修改 `SANSHENG_ENV` 选择环境,默认为 `dev`。 + +| 环境 | SANSHENG_ENV | 接口前缀 | PC 端地址 | +| --- | --- | --- | --- | +| 开发 | `dev` | `http://localhost:19174` | `http://localhost:5173` | +| 测试 | `test` | `https://qa-api.sanshengai.net` | `https://qa.sanshengai.net` | +| 生产 | `prod` | `https://api.sanshengai.net` | `https://www.sanshengai.net` | + +切换后,在浏览器扩展管理页重新加载 `extension` 目录的扩展,并退出登录后重新登录。`extension/manifest.json` 已包含上述环境的域名权限。已有 `release` 发布包使用其自身配置。 + +### 本地分析服务 + 双击 `start-tmall-ai.command`,或在 `server` 目录运行: ```bash diff --git a/extension/auth.js b/extension/auth.js index a36cd7f..3aba912 100644 --- a/extension/auth.js +++ b/extension/auth.js @@ -1,6 +1,13 @@ +// 切换环境后,在浏览器扩展管理页重新加载扩展并重新登录。 +const SANSHENG_ENV='dev'; // dev | test | prod +const SANSHENG_ENVIRONMENTS={ + dev:{apiBase:'http://localhost:19174',pcBase:'http://localhost:5173'}, + test:{apiBase:'https://qa-api.sanshengai.net',pcBase:'https://qa.sanshengai.net'}, + prod:{apiBase:'https://api.sanshengai.net',pcBase:'https://www.sanshengai.net'} +}; +if(!Object.prototype.hasOwnProperty.call(SANSHENG_ENVIRONMENTS,SANSHENG_ENV))throw new Error(`未知扩展环境:${SANSHENG_ENV}`); const SANSHENG_AUTH_CONFIG={ - apiBase:'http://localhost:19174', - pcBase:'http://localhost:5173', + ...SANSHENG_ENVIRONMENTS[SANSHENG_ENV], clientId:'sansheng-product-analysis-extension', scope:'product.analysis', callbackPath:'/extension/authorize' diff --git a/extension/background.js b/extension/background.js index 7599077..1c57c97 100644 --- a/extension/background.js +++ b/extension/background.js @@ -21,8 +21,8 @@ async function submitProductAnalysis(raw){ try{ response=await sanshengExtensionAuth.fetch('/v1/product-analysis/tasks',{method:'POST',headers:{'Content-Type':'application/json','Idempotency-Key':raw.collection.clientRequestId},body:JSON.stringify({platform:raw?.meta?.platform||'taobao',productId:String(raw?.product?.itemId||''),analysisMode:raw?.collection?.analysisMode==='current_snapshot'?'quick':(raw?.collection?.reviewCollectionMode||'standard'),userInstruction:'',clientRequestId:raw.collection.clientRequestId,snapshot:raw,source:'chrome-extension'}),signal:controller.signal}); }catch(error){ - if(error?.name==='AbortError')throw new Error('Web API 响应超时,请确认开发 API 已启动后重试'); - throw new Error('无法连接 Web API,请确认 http://localhost:19174 已启动'); + if(error?.name==='AbortError')throw new Error('Web API 响应超时,请确认当前环境 API 可用后重试'); + throw new Error(`无法连接 Web API,请确认 ${sanshengExtensionAuth.config.apiBase} 可用`); }finally{clearTimeout(timeout)} const result=await response.json().catch(()=>null); const code=result?.Code??result?.code; diff --git a/extension/manifest.json b/extension/manifest.json index 5c92b74..cea8d51 100644 --- a/extension/manifest.json +++ b/extension/manifest.json @@ -17,6 +17,8 @@ "https://*.alicdn.com/*", "https://api.sanshengai.net/*", "https://www.sanshengai.net/*", + "https://qa-api.sanshengai.net/*", + "https://qa.sanshengai.net/*", "http://localhost:19174/*", "http://localhost:5173/*" ],