WMS_MAIN/database/migrations/20260907150000-create-wms-warehouse-front-config.js
liufumingren 018bbd379d feat(warehouse): 前置仓前端配置与按域名公开拉取接口
- 新增模型 wms_warehouse_front_config 与迁移:建表、
  追加 favicon_url、追加 cert_pem(可回滚)
- systems/setting/warehouse.js 实现 getFrontConfig / saveFrontConfig / saveFrontCert、
  域名唯一性校验、按域名缓存读取(含未登记域名的短 TTL 空缓存防穿透)与缓存清理
- systemMethods/setting/warehouse.js 开放对应 system method
- 新增公共免登录接口 frontConfig.getByDomain:按域名返回公开字段
  (domain/title/logo_url/login_bg_url/favicon_url/theme),
  不返回 cert_path/key_path/sync_status 等内部字段;未登记域名返回 404
2026-09-13 15:31:02 +08:00

178 lines
5.6 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';
const FRONT_CONFIG_TABLE = 'wms_warehouse_front_config';
/**
* Migration: 仓库伪独立部署专表(单表终版)。
* - wms_warehouse_front_config:每仓库一行(一仓一域一证书),保存仓库前端
* 个性化配置(Logo / 标题 / 登录页图 / 主题)、证书元信息(只存路径不存
* 证书内容)与 nginx 同步状态机(pending → success/failed)。
* - 证书启用/停用由整行删除或 sync_status 表达;证书到期由 cert_expire_at
* 判断,不单独保留证书 status 列。
* - domain 可空列唯一索引(uk_domain):MySQL 可空列唯一索引允许多个 NULL,
* 非空值的唯一性需应用层校验(详见对应 Model 注释)。
* - 演进说明:原双表方案(wms_warehouse_front_config + wms_warehouse_cert)
* 已废弃;旧版迁移仅在开发库执行过且两表均为空,已 undo 后以本单表版本
* 重跑。生产库从未执行过本迁移,一次发布窗口直接执行本文件即可。
*
* Idempotent: 表已存在时跳过创建。
*/
/**
* 判断表是否存在。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {string} tableName 表名。
* @return {Promise<boolean>} 存在返回 true。
*/
async function tableExists(queryInterface, tableName) {
const tables = await queryInterface.showAllTables();
return tables.some(t => (typeof t === 'string' ? t : (t.tableName || t.name)) === tableName);
}
/**
* 同步状态机字段(nginx 配置下发同步)。
* @param {import('sequelize')} Sequelize Sequelize 实例。
* @return {Object} 字段定义。
*/
function syncStateFields(Sequelize) {
return {
sync_status: {
type: Sequelize.TINYINT,
allowNull: false,
defaultValue: 0,
comment: '同步状态:0-待同步,1-同步成功,2-同步失败',
},
sync_attempts: {
type: Sequelize.INTEGER(11),
allowNull: false,
defaultValue: 0,
comment: '同步尝试次数',
},
last_sync_error: {
type: Sequelize.STRING(500),
allowNull: true,
comment: '最近一次同步失败原因',
},
last_synced_at: {
type: Sequelize.DATE,
allowNull: true,
comment: '最近一次同步成功时间',
},
};
}
/**
* 时间戳字段(创建/更新时间),与项目既有建表迁移一致。
* @param {import('sequelize')} Sequelize Sequelize 实例。
* @return {Object} 字段定义。
*/
function timestampFields(Sequelize) {
return {
created_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
comment: '创建时间',
},
updated_at: {
type: Sequelize.DATE,
allowNull: false,
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
comment: '更新时间',
},
};
}
module.exports = {
/**
* 执行正向迁移:创建仓库前端配置表(单表,含证书元信息)。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @param {import('sequelize')} Sequelize Sequelize 实例。
* @return {Promise<void>} 无返回值。
*/
up: async (queryInterface, Sequelize) => {
if (await tableExists(queryInterface, FRONT_CONFIG_TABLE)) {
return;
}
await queryInterface.createTable(FRONT_CONFIG_TABLE, {
id: {
type: Sequelize.BIGINT(20).UNSIGNED,
primaryKey: true,
autoIncrement: true,
allowNull: false,
comment: '主键ID',
},
store_id: {
type: Sequelize.BIGINT(20).UNSIGNED,
allowNull: false,
comment: '仓库ID,关联 wms_store.id(is_warehouse=1),每仓一行',
},
domain: {
type: Sequelize.STRING(100),
allowNull: true,
comment: '仓库自持域名;可空列唯一索引允许多个 NULL,非空值唯一性需应用层校验',
},
title: {
type: Sequelize.STRING(100),
allowNull: true,
comment: '站点标题',
},
logo_url: {
type: Sequelize.STRING(255),
allowNull: true,
comment: 'Logo地址',
},
login_bg_url: {
type: Sequelize.STRING(255),
allowNull: true,
comment: '登录页背景图地址',
},
theme: {
type: Sequelize.JSON,
allowNull: true,
defaultValue: null,
comment: '主题配置JSON(颜色/布局等)',
},
cert_path: {
type: Sequelize.STRING(500),
allowNull: true,
comment: '证书文件路径(只存路径不存内容)',
},
key_path: {
type: Sequelize.STRING(500),
allowNull: true,
comment: '私钥文件路径(只存路径不存内容)',
},
cert_expire_at: {
type: Sequelize.DATE,
allowNull: true,
comment: '证书到期时间',
},
...syncStateFields(Sequelize),
...timestampFields(Sequelize),
});
await queryInterface.addIndex(FRONT_CONFIG_TABLE, [ 'store_id' ], {
name: 'uk_store_id',
unique: true,
});
await queryInterface.addIndex(FRONT_CONFIG_TABLE, [ 'domain' ], {
name: 'uk_domain',
unique: true,
});
await queryInterface.addIndex(FRONT_CONFIG_TABLE, [ 'sync_status' ], {
name: 'idx_sync_status',
});
},
/**
* 回滚:删除仓库前端配置表。
* @param {import('sequelize').QueryInterface} queryInterface 查询接口。
* @return {Promise<void>} 无返回值。
*/
down: async queryInterface => {
await queryInterface.dropTable(FRONT_CONFIG_TABLE);
},
};