index.js 6.6 KB

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