123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- // import { models, db, _ } from '../../utils/cloudbase.js'
- import { getDB, getModels, getCommand, getTempFileURLs } from '../../utils/cloudbase.js'
- Page({
- data: {
- role: "teacher",
- historyList: [],
- isManaging: false,
- xiazi: '',
- pageNumber: 1,
- pageSize: 10,
- hasMore: true, // 是否还有更多数据
- isLoading: false, // 防止多次触发
- },
- async onLoad(options) {
- // 列表数据
- this.setData({
- pageNumber: 1,
- hasMore: true,
- historyList: []
- }, () => {
- this.getcollect();
- });
- // 页面加载时的初始化操作
- const fileIDs = [
- 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/xiazi.png',
- ];
- const fileList = await getTempFileURLs(fileIDs)
- this.setData({
- xiazi: fileList[0].tempFileURL,
- })
-
- // // 并发下载多个 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);
- if (this.data.isLoading || !this.data.hasMore) return;
- this.setData({ isLoading: true });
- this.getcollect(true);
- },
- // 收藏列表
- async getcollect(isLoadMore = false) {
- const models = await getModels();
-
- 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;
- }
-
- const fileManagerIds = collectList.map(item => item.file_manage_id);
-
- // 查询每个文件详情
- const fileDetailPromises = fileManagerIds.map(id => {
- return models.file_manage.list({
- filter: { where: { _id: id } },
- envType: "prod"
- })
- .then(async res => {
- const record = res.data?.records?.[0];
- if (!record) {
- console.warn(`未找到 _id 为 ${id} 的文件`);
- return null;
- }
-
- // ✅ 如果 cover 是 cloud://,获取临时链接
- if (record.cover && record.cover.startsWith('cloud://')) {
- try {
- const fileList = await getTempFileURLs([record.cover]);
- if (fileList && fileList.length > 0) {
- record.coverTemp = fileList[0].tempFileURL;
- }
- } catch (err) {
- console.error(`获取文件 ${id} 封面临时链接失败`, err);
- }
- } else {
- record.coverTemp = record.cover; // 已经是 http 链接
- }
-
- return record;
- })
- .catch(err => {
- console.error(`获取文件 ${id} 失败`, err);
- return null;
- });
- });
-
- const fileDetails = await Promise.all(fileDetailPromises);
-
- // 过滤无效项,并添加 checked、download_history_id 和格式化时间
- const newFiles = fileDetails
- .filter(Boolean)
- .map((file, index) => {
- const historyRecord = collectList[index];
- if (!file) return null;
- return {
- ...file,
- checked: false,
- download_history_id: historyRecord._id,
- createdAt: this.formatTime(historyRecord.createdAt)
- };
- });
-
- this.setData({
- historyList: isLoadMore ? this.data.historyList.concat(newFiles) : newFiles,
- pageNumber: pageNumber + 1,
- hasMore: collectList.length === pageSize,
- isLoading: false
- });
-
- } catch (err) {
- console.error('获取收藏文件失败:', err);
- this.setData({ isLoading: false });
- }
- },
- formatTime(ts) {
- if (!ts) return '';
- const date = new Date(ts);
- const y = date.getFullYear();
- const m = String(date.getMonth() + 1).padStart(2, '0');
- const d = String(date.getDate()).padStart(2, '0');
- const hh = String(date.getHours()).padStart(2, '0');
- const mm = String(date.getMinutes()).padStart(2, '0');
- const ss = String(date.getSeconds()).padStart(2, '0');
- return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
- },
- // 收藏管理是否编辑
- 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 models = await getModels()
- const _ = await getCommand()
- 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: {
- wx_user_id: _.eq(userInfo._id),
- file_manage_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,
- wx_user_id: userInfo._id
- }));
- const models = await getModels()
- 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' });
- }
- }
- });
|