'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; };