index.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import {
  2. storeBindingsBehavior
  3. } from 'mobx-miniprogram-bindings'
  4. import {
  5. store
  6. } from '~/store/index'
  7. import {
  8. setVideoStatus,
  9. likeVideo,
  10. collectVideo,
  11. submitPlayLog
  12. } from '~/api/video'
  13. import {
  14. setFans
  15. } from '~/api/user'
  16. Component({
  17. behaviors: [storeBindingsBehavior],
  18. storeBindings: {
  19. store,
  20. fields: {
  21. pkData: 'pkData'
  22. },
  23. actions: {
  24. setPkData: 'setPkData'
  25. }
  26. },
  27. /**
  28. * 组件的属性列表
  29. */
  30. properties: {
  31. videoInfo: {
  32. type: Object,
  33. value: {},
  34. observer(newVal) {
  35. if (newVal.userReadExtend && newVal.userReadExtend.resourcesType == 1) {
  36. newVal.userRead.title = newVal.userRead.title.split('\n')
  37. }
  38. this.setData({
  39. videoInfoCopy: newVal
  40. })
  41. }
  42. },
  43. videoType: {
  44. type: String,
  45. // value 为public时是默认公共样式,为my时为“我的”样式,展示下载删除是否公开,pk为pk的样式文案,excellent是优秀作品展播
  46. value: 'public'
  47. },
  48. currentId: {
  49. type: Number
  50. }
  51. },
  52. data: {
  53. selfUid: wx.getStorageSync('uid'),
  54. videoInfoCopy: {}
  55. },
  56. methods: {
  57. // 播放视频
  58. playVideo() {
  59. this.triggerEvent('playVideo', this.properties.videoInfo.userRead.id)
  60. this.submitPlayLog(this.properties.videoInfo.userRead.id)
  61. },
  62. // 设置视频公开还是隐私
  63. async setVideoPublic() {
  64. let info = this.properties.videoInfo.userRead
  65. let data = {
  66. id: info.id,
  67. status: info.status === 'NORMAL' ? 'DISABLE' : 'NORMAL'
  68. }
  69. let res = await setVideoStatus(data)
  70. if (res.status == 'DISABLE') {
  71. wx.showToast({
  72. title: '该作品仅自己可见',
  73. icon: 'none',
  74. duration: 2000
  75. })
  76. }
  77. this.setData({
  78. ['videoInfoCopy.userRead.status']: info.status === 'NORMAL' ? 'DISABLE' : 'NORMAL'
  79. })
  80. },
  81. // 点赞
  82. async likeVideo() {
  83. let {
  84. id
  85. } = this.properties.videoInfo.userRead
  86. if (this.properties.videoInfo.isLike) {
  87. return
  88. }
  89. await likeVideo(id)
  90. this.setData({
  91. ['videoInfoCopy.isLike']: true,
  92. ['videoInfoCopy.userRead.likeAmount']: this.data.videoInfoCopy.userRead.likeAmount + 1
  93. })
  94. },
  95. // 下载视频
  96. download() {
  97. wx.showLoading({
  98. title: '保存到本地',
  99. mask: true
  100. })
  101. const url = this.properties.videoInfo.userRead.markPath || ''
  102. wx.downloadFile({
  103. url,
  104. success(res) {
  105. if (res.statusCode === 200) {
  106. wx.saveVideoToPhotosAlbum({
  107. filePath: res.tempFilePath,
  108. success(res) {
  109. wx.hideLoading()
  110. wx.showToast({
  111. title: '成功保存到相册!',
  112. duration: 3000,
  113. icon: 'success',
  114. mask: true
  115. })
  116. },
  117. fail() {
  118. wx.hideLoading()
  119. wx.showToast({
  120. title: '网络不给力',
  121. icon: 'error',
  122. duration: 3000,
  123. mask: true
  124. })
  125. }
  126. })
  127. }
  128. },
  129. fail() {
  130. wx.hideLoading()
  131. wx.showToast({
  132. title: '网络不给力',
  133. icon: 'error',
  134. duration: 3000,
  135. mask: true
  136. })
  137. }
  138. })
  139. },
  140. //评论
  141. openComment() {
  142. this.triggerEvent('openComment')
  143. },
  144. // 删除
  145. delete() {
  146. let {
  147. id
  148. } = this.properties.videoInfo.userRead
  149. wx.showModal({
  150. title: '确认删除吗?',
  151. content: '作品将被永久删除,无法找回。',
  152. confirmText: '确认',
  153. cancelText: '取消',
  154. success: async (res) => {
  155. if (res.confirm) {
  156. let data = {
  157. id,
  158. status: 'DEL'
  159. }
  160. await setVideoStatus(data)
  161. wx.showToast({
  162. title: '删除成功!',
  163. icon: "none"
  164. })
  165. this.triggerEvent('deleteVideo', this.properties.videoInfo.userRead.id)
  166. }
  167. }
  168. })
  169. },
  170. // 收藏课程
  171. async collect() {
  172. let {
  173. id,
  174. type,
  175. uid
  176. } = this.properties.videoInfo.userRead
  177. if (wx.getStorageSync('uid') == uid) {
  178. return wx.showToast({
  179. title: '不能收藏自己作品哦!',
  180. icon: "none"
  181. })
  182. }
  183. await collectVideo({
  184. targetCode: id,
  185. favoritesType: type
  186. })
  187. this.setData({
  188. ['videoInfoCopy.isFavorites']: !this.data.videoInfoCopy.isFavorites
  189. })
  190. },
  191. // 关注
  192. async setFans() {
  193. if (this.properties.videoInfo.isFans) {
  194. return
  195. }
  196. await setFans({
  197. uid: this.properties.videoInfo.user.uid
  198. })
  199. this.triggerEvent('setListFans', this.properties.videoInfo.user.uid)
  200. },
  201. toPkPage() {
  202. let videoInfo = this.data.videoInfoCopy
  203. if (this.properties.videoType == 'pk') {
  204. if (videoInfo.user.uid == wx.getStorageSync('uid')) {
  205. return wx.showToast({
  206. title: '不能与自己PK哦~',
  207. icon: 'none'
  208. })
  209. }
  210. this.setPkData({
  211. nickName: videoInfo.user.nickName || videoInfo.user.eid,
  212. uid: videoInfo.user.uid,
  213. avatar: videoInfo.user.avatar,
  214. score: videoInfo.userRead.score,
  215. audioPath: videoInfo.userRead.audioPath,
  216. exampleId: videoInfo.userRead.exampleId,
  217. id: videoInfo.userRead.id
  218. })
  219. }
  220. let readId = videoInfo.userRead.id
  221. let url = this.properties.videoType == 'excellent' ? `/pages/pkPage/index?videoId=${readId}` : `/pages/reading/index?videoId=${videoInfo.userRead.exampleId}&readingType=${this.properties.videoType}`
  222. wx.navigateTo({
  223. url
  224. })
  225. },
  226. jumpUserInfo() {
  227. wx.navigateTo({
  228. url: `/pages/personal/index?uid=${this.data.videoInfoCopy.user.uid}&type=user`,
  229. })
  230. },
  231. // 控制音频播放
  232. audioPlay() {
  233. this.triggerEvent('playAudio')
  234. this.submitPlayLog(this.properties.videoInfo.userRead.id)
  235. },
  236. // 统计作品播放次数
  237. async submitPlayLog(userReadId) {
  238. await submitPlayLog({
  239. userReadId,
  240. playStopTime: 1000
  241. })
  242. },
  243. }
  244. })