123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- // subpackagestow/details/details.js
- import { models, db, _ } 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);
- // 设置到页面数据中
- this.setData({
- itemlist: item
- }, () => {
- // 转换云文件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.getcourseList()
- // 加访问量
- this.getvisits()
- });
-
- // 获取图片
- 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',
- 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/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);
- });
- },
- // 相关课件推荐
- async getcourseList() {
- console.log(this.data.itemlist.rangee, 'this.data.itemlist.range');
- const { data } = await models.file_manage.list({
- filter: {
- where: {
- rangee: _.in(this.data.itemlist.rangee),
- 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: `/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 { 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() {
- // 如果已经有临时播放链接,直接用它
- 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);
- }
- },
- })
|