index.js 6.0 KB

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