index.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. submitPlayLog
  12. } from '~/api/video'
  13. import {
  14. setFans
  15. } from '~/api/user'
  16. import {
  17. setDuration
  18. } from '~/utils/util'
  19. Component({
  20. behaviors: [storeBindingsBehavior],
  21. storeBindings: {
  22. store,
  23. fields: {
  24. pkData: 'pkData'
  25. },
  26. actions: {
  27. setPkData: 'setPkData'
  28. }
  29. },
  30. /**
  31. * 组件的属性列表
  32. */
  33. properties: {
  34. videoInfo: {
  35. type: Object,
  36. value: {},
  37. observer(newVal) {
  38. if (newVal.userReadExtend && newVal.userReadExtend.resourcesType == 1) {
  39. newVal.userRead.title = newVal.userRead.title.split('\n')
  40. }
  41. this.setData({
  42. videoInfoCopy: newVal,
  43. isOfficial: newVal.userRead.type != 'APP_EXAMPLE'
  44. })
  45. }
  46. },
  47. videoType: {
  48. type: String,
  49. // value 为public时是默认公共样式,为my时为“我的”样式,展示下载删除是否公开,pk为pk的样式文案,
  50. value: 'public',
  51. },
  52. currentId: {
  53. type: Number
  54. },
  55. sliderValue: {
  56. type: Number,
  57. value: 0,
  58. },
  59. currentTime: {
  60. type: String,
  61. value: '00:00',
  62. }
  63. },
  64. data: {
  65. selfUid: wx.getStorageSync('uid'),
  66. videoInfoCopy: {},
  67. isOfficial: false
  68. },
  69. lifetimes: {
  70. attached() {
  71. let {
  72. userReadExtend,
  73. userRead
  74. } = this.data.videoInfoCopy
  75. if (userReadExtend.resourcesType == 1) {
  76. this.setData({
  77. endTime: setDuration(userRead.duration)
  78. })
  79. }
  80. }
  81. },
  82. methods: {
  83. // 播放视频
  84. playVideo() {
  85. this.triggerEvent('playVideo', this.data.videoInfoCopy.userRead.id)
  86. this.submitPlayLog(this.data.videoInfoCopy.userRead.id)
  87. },
  88. slider({
  89. detail
  90. }) {
  91. this.triggerEvent('setSeek', detail.value / 100 * this.data.videoInfoCopy.userRead.duration)
  92. },
  93. // 设置视频公开还是隐私
  94. async setVideoPublic() {
  95. let info = this.data.videoInfoCopy.userRead
  96. let data = {
  97. id: info.id,
  98. status: info.status === 'NORMAL' ? 'DISABLE' : 'NORMAL'
  99. }
  100. let res = await setVideoStatus(data)
  101. if (res.status == 'DISABLE') {
  102. wx.showToast({
  103. title: '该作品仅自己可见',
  104. icon: 'none',
  105. duration: 2000
  106. })
  107. }
  108. this.setData({
  109. ['videoInfoCopy.userRead.status']: info.status === 'NORMAL' ? 'DISABLE' : 'NORMAL'
  110. })
  111. },
  112. // 点赞
  113. async likeVideo() {
  114. let {
  115. id
  116. } = this.data.videoInfoCopy.userRead
  117. if (this.data.videoInfoCopy.isLike) {
  118. return
  119. }
  120. await likeVideo(id)
  121. this.setData({
  122. ['videoInfoCopy.isLike']: true,
  123. ['videoInfoCopy.userRead.likeAmount']: this.data.videoInfoCopy.userRead.likeAmount + 1
  124. })
  125. },
  126. // 下载视频
  127. download() {
  128. wx.showLoading({
  129. title: '保存到本地',
  130. mask: true
  131. })
  132. const url = this.data.videoInfoCopy.userRead.markPath || ''
  133. wx.downloadFile({
  134. url,
  135. success(res) {
  136. if (res.statusCode === 200) {
  137. wx.saveVideoToPhotosAlbum({
  138. filePath: res.tempFilePath,
  139. success(res) {
  140. wx.hideLoading()
  141. wx.showToast({
  142. title: '成功保存到相册!',
  143. duration: 3000,
  144. icon: 'success',
  145. mask: true
  146. })
  147. },
  148. fail() {
  149. wx.hideLoading()
  150. wx.showToast({
  151. title: '网络不给力',
  152. icon: 'error',
  153. duration: 3000,
  154. mask: true
  155. })
  156. }
  157. })
  158. }
  159. },
  160. fail() {
  161. wx.hideLoading()
  162. wx.showToast({
  163. title: '网络不给力',
  164. icon: 'error',
  165. duration: 3000,
  166. mask: true
  167. })
  168. }
  169. })
  170. },
  171. //评论
  172. openComment() {
  173. this.triggerEvent('openComment')
  174. },
  175. // 删除
  176. delete() {
  177. let {
  178. id
  179. } = this.data.videoInfoCopy.userRead
  180. wx.showModal({
  181. title: '确认删除吗?',
  182. content: '作品将被永久删除,无法找回。',
  183. confirmText: '确认',
  184. cancelText: '取消',
  185. success: async (res) => {
  186. if (res.confirm) {
  187. let data = {
  188. id,
  189. status: 'DEL'
  190. }
  191. await setVideoStatus(data)
  192. wx.showToast({
  193. title: '删除成功!',
  194. icon: "none"
  195. })
  196. this.triggerEvent('deleteVideo', this.data.videoInfoCopy.userRead.id)
  197. }
  198. }
  199. })
  200. },
  201. // 收藏课程
  202. async collect() {
  203. let {
  204. id,
  205. type,
  206. uid
  207. } = this.data.videoInfoCopy.userRead
  208. if (wx.getStorageSync('uid') == uid) {
  209. return wx.showToast({
  210. title: '不能收藏自己作品哦!',
  211. icon: "none"
  212. })
  213. }
  214. await collectVideo({
  215. targetCode: id,
  216. favoritesType: type
  217. })
  218. this.setData({
  219. ['videoInfoCopy.isFavorites']: !this.data.videoInfoCopy.isFavorites
  220. })
  221. },
  222. // 关注
  223. async setFans() {
  224. if (this.data.videoInfoCopy.isFans) {
  225. return
  226. }
  227. await setFans({
  228. uid: this.data.videoInfoCopy.user.uid
  229. })
  230. this.triggerEvent('setListFans', this.data.videoInfoCopy.user.uid)
  231. },
  232. toPkPage() {
  233. let videoInfo = this.data.videoInfoCopy
  234. if (this.properties.videoType == 'pk') {
  235. if (videoInfo.user.uid == wx.getStorageSync('uid')) {
  236. return wx.showToast({
  237. title: '不能与自己PK哦~',
  238. icon: 'none'
  239. })
  240. }
  241. this.setPkData({
  242. nickName: videoInfo.user.nickName || videoInfo.user.eid,
  243. uid: videoInfo.user.uid,
  244. avatar: videoInfo.user.avatar,
  245. score: videoInfo.userRead.score,
  246. audioPath: videoInfo.userRead.audioPath,
  247. exampleId: videoInfo.userRead.exampleId,
  248. id: videoInfo.userRead.id
  249. })
  250. }
  251. let readId = videoInfo.userRead.id
  252. let videoType = this.properties.videoType
  253. let url = ''
  254. if (!this.data.isOfficial || this.data.selfUid == videoInfo.user.uid) {
  255. url = `/pages/reading/index?videoId=${videoInfo.userRead.exampleId}`
  256. } else if (videoType == 'public' || videoType == 'follow') {
  257. url = `/pages/pkPage/index?videoId=${readId}`
  258. } else if (videoType == 'pk') {
  259. url = `/pages/reading/index?videoId=${videoInfo.userRead.exampleId}&readingType=${videoType}`
  260. } else {
  261. url = `/pages/reading/index?videoId=${videoInfo.userRead.exampleId}`
  262. }
  263. console.log(url);
  264. wx.navigateTo({
  265. url
  266. })
  267. },
  268. jumpUserInfo() {
  269. wx.navigateTo({
  270. url: `/pages/personal/index?uid=${this.data.videoInfoCopy.user.uid}&type=user`,
  271. })
  272. },
  273. // 控制音频播放
  274. audioPlay() {
  275. this.triggerEvent('playAudio')
  276. this.submitPlayLog(this.data.videoInfoCopy.userRead.id)
  277. },
  278. // 统计作品播放次数
  279. async submitPlayLog(userReadId) {
  280. await submitPlayLog({
  281. userReadId,
  282. playStopTime: 1000
  283. })
  284. },
  285. }
  286. })