From 5cc41f0f10161195808f1d0a683840be902e8b07 Mon Sep 17 00:00:00 2001 From: Guoguo Date: Wed, 6 May 2026 21:07:44 +0800 Subject: [PATCH] chore: update API domain deployment config --- admin-console/src/config/env.js | 5 +- docs/deploy/tencent-cloud.md | 156 +++++++++++++++++++++++++++++++- miniprogram/config/env.js | 5 +- 3 files changed, 159 insertions(+), 7 deletions(-) diff --git a/admin-console/src/config/env.js b/admin-console/src/config/env.js index 8ccd44a..db05b4b 100644 --- a/admin-console/src/config/env.js +++ b/admin-console/src/config/env.js @@ -1,10 +1,9 @@ const ENV = 'test' -// WARNING: Before deploying to production, replace the prod URL below with the real SCF endpoint. const API_BASES = { local: 'http://localhost:3000', - test: 'https://1426323813-ilxkhlxf4p.ap-guangzhou.tencentscf.com', - prod: 'https://replace-with-scf-prod-url' + test: 'https://api.vsai.net.cn', + prod: 'https://api.vsai.net.cn' } export { ENV, API_BASES } diff --git a/docs/deploy/tencent-cloud.md b/docs/deploy/tencent-cloud.md index dd994e2..e370548 100644 --- a/docs/deploy/tencent-cloud.md +++ b/docs/deploy/tencent-cloud.md @@ -169,7 +169,161 @@ admin/ ADMIN_COS_PREFIX=admin-test/ npm run deploy:cos ``` -## 8. 部署后验证 +## 8. 后端 SCF 代码更新 + +常规后端代码更新流程如下。当前线上函数名为 `jw-beauty-api`,部署包上传到 COS: + +```text +deploy/jw-beauty-api/hox-http-function.zip +``` + +### 8.1 拉取代码并检查 + +```bash +git status --short --branch +git pull --ff-only +``` + +如果 `server/` 有依赖变更,先同步依赖: + +```bash +cd server +npm install +``` + +部署前至少做语法检查: + +```bash +cd server +node -c src/app.js +node -c src/index.js +node -c src/lib/serverless.js +node -c scripts/local-server.js +``` + +如果只改了某个路由,也可以额外检查对应文件,例如: + +```bash +node -c src/routes/subscription.js +``` + +### 8.2 打包后端代码 + +部署包必须以 `server/` 为根目录打包,并包含: + +- `index.js` +- `package.json` +- `package-lock.json` +- `scripts/` +- `scf_bootstrap` +- `src/` +- `sql/` +- `node_modules/` + +不要把 `server/.env` 打进部署包。 + +实际使用命令: + +```bash +cd server +rm -f /tmp/hox-http-function.zip +zip -qry /tmp/hox-http-function.zip index.js package.json package-lock.json scripts scf_bootstrap src sql node_modules +``` + +### 8.3 上传部署包到 COS + +上传复用 `server/.env` 中的腾讯云/COS 凭证。命令只读取本地环境变量,不要把真实密钥写入文档或提交到 Git。 + +```bash +cd server +set -a; source ./.env; set +a +node - <<'NODE' +const fs = require('fs') +const COS = require('cos-nodejs-sdk-v5') +const cos = new COS({ + SecretId: process.env.TENCENT_SECRET_ID, + SecretKey: process.env.TENCENT_SECRET_KEY +}) + +cos.putObject({ + Bucket: process.env.COS_BUCKET, + Region: process.env.COS_REGION || process.env.TENCENT_REGION || 'ap-guangzhou', + Key: 'deploy/jw-beauty-api/hox-http-function.zip', + Body: fs.createReadStream('/tmp/hox-http-function.zip') +}, err => { + if (err) { + console.error(err) + process.exit(1) + } + console.log('uploaded deploy/jw-beauty-api/hox-http-function.zip') +}) +NODE +``` + +### 8.4 更新 SCF 函数代码 + +使用 `tccli scf UpdateFunctionCode` 指向刚上传的 COS 对象: + +```bash +cd server +set -a; source ./.env; set +a +/Users/guoguo/.local/bin/tccli scf UpdateFunctionCode \ + --region ap-guangzhou \ + --FunctionName jw-beauty-api \ + --CosBucketName "$COS_BUCKET" \ + --CosObjectName /deploy/jw-beauty-api/hox-http-function.zip \ + --CosBucketRegion "${COS_REGION:-ap-guangzhou}" \ + --secretId "$TENCENT_SECRET_ID" \ + --secretKey "$TENCENT_SECRET_KEY" +``` + +如果命令返回 `RequestId`,表示更新请求已提交成功。函数实例刷新通常需要等待几秒。 + +### 8.5 更新后快速验证 + +```bash +node - <<'NODE' +const base = 'https://api.vsai.net.cn' + +async function print(label, res) { + const text = await res.text() + console.log(label, res.status, text.slice(0, 1000)) + return text +} + +;(async () => { + await print('health', await fetch(base + '/health')) + + const loginText = await print('login', await fetch(base + '/api/v1/admin/login', { + method: 'POST', + headers: { 'content-type': 'application/json' }, + body: JSON.stringify({ username: 'admin', password: 'admin' }) + })) + + const token = JSON.parse(loginText).data && JSON.parse(loginText).data.token + if (token) { + await print('settings', await fetch(base + '/api/v1/admin/settings', { + headers: { authorization: 'Bearer ' + token } + })) + await print('dashboard', await fetch(base + '/api/v1/admin/dashboard', { + headers: { authorization: 'Bearer ' + token } + })) + } +})().catch(err => { + console.error(err) + process.exit(1) +}) +NODE +``` + +常见成功结果: + +- `GET /health` 返回 `code: 0` +- `POST /api/v1/admin/login` 使用 `admin/admin` 返回 `code: 0` +- `GET /api/v1/admin/settings` 返回 `code: 0` +- `GET /api/v1/admin/dashboard` 返回 `code: 0` + +## 9. 部署后验证 建议按顺序验证: diff --git a/miniprogram/config/env.js b/miniprogram/config/env.js index 5c0aa09..aee0d20 100644 --- a/miniprogram/config/env.js +++ b/miniprogram/config/env.js @@ -1,10 +1,9 @@ var ENV = 'test' -// !! RELEASE BLOCKER: Change ENV to 'prod' and replace prod URL before publishing !! var API_BASES = { local: 'http://localhost:3000', - test: 'https://1426323813-ilxkhlxf4p.ap-guangzhou.tencentscf.com', - prod: 'https://replace-with-scf-prod-url' // TODO: replace with actual production SCF URL + test: 'https://api.vsai.net.cn', + prod: 'https://api.vsai.net.cn' } var __DEV__ = ENV !== 'prod'