index.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import {
  2. setVideoStatus,
  3. likeVideo,
  4. collectVideo,
  5. } from '~/api/video'
  6. import {
  7. setFans
  8. } from '~/api/user'
  9. Component({
  10. /**
  11. * 组件的属性列表
  12. */
  13. properties: {
  14. videoInfo: {
  15. type: Object,
  16. value: {},
  17. observer(newVal) {
  18. this.setData({
  19. videoInfoCopy: newVal
  20. })
  21. }
  22. },
  23. videoType: {
  24. type: String,
  25. // value 为public时是默认公共样式,为my时为“我的”样式,展示下载删除是否公开,pk为pk的样式文案
  26. value: 'public'
  27. },
  28. currentId: {
  29. type: Number,
  30. }
  31. },
  32. data: {
  33. selfUid: wx.getStorageSync('uid'),
  34. videoInfoCopy: {}
  35. },
  36. methods: {
  37. // 播放视频
  38. playVideo() {
  39. this.triggerEvent('playVideo', this.properties.videoInfo.userRead.id)
  40. },
  41. // 设置视频公开还是隐私
  42. async setVideoPublic() {
  43. let info = this.properties.videoInfo.userRead
  44. let data = {
  45. id: info.id,
  46. status: info.status === 'NORMAL' ? 'DISABLE' : 'NORMAL'
  47. }
  48. let res = await setVideoStatus(data)
  49. if (res.status == 'DISABLE') {
  50. wx.showToast({
  51. title: '该作品仅自己可见',
  52. icon: 'none',
  53. duration: 2000
  54. })
  55. }
  56. this.setData({
  57. ['videoInfoCopy.userRead.status']: info.status === 'NORMAL' ? 'DISABLE' : 'NORMAL'
  58. })
  59. },
  60. // 点赞
  61. async likeVideo() {
  62. let {
  63. id
  64. } = this.properties.videoInfo.userRead
  65. if (this.properties.videoInfo.isLike) {
  66. return
  67. }
  68. await likeVideo(id)
  69. this.setData({
  70. ['videoInfoCopy.isLike']: true,
  71. ['videoInfoCopy.userRead.likeAmount']: this.data.videoInfoCopy.userRead.likeAmount + 1
  72. })
  73. },
  74. // 下载视频
  75. download() {
  76. wx.showLoading({
  77. title: '保存到本地',
  78. mask: true
  79. })
  80. const url = this.properties.videoInfo.userRead.markPath || ''
  81. wx.downloadFile({
  82. url,
  83. success(res) {
  84. if (res.statusCode === 200) {
  85. wx.saveVideoToPhotosAlbum({
  86. filePath: res.tempFilePath,
  87. success(res) {
  88. wx.hideLoading()
  89. wx.showToast({
  90. title: '成功保存到相册!',
  91. duration: 3000,
  92. icon: 'success',
  93. mask: true
  94. })
  95. },
  96. fail() {
  97. wx.hideLoading()
  98. wx.showToast({
  99. title: '网络不给力',
  100. icon: 'error',
  101. duration: 3000,
  102. mask: true
  103. })
  104. }
  105. })
  106. }
  107. },
  108. fail() {
  109. wx.hideLoading()
  110. wx.showToast({
  111. title: '网络不给力',
  112. icon: 'error',
  113. duration: 3000,
  114. mask: true
  115. })
  116. }
  117. })
  118. },
  119. //评论
  120. openComment() {
  121. this.triggerEvent('openComment', this.properties.videoInfo.userRead.id)
  122. },
  123. // 删除
  124. delete() {
  125. let {
  126. id
  127. } = this.properties.videoInfo.userRead
  128. wx.showModal({
  129. title: '确认删除吗?',
  130. content: '作品将被永久删除,无法找回。',
  131. confirmText: '确认',
  132. cancelText: '取消',
  133. success: async (res) => {
  134. if (res.confirm) {
  135. let data = {
  136. id,
  137. status: 'DEL'
  138. }
  139. await setVideoStatus(data)
  140. wx.showToast({
  141. title: '删除成功!',
  142. icon: "none"
  143. })
  144. this.triggerEvent('getList')
  145. }
  146. }
  147. })
  148. },
  149. // 收藏课程
  150. async collect() {
  151. let {
  152. id,
  153. type,
  154. uid
  155. } = this.properties.videoInfo.userRead
  156. if (wx.getStorageSync('uid') == uid) {
  157. return
  158. }
  159. await collectVideo({
  160. targetCode: id,
  161. favoritesType: type
  162. })
  163. this.setData({
  164. ['videoInfoCopy.isFavorites']: !this.data.videoInfoCopy.isFavorites
  165. })
  166. },
  167. // 关注
  168. async setFans() {
  169. if (this.properties.videoInfo.isFans) {
  170. return
  171. }
  172. await setFans({
  173. uid: this.properties.videoInfo.user.uid
  174. })
  175. this.triggerEvent('setListFans', this.properties.videoInfo.user.uid)
  176. },
  177. toPkPage() {
  178. let readId = this.data.videoInfoCopy.userRead.id
  179. let url = this.properties.videoType == 'public' ? `/pages/pkPage/index?videoId=${readId}` : `/pages/reading/index?videoId=${this.data.videoInfoCopy.userRead.exampleId}`
  180. wx.navigateTo({
  181. url
  182. })
  183. }
  184. }
  185. })