- 新增模型 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
189 lines
7.1 KiB
JavaScript
189 lines
7.1 KiB
JavaScript
'use strict';
|
||
|
||
const MainService = require('../../main');
|
||
|
||
class SettingWarehouseMethodsService extends MainService {
|
||
constructor(ctx) {
|
||
super(ctx);
|
||
const _noNeedLogin = '';
|
||
const _noNeedRight = 'getSettings,getFieldMeta,saveSettings,getFrontConfig,saveFrontConfig,saveFrontCert';
|
||
const _noNeedLog = '*';
|
||
const _noNeedAuth = 'getSettings,getFieldMeta,saveSettings,getFrontConfig,saveFrontConfig,saveFrontCert';
|
||
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
|
||
}
|
||
|
||
/**
|
||
* 获取仓库设置(subject_config)。
|
||
*
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async getSettings() {
|
||
const targetService = this.service && this.service.systems && this.service.systems.setting && this.service.systems.setting.warehouse;
|
||
|
||
if (!targetService || typeof targetService.getSettings !== 'function') {
|
||
return this.fail('获取仓库设置失败', { code: 500 });
|
||
}
|
||
|
||
try {
|
||
const result = await targetService.getSettings();
|
||
return this.success('ok', result);
|
||
} catch (error) {
|
||
return this.fail(error.message || '获取仓库设置失败', {
|
||
code: Number(error.code) || 500,
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取仓库设置字段元信息列表。
|
||
*
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async getFieldMeta() {
|
||
const targetService = this.service && this.service.systems && this.service.systems.setting && this.service.systems.setting.warehouse;
|
||
|
||
if (!targetService || typeof targetService.getFieldMeta !== 'function') {
|
||
return this.fail('获取仓库设置字段信息失败', { code: 500 });
|
||
}
|
||
|
||
try {
|
||
const list = await targetService.getFieldMeta();
|
||
return this.success('ok', { list });
|
||
} catch (error) {
|
||
return this.fail(error.message || '获取仓库设置字段信息失败', {
|
||
code: Number(error.code) || 500,
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 修改仓库设置并清理当前仓库缓存。
|
||
*
|
||
* @param {Object} data 请求参数。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async saveSettings(data = {}) {
|
||
const targetService = this.service && this.service.systems && this.service.systems.setting && this.service.systems.setting.warehouse;
|
||
|
||
if (!targetService || typeof targetService.saveSettings !== 'function') {
|
||
return this.fail('保存仓库设置失败', { code: 500 });
|
||
}
|
||
|
||
try {
|
||
const { subject_config } = data || {};
|
||
const result = await targetService.saveSettings({ subject_config });
|
||
return this.success('ok', result);
|
||
} catch (error) {
|
||
return this.fail(error.message || '保存仓库设置失败', {
|
||
code: Number(error.code) || 500,
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 查询当前仓库前端独立部署配置(settings 页回显)。
|
||
*
|
||
* 响应含证书原文(cert_pem)与私钥原文(key_pem)回显,
|
||
* 仅限登录用户查询其仓库上下文绑定的配置,供仓库主体 settings 页回显;
|
||
* 公共无鉴权接口 frontConfig.getByDomain 不经过本出口,仍只返回公开字段白名单。
|
||
*
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async getFrontConfig() {
|
||
const targetService = this.service && this.service.systems && this.service.systems.setting && this.service.systems.setting.warehouse;
|
||
|
||
if (!targetService || typeof targetService.getFrontConfig !== 'function') {
|
||
return this.fail('获取仓库前端配置失败', { code: 500 });
|
||
}
|
||
|
||
try {
|
||
const result = await targetService.getFrontConfig();
|
||
return this.success('ok', result);
|
||
} catch (error) {
|
||
return this.fail(error.message || '获取仓库前端配置失败', {
|
||
code: Number(error.code) || 500,
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 保存当前仓库前端独立部署品牌配置(upsert,一仓一行,仅品牌字段部分更新)。
|
||
*
|
||
* domain 与证书字段不再受理:domain 变更必须走 saveFrontCert 重新校验证书;
|
||
* cert_pem/key_pem/cert_path/key_path/cert_expire_at 一律被白名单过滤忽略。
|
||
*
|
||
* @param {Object} data 请求参数。
|
||
* @param {string} [data.title] 站点标题(≤100 字符,空值清空)。
|
||
* @param {string} [data.logo_url] Logo 地址(≤255 字符,空值清空)。
|
||
* @param {string} [data.favicon_url] 站点 favicon 图标地址(≤255 字符,空值清空)。
|
||
* @param {string} [data.login_bg_url] 登录页背景图地址(≤255 字符,空值清空)。
|
||
* @param {Object|string} [data.theme] 主题配置(合法 JSON 对象或 JSON 字符串,空值清空)。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async saveFrontConfig(data = {}) {
|
||
const targetService = this.service && this.service.systems && this.service.systems.setting && this.service.systems.setting.warehouse;
|
||
|
||
if (!targetService || typeof targetService.saveFrontConfig !== 'function') {
|
||
return this.fail('保存仓库前端配置失败', { code: 500 });
|
||
}
|
||
|
||
try {
|
||
const source = data || {};
|
||
const params = {};
|
||
|
||
// domain/cert_path/key_path/cert_pem/key_pem/cert_expire_at 均不受理:
|
||
// domain 变更走 saveFrontCert,证书与有效期字段由后端权威维护,前端传值直接忽略
|
||
for (const key of [ 'title', 'logo_url', 'favicon_url', 'login_bg_url', 'theme' ]) {
|
||
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
||
params[key] = source[key];
|
||
}
|
||
}
|
||
|
||
const result = await targetService.saveFrontConfig(params);
|
||
return this.success('ok', result);
|
||
} catch (error) {
|
||
return this.fail(error.message || '保存仓库前端配置失败', {
|
||
code: Number(error.code) || 500,
|
||
});
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 上传当前仓库前端独立部署证书(domain + 证书/私钥成对,需登录、仅仓库主体)。
|
||
*
|
||
* 后端自动写入 cert_expire_at(证书 validTo 权威值),证书/私钥内容直接入库(不落盘),
|
||
* 保存后清理新旧 domain 的公共拉取缓存并返回完整配置输出
|
||
* (响应含证书/私钥原文回显,口径与 getFrontConfig 一致)。
|
||
*
|
||
* @param {Object} data 请求参数。
|
||
* @param {string} data.domain 仓库自持域名(必填,非空,≤100 字符,全局唯一)。
|
||
* @param {string} data.pem 证书 PEM 文本(必填)。
|
||
* @param {string} data.key 私钥 PEM 文本(必填,与证书成对)。
|
||
* @return {Promise<Object>} 统一响应结果。
|
||
*/
|
||
async saveFrontCert(data = {}) {
|
||
const targetService = this.service && this.service.systems && this.service.systems.setting && this.service.systems.setting.warehouse;
|
||
|
||
if (!targetService || typeof targetService.saveFrontCert !== 'function') {
|
||
return this.fail('上传前端证书失败', { code: 500 });
|
||
}
|
||
|
||
try {
|
||
const source = data || {};
|
||
const { domain, pem, key } = source;
|
||
const result = await targetService.saveFrontCert({
|
||
domain,
|
||
pem,
|
||
key,
|
||
});
|
||
return this.success('ok', result);
|
||
} catch (error) {
|
||
return this.fail(error.message || '上传前端证书失败', {
|
||
code: Number(error.code) || 500,
|
||
});
|
||
}
|
||
}
|
||
}
|
||
|
||
module.exports = SettingWarehouseMethodsService;
|