index.js 5.5 KB

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