index.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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时为“我的”样式,展示下载删除是否公开
  26. value: 'public'
  27. },
  28. index: {
  29. type: Number,
  30. },
  31. currentId: {
  32. type: Number,
  33. }
  34. },
  35. data: {
  36. selfUid: wx.getStorageSync('uid'),
  37. videoInfoCopy: {}
  38. },
  39. methods: {
  40. // 播放视频
  41. playVideo() {
  42. this.triggerEvent('playVideo', this.properties.videoInfo.userRead.id)
  43. },
  44. // 分享视频
  45. shareVideo() {
  46. let videoInfo = this.properties.videoInfo
  47. console.log(videoInfo);
  48. },
  49. // 设置视频公开还是隐私
  50. async setVideoPublic() {
  51. let info = this.properties.videoInfo.userRead
  52. let data = {
  53. id: info.id,
  54. status: info.status === 'NORMAL' ? 'DISABLE' : 'NORMAL'
  55. }
  56. let res = await setVideoStatus(data)
  57. if (res.status == 'DISABLE') {
  58. wx.showToast({
  59. title: '该作品仅自己可见',
  60. icon: 'none',
  61. duration: 2000
  62. })
  63. }
  64. let index = this.properties.index
  65. let status = `list[${index}].userRead.status`;
  66. let val = info.status === 'NORMAL' ? 'DISABLE' : 'NORMAL'
  67. let options = {
  68. [status]: val
  69. }
  70. this.triggerEvent('changStatus', options)
  71. },
  72. // 点赞
  73. async likeVideo() {
  74. let {
  75. id
  76. } = this.properties.videoInfo.userRead
  77. if (this.properties.videoInfo.isLike) {
  78. return
  79. }
  80. await likeVideo(id)
  81. let index = this.properties.index
  82. let likeStr = `list[${index}].isLike`;
  83. let likeNumStr = `list[${index}].userRead.likeAmount`;
  84. let options = {
  85. [likeStr]: true,
  86. [likeNumStr]: this.properties.videoInfo.userRead.likeAmount + 1
  87. }
  88. this.triggerEvent('changStatus', options)
  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', this.properties.videoInfo.userRead.id)
  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(e) {
  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. let index = this.properties.index
  180. let isFavorites = `list[${index}].isFavorites`;
  181. let options = {
  182. [isFavorites]: !this.properties.videoInfo.isFavorites
  183. }
  184. this.triggerEvent('changStatus', options)
  185. },
  186. // 关注
  187. async setFans() {
  188. if (this.properties.videoInfo.isFans) {
  189. return
  190. }
  191. await setFans({
  192. uid: this.properties.videoInfo.user.uid
  193. })
  194. this.setData({
  195. ['videoInfoCopy.isFans']: true
  196. })
  197. }
  198. }
  199. })