import { models, db, _ } from '../../utils/cloudbase.js' Page({ data: { role: "teacher", historyList: [], isManaging: false, souchangs: '', 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/soucangs.png' ]; // 并发下载多个 fileID Promise.all( fileIDs.map(fileID => wx.cloud.downloadFile({ fileID })) ).then(results => { // 每个 result 对应一个下载结果 const tempFilePaths = results.map(r => r.tempFilePath); this.setData({ souchangs: 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.wx_collect.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; } const fileManagerIds = collectList.map(item => item.file_manager_id); const fileRes = await models.file_manage.list({ filter: { where: { _id: { $in: fileManagerIds } } }, envType: "prod", }); const newFiles = fileRes.data.records.map(item => ({ ...item, checked: false })); 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._id), })); this.setData({ historyList: updatedList }); }, // 删除 async deleteItems() { const { historyList } = this.data; const selectedItems = historyList.filter(item => item.checked); const fileids = selectedItems.map(item => item._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.wx_collect.deleteMany({ filter: { where: { where: { wx_user_id: _.eq(userInfo._id) } } }, // envType: pre 体验环境, prod 正式环境 envType: "prod", }); // 更新页面数据 const updatedList = historyList.filter(item => !fileids.includes(item._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_manager_id: item._id, wx_user_id: userInfo._id })); const { data } = await models.wx_collect.createMany({ data: newItems, envType: 'prod' }); console.log('插入成功:', data); } catch (err) { console.error('插入失败:', err); wx.showToast({ title: '插入失败', icon: 'none' }); } } });