index.js 7.3 KB

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