From eeb8c8afae7a4c25332a0d88361b7ebdad0775a3 Mon Sep 17 00:00:00 2001 From: Lei OT Date: Tue, 28 May 2024 16:09:16 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E6=A8=A1=E6=9D=BF:=20=E5=A4=B4?= =?UTF-8?q?=E9=83=A8=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/channel/whatsappUtils.js | 36 +++++++++---------- .../Conversations/Online/Input/Template.jsx | 15 +++++--- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/channel/whatsappUtils.js b/src/channel/whatsappUtils.js index 54e2f7c..8bb54ba 100644 --- a/src/channel/whatsappUtils.js +++ b/src/channel/whatsappUtils.js @@ -160,20 +160,18 @@ export const sentMsgTypeMapped = { msgcontent: { ...msg.template, components: [ - ...msg.template.components.filter((com) => !['footer', 'buttons', 'header'].includes(com.type.toLowerCase())), - ...(msg.template.components.filter((com) => 'header' === com.type.toLowerCase()).length > 0 - ? msg.template.components - .filter((com) => 'header' === com.type.toLowerCase()) - .map((ele) => ({ type: 'header', parameters: [{ text: ele.text, type: ele.format.toLowerCase(), [ele.format.toLowerCase()]: { link: ele.example.header_url[0] } }] })) - : []), + ...msg.template.components.filter((com) => !['footer', 'buttons'].includes(com.type.toLowerCase())), ...(msg.template.components.filter((com) => 'buttons' === com.type.toLowerCase()).length > 0 ? msg.template.components .filter((com) => 'buttons' === com.type.toLowerCase())[0] // .buttons.filter((btns) => ! ['phone_number', 'url'].includes( btns.type.toLowerCase())) - .buttons.filter((btns) => ! isEmpty(btns.example)) // 静态按钮不发 - .map((btn, btnI) => ({ type: 'button', sub_type: btn.type.toLowerCase(), index: btnI, - // parameters: [{ text: 'lq1FTtA8', type: 'text' }] - })) + .buttons.filter((btns) => !isEmpty(btns.example)) // 静态按钮不发 + .map((btn, btnI) => ({ + type: 'button', + sub_type: btn.type.toLowerCase(), + index: btnI, + // parameters: [{ text: 'lq1FTtA8', type: 'text' }] + })) : []), ], }, @@ -188,20 +186,18 @@ export const sentMsgTypeMapped = { template: { ...msg.template, components: [ - ...msg.template.components.filter((com) => !['footer', 'buttons', 'header'].includes(com.type.toLowerCase())), - ...(msg.template.components.filter((com) => 'header' === com.type.toLowerCase()).length > 0 - ? msg.template.components - .filter((com) => 'header' === com.type.toLowerCase()) - .map((ele) => ({ type: 'header', parameters: [{ text: ele.text, type: ele.format.toLowerCase(), [ele.format.toLowerCase()]: { link: ele.example.header_url[0] } }] })) - : []), + ...msg.template.components.filter((com) => !['footer', 'buttons'].includes(com.type.toLowerCase())), ...(msg.template.components.filter((com) => 'buttons' === com.type.toLowerCase()).length > 0 ? msg.template.components .filter((com) => 'buttons' === com.type.toLowerCase())[0] // .buttons.filter((btns) => ! ['phone_number', 'url'].includes( btns.type.toLowerCase())) - .buttons.filter((btns) => ! isEmpty(btns.example)) // 静态按钮不发 - .map((btn, btnI) => ({ type: 'button', sub_type: btn.type.toLowerCase(), index: btnI, - // parameters: [{ text: 'lq1FTtA8', type: 'text' }] - })) + .buttons.filter((btns) => !isEmpty(btns.example)) // 静态按钮不发 + .map((btn, btnI) => ({ + type: 'button', + sub_type: btn.type.toLowerCase(), + index: btnI, + // parameters: [{ text: 'lq1FTtA8', type: 'text' }] + })) : []), ], }, diff --git a/src/views/Conversations/Online/Input/Template.jsx b/src/views/Conversations/Online/Input/Template.jsx index 588553e..8d7eca0 100644 --- a/src/views/Conversations/Online/Input/Template.jsx +++ b/src/views/Conversations/Online/Input/Template.jsx @@ -68,10 +68,15 @@ const InputTemplate = ({ mobile, disabled = false, invokeSendMessage }) => { components: sortArrayByOrder(fromTemplate.components_origin.map((citem) => { const keys = ((citem?.text || '').match(/{{(.*?)}}/g) || []).map((key) => key.replace(/{{|}}/g, '')); const params = keys.map((v) => ({ type: 'text', text: getNestedValue(mergeInput, [v]) })); - const paramText = params.map((p) => p.text); + + if (citem.type.toLowerCase() === 'header' && (citem?.format || 'text').toLowerCase() !== 'text') { + params[0] = { type: citem.format.toLowerCase(), [citem.format.toLowerCase()]: { link: citem.example.header_url[0] } }; + } + + const paramText = keys.length ? params.map((p) => p.text) : []; const fillTemplate = paramText.length ? replaceTemplateString(citem?.text || '', paramText) : citem?.text || ''; valid = keys.length !== paramText.filter((s) => s).length ? false : valid; - return citem.type.toLowerCase() === 'body' ? { + return ['body', 'header'].includes(citem.type.toLowerCase()) ? { type: citem.type.toLowerCase(), parameters: params, text: fillTemplate, @@ -110,7 +115,7 @@ const InputTemplate = ({ mobile, disabled = false, invokeSendMessage }) => { const headerObj = tempItem.components.header[0]; return (
- {'text' === headerObj.format.toLowerCase() &&
{headerObj.text}
} + {'text' === headerObj.format.toLowerCase() &&
{renderForm({ tempItem }, 'header')}
} {'image' === headerObj.format.toLowerCase() && } {['document', 'video'].includes(headerObj.format.toLowerCase()) && ( @@ -146,8 +151,8 @@ const InputTemplate = ({ mobile, disabled = false, invokeSendMessage }) => { ); } - const renderForm = ({ tempItem }) => { - const templateText = tempItem.components.body?.[0]?.text || ''; + const renderForm = ({ tempItem }, key = 'body') => { + const templateText = tempItem.components?.[key]?.[0]?.text || ''; const tempArr = splitTemplate(templateText); const keys = (templateText.match(/{{(.*?)}}/g) || []).map((key) => key.replace(/{{|}}/g, '')); const paramsVal = keys.reduce((r, k) => ({ ...r, [k]: getNestedValue(valueMapped, [k]) }), {});