import { groupBy } from '@/utils/utils'; import { fetchJSON, postJSON } from '@/utils/request' import { parseRenderMessageList } from '@/lib/msgUtils'; import { API_HOST } from '@/config'; export const fetchTemplates = async () => { const data = await fetchJSON(`${API_HOST}/listtemplates`); const canUseTemplates = (data?.result?.items || []) .filter((_t) => _t.status !== 'REJECTED') .map((ele) => ({ ...ele, components_origin: ele.components, components: groupBy(ele.components, (_c) => _c.type.toLowerCase()) })); return canUseTemplates; }; /** * * @param {object} params { opisn } */ export const fetchConversationsList = async (params) => { const { result: data } = await fetchJSON(`${API_HOST}/getconversations`, params); const list = (data || []).map((ele) => ({ ...ele, customer_name: `${ele.whatsapp_name || ''}`.trim(), whatsapp_name: `${ele.whatsapp_name || ''}`.trim() })); return list; }; export const MESSAGE_PAGE_SIZE = 100; /** * * @param {object} params { opisn, whatsappid, lasttime, pagesize } */ export const fetchMessages = async (params) => { const defaultParams = { opisn: '', whatsappid: '', lasttime: '', pagesize: MESSAGE_PAGE_SIZE, }; const { errcode, result } = await fetchJSON(`${API_HOST}/getcusmessages`, {...defaultParams, ...params}); return errcode !== 0 ? [] : parseRenderMessageList((result || []).reverse()); } /** * * @param {object} params { opisn, whatsappid, colisn } */ export const fetchOrderConversationsList = async (params) => { const { errcode, result: data } = await fetchJSON(`${API_HOST}/getorderconversation`, params); if (errcode !== 0) return []; const list = data.map((ele) => ({ ...ele, customer_name: ele.whatsapp_name.trim() })); return list; }; /** * * @param {object} body { opisn, conversationid } */ export const fetchConversationItemClose = async (body) => { const { result } = await fetchJSON(`${API_HOST}/closeconversation`, body); return result; }; /** * @param {object} params { opisn, whatsappid } */ export const fetchCleanUnreadMsgCount = async (params) => { const { errcode, result } = await fetchJSON(`${API_HOST}/clean_unread_msg_count`, params); return errcode !== 0 ? {} : result; }; /** * ------------------------------------------------------------------------------------------------ * 历史记录 */