index.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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 && 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. this.setData({
  184. ['videoInfoCopy.unReadPostsCount']: 0,
  185. })
  186. },
  187. // 删除
  188. delete() {
  189. let {
  190. id
  191. } = this.data.videoInfoCopy.userRead
  192. wx.showModal({
  193. title: '确认删除吗?',
  194. content: '作品将被永久删除,无法找回。',
  195. confirmText: '确认',
  196. cancelText: '取消',
  197. success: async (res) => {
  198. if (res.confirm) {
  199. let data = {
  200. id,
  201. status: 'DEL'
  202. }
  203. await setVideoStatus(data)
  204. wx.showToast({
  205. title: '删除成功!',
  206. icon: "none"
  207. })
  208. this.triggerEvent('deleteVideo', this.data.videoInfoCopy.userRead.id)
  209. }
  210. }
  211. })
  212. },
  213. // 收藏课程
  214. async collect() {
  215. let {
  216. id,
  217. type,
  218. uid
  219. } = this.data.videoInfoCopy.userRead
  220. if (wx.getStorageSync('uid') == uid) {
  221. return wx.showToast({
  222. title: '不能收藏自己作品哦!',
  223. icon: "none"
  224. })
  225. }
  226. await collectVideo({
  227. targetCode: id,
  228. favoritesType: type
  229. })
  230. this.setData({
  231. ['videoInfoCopy.isFavorites']: !this.data.videoInfoCopy.isFavorites
  232. })
  233. },
  234. // 关注
  235. async setFans() {
  236. if (this.data.videoInfoCopy.isFans) {
  237. return
  238. }
  239. await setFans({
  240. uid: this.data.videoInfoCopy.user.uid
  241. })
  242. this.triggerEvent('setListFans', this.data.videoInfoCopy.user.uid)
  243. },
  244. jumpUserInfo() {
  245. wx.navigateTo({
  246. url: `/pages/personal/index?uid=${this.data.videoInfoCopy.user.uid}&type=user`,
  247. })
  248. },
  249. // 控制音频播放
  250. audioPlay() {
  251. this.triggerEvent('playAudio')
  252. this.submitPlayLog(this.data.videoInfoCopy.userRead.id)
  253. },
  254. toPkPage() {
  255. let videoInfo = this.data.videoInfoCopy
  256. if (this.properties.videoType == 'pk') {
  257. if (videoInfo.user.uid == wx.getStorageSync('uid')) {
  258. return wx.showToast({
  259. title: '不能与自己PK哦~',
  260. icon: 'none'
  261. })
  262. }
  263. this.setPkData({
  264. nickName: videoInfo.user.nickName || videoInfo.user.eid,
  265. uid: videoInfo.user.uid,
  266. avatar: videoInfo.user.avatar,
  267. score: videoInfo.userRead.score,
  268. audioPath: videoInfo.userRead.audioPath,
  269. exampleId: videoInfo.userRead.exampleId,
  270. id: videoInfo.userRead.id
  271. })
  272. }
  273. let readId = videoInfo.userRead.id
  274. let videoType = this.properties.videoType
  275. let url = ''
  276. if (!this.data.isOfficial || this.data.selfWork) {
  277. // autoPlay自动播放
  278. url = `/pages/reading/index?videoId=${videoInfo.userRead.exampleId}&autoPlay=true`
  279. } else if (videoType == 'public' || videoType == 'follow') {
  280. url = `/pages/pkPage/index?videoId=${readId}`
  281. } else if (videoType == 'pk') {
  282. // voluntarily自动录制
  283. url = `/pages/reading/index?videoId=${videoInfo.userRead.exampleId}&readingType=${videoType}&voluntarily=true`
  284. } else {
  285. url = `/pages/reading/index?videoId=${videoInfo.userRead.exampleId}`
  286. }
  287. wx.navigateTo({
  288. url
  289. })
  290. },
  291. // pkPage页面示范朗读
  292. changeRead() {
  293. this.setData({
  294. workType: this.data.workType == 'videoPath' ? 'example' : 'videoPath',
  295. })
  296. if (this.data.videoInfo.userReadExtend.resourcesType == 1) {
  297. this.triggerEvent('pkPageAudio', {
  298. currentTarget: {
  299. dataset: {
  300. id: this.data.videoInfoCopy.userRead.id,
  301. audio: this.data.workType == 'videoPath' ? this.data.videoInfoCopy.userRead.audioPath : this.data.videoInfoCopy.example.audioPath,
  302. isPkPage: true
  303. }
  304. }
  305. })
  306. }
  307. },
  308. // 统计作品播放次数
  309. async submitPlayLog(userReadId) {
  310. await submitPlayLog({
  311. userReadId,
  312. playStopTime: 1000
  313. })
  314. },
  315. }
  316. })