'use strict'; const MainService = require('../../main'); class SettingWarehouseMethodsService extends MainService { constructor(ctx) { super(ctx); const _noNeedLogin = ''; const _noNeedRight = 'getSettings,getFieldMeta,saveSettings,getFrontConfig,saveFrontConfig,saveFrontCert,getweChatApp'; const _noNeedLog = '*'; const _noNeedAuth = 'getSettings,getFieldMeta,saveSettings,getFrontConfig,saveFrontConfig,saveFrontCert,getweChatApp'; this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth); } /** * 获取仓库设置(subject_config)。 * * @return {Promise} 统一响应结果。 */ 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} 统一响应结果。 */ 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} 统一响应结果。 */ 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} 统一响应结果。 */ 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} 统一响应结果。 */ 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} 统一响应结果。 */ 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, }); } } /** * 获取当前仓库启用的微信小程序授权配置(wms_wechat_app)。 * * 仅返回一条启用(is_enabled=1)记录,配置统一封装在响应 data 字段中, * data 为对象而非数组;无可用记录时 data 为 null。 * * @return {Promise} 统一响应结果。 */ async getweChatApp() { const targetService = this.service && this.service.systems && this.service.systems.setting && this.service.systems.setting.warehouse; if (!targetService || typeof targetService.getWechatApp !== 'function') { return this.fail('获取微信小程序配置失败', { code: 500 }); } try { const result = await targetService.getWechatApp(); return this.success('ok', { data: result }); } catch (error) { return this.fail(error.message || '获取微信小程序配置失败', { code: Number(error.code) || 500, }); } } } module.exports = SettingWarehouseMethodsService;