diff --git a/src/channel/whatsappUtils.js b/src/channel/whatsappUtils.js index 7698c10..4ddc5ce 100644 --- a/src/channel/whatsappUtils.js +++ b/src/channel/whatsappUtils.js @@ -672,3 +672,48 @@ export const handleNotification = (title, _options) => { }; } }; + +/** + * WhatsApp 国际号码 + * + * - Make sure to remove any leading 0s or special calling codes. + * - All phone numbers in Argentina (country code "54") should have a "9" between the country code and area code. The prefix "15" must be removed so the final number will have 13 digits total: +54 9 XXX XXX XXXX + * - Phone numbers in Mexico (country code "52") need to have "1" after "+52", even if they're Nextel numbers. + */ +export const phoneNumberToWAID = (input) => { + // Remove any non-digit characters except '+' + const cleanedInput = input.replace(/[^\d+]/g, ''); + + // Check if the number starts with a valid country code + const countryCode = cleanedInput.slice(0, 2); + const isArgentina = countryCode === '54'; + const isMexico = countryCode === '52'; + + if (!isArgentina && !isMexico) { + return cleanedInput; // Return the cleaned input as is + } + + // Remove leading 0s and special calling codes + let formattedNumber = cleanedInput.replace(/^0+/, ''); + + if (isArgentina) { + // Remove the prefix "15" if present + // formattedNumber = formattedNumber.replace(/^15/, ''); + formattedNumber = formattedNumber.replace(/^54 ?15/, '54'); + // Remove leading '0' after country code + formattedNumber = formattedNumber.replace(/^54 ?0/, '54'); + + // Insert "9" between the country code and area code + formattedNumber = `54 9 ${formattedNumber.slice(2)}`; + } else if (isMexico) { + // Remove leading '0' after country code + formattedNumber = formattedNumber.replace(/^52 ?0/, '52'); + // Insert "1" after the country code + formattedNumber = `52 1 ${formattedNumber.slice(2)}`; + } + + // Remove any non-digit characters except '+' + formattedNumber = formattedNumber.replace(/[^\d+]/g, ''); + + return formattedNumber; +} diff --git a/src/views/Conversations/Online/ConversationsNewItem.jsx b/src/views/Conversations/Online/ConversationsNewItem.jsx index 70c4e7f..2f0106e 100644 --- a/src/views/Conversations/Online/ConversationsNewItem.jsx +++ b/src/views/Conversations/Online/ConversationsNewItem.jsx @@ -1,22 +1,57 @@ import { useState, useEffect } from 'react'; import { Form, Input, Modal } from 'antd'; -import { isEmpty } from '@/utils/commons'; +import { isEmpty, pick } from '@/utils/commons'; +import useConversationStore from '@/stores/ConversationStore'; +import { phoneNumberToWAID } from '@/channel/whatsappUtils'; export const ConversationItemForm = ({ initialValues, onFormInstanceReady }) => { + const [currentConversation] = useConversationStore((state) => [state.currentConversation]); + const fromCurrent = initialValues?.is_current_order ? pick(currentConversation, ['coli_sn', 'coli_id', 'opi_sn']) : {}; const [form] = Form.useForm(); + const [formatPhone, setFormatPhone] = useState(''); + const [formatVisible, setFormatVisible] = useState(false); + useEffect(() => { onFormInstanceReady(form); + setFormatPhone(phoneNumberToWAID(initialValues.phone_number)); }, []); + + useEffect(() => { + form.setFieldValue('wa_id', formatPhone); + return () => {}; + }, [formatPhone]); + + const onValuesChange = (changeValues, allValues) => { + if ('phone_number' in changeValues) { + const _t = phoneNumberToWAID(changeValues.phone_number); + setFormatPhone(_t); + setFormatVisible(_t !== changeValues.phone_number); + } + }; return ( -