// subpackagestow/details/details.js import { models, db, _, client } from '../../utils/cloudbase.js' Page({ /** * 页面的初始数据 */ data: { fileType: 'video', viewType: 'grid', courseList: [], shouchang: '', shouchangs: '', show_1: '', xiazi: '', xia: '', itemlist: {}, isCollected: false, isPlaying: false, isAudioPlaying: false, }, onLoad(options) { // 获取传递过来的数据 const itemStr = decodeURIComponent(options.item); const item = JSON.parse(itemStr); // 格式化时间 item.createdAtText = this.formatTime(item.createdAt); // 设置到页面数据中 this.setData({ itemlist: item }, () => { // 相关推荐 this.getcourseList() // 转换云文件ID为临时链接 if (item.url && item.url.length > 0) { wx.cloud.getTempFileURL({ fileList: [item.url[0]], success: res => { if (res.fileList && res.fileList.length > 0) { this.setData({ 'itemlist.playUrl': res.fileList[0].tempFileURL }); } }, fail: err => { console.error('获取临时链接失败', err); } }); } // 收藏 this.getcollect() // 加访问量 this.getvisits() }); // 获取图片 const fileIDs = [ 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/shouchang.png', 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/show_1.png', 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/xiazi.png', 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/xia_1.png', 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/shoucang_s.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({ shouchang: tempFilePaths[0], show_1: tempFilePaths[1], xiazi: tempFilePaths[2], xia: tempFilePaths[3], shouchangs: tempFilePaths[4], }); }).catch(err => { console.error('有文件下载失败:', err); }); }, formatTime(ts) { 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}`; }, // 相关课件推荐 async getcourseList() { const result = await client.callFunction({ name: "database", // 云函数部署名 data: { page: "recommende", action: "findFileRangee", data: { _id: this.data.itemlist._id, range: this.data.itemlist.range, tag_id: this.data.itemlist.tag_id, pageSize: 20, pageNum: 1, }, }, }); console.log(result, 'data++++++---------------'); this.setData({ courseList: result.result.records }) }, // 下载 goToGoodsLists(event) { // 获取绑定的数据 const item = event.currentTarget.dataset.item; // 将数据转换为 JSON 字符串并传递 const itemStr = encodeURIComponent(JSON.stringify(item)); wx.navigateTo({ url: `/subpackagestow/down/down?item=${itemStr}` }); }, // 相关课件点击详情 goToGoodsList(event) { // 获取绑定的数据 const item = event.currentTarget.dataset.item; // 将数据转换为 JSON 字符串并传递 const itemStr = encodeURIComponent(JSON.stringify(item)); wx.navigateTo({ url: `/subpackagestow/details/details?item=${itemStr}` }); }, // 是否收藏 async getcollect() { const { data } = await models.wx_collect.get({ filter: { where: { file_manager_id: this.data.itemlist._id } }, // envType: pre 体验环境, prod 正式环境 envType: "prod", }); // 返回查询到的数据 console.log(data, '123321'); // 判断是否有值 if (data && Object.keys(data).length > 0) { this.setData({ isCollected: true // 设置标志为 true }); } else { this.setData({ isCollected: false // 设置标志为 false }); } }, // 收藏 async goTocollect() { if (this.data.isCollected) { const userInfo = wx.getStorageSync('userInfo'); const { data } = await models.wx_collect.delete({ filter: { where: { $and: [ { file_manager_id: { $eq: this.data.itemlist._id, // 推荐传入_id数据标识进行操作 }, wx_user_id: { $eq: userInfo._id, // 推荐传入_id数据标识进行操作 }, }, ] } }, // envType: pre 体验环境, prod 正式环境 envType: "prod", }); // 返回删除成功的条数 console.log(data, '删除'); wx.showToast({ title: '取消收藏成功', icon: 'success' }); this.setData({ isCollected: false }) } else { const userInfo = wx.getStorageSync('userInfo'); const { data } = await models.wx_collect.create({ data: { wx_user_id: userInfo._id, // 收藏人_id file_manager_id: this.data.itemlist._id, // 收藏文件id remark: "备注备注备注备注备注备注", // 备注 }, // envType: pre 体验环境, prod 正式环境 envType: "prod", }); // 返回创建的数据 id console.log(data); wx.showToast({ title: '收藏成功', icon: 'success' }); this.setData({ isCollected: true // 设置标志为 true }); } }, // 预览 previewPDF() { const cloudUrl = this.data.itemlist.url[0]; if (!cloudUrl) return; wx.cloud.getTempFileURL({ fileList: [cloudUrl], success: res => { if (res.fileList && res.fileList.length > 0) { const tempUrl = res.fileList[0].tempFileURL; const extension = tempUrl.substring(tempUrl.lastIndexOf('.') + 1).toLowerCase(); if (['pdf', 'ppt', 'pptx'].includes(extension)) { // 先下载到本地 wx.downloadFile({ url: tempUrl, success: dlRes => { wx.openDocument({ filePath: dlRes.tempFilePath, fileType: extension, success: () => console.log('文档打开成功'), fail: err => console.error('文档打开失败', err) }); }, fail: err => { wx.showToast({ title: '下载文件失败', icon: 'none' }); console.error('下载文件失败', err); } }); } else if (['mp3', 'aac', 'wav'].includes(extension)) { this._playAudio(tempUrl); } else if (['mp4', 'mov'].includes(extension)) { this.setData({ 'itemlist.playUrl': tempUrl }); this.toggleVideo(); } else { wx.showToast({ title: '暂不支持该文件类型预览', icon: 'none' }); } } }, fail: err => { wx.showToast({ title: '获取播放链接失败', icon: 'none' }); console.error('获取临时链接失败', err); } }); }, // 视频播放还是暂停 toggleVideo() { const videoContext = wx.createVideoContext('myVideo', this); if (this.data.isPlaying) { videoContext.pause(); this.setData({ isPlaying: false }); } else { videoContext.play(); this.setData({ isPlaying: true }); } }, // 音频的播放暂停 toggleAudio() { // 如果已经有临时播放链接,直接用它 if (this.data.itemlist.playUrl) { this._playAudio(this.data.itemlist.playUrl); } else { // 否则判断 url 是否是 cloud:// 开头,需要转临时链接 const cloudUrl = this.data.itemlist.url[0]; if (cloudUrl && cloudUrl.startsWith('cloud://')) { wx.cloud.getTempFileURL({ fileList: [cloudUrl], success: res => { if (res.fileList && res.fileList.length > 0) { const tempUrl = res.fileList[0].tempFileURL; this.setData({ 'itemlist.playUrl': tempUrl }, () => { this._playAudio(tempUrl); }); } }, fail: err => { wx.showToast({ title: '获取播放链接失败', icon: 'none' }); console.error('获取临时链接失败', err); } }); } else { // 不是 cloud:// 开头,直接播放原链接 this._playAudio(cloudUrl); } } }, _playAudio(url) { if (!this.audioContext) { this.audioContext = wx.createInnerAudioContext(); this.audioContext.onPlay(() => this.setData({ isAudioPlaying: true })); this.audioContext.onPause(() => this.setData({ isAudioPlaying: false })); this.audioContext.onStop(() => this.setData({ isAudioPlaying: false })); this.audioContext.onEnded(() => this.setData({ isAudioPlaying: false })); this.audioContext.onError((res) => { wx.showToast({ title: '音频播放错误', icon: 'none' }); this.setData({ isAudioPlaying: false }); console.error('音频播放错误', res); }); } if (this.data.isAudioPlaying) { this.audioContext.pause(); } else { this.audioContext.src = url; this.audioContext.play(); } }, onUnload() { if (this.audioContext) { this.audioContext.stop(); this.audioContext.destroy(); this.audioContext = null; } }, onHide() { if (this.audioContext) { this.audioContext.pause(); } }, // 新增浏览量 async getvisits() { const userInfo = wx.getStorageSync('userInfo'); const userId = userInfo && userInfo._id ? userInfo._id : ''; const schoolId = userInfo && userInfo.school_id ? userInfo.school_id : ''; const fileId = this.data.itemlist._id; // 先查是否存在 const { data } = await models.microcode.list({ filter: { where: { school_id: schoolId, // user_id: userId, file_id: fileId } }, envType: "prod", }); console.log(data,'datadatadata'); const datalist = data.records || [] if (datalist.length > 0) { // 已存在 → 更新 visits + 1 const existItem = datalist[0]; const recordId = String(existItem._id); console.log(existItem, 'existItem'); await models.microcode.update({ data: { visits: existItem.visits + 1 , // 访问量 }, filter: { where: { $and: [ { _id: { $eq: recordId, // 推荐传入_id数据标识进行操作 }, }, ] } }, // envType: pre 体验环境, prod 正式环境 envType: "prod", }); console.log("更新成功:visits + 1"); } else { // 不存在 → 创建新记录 const { data: newData } = await models.microcode.create({ data: { visits: 1, download: 1, school_id: schoolId, // user_id: userId, file_id: fileId, }, envType: "prod", }); console.log("创建成功", newData); } }, })