index.js 8.2 KB

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