import { getMyActivity, getOneActivity, thumbsUp, deleteActivity, getShareText, playAudioEvent } from '~/api/activity' import { actionRecord } from '~/api/global' Page({ /** * 页面的初始数据 */ data: { isMy: false, pageNo: 1, totalSize: 0, list: [], // 当前用户id uid: '', // 贺卡作者id userId: '', playAudioId: null, // 当前音频播放时长 playTime: '00:00', endTime: '00:00', vProgress: 0, userAudioState: false, }, /** * 生命周期函数--监听页面加载 */ async onLoad(options) { this.setData({ uid: wx.getStorageSync('uid') }) if (options.uid) { this.setData({ userId: options.uid }) } if (options.cardId) { let oneCard = await getOneActivity(options.cardId) this.setData({ list: oneCard ? [oneCard] : [], }) await actionRecord({ action: 'NEW_YEAR_ACTIVITY_APP_LINK', }) } this.getMyActivity() this.innerAudioContext = wx.createInnerAudioContext(); this.innerAudioContext.onTimeUpdate(() => { this.setDuration('playTime', this.innerAudioContext.currentTime) let vProgress = Math.ceil((Math.ceil(this.innerAudioContext.currentTime) / this.innerAudioContext.duration) * 100) this.setData({ vProgress: vProgress < 96 ? vProgress : 100 }) }) this.innerAudioContext.onEnded((res) => { this.clearAudio() }); }, async getMyActivity() { let { list, totalSize } = await getMyActivity({ userUid: this.data.userId ? this.data.userId : this.data.uid, pageNo: this.data.pageNo, pageSize: 6 }) if (this.data.userId) { let firstCard = this.data.list.length > 0 ? this.data.list[0] : '' list = list.filter(item => { return item.id != firstCard.id }) } list = [...this.data.list, ...list] let isMy = false if (list.length > 0) { isMy = list[0].uid == wx.getStorageSync('uid') } this.setData({ isMy, list, totalSize }) }, onReachBottom() { if (this.data.totalSize > this.data.list.length) { this.setData({ pageNo: this.data.pageNo + 1 }) this.getMyActivity() } }, async playAudio({ currentTarget }) { let item = currentTarget.dataset.item if (this.data.userAudioState) { if (this.data.playAudioId == item.id) { return this.clearAudio() } this.clearAudio() } this.innerAudioContext.src = item.audioPath; this.innerAudioContext.play(); this.setDuration('endTime', item.duration) this.setData({ playAudioId: item.id, userAudioState: true }) await playAudioEvent(item.id) }, clearAudio() { this.setData({ playAudioId: null, userAudioState: false, playTime: '00:00', endTime: '00:00', vProgress: 0, }) this.innerAudioContext.stop(); }, delete({ currentTarget }) { wx.showModal({ title: '确认删除吗?', content: '作品将被永久删除,无法找回。', confirmText: '确认', cancelText: '取消', success: async (res) => { if (res.confirm) { await deleteActivity(currentTarget.dataset.id) let beforeList = this.data.list.filter(item => { return item.id != currentTarget.dataset.id }) this.setData({ list: beforeList }) wx.showToast({ title: '删除成功!', icon: "none" }) this.clearAudio() } } }) }, async setLike({ currentTarget }) { if (currentTarget.dataset.item.isLike) { return } let index = currentTarget.dataset.index await thumbsUp(currentTarget.dataset.item.id) this.setData({ [`list[${index}].isLike`]: true, [`list[${index}].likeAmount`]: ++this.data.list[index].likeAmount }) }, async jump() { await actionRecord({ action: 'NEW_YEAR_ACTIVITY_PLAY_TOO', }) wx.redirectTo({ url: '/pages/activityList/index', }) }, // 设置时间文案 setDuration(label, s) { let t = ''; s = Math.ceil(s); if (s > -1) { let min = Math.floor(s / 60) % 60; let sec = s % 60; if (min < 10) { t += "0"; } t += min + ":"; if (sec < 10) { t += "0"; } t += sec; } this.setData({ [label]: t, }) }, jumpIndex() { wx.redirectTo({ url: '/pages/index/index', }) }, createActivityImg(shareInfo) { return new Promise(async (resolve, reject) => { let title = await getShareText({ cardReadId: shareInfo.id }) resolve({ title, path: `/pages/greeting/index?uid=${shareInfo.uid}&cardId=${shareInfo.id}`, imageUrl: shareInfo.cardUrl }) }) }, onShareAppMessage({ target, from, }) { if (from == 'button') { let shareInfo = target.dataset.item const promise = new Promise(resolve => { this.createActivityImg(shareInfo).then(res => { resolve(res) }) }) actionRecord({ action: 'NEW_YEAR_ACTIVITY_SHARE', }) return { title: '', path: `/pages/greeting/index?uid=${shareInfo.uid}&cardId=${shareInfo.id}`, imageUrl: target.dataset.img, promise } } else { return { title: '拜年还能这么玩?语音贺卡超有趣,点我开玩!', path: `/pages/activityList/index?uid=${wx.getStorageSync('uid')}`, imageUrl: 'http://reader-wx.ai160.com/images/reader/card/share-new-activity.png' } } }, onShareTimeline() { return { title: '这吉祥话说得太溜了,快来听听吧!', query: `uid=${wx.getStorageSync('uid')}`, imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/xkx_logo.jpg' } }, onHide() { this.clearAudio() }, onUnload() { this.clearAudio() } })