|
@@ -28,10 +28,29 @@ Page({
|
|
|
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()
|
|
|
});
|
|
|
|
|
|
// 获取图片
|
|
@@ -195,7 +214,7 @@ Page({
|
|
|
});
|
|
|
} else if (['mp3', 'aac', 'wav'].includes(extension)) {
|
|
|
console.log('这是一个音频文件,可以跳转到播放页面或使用音频组件');
|
|
|
- // this.toggleAudio()
|
|
|
+ this.toggleAudio()
|
|
|
} else if (['mp4', 'mov'].includes(extension)) {
|
|
|
console.log('这是一个视频文件,可以跳转到视频播放页');
|
|
|
this.toggleVideo()
|
|
@@ -225,14 +244,130 @@ Page({
|
|
|
},
|
|
|
// 音频的播放暂停
|
|
|
toggleAudio() {
|
|
|
- const audioContext = wx.createAudioContext('myAudio');
|
|
|
-
|
|
|
+ // 如果已经有临时播放链接,直接用它
|
|
|
+ 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) {
|
|
|
- audioContext.pause();
|
|
|
- this.setData({ isAudioPlaying: false });
|
|
|
+ this.audioContext.pause();
|
|
|
} else {
|
|
|
- audioContext.play();
|
|
|
- this.setData({ isAudioPlaying: true });
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ },
|
|
|
})
|