WMS_MAIN/app/model/warehouse_front_config.js
liufumingren 3f5eb49081 feat(frontConfig): 前端独立部署域名两阶段切换(pending 登记 + TXT 校验通过后提升)
- 迁移 20260924180000-add-pending-domain-to-wms-warehouse-front-config
  新增 pending_* 6 列、retired_domain/retired_at 与 idx_pending_domain/idx_retired_domain
  两个索引(生产库已执行)
- setDomain 只登记待验证域名并下发 TXT 校验码,不再「设置即生效」;
  TXT 校验通过后由 promotePendingDomain 提升(旧域名写入 retired_domain 下线队列、
  清空证书 5 列并重置同步状态),消除「改域名瞬间旧域名公共接口 404」中间态
- DoH 解析改为多端点顺序尝试(dohUrls:alidns → 223.5.5.5 → doh.pub),
  单端点 NODATA/NXDOMAIN 不再直接判定,必要时降级系统 DNS 并显式限时;
  config.frontConfig.domainVerify 同步 batchSize 50→20、timeoutMs 5000→3000
- getDomain/setDomain/checkDomain 响应追加待验证域名组 7 字段(旧 9 字段口径不变)
- 同步 methods 文档(frontConfig 6 方法)与 changes 4 条变更记录、
  AI_METHOD_ROUTING.md 函数清单(42→50)

注:本提交补齐此前已在生产库执行但从未入库的迁移与代码
(生产 SequelizeMeta 已含 20260924180000,属 orphan meta 补齐入库)。
2026-09-25 23:40:56 +08:00

265 lines
8.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 仓库前端个性化配置模型(仓库伪独立部署,单表)。
* - 每仓库一行(一仓一域一证书):保存仓库前端 Logo / 标题 / 登录页图 /
* 主题等个性化配置,以及证书元信息与证书 PEM 内容(cert_pem / cert_key,由定时
* 任务读取后写文件部署,禁止写入日志/接口响应)。
* - sync_status 状态机:0-待同步,1-同步成功,2-同步失败;
* 管理后台保存后置为待同步,由定时任务同步到 nginx 后回写状态。
* - 证书启用/停用由整行删除或 sync_status 表达;证书到期由 cert_expire_at
* 判断,不单独保留证书 status 列。
* - domain_status 状态机:0-未验证,1-已验证(DNS TXT 校验通过),2-验证失败;
* 未验证/验证失败的域名禁止上传证书与对外公共拉取,管理后台设置域名后回落到 0。
* - 两阶段域名切换(方案 B):生效域名 domain 只在 DNS TXT 校验通过时由
* frontConfig.promotePendingDomain 切换;frontConfig.setDomain 只把新域名登记在
* pending_domain(+ pending_verify_host/token 校验码),旧域名全程可用(不会出现
* 「页面能打开但公共配置 404」的中间态);提升时旧域名写入 retired_domain 下线队列
* (由外部证书脚本删除其 nginx 配置与证书文件后置 NULL)、清空证书 5 列并重置同步 4 列
* (域名已变,证书必须重新上传)。
* - domain 唯一索引(uk_domain)为 MySQL 可空列唯一索引:允许多个 NULL,
* 应用层在写入非空 domain 前必须做唯一性校验,不能依赖数据库约束报错。
* - store_id 唯一索引(uk_store_id):每仓一行,由数据库层保证。
* - 不启用 paranoid:配置为"每仓库一行"的元数据,删除语义由业务层
* (清空/停用配置)表达,表结构不含 deleted_at 列。
*
* @param {Object} app Egg.js Application 实例。
* @return {Object} Sequelize Model 定义。
*/
module.exports = app => {
const { BIGINT, STRING, INTEGER, TINYINT, DATE, TEXT, JSON: JSON_TYPE } = app.Sequelize;
const WarehouseFrontConfig = app.model.define('warehouse_front_config', {
id: {
type: BIGINT(20).UNSIGNED,
primaryKey: true,
autoIncrement: true,
allowNull: false,
comment: '主键ID',
},
store_id: {
type: BIGINT(20).UNSIGNED,
allowNull: false,
references: {
model: 'store',
key: 'id',
},
comment: '仓库ID,关联 wms_store.id(is_warehouse=1),每仓一行',
},
domain: {
type: STRING(100),
allowNull: true,
comment: '仓库自持域名;可空列唯一索引允许多个 NULL,非空值唯一性需应用层校验',
},
domain_status: {
type: TINYINT,
allowNull: false,
defaultValue: 0,
validate: {
isIn: {
args: [[ 0, 1, 2 ]],
msg: '域名验证状态不合法:0-未验证,1-已验证,2-验证失败',
},
},
comment: '域名验证状态:0-未验证,1-已验证,2-验证失败',
},
domain_verify_host: {
type: STRING(255),
allowNull: true,
comment: '待校验 TXT 记录主机名(wmsv-<随机码>.<域名>)',
},
domain_verify_token: {
type: STRING(64),
allowNull: true,
comment: '待校验 TXT 记录值(32 位 hex)',
},
domain_verified_at: {
type: DATE,
allowNull: true,
comment: '域名验证通过时间',
},
domain_verify_attempts: {
type: INTEGER(11),
allowNull: false,
defaultValue: 0,
comment: '域名验证尝试次数',
},
domain_last_error: {
type: STRING(500),
allowNull: true,
comment: '最近一次域名验证失败原因',
},
pending_domain: {
type: STRING(100),
allowNull: true,
comment: '待验证域名:setDomain 只登记到本列,TXT 校验通过后才提升为生效域名 domain',
},
pending_verify_host: {
type: STRING(255),
allowNull: true,
comment: '待验证域名的 TXT 记录主机名(wmsv-<随机码>.<待验证域名>)',
},
pending_verify_token: {
type: STRING(64),
allowNull: true,
comment: '待验证域名的 TXT 记录值(32 位 hex)',
},
pending_verify_attempts: {
type: INTEGER(11),
allowNull: false,
defaultValue: 0,
comment: '待验证域名校验尝试次数',
},
pending_last_error: {
type: STRING(500),
allowNull: true,
comment: '待验证域名最近一次失败原因',
},
pending_created_at: {
type: DATE,
allowNull: true,
comment: '发起域名切换的时间',
},
retired_domain: {
type: STRING(100),
allowNull: true,
comment: '待下线旧域名:域名提升时写入,外部证书脚本删除其 nginx 配置与证书文件后置 NULL',
},
retired_at: {
type: DATE,
allowNull: true,
comment: '旧域名进入下线队列的时间',
},
title: {
type: STRING(100),
allowNull: true,
comment: '站点标题',
},
logo_url: {
type: STRING(255),
allowNull: true,
comment: 'Logo地址',
},
favicon_url: {
type: STRING(255),
allowNull: true,
comment: '站点 favicon 图标地址',
},
login_bg_url: {
type: STRING(255),
allowNull: true,
comment: '登录页背景图地址',
},
theme: {
type: JSON_TYPE,
allowNull: true,
defaultValue: null,
comment: '主题配置JSON(颜色/布局等)',
},
cert_path: {
type: STRING(500),
allowNull: true,
comment: '证书文件路径(只存路径不存内容)',
},
cert_pem: {
type: TEXT,
allowNull: true,
comment: '证书 PEM 内容(私钥不在内);内容仅由定时任务读取,禁止写入日志/接口响应',
},
key_path: {
type: STRING(500),
allowNull: true,
comment: '私钥文件路径(只存路径不存内容)',
},
cert_key: {
type: TEXT,
allowNull: true,
comment: '私钥 PEM 内容;内容仅由定时任务读取,禁止写入日志/接口响应',
},
cert_expire_at: {
type: DATE,
allowNull: true,
comment: '证书到期时间',
},
sync_status: {
type: TINYINT,
allowNull: false,
defaultValue: 0,
validate: {
isIn: {
args: [[ 0, 1, 2 ]],
msg: '同步状态不合法:0-待同步,1-同步成功,2-同步失败',
},
},
comment: '同步状态:0-待同步,1-同步成功,2-同步失败',
},
sync_attempts: {
type: INTEGER(11),
allowNull: false,
defaultValue: 0,
comment: '同步尝试次数',
},
last_sync_error: {
type: STRING(500),
allowNull: true,
comment: '最近一次同步失败原因',
},
last_synced_at: {
type: DATE,
allowNull: true,
comment: '最近一次同步成功时间',
},
created_at: {
type: DATE,
allowNull: false,
defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'),
comment: '创建时间',
},
updated_at: {
type: DATE,
allowNull: false,
defaultValue: app.Sequelize.literal('CURRENT_TIMESTAMP'),
comment: '更新时间',
},
}, {
tableName: 'wms_warehouse_front_config',
freezeTableName: true,
underscored: true,
timestamps: true,
createdAt: 'created_at',
updatedAt: 'updated_at',
paranoid: false,
comment: '仓库前端个性化配置表(含证书内容与元信息)',
indexes: [
{
name: 'uk_store_id',
unique: true,
fields: [ 'store_id' ],
},
{
name: 'uk_domain',
unique: true,
fields: [ 'domain' ],
},
{
name: 'idx_sync_status',
fields: [ 'sync_status' ],
},
{
name: 'idx_domain_status',
fields: [ 'domain_status' ],
},
{
name: 'idx_pending_domain',
fields: [ 'pending_domain' ],
},
{
name: 'idx_retired_domain',
fields: [ 'retired_domain' ],
},
],
});
return WarehouseFrontConfig;
};