import { models, db, _ } from '../../utils/cloudbase.js' Page({ data: { role: "teacher", historyList: [], isManaging: false, xiazi: '', pageNumber: 1, pageSize: 10, hasMore: true, // 是否还有更多数据 isLoading: false, // 防止多次触发 }, onLoad(options) { // 列表数据 this.setData({ pageNumber: 1, hasMore: true, historyList: [] }, () => { this.getcollect(); }); // 页面加载时的初始化操作 const fileIDs = [ 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/xiazi.png', ]; // 并发下载多个 fileID Promise.all( fileIDs.map(fileID => wx.cloud.downloadFile({ fileID })) ).then(results => { // 每个 result 对应一个下载结果 const tempFilePaths = results.map(r => r.tempFilePath); console.log('全部下载成功:', tempFilePaths); this.setData({ xiazi: tempFilePaths[0], }); }).catch(err => { console.error('有文件下载失败:', err); }); }, onReachBottom() { // 上拉触底事件的处理函数 this.loadMore(); }, loadMore() { // 加载更多数据的逻辑 console.log('加载更多'); this.getcollect(true); }, // 收藏列表 async getcollect(isLoadMore = false) { if (this.data.isLoading || !this.data.hasMore) return; this.setData({ isLoading: true }); const userInfo = wx.getStorageSync('userInfo'); const { pageNumber, pageSize } = this.data; try { const { data } = await models.parent_download_history.list({ filter: { where: { wx_user_id: userInfo._id } }, pageSize, pageNumber, getCount: true, envType: "prod", }); const collectList = data.records || []; if (collectList.length === 0) { this.setData({ hasMore: false, isLoading: false }); return; } console.log(collectList, 'collectList'); const fileManagerIds = collectList.map(item => item.file_manage_id); console.log(fileManagerIds, 'fileManagerIds'); // 第二步:循环获取每个文件数据(单条查询) const fileDetailPromises = fileManagerIds.map(id => { return models.file_manage.list({ filter: { where: { _id: id } }, envType: "prod" }).then(res => { const record = res.data?.records?.[0]; if (!record) { console.warn(`未找到 _id 为 ${id} 的文件`); } return record || null; }) .catch(err => { console.error(`获取文件 ${id} 失败`, err); return null; }); }); // 第三步:等待所有请求完成 const fileDetails = await Promise.all(fileDetailPromises); console.log(fileDetails, 'fileDetails'); // 第四步:过滤无效项,并添加 checked 字段 const newFiles = fileDetails .filter(Boolean) // 去掉 null 或 undefined .map((file, index) => { const historyRecord = collectList[index]; // 获取原 download_history 的记录 if (!file) return null; return { ...file, checked: false, download_history_id: historyRecord._id // 关键:存 download_history 的 id }; }); this.setData({ historyList: isLoadMore ? this.data.historyList.concat(newFiles) : newFiles, pageNumber: pageNumber + 1, hasMore: collectList.length === pageSize, // 如果返回的数量小于 pageSize,说明已经到底 isLoading: false }); } catch (err) { console.error('获取收藏文件失败:', err); this.setData({ isLoading: false }); } }, // 收藏管理是否编辑 toggleManageMode() { const { isManaging, historyList } = this.data; if (isManaging) { if (historyList && historyList.length > 0) { historyList.forEach(item => item.checked = false); } this.setData({ isManaging: false }); } else { this.setData({ isManaging: true }); } }, // 全选按钮 selectAll() { const { historyList } = this.data; if (!historyList || historyList.length === 0) return; const allChecked = historyList.every(item => item.checked); const updatedList = historyList.map(item => ({ ...item, checked: !allChecked })); this.setData({ historyList: updatedList }); }, // 单选 onCheckboxGroupChange(e) { const selectedIds = e.detail.value; const updatedList = this.data.historyList.map(item => ({ ...item, checked: selectedIds.includes(item.download_history_id), })); this.setData({ historyList: updatedList }); }, // 删除 async deleteItems() { const { historyList } = this.data; const selectedItems = historyList.filter(item => item.checked); const fileids = selectedItems.map(item => item.download_history_id); const userInfo = wx.getStorageSync('userInfo') if (fileids.length === 0) { wx.showToast({ title: '请先选择要删除的数据', icon: 'none' }); return; } console.log(userInfo._id, 'userInfo._id'); const { data } = await models.parent_download_history.deleteMany({ filter: { where: { where: { user_id: _.eq(userInfo._id) } } }, // envType: pre 体验环境, prod 正式环境 envType: "prod", }); // 更新页面数据 const updatedList = historyList.filter(item => !fileids.includes(item.download_history_id)); this.setData({ historyList: updatedList, isManaging: false }, () => { // 回调中触发插入逻辑 this.insertNewItems(); }); }, async insertNewItems() { try { const userInfo = wx.getStorageSync('userInfo'); const { historyList } = this.data; if (!historyList.length) { // wx.showToast({ title: '没有收藏的记录数据', icon: 'none' }); return; } const newItems = historyList.map(item => ({ file_manage_id: item._id, user_id: userInfo._id })); const { data } = await models.parent_download_history.createMany({ data: newItems, envType: 'prod' }); console.log('插入成功:', data); } catch (err) { console.error('插入失败:', err); wx.showToast({ title: '插入失败', icon: 'none' }); } } });