@kne/fastify-message 1.0.4 → 1.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -7,6 +7,7 @@ module.exports = fp(async function (fastify, options) {
7
7
  {
8
8
  name: 'message',
9
9
  dbTableNamePrefix: 't_message_',
10
+ prefix: '/api/v1/message',
10
11
  getUserModel: () => {
11
12
  if (!fastify.account) {
12
13
  throw new Error('fastify-account plugin must be registered before fastify-trtc-conference,or set options.getUserModel');
@@ -16,9 +16,15 @@ module.exports = fp(async (fastify, options) => {
16
16
  properties: {
17
17
  currentPage: { type: 'integer', default: 1, description: '页码' },
18
18
  perPage: { type: 'integer', default: 20, description: '每页数量' },
19
- 'filter[type]': { type: 'integer', description: '发送类型: 0=邮件, 1=短信' },
20
- 'filter[code]': { type: 'string', description: '模板编码' },
21
- 'filter[name]': { type: 'string', description: '发送对象(邮箱/手机号)' }
19
+ filter: {
20
+ type: 'object',
21
+ default: {},
22
+ properties: {
23
+ type: { type: 'integer', description: '发送类型: 0=邮件, 1=短信' },
24
+ code: { type: 'string', description: '模板编码' },
25
+ name: { type: 'string', description: '发送对象(邮箱/手机号)' }
26
+ }
27
+ }
22
28
  }
23
29
  },
24
30
  response: {
@@ -34,11 +40,11 @@ module.exports = fp(async (fastify, options) => {
34
40
  type: 'object',
35
41
  properties: {
36
42
  id: { type: 'string', description: '记录ID' },
37
- name: { type: 'string', description: '发送对象' },
38
- type: { type: 'integer', description: '发送类型' },
43
+ name: { type: 'string', description: '发送对象(邮箱/手机号)' },
44
+ type: { type: 'integer', description: '发送类型: 0=邮件, 1=短信' },
39
45
  code: { type: 'string', description: '模板编码' },
40
- props: { type: 'object', description: '模板变量' },
41
- content: { type: 'object', description: '消息内容' },
46
+ props: { type: 'object', description: '模板变量', additionalProperties: true },
47
+ content: { type: 'object', description: '消息内容', additionalProperties: true },
42
48
  templateId: { type: 'string', description: '模板ID' },
43
49
  createdAt: { type: 'string', description: '创建时间' }
44
50
  }
@@ -50,15 +56,55 @@ module.exports = fp(async (fastify, options) => {
50
56
  }
51
57
  },
52
58
  async request => {
53
- const { currentPage = 1, perPage = 20 } = request.query;
54
- const { 'filter[type]': type, 'filter[code]': code, 'filter[name]': name } = request.query;
59
+ const { currentPage = 1, perPage = 20, filter = {} } = request.query;
60
+ const { type, code, name } = filter;
55
61
 
56
- const filter = {};
57
- if (type !== undefined) filter.type = type;
58
- if (code) filter.code = code;
59
- if (name) filter.name = name;
62
+ const where = {};
63
+ if (type !== undefined) where.type = type;
64
+ if (code) where.code = code;
65
+ if (name) where.name = name;
60
66
 
61
- return await services.record.list({ filter, perPage, currentPage });
67
+ return await services.record.list({ filter: where, perPage, currentPage });
68
+ }
69
+ );
70
+
71
+ // 发送消息
72
+ fastify.post(
73
+ `${options.prefix}/templates/send`,
74
+ {
75
+ onRequest: options.getAuthenticate('template:send'),
76
+ schema: {
77
+ description: '根据模版发送消息',
78
+ summary: '发送消息',
79
+ body: {
80
+ type: 'object',
81
+ required: ['templateId', 'name'],
82
+ properties: {
83
+ templateId: { type: 'string', description: '模版ID' },
84
+ name: { type: 'string', description: '发送对象(邮箱/手机号)' },
85
+ props: { type: 'object', description: '模版变量', additionalProperties: true }
86
+ }
87
+ },
88
+ response: {
89
+ 200: {
90
+ type: 'object',
91
+ properties: {
92
+ success: { type: 'boolean', description: '是否发送成功' }
93
+ }
94
+ }
95
+ }
96
+ }
97
+ },
98
+ async request => {
99
+ const { templateId, name, props = {} } = request.body;
100
+ try {
101
+ return await services.template.send({ templateId, name, props });
102
+ } catch (error) {
103
+ if (error.message === '模版已禁用,无法发送消息') {
104
+ throw fastify.httpErrors.badRequest(error.message);
105
+ }
106
+ throw fastify.httpErrors.notFound(error.message);
107
+ }
62
108
  }
63
109
  );
64
110
 
@@ -82,11 +128,11 @@ module.exports = fp(async (fastify, options) => {
82
128
  type: 'object',
83
129
  properties: {
84
130
  id: { type: 'string', description: '记录ID' },
85
- name: { type: 'string', description: '发送对象' },
86
- type: { type: 'integer', description: '发送类型' },
131
+ name: { type: 'string', description: '发送对象(邮箱/手机号)' },
132
+ type: { type: 'integer', description: '发送类型: 0=邮件, 1=短信' },
87
133
  code: { type: 'string', description: '模板编码' },
88
- props: { type: 'object', description: '模板变量' },
89
- content: { type: 'object', description: '消息内容' },
134
+ props: { type: 'object', description: '模板变量', additionalProperties: true },
135
+ content: { type: 'object', description: '消息内容', additionalProperties: true },
90
136
  templateId: { type: 'string', description: '模板ID' },
91
137
  createdAt: { type: 'string', description: '创建时间' }
92
138
  }
@@ -117,10 +163,16 @@ module.exports = fp(async (fastify, options) => {
117
163
  properties: {
118
164
  currentPage: { type: 'integer', default: 1, description: '页码' },
119
165
  perPage: { type: 'integer', default: 20, description: '每页数量' },
120
- 'filter[type]': { type: 'integer', description: '模版类型: 0=邮件, 1=短信' },
121
- 'filter[code]': { type: 'string', description: '模版编码' },
122
- 'filter[level]': { type: 'integer', description: '模版级别: 0=系统, 1=业务' },
123
- 'filter[status]': { type: 'integer', description: '状态: 0=启用, 1=禁用' }
166
+ filter: {
167
+ type: 'object',
168
+ default: {},
169
+ properties: {
170
+ type: { type: 'integer', description: '模版类型: 0=邮件, 1=短信' },
171
+ code: { type: 'string', description: '模版编码' },
172
+ level: { type: 'integer', description: '模版级别: 0=系统, 1=业务' },
173
+ status: { type: 'integer', description: '状态: 0=启用, 1=禁用' }
174
+ }
175
+ }
124
176
  }
125
177
  },
126
178
  response: {
@@ -138,9 +190,9 @@ module.exports = fp(async (fastify, options) => {
138
190
  id: { type: 'string', description: '模版ID' },
139
191
  name: { type: 'string', description: '模版名称' },
140
192
  code: { type: 'string', description: '模版编码' },
141
- type: { type: 'integer', description: '模版类型' },
142
- level: { type: 'integer', description: '模版级别' },
143
- status: { type: 'integer', description: '状态' },
193
+ type: { type: 'integer', description: '模版类型: 0=邮件, 1=短信' },
194
+ level: { type: 'integer', description: '模版级别: 0=系统, 1=业务' },
195
+ status: { type: 'integer', description: '状态: 0=启用, 1=禁用' },
144
196
  content: { type: 'string', description: '模版内容' },
145
197
  createdAt: { type: 'string', description: '创建时间' }
146
198
  }
@@ -152,16 +204,16 @@ module.exports = fp(async (fastify, options) => {
152
204
  }
153
205
  },
154
206
  async request => {
155
- const { currentPage = 1, perPage = 20 } = request.query;
156
- const { 'filter[type]': type, 'filter[code]': code, 'filter[level]': level, 'filter[status]': status } = request.query;
207
+ const { currentPage = 1, perPage = 20, filter = {} } = request.query;
208
+ const { type, code, level, status } = filter;
157
209
 
158
- const filter = {};
159
- if (type !== undefined) filter.type = type;
160
- if (code) filter.code = code;
161
- if (level !== undefined) filter.level = level;
162
- if (status !== undefined) filter.status = status;
210
+ const where = {};
211
+ if (type !== undefined) where.type = type;
212
+ if (code) where.code = code;
213
+ if (level !== undefined) where.level = level;
214
+ if (status !== undefined) where.status = status;
163
215
 
164
- return await services.template.list({ filter, perPage, currentPage });
216
+ return await services.template.list({ filter: where, perPage, currentPage });
165
217
  }
166
218
  );
167
219
 
@@ -187,9 +239,9 @@ module.exports = fp(async (fastify, options) => {
187
239
  id: { type: 'string', description: '模版ID' },
188
240
  name: { type: 'string', description: '模版名称' },
189
241
  code: { type: 'string', description: '模版编码' },
190
- type: { type: 'integer', description: '模版类型' },
191
- level: { type: 'integer', description: '模版级别' },
192
- status: { type: 'integer', description: '状态' },
242
+ type: { type: 'integer', description: '模版类型: 0=邮件, 1=短信' },
243
+ level: { type: 'integer', description: '模版级别: 0=系统, 1=业务' },
244
+ status: { type: 'integer', description: '状态: 0=启用, 1=禁用' },
193
245
  content: { type: 'string', description: '模版内容' },
194
246
  createdAt: { type: 'string', description: '创建时间' }
195
247
  }
@@ -174,6 +174,24 @@ module.exports = fp(async (fastify, options) => {
174
174
  throw new Error('模版不存在');
175
175
  }
176
176
  return template;
177
+ },
178
+
179
+ send: async ({ templateId, name, props = {} }) => {
180
+ const tpl = await models.template.findByPk(templateId);
181
+ if (!tpl) {
182
+ throw new Error('模版不存在');
183
+ }
184
+ if (tpl.status !== 0) {
185
+ throw new Error('模版已禁用,无法发送消息');
186
+ }
187
+ await sendMessage({
188
+ type: tpl.type,
189
+ name,
190
+ props,
191
+ code: tpl.code,
192
+ level: tpl.level
193
+ });
194
+ return { success: true };
177
195
  }
178
196
  }
179
197
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kne/fastify-message",
3
- "version": "1.0.4",
3
+ "version": "1.0.5",
4
4
  "description": "管理消息发送,支持sms消息,email消息等.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -55,6 +55,7 @@
55
55
  "nodemon": "^3.1.3",
56
56
  "nyc": "^17.1.0",
57
57
  "prettier": "^3.2.5",
58
+ "qs": "^6.15.1",
58
59
  "sqlite3": "^6.0.1"
59
60
  },
60
61
  "dependencies": {