|
| 1 | +# API参考 |
| 2 | + |
| 3 | +## 服务端 |
| 4 | + |
| 5 | +### `BaseNotificationChannel` |
| 6 | + |
| 7 | +是用户渠道类型的抽象类,定义了通知渠道需要的接口,扩展新的通知渠道类型需要继承此类,并实现其中的方法 |
| 8 | + |
| 9 | +```ts |
| 10 | +export abstract class BaseNotificationChannel<Message = any> { |
| 11 | + constructor(protected app: Application) {} |
| 12 | + abstract send(params: { |
| 13 | + channel: ChannelOptions; |
| 14 | + message: Message; |
| 15 | + }): Promise<{ message: Message; status: 'success' | 'fail'; reason?: string }>; |
| 16 | +} |
| 17 | +``` |
| 18 | + |
| 19 | +### `PluginNotificationManagerServer` |
| 20 | + |
| 21 | +通知管理服务端插件,提供通知渠道类型注册方法和通知下发方法。 |
| 22 | + |
| 23 | +#### `registerChannelType()` |
| 24 | + |
| 25 | +注册渠道类型的服务端,参考样例 |
| 26 | + |
| 27 | +```ts |
| 28 | +import PluginNotificationManagerServer from '@nocobase/plugin-notification-manager'; |
| 29 | +import { Plugin } from '@nocobase/server'; |
| 30 | +import { ExampleSever } from './example-server'; |
| 31 | +export class PluginNotificationExampleServer extends Plugin { |
| 32 | + async load() { |
| 33 | + const notificationServer = this.pm.get(PluginNotificationManagerServer) as PluginNotificationManagerServer; |
| 34 | + notificationServer.registerChannelType({ type: 'example-sms', Channel: ExampleSever }); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +export default PluginNotificationExampleServer; |
| 39 | +``` |
| 40 | + |
| 41 | +##### 签名 |
| 42 | + |
| 43 | +`registerChannelType({ type, Channel }: {type: string, Channel: BaseNotificationChannel })` |
| 44 | + |
| 45 | +#### `send()` |
| 46 | + |
| 47 | +通知下发方法,调用此方法可下发通知 |
| 48 | + |
| 49 | +```ts |
| 50 | +send('in-app-message', |
| 51 | + message:[ |
| 52 | + receivers: [1,2,3], |
| 53 | + receiverType: 'userId', |
| 54 | + content: '站内信测试', |
| 55 | + title: '站内信测试标题' |
| 56 | + ], |
| 57 | + triggerFrom: 'workflow') |
| 58 | + |
| 59 | + send('email', |
| 60 | + message:[ |
| 61 | + |
| 62 | + receiverType: 'email', |
| 63 | + content: '邮箱测试', |
| 64 | + title: '邮箱测试标题' |
| 65 | + ], |
| 66 | + triggerFrom: 'workflow') |
| 67 | +``` |
| 68 | + |
| 69 | +##### 签名 |
| 70 | + |
| 71 | +`send(sendConfig: {channelName:stirng, message: Object, receivers: ReceiversType, triggerFrom: string })` |
| 72 | + |
| 73 | +接收人`receivers`目前只支持两种格式:nocobase站内用户ID`userId`和渠道特定配置`channel-self-defined` |
| 74 | + |
| 75 | +```ts |
| 76 | +type ReceiversType = |
| 77 | + | { value: number[]; type: 'userId' } |
| 78 | + | { value: any; type: 'channel-self-defined'; channelType: string }; |
| 79 | +``` |
| 80 | + |
| 81 | +##### 详细信息 |
| 82 | + |
| 83 | +sendConfig |
| 84 | + |
| 85 | +| 属性 | 类型 | 描述 | |
| 86 | +| ------------ | ------------ | --------- | |
| 87 | +| `channelName` | `string` | 渠道标识 | |
| 88 | +| `message` | `object` | 消息对象 | |
| 89 | +| `receivers` | `ReceiversType` | 接收人 | |
| 90 | +| `triggerFrom` | `string` | 触发来源 | |
| 91 | + |
| 92 | +## 客户端 |
| 93 | + |
| 94 | +### `PluginNotificationManagerClient` |
| 95 | + |
| 96 | +#### `channelTypes` |
| 97 | + |
| 98 | +已注册渠道类型库 |
| 99 | + |
| 100 | +##### 签名 |
| 101 | + |
| 102 | +`channelTypes: Registry<registerTypeOptions>` |
| 103 | + |
| 104 | +#### `registerChannelType()` |
| 105 | + |
| 106 | +注册客户端渠道类型 |
| 107 | + |
| 108 | +##### 签名 |
| 109 | + |
| 110 | +`registerChannelType(params: registerTypeOptions)` |
| 111 | + |
| 112 | +##### 类型 |
| 113 | + |
| 114 | +```ts |
| 115 | +type registerTypeOptions = { |
| 116 | + title: string; // 渠道显示标题 |
| 117 | + type: string; // 渠道标识 |
| 118 | + components: { |
| 119 | + ChannelConfigForm?: ComponentType // 渠道配置表单组件; |
| 120 | + MessageConfigForm?: ComponentType<{ variableOptions: any }> // 消息配置表单组件; |
| 121 | + ContentConfigForm?: ComponentType<{ variableOptions: any }> // 内容配置表单组件(只是消息内容,不包括接收人的配置); |
| 122 | + }; |
| 123 | + meta?: { // 渠道配置元信息 |
| 124 | + createable?: boolean //是否支持新增渠道; |
| 125 | + eidtable?: boolean //渠道配置信息是否可编辑; |
| 126 | + deletable?: boolean //渠道配置信息是否可删除; |
| 127 | + }; |
| 128 | +}; |
| 129 | + |
| 130 | +type RegisterChannelType = (params: ChannelType) => void |
| 131 | +``` |
0 commit comments