Files
cunkebao_v3/Cunkebao/api/wechat-accounts.ts

287 lines
9.5 KiB
TypeScript
Raw Normal View History

2025-04-01 17:36:32 +08:00
import { api } from "@/lib/api";
import {
ServerWechatAccountsResponse,
QueryWechatAccountParams,
WechatAccountDetailResponse
} from "@/types/wechat-account";
/**
*
* @param params
* @returns
*/
export const fetchWechatAccountList = async (params: QueryWechatAccountParams = {}): Promise<ServerWechatAccountsResponse> => {
const queryParams = new URLSearchParams();
// 添加查询参数
if (params.page) queryParams.append('page', params.page.toString());
if (params.limit) queryParams.append('limit', params.limit.toString());
if (params.keyword) queryParams.append('nickname', params.keyword); // 使用nickname作为关键词搜索参数
if (params.sort) queryParams.append('sort', params.sort);
if (params.order) queryParams.append('order', params.order);
// 发起API请求
return api.get<ServerWechatAccountsResponse>(`/v1/device/wechats?${queryParams.toString()}`);
};
/**
*
* @param id ID
* @returns
*/
export const fetchWechatAccountDetail = async (id: string | number): Promise<WechatAccountDetailResponse> => {
return api.get<WechatAccountDetailResponse>(`/v1/device/wechats/${id}`);
};
/**
*
* @returns
*/
export const refreshWechatAccounts = async (): Promise<{ code: number; msg: string; data: any }> => {
return api.put<{ code: number; msg: string; data: any }>('/v1/device/wechats/refresh', {});
};
/**
*
* @param sourceId ID
* @param targetId ID
* @returns
*/
export const transferWechatFriends = async (sourceId: string | number, targetId: string | number): Promise<{ code: number; msg: string; data: any }> => {
return api.post<{ code: number; msg: string; data: any }>('/v1/device/wechats/transfer-friends', {
source_id: sourceId,
target_id: targetId
});
};
/**
* 使
* @param serverAccount
* @returns 使
*/
export const transformWechatAccount = (serverAccount: any): import("@/types/wechat-account").WechatAccount => {
// 从deviceInfo中提取设备信息
let deviceId = '';
let deviceName = '';
if (serverAccount.deviceInfo) {
2025-04-08 16:01:59 +08:00
// 尝试解析设备信息字符串
2025-04-01 17:36:32 +08:00
const deviceInfo = serverAccount.deviceInfo.split(' ');
2025-04-08 16:01:59 +08:00
if (deviceInfo.length > 0) {
// 提取数字部分作为设备ID确保是整数
const possibleId = deviceInfo[0].trim();
// 验证是否为数字
deviceId = /^\d+$/.test(possibleId) ? possibleId : '';
// 提取设备名称
if (deviceInfo.length > 1) {
deviceName = deviceInfo[1] ? deviceInfo[1].replace(/[()]/g, '').trim() : '';
}
}
}
// 如果从deviceInfo无法获取有效的设备ID使用imei作为备选
if (!deviceId && serverAccount.imei) {
deviceId = serverAccount.imei;
}
// 如果仍然没有设备ID使用微信账号的ID作为最后的备选
if (!deviceId && serverAccount.id) {
deviceId = serverAccount.id.toString();
}
// 如果没有设备名称,使用备用名称
if (!deviceName) {
deviceName = serverAccount.deviceMemo || '未命名设备';
2025-04-01 17:36:32 +08:00
}
// 假设每天最多可添加20个好友
const maxDailyAdds = 20;
const todayAdded = serverAccount.todayNewFriendCount || 0;
return {
id: serverAccount.id.toString(),
avatar: serverAccount.avatar || '',
nickname: serverAccount.nickname || serverAccount.accountNickname || '未命名',
wechatId: serverAccount.wechatId || '',
deviceId,
deviceName,
friendCount: serverAccount.totalFriend || 0,
todayAdded,
remainingAdds: serverAccount.canAddFriendCount || (maxDailyAdds - todayAdded),
maxDailyAdds,
status: serverAccount.wechatAlive === 1 ? "normal" : "abnormal" as "normal" | "abnormal",
lastActive: new Date().toLocaleString() // 服务端未提供,使用当前时间
};
2025-04-01 18:13:03 +08:00
};
/**
*
* @param detailResponse
* @returns
*/
export const transformWechatAccountDetail = (detailResponse: WechatAccountDetailResponse): any => {
if (!detailResponse || !detailResponse.data) {
return null;
}
const { basicInfo, statistics, accountInfo, restrictions, friends } = detailResponse.data;
2025-04-08 16:01:59 +08:00
// 设备信息处理 - 改进处理方式
2025-04-01 18:13:03 +08:00
let deviceId = '';
let deviceName = '';
if (basicInfo.deviceInfo) {
2025-04-08 16:01:59 +08:00
// 尝试解析设备信息字符串
2025-04-01 18:13:03 +08:00
const deviceInfoParts = basicInfo.deviceInfo.split(' ');
2025-04-08 16:01:59 +08:00
if (deviceInfoParts.length > 0) {
// 提取数字部分作为设备ID确保是整数
const possibleId = deviceInfoParts[0].trim();
// 验证是否为数字
deviceId = /^\d+$/.test(possibleId) ? possibleId : '';
// 提取设备名称
if (deviceInfoParts.length > 1) {
deviceName = deviceInfoParts[1].replace(/[()]/g, '').trim();
}
}
}
// 如果从deviceInfo无法获取有效的设备ID直接使用微信账号ID作为备选
if (!deviceId && basicInfo.id) {
deviceId = basicInfo.id.toString();
}
// 如果没有设备名称,使用备用名称
if (!deviceName) {
deviceName = '未命名设备';
2025-04-01 18:13:03 +08:00
}
// 账号年龄计算
let accountAgeYears = 0;
let accountAgeMonths = 0;
if (accountInfo.createTime) {
const createDate = new Date(accountInfo.createTime);
const currentDate = new Date();
const diffInMonths = (currentDate.getFullYear() - createDate.getFullYear()) * 12 +
(currentDate.getMonth() - createDate.getMonth());
accountAgeYears = Math.floor(diffInMonths / 12);
accountAgeMonths = diffInMonths % 12;
}
// 转换限制记录
const restrictionRecords = restrictions?.map((restriction, index) => ({
id: `${index}`,
date: restriction.startTime,
reason: restriction.reason,
recoveryTime: restriction.endTime,
type: mapRestrictionType(restriction.type)
})) || [];
// 转换好友数据
const transformedFriends = friends?.map(friend => ({
id: friend.id.toString(),
avatar: friend.avatar || `/placeholder.svg?height=40&width=40&text=${friend.nickname?.[0] || ''}`,
nickname: friend.nickname,
wechatId: friend.wechatId,
remark: '', // 服务端未提供
addTime: friend.createTime,
lastInteraction: '', // 服务端未提供
tags: [], // 服务端未提供
region: friend.region || '',
source: '', // 服务端未提供
notes: '', // 服务端未提供
})) || [];
// 创建每周统计数据(模拟数据,服务端未提供)
const weeklyStats = Array.from({ length: 7 }, (_, i) => ({
date: `Day ${i + 1}`,
friends: Math.floor(Math.random() * 50) + 50,
messages: Math.floor(Math.random() * 100) + 100,
}));
return {
id: basicInfo.id.toString(),
avatar: basicInfo.avatar || '',
nickname: basicInfo.nickname || '',
wechatId: basicInfo.wechatId || '',
deviceId,
deviceName,
friendCount: statistics.totalFriend || 0,
todayAdded: 0, // 服务端未提供默认为0
status: basicInfo.status === '在线' ? 'normal' : 'abnormal',
lastActive: accountInfo.lastUpdateTime || new Date().toLocaleString(),
messageCount: statistics.thirtyDayMsgCount || 0,
activeRate: 0, // 服务端未提供默认为0
accountAge: {
years: accountAgeYears,
months: accountAgeMonths,
},
totalChats: statistics.sevenDayMsgCount + statistics.yesterdayMsgCount || 0,
chatFrequency: Math.floor((statistics.sevenDayMsgCount || 0) / 7), // 每日平均聊天次数
restrictionRecords,
isVerified: true, // 服务端未提供默认为true
firstMomentDate: accountInfo.createTime || '',
accountWeight: accountInfo.weight || 50,
weightFactors: {
restrictionFactor: restrictionRecords.length > 0 ? 0.8 : 1.0,
verificationFactor: 1.0,
ageFactor: Math.min(1.0, accountAgeYears * 0.1 + 0.5),
activityFactor: statistics.totalFriend > 0 ? 0.9 : 0.7,
},
weeklyStats,
friends: transformedFriends,
};
};
/**
*
* @param type
* @returns
*/
const mapRestrictionType = (type: string): "friend_limit" | "marketing" | "spam" | "other" => {
const typeMap: Record<string, "friend_limit" | "marketing" | "spam" | "other"> = {
'friend': 'friend_limit',
'marketing': 'marketing',
'spam': 'spam'
};
return typeMap[type] || 'other';
2025-04-03 10:12:02 +08:00
};
/**
*
* @param wechatId ID
* @param page
* @param limit
* @param keyword
* @returns
*/
export const fetchWechatFriends = async (
wechatId: string | number,
page: number = 1,
limit: number = 20,
keyword: string = ""
): Promise<{ code: number; msg: string; data: any }> => {
try {
const params = new URLSearchParams({
wechatId: String(wechatId),
page: String(page),
limit: String(limit)
});
if (keyword) {
params.append('keyword', keyword);
}
const url = `/v1/device/wechats/friends?${params.toString()}`;
const response = await api.get<{ code: number; msg: string; data: any }>(url);
return response;
} catch (error) {
console.error('获取微信好友列表失败:', error);
throw error;
}
}