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.

184 lines
4.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',
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,
});
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,
},
}).then((res) => {
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,
});
if (res.chapter.AudioUrl?.url) {
const time = `00:00/${this.formatTime(res.chapter.AudioUrl.duration)}`;
this.setData({
time,
});
}
});
},
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: '收藏成功',
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() {
5 months ago
const { chapter, play } = 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;
5 months ago
this.innerAudioContext.play();
this.setData({
play: true,
});
const listener = () => {
if (this.innerAudioContext) {
const { currentTime, duration } = this.innerAudioContext;
const time = `${this.formatTime(currentTime)}/${this.formatTime(duration)}`;
const progress = (currentTime / duration) * 100;
this.setData({
time,
progress,
});
}
};
this.innerAudioContext.onTimeUpdate(listener);
this.innerAudioContext.onEnded(() => {
this.setData({
play: false,
});
});
this.innerAudioContext.onStop(() => {
this.setData({
play: false,
});
});
}
},
handlePause() {
if (this.innerAudioContext) {
this.innerAudioContext.pause();
this.setData({
play: false,
});
}
},
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
export {};