index.js 6.8 KB

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