322 lines
8.2 KiB
TypeScript
322 lines
8.2 KiB
TypeScript
import {
|
||
asyncChatSessions,
|
||
asyncWeChatGroup,
|
||
asyncCountLables,
|
||
useCkChatStore,
|
||
updateIsLoadWeChat,
|
||
getIsLoadWeChat,
|
||
} from "@/store/module/ckchat/ckchat";
|
||
import { useContactStore } from "@/store/module/weChat/contacts";
|
||
import { useWebSocketStore } from "@/store/module/websocket/websocket";
|
||
import { useUserStore } from "@/store/module/user";
|
||
import { weChatGroupService, contractService } from "@/utils/db";
|
||
|
||
import {
|
||
getControlTerminalList,
|
||
getContactList,
|
||
getGroupList,
|
||
getCustomerList,
|
||
getLabelsListByGroup,
|
||
getMessageList,
|
||
} from "./api";
|
||
|
||
import {
|
||
KfUserListData,
|
||
ContractData,
|
||
weChatGroup,
|
||
} from "@/pages/pc/ckbox/data";
|
||
|
||
import { updateCustomerList } from "@weChatStore/customer";
|
||
//获取触客宝基础信息
|
||
export const chatInitAPIdata = async () => {
|
||
try {
|
||
let contractList = [];
|
||
let groupList = [];
|
||
|
||
if (getIsLoadWeChat()) {
|
||
//获取联系人列表
|
||
contractList = await contractService.findAll();
|
||
//获取群列表
|
||
groupList = await weChatGroupService.findAll();
|
||
} else {
|
||
//获取联系人列表
|
||
contractList = await getAllContactList();
|
||
|
||
//获取群列表
|
||
groupList = await getAllGroupList();
|
||
|
||
updateIsLoadWeChat(true);
|
||
}
|
||
//获取联系人列表 - 使用新的 contacts store
|
||
// 注意:这里需要将 contractList 和 groupList 转换为统一的 Contact 格式
|
||
// 暂时保留旧的数据库写入逻辑,后续需要统一到 ContactManager
|
||
await contractService.createManyWithServerId(contractList);
|
||
await asyncWeChatGroup(groupList);
|
||
|
||
//获取标签列表
|
||
const countLables = await getCountLables();
|
||
await asyncCountLables(countLables);
|
||
|
||
// 获取显示名称的辅助函数(优先 conRemark,其次 nickname)
|
||
const getDisplayName = (session: any) => {
|
||
return session.conRemark || session.nickname || "";
|
||
};
|
||
|
||
const messageList = await getAllMessageList();
|
||
|
||
//排序功能
|
||
const sortedSessions = messageList.sort((a, b) => {
|
||
// 获取置顶状态
|
||
const aTop = a.config?.top || false;
|
||
const bTop = b.config?.top || false;
|
||
|
||
// 首先按置顶状态排序(置顶的排在前面)
|
||
if (aTop !== bTop) {
|
||
return aTop ? -1 : 1;
|
||
}
|
||
|
||
// 如果都是置顶或都不是置顶,则按未读消息数量降序排列(未读消息多的排在前面)
|
||
const aUnread = a.config?.unreadCount || 0;
|
||
const bUnread = b.config?.unreadCount || 0;
|
||
|
||
if (aUnread !== bUnread) {
|
||
return bUnread - aUnread;
|
||
}
|
||
|
||
// 如果未读消息数量相同,则按显示名称排序(字母排序)
|
||
const aName = getDisplayName(a).toLowerCase();
|
||
const bName = getDisplayName(b).toLowerCase();
|
||
|
||
if (aName !== bName) {
|
||
return aName.localeCompare(bName, "zh-CN");
|
||
}
|
||
|
||
// 如果名称也相同,则按时间降序排列(最新的在前面)
|
||
// 如果lastUpdateTime不存在,则将其排在最后
|
||
if (!a.lastUpdateTime) return 1;
|
||
if (!b.lastUpdateTime) return -1;
|
||
|
||
const timeCompare =
|
||
new Date(b.lastUpdateTime).getTime() -
|
||
new Date(a.lastUpdateTime).getTime();
|
||
|
||
return timeCompare;
|
||
});
|
||
//会话数据同步
|
||
asyncChatSessions(sortedSessions);
|
||
|
||
return {
|
||
contractList,
|
||
groupList,
|
||
};
|
||
} catch (error) {
|
||
console.error("获取联系人列表失败:", error);
|
||
return [];
|
||
}
|
||
};
|
||
|
||
// 递归获取所有联系人列表
|
||
export const getAllMessageList = async () => {
|
||
try {
|
||
let allMessages = [];
|
||
let page = 1;
|
||
const limit = 500;
|
||
let hasMore = true;
|
||
|
||
while (hasMore) {
|
||
const Result = await getMessageList({
|
||
page,
|
||
limit,
|
||
});
|
||
const messageList = Result;
|
||
if (
|
||
!messageList ||
|
||
!Array.isArray(messageList) ||
|
||
messageList.length === 0
|
||
) {
|
||
hasMore = false;
|
||
break;
|
||
}
|
||
|
||
allMessages = [...allMessages, ...messageList];
|
||
|
||
// 如果返回的数据少于请求的数量,说明已经没有更多数据了
|
||
if (messageList.length == 0) {
|
||
hasMore = false;
|
||
} else {
|
||
page = page + 1;
|
||
}
|
||
}
|
||
return allMessages;
|
||
} catch (error) {
|
||
console.error("获取所有联系人列表失败:", error);
|
||
return [];
|
||
}
|
||
};
|
||
|
||
//发起soket连接
|
||
export const initSocket = () => {
|
||
// 从store获取token和accountId
|
||
const { token2 } = useUserStore.getState();
|
||
const { getAccountId } = useCkChatStore.getState();
|
||
const Token = token2;
|
||
const accountId = getAccountId();
|
||
// 使用WebSocket store初始化连接
|
||
const { connect } = useWebSocketStore.getState();
|
||
|
||
// 连接WebSocket
|
||
connect({
|
||
accessToken: Token,
|
||
accountId: Number(accountId),
|
||
client: "kefu-client",
|
||
cmdType: "CmdSignIn",
|
||
seq: +new Date(),
|
||
});
|
||
};
|
||
|
||
export const getCountLables = async () => {
|
||
const Result = await getLabelsListByGroup({});
|
||
const LablesRes = Result.list;
|
||
return [
|
||
...[
|
||
{
|
||
id: 0,
|
||
groupName: "默认群分组",
|
||
groupType: 2,
|
||
},
|
||
],
|
||
...LablesRes,
|
||
...[
|
||
{
|
||
id: 0,
|
||
groupName: "未分组",
|
||
groupType: 1,
|
||
},
|
||
],
|
||
];
|
||
};
|
||
/**
|
||
* 根据标签组织联系人
|
||
* @param contractList 联系人列表
|
||
* @param countLables 标签列表
|
||
* @returns 按标签分组的联系人
|
||
*/
|
||
|
||
//获取控制终端列表
|
||
export const getControlTerminalListByWechatAccountIds = (
|
||
WechatAccountIds: number[],
|
||
) => {
|
||
return Promise.all(
|
||
WechatAccountIds.map(id => getControlTerminalList({ id: id })),
|
||
);
|
||
};
|
||
// 递归获取所有联系人列表
|
||
export const getAllContactList = async () => {
|
||
try {
|
||
let allContacts = [];
|
||
let page = 1;
|
||
const limit = 500;
|
||
let hasMore = true;
|
||
|
||
while (hasMore) {
|
||
const Result = await getContactList({
|
||
page,
|
||
limit,
|
||
});
|
||
const contractList = Result.list;
|
||
if (
|
||
!contractList ||
|
||
!Array.isArray(contractList) ||
|
||
contractList.length === 0
|
||
) {
|
||
hasMore = false;
|
||
break;
|
||
}
|
||
|
||
allContacts = [...allContacts, ...contractList];
|
||
|
||
// 如果返回的数据少于请求的数量,说明已经没有更多数据了
|
||
if (contractList.length == 0) {
|
||
hasMore = false;
|
||
} else {
|
||
page = page + 1;
|
||
}
|
||
}
|
||
return allContacts;
|
||
} catch (error) {
|
||
console.error("获取所有联系人列表失败:", error);
|
||
return [];
|
||
}
|
||
};
|
||
// 递归获取所有群列表
|
||
export const getAllGroupList = async () => {
|
||
try {
|
||
let allContacts = [];
|
||
let page = 1;
|
||
const limit = 500;
|
||
let hasMore = true;
|
||
|
||
while (hasMore) {
|
||
const Result = await getGroupList({
|
||
page,
|
||
limit,
|
||
});
|
||
const contractList = Result.list;
|
||
if (
|
||
!contractList ||
|
||
!Array.isArray(contractList) ||
|
||
contractList.length === 0
|
||
) {
|
||
hasMore = false;
|
||
break;
|
||
}
|
||
|
||
allContacts = [...allContacts, ...contractList];
|
||
|
||
// 如果返回的数据少于请求的数量,说明已经没有更多数据了
|
||
if (contractList.length < limit) {
|
||
hasMore = false;
|
||
} else {
|
||
// 获取最后一条数据的id作为下一次请求的page
|
||
const lastContact = contractList[contractList.length - 1];
|
||
page = lastContact.id;
|
||
}
|
||
}
|
||
|
||
return allContacts;
|
||
} catch (error) {
|
||
console.error("获取所有群列表失败:", error);
|
||
return [];
|
||
}
|
||
};
|
||
|
||
// 提取不重复的wechatAccountId组
|
||
export const getUniqueWechatAccountIds = (
|
||
contacts: ContractData[],
|
||
groupList: weChatGroup[],
|
||
) => {
|
||
if (!contacts || !Array.isArray(contacts) || contacts.length === 0) {
|
||
return [];
|
||
}
|
||
|
||
// 使用Set来存储不重复的wechatAccountId
|
||
const uniqueAccountIdsSet = new Set<number>();
|
||
|
||
// 遍历联系人列表,将每个wechatAccountId添加到Set中
|
||
contacts.forEach(contact => {
|
||
if (contact && contact.wechatAccountId) {
|
||
uniqueAccountIdsSet.add(contact.wechatAccountId);
|
||
}
|
||
});
|
||
|
||
// 遍历联系人列表,将每个wechatAccountId添加到Set中
|
||
groupList.forEach(group => {
|
||
if (group && group.wechatAccountId) {
|
||
uniqueAccountIdsSet.add(group.wechatAccountId);
|
||
}
|
||
});
|
||
|
||
// 将Set转换为数组并返回
|
||
return Array.from(uniqueAccountIdsSet);
|
||
};
|