Skip to content

Commit ef78a98

Browse files
author
funcodingdev
committed
feat: 添加菜单项构建函数,更新菜单项描述和字段
1 parent 55bb14d commit ef78a98

3 files changed

Lines changed: 159 additions & 48 deletions

File tree

nodes/WeCom/resources/kf/execute.ts

Lines changed: 77 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,81 @@ function dateTimeToUnixTimestamp(dateTime: string | number): number {
1313
return Math.floor(new Date(dateTime).getTime() / 1000);
1414
}
1515

16+
function buildMsgMenuMessage(
17+
menuItems: IDataObject[],
18+
head_content: string,
19+
tail_content: string,
20+
): IDataObject {
21+
const list = menuItems.map((item: IDataObject) => {
22+
const type = String(item.type || '');
23+
const content = String(item.content || '');
24+
25+
if (type === 'click') {
26+
return {
27+
type,
28+
click: {
29+
id: String(item.reply_content || ''),
30+
content,
31+
},
32+
};
33+
}
34+
35+
if (type === 'view') {
36+
return {
37+
type,
38+
view: {
39+
url: String(item.url || ''),
40+
content,
41+
},
42+
};
43+
}
44+
45+
if (type === 'miniprogram') {
46+
return {
47+
type,
48+
miniprogram: {
49+
appid: String(item.appid || ''),
50+
pagepath: String(item.pagepath || ''),
51+
content,
52+
},
53+
};
54+
}
55+
56+
if (type === 'text') {
57+
const text: IDataObject = {
58+
content,
59+
};
60+
61+
if (item.no_newline !== undefined) {
62+
text.no_newline = item.no_newline ? 1 : 0;
63+
}
64+
65+
return {
66+
type,
67+
text,
68+
};
69+
}
70+
71+
return {
72+
type,
73+
};
74+
});
75+
76+
const messageContent: IDataObject = {
77+
list,
78+
};
79+
80+
if (head_content) {
81+
messageContent.head_content = head_content;
82+
}
83+
84+
if (tail_content) {
85+
messageContent.tail_content = tail_content;
86+
}
87+
88+
return messageContent;
89+
}
90+
1691
export async function executeKf(
1792
this: IExecuteFunctions,
1893
operation: string,
@@ -187,27 +262,7 @@ export async function executeKf(
187262
const head_content = this.getNodeParameter('msgmenu_head_content', i) as string;
188263
const tail_content = this.getNodeParameter('msgmenu_tail_content', i, '') as string;
189264
const menuItems = this.getNodeParameter('msgmenu_list.items', i, []) as IDataObject[];
190-
191-
const list = menuItems.map((item: IDataObject) => {
192-
const menuItem: IDataObject = {
193-
type: item.type,
194-
content: item.content,
195-
};
196-
197-
if (item.type === 'click') {
198-
menuItem.id = item.reply_content || '';
199-
} else if (item.type === 'view') {
200-
menuItem.url = item.url || '';
201-
} else if (item.type === 'miniprogram') {
202-
menuItem.appid = item.appid || '';
203-
menuItem.pagepath = item.pagepath || '';
204-
}
205-
206-
return menuItem;
207-
});
208-
209-
messageContent = { head_content, list };
210-
if (tail_content) messageContent.tail_content = tail_content;
265+
messageContent = buildMsgMenuMessage(menuItems, head_content, tail_content);
211266
} else if (msgtype === 'location') {
212267
const name = this.getNodeParameter('location_name', i) as string;
213268
const address = this.getNodeParameter('location_address', i) as string;
@@ -243,27 +298,7 @@ export async function executeKf(
243298
const head_content = this.getNodeParameter('msgmenu_head_content', i) as string;
244299
const tail_content = this.getNodeParameter('msgmenu_tail_content', i, '') as string;
245300
const menuItems = this.getNodeParameter('msgmenu_list.items', i, []) as IDataObject[];
246-
247-
const list = menuItems.map((item: IDataObject) => {
248-
const menuItem: IDataObject = {
249-
type: item.type,
250-
content: item.content,
251-
};
252-
253-
if (item.type === 'click') {
254-
menuItem.id = item.reply_content || '';
255-
} else if (item.type === 'view') {
256-
menuItem.url = item.url || '';
257-
} else if (item.type === 'miniprogram') {
258-
menuItem.appid = item.appid || '';
259-
menuItem.pagepath = item.pagepath || '';
260-
}
261-
262-
return menuItem;
263-
});
264-
265-
messageContent = { head_content, list };
266-
if (tail_content) messageContent.tail_content = tail_content;
301+
messageContent = buildMsgMenuMessage(menuItems, head_content, tail_content);
267302
} else {
268303
throw new NodeOperationError(
269304
this.getNode(),

nodes/WeCom/resources/kf/sendKfEventMsg.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,18 +111,28 @@ export const sendKfEventMsgDescription: INodeProperties[] = [
111111
displayName: '菜单项',
112112
values: [
113113
{
114-
displayName: '回复内容',
114+
displayName: '菜单 ID',
115115
name: 'reply_content',
116116
type: 'string',
117117
default: '',
118-
description: '点击后自动回复的内容(菜单ID)',
119-
placeholder: 'product_inquiry',
118+
displayOptions: {
119+
show: {
120+
type: ['click'],
121+
},
122+
},
123+
description: '点击菜单的唯一 ID',
124+
placeholder: 'menu_101',
120125
},
121126
{
122127
displayName: '小程序AppID',
123128
name: 'appid',
124129
type: 'string',
125130
default: '',
131+
displayOptions: {
132+
show: {
133+
type: ['miniprogram'],
134+
},
135+
},
126136
description: '小程序的AppID',
127137
placeholder: 'wx1234567890abcdef',
128138
},
@@ -131,13 +141,19 @@ export const sendKfEventMsgDescription: INodeProperties[] = [
131141
name: 'pagepath',
132142
type: 'string',
133143
default: '',
144+
displayOptions: {
145+
show: {
146+
type: ['miniprogram'],
147+
},
148+
},
134149
description: '小程序的页面路径',
135150
placeholder: 'pages/index/index',
136151
},
137152
{
138153
displayName: '菜单文案',
139154
name: 'content',
140155
type: 'string',
156+
required: true,
141157
default: '',
142158
description: '菜单项显示的文字',
143159
placeholder: '产品咨询',
@@ -162,6 +178,11 @@ export const sendKfEventMsgDescription: INodeProperties[] = [
162178
value: 'miniprogram',
163179
description: '点击后打开小程序',
164180
},
181+
{
182+
name: '文本',
183+
value: 'text',
184+
description: '纯文本项,可与其他菜单类型混排',
185+
},
165186
],
166187
default: 'click',
167188
description: '菜单项的类型',
@@ -171,9 +192,26 @@ export const sendKfEventMsgDescription: INodeProperties[] = [
171192
name: 'url',
172193
type: 'string',
173194
default: '',
195+
displayOptions: {
196+
show: {
197+
type: ['view'],
198+
},
199+
},
174200
description: '点击后跳转的URL',
175201
placeholder: 'https://example.com',
176202
},
203+
{
204+
displayName: '内容后不换行',
205+
name: 'no_newline',
206+
type: 'boolean',
207+
default: false,
208+
displayOptions: {
209+
show: {
210+
type: ['text'],
211+
},
212+
},
213+
description: '仅文本菜单项可用,开启后对应 no_newline=1',
214+
},
177215
],
178216
},
179217
],

nodes/WeCom/resources/kf/sendKfMsg.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,18 +360,28 @@ export const sendKfMsgDescription: INodeProperties[] = [
360360
displayName: '菜单项',
361361
values: [
362362
{
363-
displayName: '回复内容',
363+
displayName: '菜单 ID',
364364
name: 'reply_content',
365365
type: 'string',
366366
default: '',
367-
description: '点击后自动回复的内容',
368-
placeholder: '您选择了选项1',
367+
displayOptions: {
368+
show: {
369+
type: ['click'],
370+
},
371+
},
372+
description: '点击菜单的唯一 ID,建议只使用字母、数字和下划线',
373+
placeholder: 'menu_101',
369374
},
370375
{
371376
displayName: '小程序AppID',
372377
name: 'appid',
373378
type: 'string',
374379
default: '',
380+
displayOptions: {
381+
show: {
382+
type: ['miniprogram'],
383+
},
384+
},
375385
description: '小程序的AppID',
376386
placeholder: 'wx1234567890abcdef',
377387
},
@@ -380,13 +390,19 @@ export const sendKfMsgDescription: INodeProperties[] = [
380390
name: 'pagepath',
381391
type: 'string',
382392
default: '',
393+
displayOptions: {
394+
show: {
395+
type: ['miniprogram'],
396+
},
397+
},
383398
description: '小程序的页面路径',
384399
placeholder: 'pages/index/index',
385400
},
386401
{
387402
displayName: '菜单文案',
388403
name: 'content',
389404
type: 'string',
405+
required: true,
390406
default: '',
391407
description: '菜单项显示的文字',
392408
placeholder: '选项1',
@@ -411,6 +427,11 @@ export const sendKfMsgDescription: INodeProperties[] = [
411427
value: 'miniprogram',
412428
description: '点击后打开小程序',
413429
},
430+
{
431+
name: '文本',
432+
value: 'text',
433+
description: '纯文本项,可与其他菜单类型混排',
434+
},
414435
],
415436
default: 'click',
416437
description: '菜单项的类型',
@@ -420,9 +441,26 @@ export const sendKfMsgDescription: INodeProperties[] = [
420441
name: 'url',
421442
type: 'string',
422443
default: '',
444+
displayOptions: {
445+
show: {
446+
type: ['view'],
447+
},
448+
},
423449
description: '点击后跳转的URL',
424450
placeholder: 'https://example.com',
425451
},
452+
{
453+
displayName: '内容后不换行',
454+
name: 'no_newline',
455+
type: 'boolean',
456+
default: false,
457+
displayOptions: {
458+
show: {
459+
type: ['text'],
460+
},
461+
},
462+
description: '仅文本菜单项可用,开启后对应 no_newline=1',
463+
},
426464
],
427465
},
428466
],

0 commit comments

Comments
 (0)