index.js 5.2 KB

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