index.js 7.3 KB

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