index.js 7.0 KB

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