feat(shops): 店铺视频号授权支持 + 部署优化

- shops.js: 视频号授权需传 appId/toUserName,新增授权请求/响应调试缓存
- index.js: SPH 运单查询添加调试缓存
- deploy-remote.cjs: 健康检查优化(30次→5次/10s),失败自动回滚 current.md
- config.default.js: 数据库连接池 20→225
This commit is contained in:
liufumingren 2026-08-01 03:45:26 +08:00
parent 315e366c4c
commit da56253d4a
4 changed files with 75 additions and 9 deletions

View File

@ -2288,6 +2288,14 @@ class indexService extends MainService {
const accessToken = String(shopPlain.access_token || '').trim();
const expressList = await this.service.systems.express.getExpressList(true);
const expressNameMap = buildExpressNameMap(expressList, 'SPH');
await this.app.setCacheAsync('SPHquery', JSON.stringify({
interface_url: 'https://api.polyapi.com/OpenAPI/do',
appkey: this.app.config.poloApi.appKey,
app_secret: this.app.config.poloApi.appSecret,
platid,
version: '1.5',
token: accessToken,
}), 3600);
const responseData = await service.systems.poloApi.request('Differ.JH.Logistics.GetWayBillCondition', {
}, {
interface_url: 'https://api.polyapi.com/OpenAPI/do',
@ -2297,7 +2305,7 @@ class indexService extends MainService {
version: '1.5',
token: accessToken,
});
console.log('请求的店铺的platformCode', platformCode, '接口响应', JSON.stringify(responseData));
await this.app.setCacheAsync('SPHrequest', JSON.stringify(responseData), 3600);
const parsed = formatDyWaybillSearchResponse(responseData, expressNameMap);
list = parsed.list;
break;

View File

@ -696,6 +696,8 @@ class ShopsService extends Service {
'callbackurl',
{ required: true, maxLength: 500 }
);
const appId = data.appId || '';
const toUserName = data.toUserName || '';
const shop = await this.findShopById(shopId, { allowDeleted: false });
const shopPlain = typeof shop.get === 'function' ? shop.get({ plain: true }) : shop;
const platformCode = await this.ensureShopPrintPlatformExists(shopPlain.platform_code);
@ -719,6 +721,16 @@ class ShopsService extends Service {
if (platName === '电商京东') {
requestData.VenderID = shopPlain.id;
}
if (platName === '视频号') {
if(!appId || !toUserName) {
throw this.createError('视频号授权需要提供 appId 和 toUserName', 400);
}
requestData.appId = appId;
requestData.toUserName = toUserName;
delete requestData.RedirectCodeUrl;
}
// 缓存最后一次授权查询请求数据,便于排查问题
await this.app.setCacheAsync('last_query_auth_polo', JSON.stringify(requestData), 3600);
const respone = await ctx.curl(authUrl, {
method: 'POST',
@ -726,6 +738,7 @@ class ShopsService extends Service {
timeout: 5000,
data: requestData,
});
await this.app.setCacheAsync('last_respone_auth_polo', JSON.stringify(respone), 3600);
if (respone.data && respone.data.code === 0) {
const sessionKey = respone.data.data || null;
if (sessionKey) {

View File

@ -171,7 +171,7 @@ module.exports = appInfo => {
deletedAt: 'deleted_at', // 软删除字段名
},
pool: {
max: 20,
max: 225,
min: 1,
idle: 10000,
acquire: 30000,

View File

@ -162,17 +162,17 @@ function main() {
const switchOutput = runSsh(config, switchAndStartScript, { stdio: 'pipe' });
console.log(String(switchOutput || '').trim());
// 6.5. 健康检查新实例(curl 循环等待,最多 60 秒)
// 6.5. 健康检查新实例(curl 循环等待,最多 10 秒)
const oldDir = `${config.deployRoot}/${currentVersion}`;
const healthCheckScript = [
'set -euo pipefail',
`TARGET_PORT=${targetPort}`,
'for attempt in $(seq 1 30); do',
' if curl -fsS --connect-timeout 2 --max-time 4 "http://127.0.0.1:$TARGET_PORT/" >/dev/null 2>&1; then',
'for attempt in $(seq 1 5); do',
' if curl -fsS --connect-timeout 2 --max-time 3 "http://127.0.0.1:$TARGET_PORT/" >/dev/null 2>&1; then',
` echo "新实例 (${targetVersion}) 健康检查通过 (端口 $TARGET_PORT)"`,
' break',
' fi',
' if [ "$attempt" -eq 30 ]; then',
' if [ "$attempt" -eq 5 ]; then',
` echo "新实例 (${targetVersion}) 健康检查超时 (端口 $TARGET_PORT)" >&2`,
' exit 1',
' fi',
@ -184,11 +184,56 @@ function main() {
const healthOutput = runSsh(config, healthCheckScript, { stdio: 'pipe' });
console.log(String(healthOutput || '').trim());
} catch (err) {
console.warn(`[WARN] 新实例健康检查失败: ${err.message}`);
console.warn(`[WARN] 新实例 (${targetVersion}) 可能未完全就绪,请人工确认 127.0.0.1:${targetPort} 状态`);
// 健康检查失败 → 不做切换,回滚 current.md 并清理失败的新实例
console.error(`[ERROR] 新实例健康检查失败 (10s 超时): ${err.message}`);
console.error(`[ERROR] 发布中止:不切换 Nginx upstream,不停旧实例`);
// 回滚:将 current.md 恢复为旧版本
const revertCurrentMdScript = [
'set -euo pipefail',
`printf '%s\\n' ${quoteForBash(currentVersion)} > ${quoteForBash(currentMdPath)}`,
`echo "已回滚 current.md -> ${currentVersion}"`,
].join('\n');
try {
const revertOutput = runSsh(config, revertCurrentMdScript, { stdio: 'pipe' });
console.log(String(revertOutput || '').trim());
} catch (revertErr) {
console.error(`[ERROR] 回滚 current.md 失败: ${revertErr.message}`);
}
// 清理:杀死启动失败的新实例进程
const killNewScript = [
'set -euo pipefail',
`NEW_DIR=${quoteForBash(targetDir)}`,
`NEW_VERSION=${quoteForBash(targetVersion)}`,
'NEW_PIDS=$(ps -eo pid=,args= | awk -v root="$NEW_DIR" \'index($0, root) && $2 ~ /node$/ { print $1 }\')',
'if [ -n "$NEW_PIDS" ]; then',
' echo "正在清理失败的新实例 ($NEW_VERSION): $NEW_PIDS"',
' kill $NEW_PIDS || true',
' sleep 2',
' NEW_PIDS_REMAIN=$(ps -eo pid=,args= | awk -v root="$NEW_DIR" \'index($0, root) && $2 ~ /node$/ { print $1 }\')',
' if [ -n "$NEW_PIDS_REMAIN" ]; then',
' echo "新实例仍有残留进程,强制停止: $NEW_PIDS_REMAIN"',
' kill -9 $NEW_PIDS_REMAIN || true',
' fi',
' echo "失败的新实例 ($NEW_VERSION) 已清理"',
'else',
' echo "未找到新实例 ($NEW_VERSION) 进程,无需清理"',
'fi',
].join('\n');
try {
const killOutput = runSsh(config, killNewScript, { stdio: 'pipe' });
console.log(String(killOutput || '').trim());
} catch (killErr) {
console.error(`[ERROR] 清理失败新实例进程出错: ${killErr.message}`);
}
console.error(`[ERROR] 发布失败: ${envName} -> ${config.username}@${config.host}:${targetDir}`);
console.error(`[ERROR] 旧实例 (${currentVersion}) 仍在运行,线上服务不受影响`);
process.exit(1);
}
// 6.55. 切换 Nginx upstream 端口并 reload
// 6.55. 切换 Nginx upstream 端口并 reload(仅在健康检查通过后执行)
const nginxUpstreamPath = `${config.deployRoot}/nginx-upstream.conf`;
const switchNginxScript = [
'set -euo pipefail',