index.js 8.2 KB

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