index.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. this.setData({
  35. videoInfoCopy: newVal
  36. })
  37. }
  38. },
  39. videoType: {
  40. type: String,
  41. // value 为public时是默认公共样式,为my时为“我的”样式,展示下载删除是否公开,pk为pk的样式文案
  42. value: 'public'
  43. },
  44. currentId: {
  45. type: Number,
  46. }
  47. },
  48. data: {
  49. selfUid: wx.getStorageSync('uid'),
  50. videoInfoCopy: {}
  51. },
  52. methods: {
  53. // 播放视频
  54. playVideo() {
  55. this.triggerEvent('playVideo', this.properties.videoInfo.userRead.id)
  56. },
  57. // 设置视频公开还是隐私
  58. async setVideoPublic() {
  59. let info = this.properties.videoInfo.userRead
  60. let data = {
  61. id: info.id,
  62. status: info.status === 'NORMAL' ? 'DISABLE' : 'NORMAL'
  63. }
  64. let res = await setVideoStatus(data)
  65. if (res.status == 'DISABLE') {
  66. wx.showToast({
  67. title: '该作品仅自己可见',
  68. icon: 'none',
  69. duration: 2000
  70. })
  71. }
  72. this.setData({
  73. ['videoInfoCopy.userRead.status']: info.status === 'NORMAL' ? 'DISABLE' : 'NORMAL'
  74. })
  75. },
  76. // 点赞
  77. async likeVideo() {
  78. let {
  79. id
  80. } = this.properties.videoInfo.userRead
  81. if (this.properties.videoInfo.isLike) {
  82. return
  83. }
  84. await likeVideo(id)
  85. this.setData({
  86. ['videoInfoCopy.isLike']: true,
  87. ['videoInfoCopy.userRead.likeAmount']: this.data.videoInfoCopy.userRead.likeAmount + 1
  88. })
  89. },
  90. // 下载视频
  91. download() {
  92. wx.showLoading({
  93. title: '保存到本地',
  94. mask: true
  95. })
  96. const url = this.properties.videoInfo.userRead.markPath || ''
  97. wx.downloadFile({
  98. url,
  99. success(res) {
  100. if (res.statusCode === 200) {
  101. wx.saveVideoToPhotosAlbum({
  102. filePath: res.tempFilePath,
  103. success(res) {
  104. wx.hideLoading()
  105. wx.showToast({
  106. title: '成功保存到相册!',
  107. duration: 3000,
  108. icon: 'success',
  109. mask: true
  110. })
  111. },
  112. fail() {
  113. wx.hideLoading()
  114. wx.showToast({
  115. title: '网络不给力',
  116. icon: 'error',
  117. duration: 3000,
  118. mask: true
  119. })
  120. }
  121. })
  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. openComment() {
  137. this.triggerEvent('openComment')
  138. },
  139. // 删除
  140. delete() {
  141. let {
  142. id
  143. } = this.properties.videoInfo.userRead
  144. wx.showModal({
  145. title: '确认删除吗?',
  146. content: '作品将被永久删除,无法找回。',
  147. confirmText: '确认',
  148. cancelText: '取消',
  149. success: async (res) => {
  150. if (res.confirm) {
  151. let data = {
  152. id,
  153. status: 'DEL'
  154. }
  155. await setVideoStatus(data)
  156. wx.showToast({
  157. title: '删除成功!',
  158. icon: "none"
  159. })
  160. this.triggerEvent('getList')
  161. }
  162. }
  163. })
  164. },
  165. // 收藏课程
  166. async collect() {
  167. let {
  168. id,
  169. type,
  170. uid
  171. } = this.properties.videoInfo.userRead
  172. if (wx.getStorageSync('uid') == uid) {
  173. return
  174. }
  175. await collectVideo({
  176. targetCode: id,
  177. favoritesType: type
  178. })
  179. this.setData({
  180. ['videoInfoCopy.isFavorites']: !this.data.videoInfoCopy.isFavorites
  181. })
  182. },
  183. // 关注
  184. async setFans() {
  185. if (this.properties.videoInfo.isFans) {
  186. return
  187. }
  188. await setFans({
  189. uid: this.properties.videoInfo.user.uid
  190. })
  191. this.triggerEvent('setListFans', this.properties.videoInfo.user.uid)
  192. },
  193. toPkPage() {
  194. let videoInfo = this.data.videoInfoCopy
  195. if (this.properties.videoType == 'pk') {
  196. console.log(videoInfo);
  197. this.setPkData({
  198. nickName: videoInfo.user.nickName || videoInfo.user.eid,
  199. avatar: videoInfo.user.avatar,
  200. score: videoInfo.userRead.score,
  201. audioPath: videoInfo.userRead.audioPath,
  202. exampleId: videoInfo.userRead.exampleId
  203. })
  204. }
  205. let readId = videoInfo.userRead.id
  206. let url = this.properties.videoType == 'public' ? `/pages/pkPage/index?videoId=${readId}` : `/pages/reading/index?videoId=${videoInfo.userRead.exampleId}&readingType=pk`
  207. wx.navigateTo({
  208. url
  209. })
  210. },
  211. jumpUserInfo() {
  212. wx.navigateTo({
  213. url: `/pages/personal/index?uid=${this.data.videoInfoCopy.user.uid}`,
  214. })
  215. },
  216. // 控制音频播放
  217. audioPlay() {
  218. this.triggerEvent('playAudio')
  219. }
  220. }
  221. })