diff --git a/packages/core/client/src/schema-component/common/utils/uitls.tsx b/packages/core/client/src/schema-component/common/utils/uitls.tsx index cb281ce3b3..d23e8cf5ee 100644 --- a/packages/core/client/src/schema-component/common/utils/uitls.tsx +++ b/packages/core/client/src/schema-component/common/utils/uitls.tsx @@ -7,11 +7,8 @@ * For more information, please refer to: https://www.nocobase.com/agreement. */ -import helpers from '@budibase/handlebars-helpers'; -import { dayjs, getPickerFormat } from '@nocobase/utils/client'; -import Handlebars from 'handlebars'; +import { dayjs, getPickerFormat, Handlebars } from '@nocobase/utils/client'; import _, { every, findIndex, some } from 'lodash'; -import url from 'url'; import { replaceVariableValue } from '../../../block-provider/hooks'; import { VariableOption, VariablesContextType } from '../../../variables/types'; import { isVariable } from '../../../variables/utils/isVariable'; @@ -170,36 +167,6 @@ const getVariablesData = (localVariables) => { }); return data; }; -const allHelpers = helpers(); - -//遍历所有 helper 并手动注册到 Handlebars -Object.keys(allHelpers).forEach(function (helperName) { - Handlebars.registerHelper(helperName, allHelpers[helperName]); -}); -// 自定义 helper 来处理对象 -Handlebars.registerHelper('json', function (context) { - return JSON.stringify(context); -}); - -//重写urlParse -Handlebars.registerHelper('urlParse', function (str) { - try { - return JSON.stringify(url.parse(str)); - } catch (error) { - return `Invalid URL: ${str}`; - } -}); - -Handlebars.registerHelper('dateFormat', (date, format, tz) => { - if (typeof tz === 'string') { - return dayjs(date).tz(tz).format(format); - } - return dayjs(date).format(format); -}); - -Handlebars.registerHelper('isNull', (value) => { - return _.isNull(value); -}); export async function getRenderContent(templateEngine, content, variables, localVariables, defaultParse) { if (content && templateEngine === 'handlebars') { diff --git a/packages/core/utils/package.json b/packages/core/utils/package.json index 83f3327ad5..6a2e851f83 100644 --- a/packages/core/utils/package.json +++ b/packages/core/utils/package.json @@ -11,6 +11,7 @@ "deepmerge": "^4.2.2", "flat-to-nested": "^1.1.1", "graphlib": "^2.1.8", + "handlebars": "^4.7.8", "multer": "^1.4.5-lts.1", "object-path": "^0.11.8" }, diff --git a/packages/core/utils/src/client.ts b/packages/core/utils/src/client.ts index c58b8e32fc..a7594ed60a 100644 --- a/packages/core/utils/src/client.ts +++ b/packages/core/utils/src/client.ts @@ -15,6 +15,7 @@ export * from './common'; export * from './date'; export * from './forEach'; export * from './getValuesByPath'; +export * from './handlebars'; export * from './isValidFilter'; export * from './json-templates'; export * from './log'; diff --git a/packages/core/utils/src/handlebars.ts b/packages/core/utils/src/handlebars.ts new file mode 100644 index 0000000000..373fbab172 --- /dev/null +++ b/packages/core/utils/src/handlebars.ts @@ -0,0 +1,48 @@ +/** + * This file is part of the NocoBase (R) project. + * Copyright (c) 2020-2024 NocoBase Co., Ltd. + * Authors: NocoBase Team. + * + * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License. + * For more information, please refer to: https://www.nocobase.com/agreement. + */ + +import url from 'url'; +import Handlebars from 'handlebars'; +import helpers from '@budibase/handlebars-helpers'; +import _ from 'lodash'; + +import { dayjs } from './dayjs'; + +const allHelpers = helpers(); + +//遍历所有 helper 并手动注册到 Handlebars +Object.keys(allHelpers).forEach(function (helperName) { + Handlebars.registerHelper(helperName, allHelpers[helperName]); +}); +// 自定义 helper 来处理对象 +Handlebars.registerHelper('json', function (context) { + return JSON.stringify(context); +}); + +//重写urlParse +Handlebars.registerHelper('urlParse', function (str) { + try { + return JSON.stringify(url.parse(str)); + } catch (error) { + return `Invalid URL: ${str}`; + } +}); + +Handlebars.registerHelper('dateFormat', (date, format, tz) => { + if (typeof tz === 'string') { + return dayjs(date).tz(tz).format(format); + } + return dayjs(date).format(format); +}); + +Handlebars.registerHelper('isNull', (value) => { + return _.isNull(value); +}); + +export { Handlebars }; diff --git a/packages/core/utils/src/index.ts b/packages/core/utils/src/index.ts index 13da3540c8..fb8a5cb474 100644 --- a/packages/core/utils/src/index.ts +++ b/packages/core/utils/src/index.ts @@ -17,6 +17,7 @@ export * from './date'; export * from './dayjs'; export * from './forEach'; export * from './fs-exists'; +export * from './handlebars'; export * from './isValidFilter'; export * from './json-templates'; export * from './koa-multer'; diff --git a/packages/plugins/@nocobase/plugin-notification-manager/src/server/manager.ts b/packages/plugins/@nocobase/plugin-notification-manager/src/server/manager.ts index 06a6c9847f..2286f617ee 100644 --- a/packages/plugins/@nocobase/plugin-notification-manager/src/server/manager.ts +++ b/packages/plugins/@nocobase/plugin-notification-manager/src/server/manager.ts @@ -17,6 +17,7 @@ import type { SendUserOptions, WriteLogOptions, } from './types'; +import { compile } from './utils/compile'; export class NotificationManager implements NotificationManager { private plugin: PluginNotificationManagerServer; @@ -68,8 +69,9 @@ export class NotificationManager implements NotificationManager { } } async sendToUsers(options: SendUserOptions) { - const { userIds, channels, message, data } = options; this.plugin.logger.info(`notificationManager.sendToUsers options: ${JSON.stringify(options)}`); + const { userIds, channels, message: template = {}, data = {} } = options; + const message = compile(template, data); return await Promise.all( channels.map((channelName) => this.send({ channelName, message, triggerFrom: 'sendToUsers', receivers: { value: userIds, type: 'userId' } }), diff --git a/packages/plugins/@nocobase/plugin-notification-manager/src/server/utils/compile.ts b/packages/plugins/@nocobase/plugin-notification-manager/src/server/utils/compile.ts new file mode 100644 index 0000000000..e9496edb58 --- /dev/null +++ b/packages/plugins/@nocobase/plugin-notification-manager/src/server/utils/compile.ts @@ -0,0 +1,33 @@ +/** + * This file is part of the NocoBase (R) project. + * Copyright (c) 2020-2024 NocoBase Co., Ltd. + * Authors: NocoBase Team. + * + * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License. + * For more information, please refer to: https://www.nocobase.com/agreement. + */ + +import { Handlebars } from '@nocobase/utils'; + +export function compile(template: Record, data: Record): Record { + if (!template) { + return {}; + } + const result = Object.keys(template).reduce((object, key) => { + let c; + let value = object[key]; + switch (typeof template[key]) { + case 'object': + value = compile(template[key], data); + break; + case 'string': + c = Handlebars.compile(template[key]); + value = c(data); + break; + default: + break; + } + return Object.assign(object, { [key]: value }); + }, {}); + return result; +}