292 lines
7.2 KiB
TypeScript
292 lines
7.2 KiB
TypeScript
import {
|
||
asyncKfUserList,
|
||
asyncContractList,
|
||
asyncChatSessions,
|
||
asyncWeChatGroup,
|
||
asyncCountLables,
|
||
useCkChatStore,
|
||
} from "@/store/module/ckchat/ckchat";
|
||
import { useWebSocketStore } from "@/store/module/websocket/websocket";
|
||
|
||
import {
|
||
loginWithToken,
|
||
getControlTerminalList,
|
||
getContactList,
|
||
getGroupList,
|
||
} from "./api";
|
||
|
||
import { useUserStore } from "@/store/module/user";
|
||
|
||
import { KfUserListData } from "@/pages/pc/ckbox/data";
|
||
|
||
import { WechatGroup } from "./api";
|
||
const { login2 } = useUserStore.getState();
|
||
//获取触客宝基础信息
|
||
export const chatInitAPIdata = async () => {
|
||
try {
|
||
//获取联系人列表
|
||
const contractList = await getAllContactList();
|
||
|
||
//获取联系人列表
|
||
asyncContractList(contractList);
|
||
|
||
// 提取不重复的wechatAccountId组
|
||
const uniqueWechatAccountIds: number[] =
|
||
getUniqueWechatAccountIds(contractList);
|
||
|
||
//获取控制终端列表
|
||
const kfUserList: KfUserListData[] =
|
||
await getControlTerminalListByWechatAccountIds(uniqueWechatAccountIds);
|
||
|
||
//获取用户列表
|
||
await asyncKfUserList(kfUserList);
|
||
|
||
//获取群列表
|
||
const groupList = await getAllGroupList();
|
||
|
||
await asyncWeChatGroup(groupList);
|
||
|
||
//获取标签列表
|
||
const countLables = await getCountLables();
|
||
await asyncCountLables(countLables);
|
||
|
||
//获取消息会话列表并按lastUpdateTime排序
|
||
const filterUserSessions = contractList?.filter(
|
||
v => v?.config && v.config?.chat,
|
||
);
|
||
const filterGroupSessions = groupList?.filter(
|
||
v => v?.config && v.config?.chat,
|
||
);
|
||
//排序功能
|
||
const sortedSessions = [...filterUserSessions, ...filterGroupSessions].sort(
|
||
(a, b) => {
|
||
// 如果lastUpdateTime不存在,则将其排在最后
|
||
if (!a.lastUpdateTime) return 1;
|
||
if (!b.lastUpdateTime) return -1;
|
||
|
||
// 首先按时间降序排列(最新的在前面)
|
||
const timeCompare =
|
||
new Date(b.lastUpdateTime).getTime() -
|
||
new Date(a.lastUpdateTime).getTime();
|
||
|
||
// 如果时间相同,则按未读消息数量降序排列
|
||
if (timeCompare === 0) {
|
||
// 如果unreadCount不存在,则将其排在后面
|
||
const aUnread = a.unreadCount || 0;
|
||
const bUnread = b.unreadCount || 0;
|
||
return bUnread - aUnread; // 未读消息多的排在前面
|
||
}
|
||
|
||
return timeCompare;
|
||
},
|
||
);
|
||
//会话数据同步
|
||
asyncChatSessions(sortedSessions);
|
||
|
||
return {
|
||
contractList,
|
||
groupList,
|
||
kfUserList,
|
||
};
|
||
} catch (error) {
|
||
console.error("获取联系人列表失败:", error);
|
||
return [];
|
||
}
|
||
};
|
||
//发起soket连接
|
||
export const initSocket = () => {
|
||
// 检查WebSocket是否已经连接
|
||
const { status } = useWebSocketStore.getState();
|
||
|
||
// 如果已经连接或正在连接,则不重复连接
|
||
if (["connected", "connecting"].includes(status)) {
|
||
console.log("WebSocket已连接或正在连接,跳过重复连接", { status });
|
||
return;
|
||
}
|
||
|
||
// 从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 LablesRes = await Promise.all(
|
||
[1, 2].map(item =>
|
||
WechatGroup({
|
||
groupType: item,
|
||
}),
|
||
),
|
||
);
|
||
const [friend, group] = LablesRes;
|
||
const countLables = [
|
||
...[
|
||
{
|
||
id: 0,
|
||
groupName: "默认群分组",
|
||
groupType: 2,
|
||
},
|
||
],
|
||
...group,
|
||
...friend,
|
||
...[
|
||
{
|
||
id: 0,
|
||
groupName: "未分组",
|
||
groupType: 1,
|
||
},
|
||
],
|
||
];
|
||
|
||
return countLables;
|
||
};
|
||
/**
|
||
* 根据标签组织联系人
|
||
* @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 prevId = 0;
|
||
const count = 1000;
|
||
let hasMore = true;
|
||
|
||
while (hasMore) {
|
||
const contractList = await getContactList({
|
||
prevId,
|
||
count,
|
||
});
|
||
|
||
if (
|
||
!contractList ||
|
||
!Array.isArray(contractList) ||
|
||
contractList.length === 0
|
||
) {
|
||
hasMore = false;
|
||
break;
|
||
}
|
||
|
||
allContacts = [...allContacts, ...contractList];
|
||
|
||
// 如果返回的数据少于请求的数量,说明已经没有更多数据了
|
||
if (contractList.length < count) {
|
||
hasMore = false;
|
||
} else {
|
||
// 获取最后一条数据的id作为下一次请求的prevId
|
||
const lastContact = contractList[contractList.length - 1];
|
||
prevId = lastContact.id;
|
||
}
|
||
}
|
||
return allContacts;
|
||
} catch (error) {
|
||
console.error("获取所有联系人列表失败:", error);
|
||
return [];
|
||
}
|
||
};
|
||
|
||
// 提取不重复的wechatAccountId组
|
||
export const getUniqueWechatAccountIds = contacts => {
|
||
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);
|
||
}
|
||
});
|
||
|
||
// 将Set转换为数组并返回
|
||
return Array.from(uniqueAccountIdsSet);
|
||
};
|
||
// 递归获取所有群列表
|
||
export const getAllGroupList = async () => {
|
||
try {
|
||
let allContacts = [];
|
||
let prevId = 0;
|
||
const count = 1000;
|
||
let hasMore = true;
|
||
|
||
while (hasMore) {
|
||
const contractList = await getGroupList({
|
||
prevId,
|
||
count,
|
||
});
|
||
|
||
if (
|
||
!contractList ||
|
||
!Array.isArray(contractList) ||
|
||
contractList.length === 0
|
||
) {
|
||
hasMore = false;
|
||
break;
|
||
}
|
||
|
||
allContacts = [...allContacts, ...contractList];
|
||
|
||
// 如果返回的数据少于请求的数量,说明已经没有更多数据了
|
||
if (contractList.length < count) {
|
||
hasMore = false;
|
||
} else {
|
||
// 获取最后一条数据的id作为下一次请求的prevId
|
||
const lastContact = contractList[contractList.length - 1];
|
||
prevId = lastContact.id;
|
||
}
|
||
}
|
||
|
||
return allContacts;
|
||
} catch (error) {
|
||
console.error("获取所有群列表失败:", error);
|
||
return [];
|
||
}
|
||
};
|
||
|
||
//获取token
|
||
const getToken = () => {
|
||
return new Promise((resolve, reject) => {
|
||
const params = {
|
||
grant_type: "password",
|
||
password: "kr123456",
|
||
username: "kr_xf3",
|
||
// username: "karuo",
|
||
// password: "zhiqun1984",
|
||
};
|
||
loginWithToken(params)
|
||
.then(res => {
|
||
login2(res.access_token);
|
||
resolve(res.access_token);
|
||
})
|
||
.catch(err => {
|
||
reject(err);
|
||
});
|
||
});
|
||
};
|