diff --git a/app/service/systemMethods/index.js b/app/service/systemMethods/index.js index a8fae05..080f03b 100644 --- a/app/service/systemMethods/index.js +++ b/app/service/systemMethods/index.js @@ -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; diff --git a/app/service/systems/shops.js b/app/service/systems/shops.js index 521e633..8e792b7 100644 --- a/app/service/systems/shops.js +++ b/app/service/systems/shops.js @@ -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) { diff --git a/config/config.default.js b/config/config.default.js index 0e4a199..79dc675 100644 --- a/config/config.default.js +++ b/config/config.default.js @@ -171,7 +171,7 @@ module.exports = appInfo => { deletedAt: 'deleted_at', // 软删除字段名 }, pool: { - max: 20, + max: 225, min: 1, idle: 10000, acquire: 30000, diff --git a/script/deploy-remote.cjs b/script/deploy-remote.cjs index 13fc2fd..c89dde2 100644 --- a/script/deploy-remote.cjs +++ b/script/deploy-remote.cjs @@ -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',