index.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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.uid) {
  41. this.setData({
  42. userId: options.uid
  43. })
  44. }
  45. if (options.cardId) {
  46. let oneCard = await getOneActivity(options.cardId)
  47. this.setData({
  48. list: oneCard ? [oneCard] : [],
  49. })
  50. await actionRecord({
  51. action: 'NEW_YEAR_ACTIVITY_APP_LINK',
  52. })
  53. }
  54. this.getMyActivity()
  55. this.innerAudioContext = wx.createInnerAudioContext();
  56. this.innerAudioContext.onTimeUpdate(() => {
  57. this.setDuration('playTime', this.innerAudioContext.currentTime)
  58. this.setData({
  59. vProgress: Math.ceil((Math.ceil(this.innerAudioContext.currentTime) / this.innerAudioContext.duration) * 100)
  60. })
  61. })
  62. this.innerAudioContext.onEnded((res) => {
  63. this.clearAudio()
  64. });
  65. },
  66. async getMyActivity() {
  67. let {
  68. list,
  69. totalSize
  70. } = await getMyActivity({
  71. userUid: this.data.userId ? this.data.userId : this.data.uid,
  72. pageNo: this.data.pageNo,
  73. pageSize: 6
  74. })
  75. if (this.data.userId) {
  76. let firstCard = this.data.list.length > 0 ? this.data.list[0] : ''
  77. list = list.filter(item => {
  78. return item.id != firstCard.id
  79. })
  80. }
  81. list = [...this.data.list, ...list]
  82. let isMy = false
  83. if (list.length > 0) {
  84. isMy = list[0].uid == wx.getStorageSync('uid')
  85. }
  86. this.setData({
  87. isMy,
  88. list,
  89. totalSize
  90. })
  91. },
  92. onReachBottom() {
  93. if (this.data.totalSize > this.data.list.length) {
  94. this.setData({
  95. pageNo: this.data.pageNo + 1
  96. })
  97. this.getMyActivity()
  98. }
  99. },
  100. async playAudio({
  101. currentTarget
  102. }) {
  103. let item = currentTarget.dataset.item
  104. if (this.data.userAudioState) {
  105. if (this.data.playAudioId == item.id) {
  106. return this.clearAudio()
  107. }
  108. this.clearAudio()
  109. }
  110. this.innerAudioContext.src = item.audioPath;
  111. this.innerAudioContext.play();
  112. this.setDuration('endTime', item.duration)
  113. this.setData({
  114. playAudioId: item.id,
  115. userAudioState: true
  116. })
  117. await playAudioEvent(item.id)
  118. },
  119. clearAudio() {
  120. this.setData({
  121. playAudioId: null,
  122. userAudioState: false,
  123. playTime: '00:00',
  124. endTime: '00:00',
  125. vProgress: 0,
  126. })
  127. this.innerAudioContext.stop();
  128. },
  129. delete({
  130. currentTarget
  131. }) {
  132. wx.showModal({
  133. title: '确认删除吗?',
  134. content: '作品将被永久删除,无法找回。',
  135. confirmText: '确认',
  136. cancelText: '取消',
  137. success: async (res) => {
  138. if (res.confirm) {
  139. await deleteActivity(currentTarget.dataset.id)
  140. let beforeList = this.data.list.filter(item => {
  141. return item.id != currentTarget.dataset.id
  142. })
  143. this.setData({
  144. list: beforeList
  145. })
  146. wx.showToast({
  147. title: '删除成功!',
  148. icon: "none"
  149. })
  150. this.clearAudio()
  151. }
  152. }
  153. })
  154. },
  155. async setLike({
  156. currentTarget
  157. }) {
  158. if (currentTarget.dataset.item.isLike) {
  159. return
  160. }
  161. let index = currentTarget.dataset.index
  162. await thumbsUp(currentTarget.dataset.item.id)
  163. this.setData({
  164. [`list[${index}].isLike`]: true,
  165. [`list[${index}].likeAmount`]: ++this.data.list[index].likeAmount
  166. })
  167. },
  168. async jump() {
  169. await actionRecord({
  170. action: 'NEW_YEAR_ACTIVITY_PLAY_TOO',
  171. })
  172. wx.redirectTo({
  173. url: '/pages/activityList/index',
  174. })
  175. }, // 设置时间文案
  176. setDuration(label, s) {
  177. let t = '';
  178. s = Math.ceil(s);
  179. if (s > -1) {
  180. let min = Math.floor(s / 60) % 60;
  181. let sec = s % 60;
  182. if (min < 10) {
  183. t += "0";
  184. }
  185. t += min + ":";
  186. if (sec < 10) {
  187. t += "0";
  188. }
  189. t += sec;
  190. }
  191. this.setData({
  192. [label]: t,
  193. })
  194. },
  195. jumpIndex() {
  196. wx.redirectTo({
  197. url: '/pages/index/index',
  198. })
  199. },
  200. createActivityImg(shareInfo) {
  201. return new Promise(async (resolve, reject) => {
  202. let title = await getShareText({
  203. cardReadId: shareInfo.id
  204. })
  205. resolve({
  206. title,
  207. path: `/pages/greeting/index?uid=${shareInfo.uid}&cardId=${shareInfo.id}`,
  208. imageUrl: shareInfo.cardUrl
  209. })
  210. })
  211. },
  212. onShareAppMessage({
  213. target,
  214. from,
  215. }) {
  216. if (from == 'button') {
  217. let shareInfo = target.dataset.item
  218. const promise = new Promise(resolve => {
  219. this.createActivityImg(shareInfo).then(res => {
  220. resolve(res)
  221. })
  222. })
  223. actionRecord({
  224. action: 'NEW_YEAR_ACTIVITY_SHARE',
  225. })
  226. return {
  227. title: '',
  228. path: `/pages/greeting/index?uid=${shareInfo.uid}&cardId=${shareInfo.id}`,
  229. imageUrl: target.dataset.img,
  230. promise
  231. }
  232. } else {
  233. return {
  234. title: '拜年还能这么玩?语音贺卡超有趣,点我开玩!',
  235. path: `/pages/activityList/index?uid=${wx.getStorageSync('uid')}`,
  236. imageUrl: 'http://reader-wx.ai160.com/images/reader/card/share-new-activity.png'
  237. }
  238. }
  239. },
  240. onShareTimeline() {
  241. return {
  242. title: '这吉祥话说得太溜了,快来听听吧!',
  243. query: `uid=${wx.getStorageSync('uid')}`,
  244. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/xkx_logo.jpg'
  245. }
  246. },
  247. onHide() {
  248. this.clearAudio()
  249. },
  250. onUnload() {
  251. this.clearAudio()
  252. }
  253. })