Files
doodle-mini/miniprogram/utils/downloadLogs.ts
T
2026-05-25 18:32:18 +08:00

78 lines
2.0 KiB
TypeScript

import { getUser } from './auth';
export interface DownloadLogRecord {
_id: string;
userId: string;
worksheetId: string;
createdAt: string;
worksheet?: {
_id: string;
title: string;
subtitle: string;
category: string;
subcategory: string;
previewImg: string;
} | null;
}
/**
* 记录下载日志
*/
export async function addDownloadLog(worksheetId: string): Promise<boolean> {
if (!worksheetId) return false;
try {
await getUser();
const res = await wx.cloud.callFunction({
name: 'userDownloadLogs',
data: { action: 'add', worksheetId },
});
const result = (res?.result as any) || {};
return result.code === 0;
} catch (e) {
console.error('addDownloadLog failed', e);
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 [];
}
}
/**
* 获取当前用户下载(打印)总数
*/
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;
}
}