123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- // subpackagestow/details/details.js
- import { models, db, _ } from '../../utils/cloudbase.js'
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- fileType: 'video',
- viewType: 'grid',
- courseList: [],
- shouchang: '',
- show_1: '',
- xiazi: '',
- xia: '',
- itemlist: {},
- // isCollected: false,
- isPlaying: false,
- isAudioPlaying: false,
- },
- onLoad(options) {
- // 获取传递过来的数据
- const itemStr = decodeURIComponent(options.item);
- const item = JSON.parse(itemStr);
- // 设置到页面数据中
- this.setData({
- itemlist: item
- }, () => {
- // 收藏
- // this.getcollect()
- // 相关推荐
- this.getcourseList()
- });
-
- // 获取图片
- const fileIDs = [
- 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/shouchang.png',
- 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/show_1.png',
- 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/xiazi.png',
- 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/xia_1.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);
- });
- },
- // 相关课件推荐
- async getcourseList() {
- const { data } = await models.file_manage.list({
- filter: {
- where: {
- level: this.data.itemlist.level,
- dan: this.data.itemlist.dan,
- tag_id: this.data.itemlist.tag_id
- }
- },
- pageSize: 20, // 分页大小,建议指定,如需设置为其它值,需要和 pageNumber 配合使用,两者同时指定才会生效
- pageNumber: 1, // 第几页
- getCount: true, // 开启用来获取总数
- // envType: pre 体验环境, prod 正式环境
- envType: "prod",
- });
-
- // 返回查询到的数据列表 records 和 总数 total
- console.log(data, 'data');
- this.setData({
- courseList: data.records
- })
- },
- // 下载
- goToGoodsLists(event) {
- // 获取绑定的数据
- const item = event.currentTarget.dataset.item;
- // 将数据转换为 JSON 字符串并传递
- const itemStr = encodeURIComponent(JSON.stringify(item));
- wx.navigateTo({
- url: `/subpackages/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 { data } = await models.wx_collect.delete({
- // filter: {
- // where: {
- // file_manager_id: _.eq(this.data.itemlist._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() {
- wx.cloud.downloadFile({
- fileID: this.data.itemlist.url[0],
- success: res => {
- const filePath = res.tempFilePath;
- const extension = filePath.substring(filePath.lastIndexOf('.') + 1).toLowerCase();
- console.log('文件后缀:', extension);
- // 根据文件类型处理
- if (['pdf', 'ppt', 'pptx'].includes(extension)) {
- wx.openDocument({
- filePath: filePath,
- fileType: extension,
- success: () => {
- console.log('文档打开成功');
- },
- fail: err => {
- console.error('文档打开失败', err);
- }
- });
- } else if (['mp3', 'aac', 'wav'].includes(extension)) {
- console.log('这是一个音频文件,可以跳转到播放页面或使用音频组件');
- // this.toggleAudio()
- } else if (['mp4', 'mov'].includes(extension)) {
- console.log('这是一个视频文件,可以跳转到视频播放页');
- this.toggleVideo()
- } else {
- wx.showToast({
- title: '暂不支持该文件类型预览',
- icon: 'none'
- });
- }
- },
- fail: err => {
- 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() {
- const audioContext = wx.createAudioContext('myAudio');
-
- if (this.data.isAudioPlaying) {
- audioContext.pause();
- this.setData({ isAudioPlaying: false });
- } else {
- audioContext.play();
- this.setData({ isAudioPlaying: true });
- }
- }
- })
|