120 lines
3.3 KiB
JavaScript
120 lines
3.3 KiB
JavaScript
"use strict";
|
||
|
||
import Joi from "joi";
|
||
|
||
/**
|
||
* 每个应用的用户个人配置白名单
|
||
*
|
||
* Joi 对象默认拒绝未声明的 key,客户端传 { role: ["admin"] } 会直接 400,
|
||
* 无法借配置接口改权限。键默认可选,因此同一份 schema 既能校验全量也能校验部分更新。
|
||
*/
|
||
export const APP_CONFIG_SCHEMAS = {
|
||
"wxapp-escort": Joi.object({
|
||
theme: Joi.string().valid("light", "dark", "system"),
|
||
language: Joi.string().valid("zh-CN", "en-US"),
|
||
notifications: Joi.object({
|
||
orderStatus: Joi.boolean(),
|
||
marketing: Joi.boolean(),
|
||
}),
|
||
}),
|
||
|
||
"wxapp_companion": Joi.object({
|
||
pageSize: Joi.number().integer().min(10).max(200),
|
||
defaultFilter: Joi.string().valid("all", "pending", "done"),
|
||
soundAlert: Joi.boolean(),
|
||
}),
|
||
|
||
tcm: Joi.object({
|
||
theme: Joi.string().valid("light", "dark", "system"),
|
||
language: Joi.string().valid("zh-CN", "en-US"),
|
||
}),
|
||
};
|
||
|
||
/**
|
||
* 默认值放代码里,数据库只保存用户改过的项。
|
||
* 默认值调整后对未覆盖该 key 的用户立即生效,不需要批量回填。
|
||
*/
|
||
export const APP_CONFIG_DEFAULTS = {
|
||
"wxapp-escort": {
|
||
theme: "system",
|
||
language: "zh-CN",
|
||
notifications: { orderStatus: true, marketing: false },
|
||
},
|
||
"wxapp_companion": {
|
||
pageSize: 100,
|
||
defaultFilter: "all",
|
||
soundAlert: true,
|
||
},
|
||
tcm: {
|
||
theme: "system",
|
||
language: "zh-CN",
|
||
},
|
||
};
|
||
|
||
export function hasConfigSchema(appId) {
|
||
return !!APP_CONFIG_SCHEMAS[appId];
|
||
}
|
||
|
||
export function validateAppConfig(appId, input) {
|
||
const schema = APP_CONFIG_SCHEMAS[appId];
|
||
if (!schema) throw new Error(`未配置 ${appId} 的 config schema`);
|
||
|
||
const { error, value } = schema.validate(input, { abortEarly: false });
|
||
if (error) throw new Error(error.details[0].message);
|
||
|
||
// 单个用户的单应用配置限制 8KB,防止被当成任意存储
|
||
if (Buffer.byteLength(JSON.stringify(value)) > 8192) {
|
||
throw new Error("应用配置过大");
|
||
}
|
||
return value;
|
||
}
|
||
|
||
export function isPlainObject(value) {
|
||
return !!value && typeof value === "object" && !Array.isArray(value) && !(value instanceof Date);
|
||
}
|
||
|
||
/**
|
||
* Map(Mongoose / JS Map)与 lean 返回的普通对象都要兼容
|
||
*/
|
||
export function plainConfig(value) {
|
||
if (!value) return {};
|
||
if (value instanceof Map) return Object.fromEntries(value);
|
||
return value;
|
||
}
|
||
|
||
/**
|
||
* 合并默认值与已存配置:已知嵌套对象做一层深合并,
|
||
* 库里只存了部分字段时默认值仍能补齐。
|
||
*/
|
||
export function resolveConfig(appId, stored) {
|
||
const defaults = APP_CONFIG_DEFAULTS[appId] || {};
|
||
const current = plainConfig(stored);
|
||
const out = { ...defaults };
|
||
|
||
for (const [key, value] of Object.entries(current || {})) {
|
||
out[key] = isPlainObject(defaults[key]) && isPlainObject(value)
|
||
? { ...defaults[key], ...value }
|
||
: value;
|
||
}
|
||
|
||
return out;
|
||
}
|
||
|
||
/**
|
||
* 展开为叶子路径:{ notifications: { marketing: false } }
|
||
* -> { "notifications.marketing": false }
|
||
* 数组和标量整体替换,不做逐项合并。
|
||
*/
|
||
export function flattenLeaves(input, base = "") {
|
||
const out = {};
|
||
for (const [key, value] of Object.entries(input || {})) {
|
||
const path = base ? `${base}.${key}` : key;
|
||
if (isPlainObject(value)) {
|
||
Object.assign(out, flattenLeaves(value, path));
|
||
} else {
|
||
out[path] = value;
|
||
}
|
||
}
|
||
return out;
|
||
}
|