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.

265 lines
6.2 KiB

5 months ago
const app = getApp<IAppOption>();
Page({
5 months ago
data: {
show: false,
5 months ago
BookId: '',
ChapterId: '',
chapterList: [],
book: {} as any,
chapter: {} as any,
prevAndNext: {} as any,
5 months ago
progress: 0,
url: 'http://m10.music.126.net/20241126110303/3f2481d2d6d50acd2009359539eadda0/ymusic/5353/0f0f/0358/d99739615f8e5153d77042092f07fd77.mp3',
play: false,
5 months ago
time: '00:00/00:00',
4 months ago
currentTime: 0,
drag: false,
5 months ago
toastShow: false,
toastType: 3,
5 months ago
},
innerAudioContext: null as WechatMiniprogram.InnerAudioContext | null,
5 months ago
onLoad(options) {
5 months ago
this.innerAudioContext = wx.createInnerAudioContext();
5 months ago
this.setData({
BookId: options.id,
});
4 months ago
if (options.cid) {
this.setData({
ChapterId: options.cid,
});
}
app.waitLogin(false, true).then(() => {
5 months ago
this.getChapterList();
this.getDetail();
});
5 months ago
},
onUnload() {
if (this.innerAudioContext) {
this.innerAudioContext?.stop();
5 months ago
this.innerAudioContext?.destroy();
5 months ago
}
},
5 months 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,
},
5 months ago
showMsg: false,
})
.then((res) => {
5 months ago
this.setData({
5 months ago
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,
5 months ago
});
5 months 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,
});
4 months ago
this.handleView();
5 months ago
})
4 months ago
.catch((err) => {
if (err.data.code === 403) {
this.setData({
toastShow: true,
toastType: 3,
});
}
5 months ago
});
5 months ago
},
4 months ago
handleView() {
wx.ajax({
method: 'POST',
url: '?r=shizhong/book/add-browse-record',
data: {
BookId: this.data.BookId,
ChapterId: this.data.ChapterId,
},
});
},
5 months ago
handleDirectory() {
this.setData({
show: true,
});
},
handleSwitchDirectory(e) {
const { id } = e.currentTarget.dataset;
this.setData({
ChapterId: id,
});
this.getDetail();
this.onClose();
this.handlePause();
},
onClose() {
this.setData({
show: false,
});
},
handleCollect() {
const { Id } = this.data.book;
wx.ajax({
method: 'POST',
url: '?r=shizhong/book/collect',
data: { BookId: Id },
}).then(() => {
wx.showToast({
title: '您已收藏这本书,可在我的页查看',
5 months 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,
});
});
},
5 months ago
handlePlay() {
4 months ago
const { chapter, play, currentTime } = this.data;
5 months ago
if (this.innerAudioContext) {
if (play) {
this.innerAudioContext.stop();
return;
}
this.innerAudioContext.stop();
5 months ago
this.innerAudioContext.src = chapter.AudioUrl.url;
4 months ago
this.innerAudioContext.seek(currentTime);
5 months ago
this.innerAudioContext.play();
const listener = () => {
4 months ago
if (this.innerAudioContext && !this.data.drag) {
5 months ago
const { currentTime, duration } = this.innerAudioContext;
const time = `${this.formatTime(currentTime)}/${this.formatTime(duration)}`;
const progress = (currentTime / duration) * 100;
this.setData({
time,
progress,
4 months ago
currentTime,
play: true,
5 months ago
});
}
};
this.innerAudioContext.onTimeUpdate(listener);
this.innerAudioContext.onEnded(() => {
4 months ago
this.innerAudioContext?.seek(0);
4 months ago
const time = `00:00/${this.formatTime(chapter.AudioUrl.duration)}`;
5 months ago
this.setData({
play: false,
4 months ago
progress: 0,
time,
currentTime: 0,
5 months ago
});
});
4 months ago
this.innerAudioContext.onPlay(() => {
this.setData({
play: true,
});
});
this.innerAudioContext.onPause(() => {
5 months ago
this.setData({
play: false,
});
});
}
},
handlePause() {
if (this.innerAudioContext) {
this.innerAudioContext.pause();
}
},
4 months ago
handleDragStart() {
this.setData({
drag: true,
});
},
handleDragEnd() {
this.setData({
drag: false,
});
if (this.data.play) {
this.innerAudioContext?.seek(this.data.currentTime);
}
},
handleDrag(e: any) {
const current = e.detail.value;
const duration = this.innerAudioContext?.duration || this.data.chapter.AudioUrl.duration;
const currentTime = (current / 100) * duration;
const time = `${this.formatTime(currentTime)}/${this.formatTime(duration)}`;
this.setData({
time,
currentTime,
});
},
5 months 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}`;
},
5 months ago
handleAuthClose() {
this.setData({
toastShow: false,
});
wx.navigateBack();
},
handleAuthConform() {
4 months ago
wx.navigateTo({
url: '/pages/login/index?back=1',
5 months ago
});
},
handleType() {
wx.redirectTo({
url: `/pages/classify/index?id=${this.data.chapter.CateId}`,
});
},
});
5 months ago
export {};