|
|
|
|
|
|
|
import { groupBy } from '@/utils/utils';
|
|
|
|
import { fetchJSON, postJSON } from '@/utils/request'
|
|
|
|
import { parseRenderMessageList } from '@/lib/msgUtils';
|
|
|
|
|
|
|
|
const API_HOST = 'https://p9axztuwd7x8a7.mycht.cn/whatsapp_callback';
|
|
|
|
|
|
|
|
const NAME_SPACE = 'CONVERSATION/';
|
|
|
|
export const initWebsocket = (socket) => ({
|
|
|
|
type: NAME_SPACE + 'INIT_WEBSOCKET',
|
|
|
|
payload: socket,
|
|
|
|
});
|
|
|
|
export const closeWebsocket0 = () => ({
|
|
|
|
type: NAME_SPACE + 'CLOSE_WEBSOCKET',
|
|
|
|
payload: null,
|
|
|
|
});
|
|
|
|
export const updateWebsocketState = (v) => ({
|
|
|
|
type: NAME_SPACE + 'MODIFY_WEBSOCKET_STATE',
|
|
|
|
payload: v,
|
|
|
|
});
|
|
|
|
export const updateWebsocketRetrytimes = (v) => ({
|
|
|
|
type: NAME_SPACE + 'MODIFY_WEBSOCKET_RETRYTIMES',
|
|
|
|
payload: v,
|
|
|
|
});
|
|
|
|
|
|
|
|
export const receivedNewMessage = (targetId, message) => ({
|
|
|
|
type: NAME_SPACE + 'RECEIVED_NEW_MESSAGE',
|
|
|
|
payload: {
|
|
|
|
targetId,
|
|
|
|
message,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
export const sentNewMessage = (message) => ({
|
|
|
|
type: NAME_SPACE + 'SENT_NEW_MESSAGE',
|
|
|
|
payload: {
|
|
|
|
targetId: message.conversationid,
|
|
|
|
message,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
export const addError = (error) => {
|
|
|
|
return {
|
|
|
|
type: NAME_SPACE + 'ADD_ERROR',
|
|
|
|
payload: error,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export const receivedTemplates = (data) => ({
|
|
|
|
type: NAME_SPACE + 'SET_TEMPLATE_LIST',
|
|
|
|
payload: data,
|
|
|
|
});
|
|
|
|
|
|
|
|
export const receivedConversationList = (data) => {
|
|
|
|
return {
|
|
|
|
type: NAME_SPACE + 'SET_CONVERSATION_LIST',
|
|
|
|
payload: data,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
export const addConversationList = (data) => {
|
|
|
|
return {
|
|
|
|
type: NAME_SPACE + 'ADD_TO_CONVERSATIONS_LIST',
|
|
|
|
payload: data,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
// export const updateConversationListItemNew = (message) => ({
|
|
|
|
// type: NAME_SPACE + 'UPDATE_CONVERSATION_LIST_ITEM_NEW',
|
|
|
|
// payload: message,
|
|
|
|
// });
|
|
|
|
|
|
|
|
export const receivedCustomerProfile = (data) => ({
|
|
|
|
type: NAME_SPACE + 'SET_CUSTOMER_ORDER_PROFILE',
|
|
|
|
payload: data,
|
|
|
|
});
|
|
|
|
/**
|
|
|
|
* @deprecated 在更新list时操作
|
|
|
|
*/
|
|
|
|
export const setActiveConversations = (obj) => ({
|
|
|
|
type: NAME_SPACE + 'SET_ACTIVE_CONVERSATIONS',
|
|
|
|
payload: obj,
|
|
|
|
});
|
|
|
|
export const setCurrentConversation = (obj) => ({
|
|
|
|
type: NAME_SPACE + 'SET_CURRENT_CONVERSATION',
|
|
|
|
payload: obj,
|
|
|
|
});
|
|
|
|
export const updateMessageItem = (message) => ({
|
|
|
|
type: NAME_SPACE + 'UPDATE_SENT_MESSAGE_ITEM',
|
|
|
|
payload: message,
|
|
|
|
});
|
|
|
|
export const receivedMessageList = (targetId, data) => ({
|
|
|
|
type: NAME_SPACE + 'RECEIVED_MESSAGE_LIST',
|
|
|
|
payload: { targetId, data },
|
|
|
|
})
|
|
|
|
export const setReplyTo = (data) => {
|
|
|
|
return {
|
|
|
|
type: NAME_SPACE + 'SET_REFERENCE_MSG',
|
|
|
|
payload: data,
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const fetchConversationsList = async (opisn) => {
|
|
|
|
const { result: data } = await fetchJSON(`${API_HOST}/getconversations`, { opisn });
|
|
|
|
const list = data.map((ele) => ({ ...ele, customer_name: ele.whatsapp_name.trim() }));
|
|
|
|
return list;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const fetchMessages = async (params) => {
|
|
|
|
const { result } = await fetchJSON(`${API_HOST}/getcusmessages`, params);
|
|
|
|
return parseRenderMessageList(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const fetchCustomerProfile = async (colisn) => {
|
|
|
|
const { result } = await fetchJSON(`${API_HOST}/getorderinfo`, { colisn });
|
|
|
|
const data = result?.[0] || {};
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const postConversationItemClose = async (body) => {
|
|
|
|
const getParams = body ? new URLSearchParams(body).toString() : '';
|
|
|
|
const { result } = await postJSON(`${API_HOST}/closeconversation?${getParams}`);
|
|
|
|
return result;
|
|
|
|
};
|