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

203 lines
6.0 KiB
TypeScript
Raw Normal View History

2025-04-01 17:36:32 +08:00
import { api } from "@/lib/api";
import {
ServerWechatAccountsResponse,
QueryWechatAccountParams,
} from "@/types/wechat-account";
// 添加接口返回数据类型定义
interface WechatAccountSummary {
accountAge: string;
activityLevel: {
allTimes: number;
dayTimes: number;
};
accountWeight: {
scope: number;
ageWeight: number;
activityWeigth: number;
restrictWeight: number;
realNameWeight: number;
};
statistics: {
todayAdded: number;
addLimit: number;
};
restrictions: {
id: number;
level: string;
reason: string;
date: string;
}[];
}
interface WechatAccountSummaryResponse {
code: number;
msg: string;
data: WechatAccountSummary;
}
2025-04-01 17:36:32 +08:00
/**
*
* @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/wechats?${queryParams.toString()}`);
2025-04-01 17:36:32 +08:00
};
/**
*
* @returns
*/
export const refreshWechatAccounts = async (): Promise<{ code: number; msg: string; data: any }> => {
return api.put<{ code: number; msg: string; data: any }>('/v1/wechats/refresh', {});
2025-04-01 17:36:32 +08:00
};
/**
*
* @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/wechats/transfer-friends', {
2025-04-01 17:36:32 +08:00
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 wechatId ID
* @param page
* @param pageSize
* @param searchQuery
* @returns
2025-04-01 18:13:03 +08:00
*/
export const fetchWechatFriends = async (wechatId: string, page: number = 1, pageSize: number = 20, searchQuery: string = '') => {
try {
return api.get(`/v1/wechats/${wechatId}/friends?page=${page}&limit=${pageSize}${searchQuery ? `&search=${searchQuery}` : ''}`, true);
} catch (error) {
console.error("获取好友列表失败:", error);
throw error;
2025-04-01 18:13:03 +08:00
}
};
/**
*
* @param id ID
* @returns
2025-04-03 10:12:02 +08:00
*/
export const fetchWechatAccountSummary = async (id: string): Promise<WechatAccountSummaryResponse> => {
2025-04-03 10:12:02 +08:00
try {
return api.get<WechatAccountSummaryResponse>(`/v1/wechats/${id}/summary`);
2025-04-03 10:12:02 +08:00
} catch (error) {
console.error("获取账号概览失败:", error);
2025-04-03 10:12:02 +08:00
throw error;
}
};
/**
*
* @param wechatId ID
* @param friendId ID
* @returns
*/
export interface WechatFriendDetail {
id: number;
avatar: string;
nickname: string;
region: string;
wechatId: string;
addDate: string;
tags: string[];
playDate: string;
memo: string;
source: string;
}
interface WechatFriendDetailResponse {
code: number;
msg: string;
data: WechatFriendDetail;
}
export const fetchWechatFriendDetail = async (wechatId: string, friendId: string): Promise<WechatFriendDetailResponse> => {
try {
return api.get<WechatFriendDetailResponse>(`/v1/wechats/${wechatId}/friend/${friendId}`);
} catch (error) {
console.error("获取好友详情失败:", error);
throw error;
}
};