index.js 11 KB

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