index.js 6.4 KB

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