feat: 完成全部接口替换为新后端接口
This commit is contained in:
@@ -1,10 +1,16 @@
|
||||
import { getUser } from './auth';
|
||||
import { apiRequest } from '../services/api-request';
|
||||
import {
|
||||
legacyWorksheet,
|
||||
memberStats,
|
||||
type MemberPage,
|
||||
} from '../services/member-api';
|
||||
import { generateUUID } from './uuid';
|
||||
|
||||
export interface DownloadLogRecord {
|
||||
_id: string;
|
||||
userId: string;
|
||||
worksheetId: string;
|
||||
exportId?: string;
|
||||
exportId?: string | null;
|
||||
createdAt: string;
|
||||
worksheet?: {
|
||||
_id: string;
|
||||
@@ -20,66 +26,39 @@ export interface DownloadLogRecord {
|
||||
} | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录下载日志
|
||||
*/
|
||||
/** 一次成功保存沿用同一个 exportId,服务端保证记录及下载计数幂等。 */
|
||||
export async function addDownloadLog(
|
||||
worksheetId: string,
|
||||
exportId?: string,
|
||||
exportId = generateUUID(),
|
||||
expectedUserId?: string,
|
||||
): Promise<boolean> {
|
||||
if (!worksheetId) return false;
|
||||
try {
|
||||
await getUser();
|
||||
const res = await wx.cloud.callFunction({
|
||||
name: 'userDownloadLogs',
|
||||
data: { action: 'add', worksheetId, exportId },
|
||||
const user = await getUser();
|
||||
if (expectedUserId && user.id !== expectedUserId) return false;
|
||||
await apiRequest('/me/downloads', {
|
||||
method: 'POST',
|
||||
data: { worksheetId, exportId },
|
||||
});
|
||||
const result = (res?.result as any) || {};
|
||||
return result.code === 0;
|
||||
} catch (e) {
|
||||
console.error('addDownloadLog failed', e);
|
||||
return true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** 收藏页下载历史展示条数上限 */
|
||||
export const DOWNLOAD_HISTORY_MAX = 30;
|
||||
|
||||
/**
|
||||
* 获取下载历史(默认最近 30 条)
|
||||
*/
|
||||
export async function getDownloadLogs(
|
||||
page = 1,
|
||||
pageSize = DOWNLOAD_HISTORY_MAX,
|
||||
): Promise<DownloadLogRecord[]> {
|
||||
try {
|
||||
await getUser();
|
||||
const res = await wx.cloud.callFunction({
|
||||
name: 'userDownloadLogs',
|
||||
data: { action: 'list', page, pageSize },
|
||||
});
|
||||
const result = (res?.result as any) || {};
|
||||
return result.code === 0 ? result.data : [];
|
||||
} catch (e) {
|
||||
console.error('getDownloadLogs failed', e);
|
||||
return [];
|
||||
}
|
||||
const result = await apiRequest<MemberPage>('/me/downloads', {
|
||||
data: { page, pageSize },
|
||||
});
|
||||
return result.items.map((row) => ({
|
||||
...row,
|
||||
_id: row.id,
|
||||
worksheet: legacyWorksheet(row.worksheet),
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户下载(打印)总数
|
||||
*/
|
||||
export async function getDownloadLogCount(): Promise<number> {
|
||||
try {
|
||||
await getUser();
|
||||
const res = await wx.cloud.callFunction({
|
||||
name: 'userDownloadLogs',
|
||||
data: { action: 'count' },
|
||||
});
|
||||
const result = (res?.result as any) || {};
|
||||
return result.code === 0 ? Number(result.data?.count || 0) : 0;
|
||||
} catch (e) {
|
||||
console.error('getDownloadLogCount failed', e);
|
||||
return 0;
|
||||
}
|
||||
return (await memberStats()).downloadCount;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user