123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250 |
- 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) {
- console.log(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)
- this.setData({
- vProgress: Math.ceil((Math.ceil(this.innerAudioContext.currentTime) / this.innerAudioContext.duration) * 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.navigateTo({
- 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,
- })
- },
- createActivityImg(imageUrl, cardReadId) {
- return new Promise(async (resolve, reject) => {
- let title = await getShareText({
- cardReadId
- })
- resolve({
- title,
- path: `/pages/greeting/index?uid=${wx.getStorageSync('uid')}&cardId=${cardReadId}`,
- imageUrl
- })
- })
- },
- onShareAppMessage({
- target,
- from,
- }) {
- console.log(target.dataset.item);
- if (from == 'button') {
- const promise = new Promise(resolve => {
- this.createActivityImg(target.dataset.item.cardUrl, target.dataset.item.id).then(res => {
- resolve(res)
- })
- })
- actionRecord({
- action: 'NEW_YEAR_ACTIVITY_SHARE',
- })
- return {
- title: '',
- path: `/pages/greeting/index?uid=${wx.getStorageSync('uid')}&cardId=${target.dataset.item.id}`,
- imageUrl: target.dataset.img,
- promise
- }
- } else {
- return {
- title: '课文朗读,从未如此有趣。',
- path: `/pages/index/index?uid=${wx.getStorageSync('uid')}`,
- imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/shareContent.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()
- }
- })
|