You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

350 lines
8.9 KiB

1 year ago
import { decryptData } from '@/utils/crypto';
1 year ago
const app = getApp<IAppOption>();
Page({
1 year ago
data: {
show: false,
1 year ago
BookId: '',
ChapterId: '',
chapterList: [],
book: {} as any,
chapter: {} as any,
prevAndNext: {} as any,
1 year ago
progress: 0,
url: 'http://m10.music.126.net/20241126110303/3f2481d2d6d50acd2009359539eadda0/ymusic/5353/0f0f/0358/d99739615f8e5153d77042092f07fd77.mp3',
play: false,
1 year ago
time: '00:00/00:00',
1 year ago
currentTime: 0,
drag: false,
chapterTip: false,
toastShow: false,
toastType: 3,
previewImg: true,
listener: false,
globalStop: false,
1 year ago
},
BackgroundAudioManager: null as WechatMiniprogram.BackgroundAudioManager | null,
1 year ago
onLoad(options) {
this.BackgroundAudioManager = wx.getBackgroundAudioManager();
1 year ago
this.setData({
BookId: options.id,
globalStop: this.BackgroundAudioManager.paused,
1 year ago
});
1 year ago
if (options.cid) {
this.setData({
ChapterId: options.cid,
});
}
const previewImg = !['mac', 'windows'].includes(wx.getDeviceInfo().platform);
this.setData({
previewImg,
});
app.waitLogin(false, true).then(() => {
1 year ago
this.getChapterList();
this.getDetail();
});
1 year ago
},
1 year ago
getChapterList() {
wx.ajax({
method: 'GET',
url: '?r=shizhong/book/chapter-list',
data: {
BookId: this.data.BookId,
},
}).then((res) => {
this.setData({
chapterList: res.list,
});
});
},
getDetail() {
wx.ajax({
method: 'GET',
url: '?r=shizhong/book/detail',
data: {
BookId: this.data.BookId,
ChapterId: this.data.ChapterId,
},
showMsg: false,
})
.then((res) => {
if (res.chapter.content_encrypt === 1) {
res.chapter.Content = decryptData(res.chapter.Content);
}
1 year ago
this.setData({
book: {
...res.book,
PublishDate: res.book.PublishTime.split(' ')[0],
},
chapter: res.chapter,
prevAndNext: {
prev: Array.isArray(res.prevAndNext.prev) ? null : res.prevAndNext.prev,
next: Array.isArray(res.prevAndNext.next) ? null : res.prevAndNext.next,
},
ChapterId: res.chapter.Id,
show: !this.data.ChapterId,
1 year ago
});
if (res.chapter.AudioUrl?.url) {
const time = `00:00/${this.formatTime(res.chapter.AudioUrl.duration)}`;
this.setData({
time,
});
}
wx.setNavigationBarTitle({
title: res.book.BookName,
});
1 year ago
this.handleView();
this.initBackgroundAudioManager();
})
1 year ago
.catch((err) => {
if (err.data.code === 403) {
this.setData({
toastShow: true,
toastType: 3,
});
}
});
1 year ago
},
1 year ago
handleView() {
wx.ajax({
method: 'POST',
url: '?r=shizhong/book/add-browse-record',
data: {
BookId: this.data.BookId,
ChapterId: this.data.ChapterId,
},
});
},
1 year ago
handleDirectory() {
this.setData({
show: true,
});
},
handleSwitchDirectory(e) {
const { id } = e.currentTarget.dataset;
this.setData({
ChapterId: id,
});
this.getDetail();
this.onClose();
},
onClose() {
const chapterTip = !wx.getStorageSync('chapterTip');
1 year ago
this.setData({
show: false,
chapterTip,
1 year ago
});
if (chapterTip) {
setTimeout(() => {
this.handleTipClsoe();
}, 5000);
}
1 year ago
},
handleCollect() {
const { Id } = this.data.book;
wx.ajax({
method: 'POST',
url: '?r=shizhong/book/collect',
data: { BookId: Id },
}).then(() => {
wx.showToast({
title: '您已收藏这本书,可在我的页查看',
1 year ago
icon: 'none',
});
this.setData({
[`book.IsCollect`]: 1,
});
});
},
handleUnCollect() {
const { Id } = this.data.book;
wx.ajax({
method: 'POST',
url: '?r=shizhong/book/cancel-collect',
data: { BookId: Id },
}).then(() => {
wx.showToast({
title: '取消收藏',
icon: 'none',
});
this.setData({
[`book.IsCollect`]: 0,
});
});
},
initBackgroundAudioManager() {
const { chapter } = this.data;
if (this.BackgroundAudioManager) {
if (this.BackgroundAudioManager.src === chapter.AudioUrl.url && !this.BackgroundAudioManager.paused) {
this.setData({
play: true,
});
this.backgroundAudioManagerListener();
} else {
this.backgroundAudioManagerListener();
1 year ago
this.setData({
play: false,
1 year ago
progress: 0,
currentTime: 0,
1 year ago
});
}
}
},
backgroundAudioManagerListener() {
const { chapter } = this.data;
if (!this.BackgroundAudioManager) return;
const timeUpdateListener = () => {
const isCurrent = this.BackgroundAudioManager?.src === chapter.AudioUrl.url;
if (this.BackgroundAudioManager && !this.data.drag && this.data.play && isCurrent) {
const { currentTime, duration } = this.BackgroundAudioManager;
const time = `${this.formatTime(currentTime)}/${this.formatTime(duration)}`;
const progress = (currentTime / duration) * 100;
1 year ago
this.setData({
time,
progress,
currentTime,
1 year ago
});
}
};
const endedListener = () => {
const time = `00:00/${this.formatTime(chapter.AudioUrl.duration)}`;
this.setData({
play: false,
progress: 0,
time,
currentTime: 0,
globalStop: true,
1 year ago
});
};
const playListener = () => {
const isCurrent = this.BackgroundAudioManager?.src === chapter.AudioUrl.url;
if (!isCurrent) return;
this.setData({
play: true,
1 year ago
});
};
const pauseListener = () => {
this.setData({
play: false,
});
};
const stopListener = () => {
const time = `00:00/${this.formatTime(chapter.AudioUrl.duration)}`;
this.setData({
play: false,
globalStop: true,
progress: 0,
time,
currentTime: 0,
});
};
this.BackgroundAudioManager.onTimeUpdate(timeUpdateListener);
this.BackgroundAudioManager.onEnded(endedListener);
this.BackgroundAudioManager.onPlay(playListener);
this.BackgroundAudioManager.onPause(pauseListener);
this.BackgroundAudioManager.onStop(stopListener);
this.setData({
listener: true,
});
},
handlePlay() {
const { chapter, currentTime, play, listener, globalStop } = this.data;
if (play) return;
if (this.BackgroundAudioManager) {
if (this.BackgroundAudioManager.src !== chapter.AudioUrl.url || globalStop) {
this.BackgroundAudioManager.title = chapter.ChapterName;
this.BackgroundAudioManager.coverImgUrl = chapter.CoverUrl;
this.BackgroundAudioManager.src = chapter.AudioUrl.url;
} else {
this.BackgroundAudioManager.play();
this.BackgroundAudioManager.seek(this.BackgroundAudioManager.currentTime || currentTime);
}
if (!listener) {
this.backgroundAudioManagerListener();
}
1 year ago
}
this.setData({
globalStop: false,
});
1 year ago
},
handlePause() {
const { play } = this.data;
if (this.BackgroundAudioManager && play) {
this.setData({
play: false,
});
this.BackgroundAudioManager.pause();
1 year ago
}
},
1 year ago
handleDragStart() {
this.setData({
drag: true,
});
},
handleDragEnd() {
this.setData({
drag: false,
});
if (this.data.play) {
this.BackgroundAudioManager?.seek(this.data.currentTime);
1 year ago
}
},
handleDrag(e: any) {
const current = e.detail.value;
const duration = this.BackgroundAudioManager?.duration || this.data.chapter.AudioUrl.duration;
1 year ago
const currentTime = (current / 100) * duration;
const time = `${this.formatTime(currentTime)}/${this.formatTime(duration)}`;
this.setData({
time,
currentTime,
});
},
1 year ago
formatTime(time: number) {
const minutes = Math.floor(time / 60);
const seconds = Math.floor(time % 60);
return `${minutes < 10 ? `0${minutes}` : minutes}:${seconds < 10 ? `0${seconds}` : seconds}`;
},
handleAuthClose() {
this.setData({
toastShow: false,
});
wx.navigateBack({
fail() {
wx.reLaunch({
url: '/pages/classify/index',
});
},
});
},
handleAuthConform() {
const { BookId, ChapterId } = this.data;
const backPath = `/pages/article/index?id=${BookId}&cid=${ChapterId}`;
app.globalData.backPath = backPath;
1 year ago
wx.navigateTo({
url: '/pages/login/index?back=1',
});
},
handleType() {
wx.redirectTo({
url: `/pages/classify/index?id=${this.data.chapter.CateId}`,
});
},
handleTipClsoe() {
if (this.data.chapterTip) {
wx.setStorageSync('chapterTip', 'open');
}
this.setData({
chapterTip: false,
});
},
});
1 year ago
export {};