index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import {
  2. getMyActivity,
  3. thumbsUp,
  4. deleteActivity,
  5. getShareText,
  6. playAudioEvent
  7. } from '~/api/activity'
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. isMy: false,
  14. pageNo: 1,
  15. totalSize: 0,
  16. list: [],
  17. uid: '',
  18. playAudioId: null,
  19. // 当前音频播放时长
  20. playTime: '00:00',
  21. endTime: '00:00',
  22. vProgress: 0,
  23. userAudioState: false,
  24. },
  25. /**
  26. * 生命周期函数--监听页面加载
  27. */
  28. onLoad(options) {
  29. this.getMyActivity()
  30. this.setData({
  31. uid: wx.getStorageSync('uid')
  32. })
  33. this.innerAudioContext = wx.createInnerAudioContext();
  34. this.innerAudioContext.onTimeUpdate(() => {
  35. this.setDuration('playTime', this.innerAudioContext.currentTime)
  36. this.setData({
  37. vProgress: Math.ceil((Math.ceil(this.innerAudioContext.currentTime) / this.innerAudioContext.duration) * 100)
  38. })
  39. })
  40. this.innerAudioContext.onEnded((res) => {
  41. this.clearAudio()
  42. });
  43. },
  44. async getMyActivity() {
  45. let {
  46. list,
  47. totalSize
  48. } = await getMyActivity({
  49. pageNo: this.data.pageNo,
  50. pageSize: 6
  51. })
  52. list = [...this.data.list, ...list]
  53. let isMy = false
  54. if (list.length > 0) {
  55. isMy = list[0].uid == wx.getStorageSync('uid')
  56. }
  57. this.setData({
  58. list,
  59. totalSize
  60. })
  61. },
  62. onReachBottom() {
  63. if (this.data.totalSize > this.data.list.length) {
  64. this.setData({
  65. pageNo: this.data.pageNo + 1
  66. })
  67. this.getMyActivity()
  68. }
  69. },
  70. async playAudio({
  71. currentTarget
  72. }) {
  73. let item = currentTarget.dataset.item
  74. if (this.data.userAudioState) {
  75. if (this.data.playAudioId == item.id) {
  76. return this.clearAudio()
  77. }
  78. this.clearAudio()
  79. }
  80. this.innerAudioContext.src = item.audioPath;
  81. this.innerAudioContext.play();
  82. this.setDuration('endTime', item.duration)
  83. this.setData({
  84. playAudioId: item.id,
  85. userAudioState: true
  86. })
  87. await playAudioEvent(item.id)
  88. },
  89. clearAudio() {
  90. this.setData({
  91. playAudioId: null,
  92. userAudioState: false,
  93. playTime: '00:00',
  94. endTime: '00:00',
  95. vProgress: 0,
  96. })
  97. this.innerAudioContext.stop();
  98. },
  99. delete({
  100. currentTarget
  101. }) {
  102. wx.showModal({
  103. title: '确认删除吗?',
  104. content: '作品将被永久删除,无法找回。',
  105. confirmText: '确认',
  106. cancelText: '取消',
  107. success: async (res) => {
  108. if (res.confirm) {
  109. await deleteActivity(currentTarget.dataset.id)
  110. let beforeList = this.data.list.filter(item => {
  111. return item.id != currentTarget.dataset.id
  112. })
  113. this.setData({
  114. list: beforeList
  115. })
  116. wx.showToast({
  117. title: '删除成功!',
  118. icon: "none"
  119. })
  120. this.clearAudio()
  121. }
  122. }
  123. })
  124. },
  125. async setLike({
  126. currentTarget
  127. }) {
  128. if (currentTarget.dataset.item.isLike) {
  129. return
  130. }
  131. let index = currentTarget.dataset.index
  132. await thumbsUp(currentTarget.dataset.item.id)
  133. this.setData({
  134. [`list[${index}].isLike`]: true,
  135. [`list[${index}].likeAmount`]: ++this.data.list[index].likeAmount
  136. })
  137. },
  138. jump() {
  139. wx.navigateTo({
  140. url: '/pages/activityList/index',
  141. })
  142. }, // 设置时间文案
  143. setDuration(label, s) {
  144. let t = '';
  145. s = Math.ceil(s);
  146. if (s > -1) {
  147. let min = Math.floor(s / 60) % 60;
  148. let sec = s % 60;
  149. if (min < 10) {
  150. t += "0";
  151. }
  152. t += min + ":";
  153. if (sec < 10) {
  154. t += "0";
  155. }
  156. t += sec;
  157. }
  158. this.setData({
  159. [label]: t,
  160. })
  161. },
  162. createActivityImg(imageUrl, cardReadId) {
  163. return new Promise(async (resolve, reject) => {
  164. let title = await getShareText({
  165. cardReadId
  166. })
  167. resolve({
  168. title,
  169. path: `/pages/greeting/index?&uid=${wx.getStorageSync('uid')}`,
  170. imageUrl
  171. })
  172. })
  173. },
  174. onShareAppMessage({
  175. target,
  176. from,
  177. }) {
  178. if (from == 'button') {
  179. const promise = new Promise(resolve => {
  180. this.createActivityImg(target.dataset.item.cardUrl, target.dataset.item.id).then(res => {
  181. resolve(res)
  182. })
  183. })
  184. return {
  185. title: '',
  186. path: `/pages/greeting/index?&uid=${wx.getStorageSync('uid')}`,
  187. imageUrl: target.dataset.img,
  188. promise
  189. }
  190. } else {
  191. return {
  192. title: '课文朗读,从未如此有趣。',
  193. path: `/pages/index/index?&uid=${wx.getStorageSync('uid')}`,
  194. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/shareContent.png'
  195. }
  196. }
  197. },
  198. onHide() {
  199. this.clearAudio()
  200. },
  201. onUnload() {
  202. this.clearAudio()
  203. }
  204. })