index.js 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839
  1. import {
  2. getreadInfo,
  3. likeVideo,
  4. collectVideo,
  5. submitPlayLog
  6. } from '~/api/video'
  7. import {
  8. publishWorks,
  9. uploadPk,
  10. postWorksScore
  11. } from '~/api/works'
  12. import {
  13. buyVip,
  14. getVipInfo
  15. } from '~/api/user'
  16. import {
  17. userEvent
  18. } from '~/api/global'
  19. import {
  20. createStoreBindings
  21. } from 'mobx-miniprogram-bindings'
  22. import {
  23. store
  24. } from '~/store/index'
  25. import {
  26. setDuration
  27. } from '~/utils/util'
  28. import share from '~/mixins/share'
  29. import event from '~/mixins/event'
  30. const app = getApp()
  31. let aiengine = require('~/utils/ChivoxAiEngine')
  32. let sha1 = require('~/utils/sha1');
  33. // 文章行高
  34. let rowH = 0
  35. let videoContext = null
  36. // 滚动变色定时器
  37. let stl = null
  38. // 倒计时
  39. let setTimeoutObj = null
  40. // 录音
  41. let innerAudioContext = null
  42. // 试听
  43. let resultAudioContext = null
  44. /*创建基础引擎*/
  45. let wsEngine = aiengine.createWsEngine({});
  46. /*微信录音*/
  47. let recorderManager = wx.getRecorderManager();
  48. Page({
  49. behaviors: [event, share],
  50. data: {
  51. videoInfo: {},
  52. videoPath: '',
  53. currentRow: null,
  54. state: false,
  55. // 示例播放状态
  56. exampleState: false,
  57. // 是否静音播放视频
  58. muted: false,
  59. countDown: {
  60. state: false,
  61. num: 3,
  62. },
  63. contentH: 0,
  64. percent: 0,
  65. scrollTop: 0,
  66. //如果readingReset为true就是重读
  67. readingReset: false,
  68. //readingType为public是普通阅读,为pk是pk逻辑,readMatch为朗读赛
  69. readingType: 'public',
  70. uploadState: false,
  71. article: [],
  72. silderData: {
  73. currentTime: '00:00',
  74. endTime: '00:00',
  75. silderValue: 0
  76. },
  77. // 朗读赛的id
  78. activityId: '',
  79. // 0免费1收费
  80. free: 1,
  81. isIos: app.globalData.isIOS,
  82. isVip: false,
  83. tempFilePath: ""
  84. },
  85. onLoad(options) {
  86. let videoId = null
  87. let params = decodeURIComponent(options.scene).split('&')
  88. videoId = !options.scene ? options.videoId : params[0]
  89. wx.setNavigationBarTitle({
  90. title: options.navBarTitle
  91. })
  92. if (this.data.isIos) {
  93. this.setData({
  94. readingReset: options.reset || false,
  95. readingType: options.readingType || 'public',
  96. uploadHide: options.uploadHide,
  97. activityId: options.activityId || '',
  98. free: 0
  99. })
  100. } else {
  101. this.setData({
  102. readingReset: options.reset || false,
  103. readingType: options.readingType || 'public',
  104. uploadHide: options.uploadHide,
  105. activityId: options.activityId || '',
  106. free: options.free ? Number(options.free) : 1
  107. })
  108. }
  109. this.getreadInfo(videoId, options.reset).then(res => {
  110. wx.nextTick(() => {
  111. if (options.voluntarily && this.data.isVip) {
  112. this.setCountDown()
  113. }
  114. if (options.autoPlay) {
  115. this.videoPlay()
  116. }
  117. })
  118. })
  119. // 手工绑定
  120. this.storeBindings = createStoreBindings(this, {
  121. store,
  122. fields: {
  123. userInfo: 'userInfo',
  124. readDetail: 'readDetail',
  125. pkData: 'pkData'
  126. },
  127. actions: {
  128. setUser: 'setUser',
  129. setReadDetail: 'setReadDetail'
  130. }
  131. })
  132. // 录音授权
  133. wx.getSetting({
  134. success(res) {
  135. if (!res.authSetting['scope.record']) {
  136. wx.authorize({
  137. scope: 'scope.record',
  138. success() {
  139. // 用户已经同意小程序使用录音功能,后续调用接口不会弹窗询问
  140. },
  141. fail() {
  142. wx.showModal({
  143. title: '授权提示',
  144. content: '请先开启录音功能',
  145. success(res) {
  146. wx.openSetting({
  147. success(res) {}
  148. })
  149. }
  150. })
  151. }
  152. })
  153. }
  154. }
  155. })
  156. /*监听评测结果:必须在基础引擎创建后,调用任何评测接口前设置监听,否则有可能收不到相关事件。*/
  157. wsEngine.onResult((res) => {
  158. console.log('触发评分结束了');
  159. this.getRecordScore(res)
  160. });
  161. wsEngine.onErrorResult((res) => {
  162. console.log("===收到错误结果=============", res)
  163. userEvent({
  164. action: 'WXSCORE',
  165. targetContent: res
  166. })
  167. });
  168. this.innerAudioContext = wx.createInnerAudioContext();
  169. this.innerAudioContext.onTimeUpdate(res => {
  170. this.setData({
  171. ["silderData.sliderValue"]: Math.round(this.innerAudioContext.currentTime / this.innerAudioContext.duration * 100),
  172. ["silderData.currentTime"]: setDuration(this.innerAudioContext.currentTime)
  173. })
  174. })
  175. this.resultAudioContext = wx.createInnerAudioContext();
  176. this.resultAudioContext.onTimeUpdate(res => {
  177. this.setData({
  178. ["silderData.sliderValue"]: Math.round(this.resultAudioContext.currentTime / this.resultAudioContext.duration * 100),
  179. ["silderData.currentTime"]: setDuration(this.resultAudioContext.currentTime)
  180. })
  181. })
  182. this.resultAudioContext.onError(res => {
  183. console.log(res, 'resultAudioContext');
  184. })
  185. this.innerAudioContext.onError(res => {
  186. console.log(res, 'bbbb');
  187. })
  188. this.resultAudioContext.onEnded(res => {
  189. console.log('102-resultAudioContext.ended');
  190. this.setData({
  191. exampleState: false
  192. })
  193. if (this.data.videoInfo.userReadExtend.resourcesType == 0) {
  194. this.videoContext.stop()
  195. this.videoContext.seek(0)
  196. }
  197. })
  198. this.resultAudioContext.onStop((res) => {
  199. console.log('109-resultAudioContext.onStop');
  200. this.setData({
  201. exampleState: false
  202. })
  203. if (this.data.videoInfo.userReadExtend.resourcesType == 0) {
  204. this.videoContext.stop()
  205. this.videoContext.seek(0)
  206. }
  207. });
  208. this.resultAudioContext.onEnded(res => {
  209. this.setData({
  210. ["silderData.sliderValue"]: 100
  211. })
  212. })
  213. },
  214. onShow() {
  215. this.getVipInfo()
  216. },
  217. // 获取是否vip
  218. async getVipInfo() {
  219. let vipTime = await getVipInfo()
  220. // if (this.data.isIos) {
  221. // this.setData({
  222. // isVip: '183760000000'
  223. // })
  224. // }else{
  225. this.setData({
  226. isVip: vipTime != ''
  227. })
  228. // }
  229. },
  230. // 获取阅读内容
  231. getreadInfo(videoId, reset = false) {
  232. return new Promise(async (resolve, reject) => {
  233. let videoInfo = await getreadInfo(videoId)
  234. let data = JSON.parse(videoInfo.userReadExtend.lessonText)
  235. data = data.map((item, index) => {
  236. item.time = Number(item.time)
  237. item.readTime = data[index + 1] ? data[index + 1].time - item.time : ''
  238. return item
  239. })
  240. this.setData({
  241. videoPath: videoInfo.userRead.originVideo,
  242. article: data,
  243. videoInfo,
  244. ["silderData.endTime"]: setDuration(videoInfo.userRead.duration)
  245. })
  246. if (!reset) {
  247. this.getHeight()
  248. }
  249. if (this.data.videoInfo.userReadExtend.resourcesType == 0) {
  250. this.videoContext = wx.createVideoContext('myVideo')
  251. } else {
  252. this.innerAudioContext.src = videoInfo.userRead.originVideo
  253. this.innerAudioContext.onEnded(res => {
  254. console.log("138innerAudioContext触发的");
  255. this.resetReading()
  256. })
  257. this.innerAudioContext.onStop((res) => {
  258. console.log("143innerAudioContext触发的");
  259. });
  260. }
  261. resolve()
  262. })
  263. },
  264. // 开始录制
  265. setCountDown() {
  266. if (!this.data.isVip && !!this.data.free) {
  267. this.resetReading()
  268. console.log('zzz');
  269. return this.selectComponent('#buyVip').open({
  270. isVip: this.data.isVip
  271. })
  272. }
  273. if (this.data.state) {
  274. this.resetReading()
  275. return
  276. }
  277. if (!this.data.readingReset) {
  278. this.getHeight()
  279. }
  280. this.resetReading()
  281. this.setData({
  282. readingReset: false,
  283. 'countDown.state': true
  284. })
  285. this.stl = setInterval(async () => {
  286. if (this.data.countDown.num == 0) {
  287. clearInterval(this.stl)
  288. this.setData({
  289. state: true,
  290. countDown: {
  291. state: false,
  292. num: 3
  293. }
  294. })
  295. await this.playMediaState()
  296. if (this.data.videoInfo.userReadExtend.businessType != 2) {
  297. await this.soundRecording()
  298. } else {
  299. await this.songRecording()
  300. }
  301. this.startRecording()
  302. } else {
  303. this.setData({
  304. 'countDown.num': --this.data.countDown.num
  305. })
  306. }
  307. }, 1000)
  308. },
  309. // 录音
  310. soundRecording() {
  311. /*调用微信开始录音接口,并启动语音评测*/
  312. let timeStamp = new Date().getTime()
  313. let sig = sha1(`16075689600000da${timeStamp}caa8e60da6042731c230fe431ac9c7fd`)
  314. let app = {
  315. applicationId: '16075689600000da',
  316. sig, //签名字符串
  317. alg: 'sha1',
  318. timestamp: timeStamp + '',
  319. userId: wx.getStorageSync('uid')
  320. }
  321. let lessonText = JSON.parse(this.data.videoInfo.userReadExtend.lessonText).map((item) => {
  322. return item.text
  323. }).join('\n')
  324. // userReadExtend 中 businessType 0:中文/ 1: 英文 / 2: 歌曲
  325. let businessType = this.data.videoInfo.userReadExtend.businessType
  326. // https://www.chivox.com/opendoc/#/ChineseDoc/coreCn/Chinese/cn.sent.raw?id=%e5%8f%82%e6%95%b0%e8%af%b4%e6%98%8e <----参数说明
  327. console.log('启动了', businessType == 0 ? "cn.pred.raw" : "en.pred.score");
  328. wsEngine.start({
  329. request: {
  330. coreType: businessType == 0 ? "cn.pred.raw" : "en.pred.score",
  331. refText: lessonText,
  332. rank: 100,
  333. result: {
  334. details: {
  335. gop_adjust: 0.5 //评测系数
  336. }
  337. }
  338. },
  339. app,
  340. audio: {
  341. audioType: "mp3",
  342. channel: 1,
  343. sampleBytes: 2,
  344. sampleRate: 16000
  345. },
  346. success: (res) => {
  347. /*引擎启动成功,可以启动录音机开始录音,并将音频片传给引擎*/
  348. let recorderOptions = {
  349. duration: 600000,
  350. sampleRate: 44100, //采样率
  351. numberOfChannels: 1, //录音通道数
  352. encodeBitRate: 192000, //编码码率
  353. format: 'mp3', //音频格式,有效值aac/mp3
  354. frameSize: 50 //指定帧大小,单位 KB
  355. };
  356. recorderManager.start(recorderOptions);
  357. },
  358. fail: (res) => {
  359. console.log("fail============= ", res);
  360. },
  361. });
  362. recorderManager.onError(res => {
  363. console.log(res, 'recorderManagerError');
  364. })
  365. //监听录音开始事件
  366. recorderManager.onStart(() => {});
  367. //监听录音结束事件
  368. recorderManager.onStop((res) => {
  369. console.log('录音结束', res);
  370. this.setData({
  371. tempFilePath: res.tempFilePath,
  372. });
  373. //录音机结束后,驰声引擎执行结束操作,等待评测返回结果
  374. wsEngine.stop({
  375. success: () => {
  376. console.log('====== wsEngine stop success ======');
  377. },
  378. fail: (res) => {
  379. console.log('录音结束报错', res);
  380. },
  381. });
  382. });
  383. //监听已录制完指定帧大小的文件事件。如果设置了 frameSize,则会回调此事件。
  384. recorderManager.onFrameRecorded((res) => {
  385. let {
  386. frameBuffer
  387. } = res
  388. //TODO 调用feed接口传递音频片给驰声评测引擎
  389. wsEngine.feed({
  390. data: frameBuffer, // frameBuffer为微信录音机回调的音频数据
  391. success: () => {},
  392. fail: (res) => {
  393. console.log('监听已录制完指定帧大小报错', res)
  394. },
  395. });
  396. });
  397. },
  398. songRecording() {
  399. //开始录音,在开始录音回调中feed音频片
  400. let recorderOptions = {
  401. duration: 600000,
  402. sampleRate: 44100, //采样率
  403. numberOfChannels: 1, //录音通道数
  404. encodeBitRate: 192000, //编码码率
  405. format: 'mp3', //音频格式,有效值aac/mp3
  406. frameSize: 50 //指定帧大小,单位 KB
  407. };
  408. recorderManager.start(recorderOptions);
  409. recorderManager.onError(res => {
  410. console.log(res, 'songError');
  411. })
  412. //监听录音开始事件
  413. recorderManager.onStart(() => {});
  414. //监听录音结束事件
  415. recorderManager.onStop(async (res) => {
  416. this.setData({
  417. tempFilePath: res.tempFilePath,
  418. });
  419. let detail = {
  420. integrity: 80,
  421. tone: 80,
  422. accuracy: 80,
  423. fluency: 80,
  424. myOverall: 80,
  425. businessType: this.data.videoInfo.userReadExtend.businessType,
  426. tempFilePath: this.data.tempFilePath,
  427. title: this.data.videoInfo.userRead.title,
  428. id: this.data.videoInfo.userRead.exampleId,
  429. coverImg: this.data.videoInfo.userRead.coverImg,
  430. resourcesType: this.data.videoInfo.userReadExtend.resourcesType,
  431. aBg: this.data.videoInfo.userReadExtend.resourcesType == 1 ? this.data.videoInfo.userReadExtend.backgroundVirtualImg : '',
  432. originVideo: this.data.videoInfo.userRead.originVideo
  433. }
  434. this.setReadDetail(detail)
  435. await userEvent({
  436. action: 'WXSCORE',
  437. })
  438. if (this.data.readingType == 'public' || this.data.readingType == 'readMatch') {
  439. wx.navigateTo({
  440. url: `/pages/score/index?readingType=${this.data.readingType}&activityId=${this.data.activityId}&free=${this.data.free}`,
  441. events: {
  442. // 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据
  443. goback: (data) => {
  444. this.setData({
  445. readingReset: data.reset || false,
  446. readingType: data.readingType || 'public',
  447. uploadHide: data.uploadHide
  448. })
  449. }
  450. },
  451. })
  452. } else {
  453. this.uploadAudio()
  454. }
  455. });
  456. },
  457. // 直接跳转的时候用的,勿动
  458. // util() {
  459. // wx.navigateTo({
  460. // url: `/pages/score/index?readingType=${this.data.readingType}`,
  461. // events: {
  462. // // 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据
  463. // someEvent: (data) => {
  464. // console.log(data)
  465. // this.setData({
  466. // readingReset: data.reset || false,
  467. // readingType: data.readingType || 'public',
  468. // uploadHide: data.uploadHide
  469. // })
  470. // console.log(this.data, 'ggggggggg');
  471. // }
  472. // },
  473. // })
  474. // },
  475. // 获取测评结果
  476. async getRecordScore(res) {
  477. let result = res.result;
  478. //0是中文,1是英文
  479. let businessType = this.data.videoInfo.userReadExtend.businessType
  480. let integrity = Math.floor(result.integrity); //完成度
  481. let accuracy = Math.floor(result.accuracy); // 准确度 发音分
  482. let fluency = Math.floor(result.fluency.overall); //流利度
  483. let tone = 0 // 语调声调
  484. let myOverall = 0;
  485. if (businessType == 0) {
  486. tone = Math.floor(result.tone);
  487. myOverall = Math.floor(integrity * 0.5 + accuracy * 0.3 + fluency * 0.1 + tone * 0.1);
  488. } else if (businessType == 1) {
  489. myOverall = Math.floor(integrity * 0.5 + accuracy * 0.3 + fluency * 0.2);
  490. }
  491. let detail = {
  492. integrity,
  493. tone,
  494. accuracy,
  495. fluency,
  496. myOverall,
  497. businessType: this.data.videoInfo.userReadExtend.businessType,
  498. tempFilePath: this.data.tempFilePath,
  499. title: this.data.videoInfo.userRead.title,
  500. id: this.data.videoInfo.userRead.exampleId,
  501. coverImg: this.data.videoInfo.userRead.coverImg,
  502. resourcesType: this.data.videoInfo.userReadExtend.resourcesType,
  503. aBg: this.data.videoInfo.userReadExtend.resourcesType == 1 ? this.data.videoInfo.userReadExtend.backgroundVirtualImg : '',
  504. originVideo: this.data.videoInfo.userRead.originVideo
  505. }
  506. console.log('评测结果2', detail);
  507. this.setReadDetail(detail)
  508. await userEvent({
  509. action: 'WXSCORE',
  510. })
  511. if (this.data.readingType == 'public' || this.data.readingType == 'readMatch') {
  512. wx.navigateTo({
  513. url: `/pages/score/index?readingType=${this.data.readingType}&activityId=${this.data.activityId}&free=${this.data.free}`,
  514. events: {
  515. // 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据
  516. goback: (data) => {
  517. this.setData({
  518. readingReset: data.reset || false,
  519. readingType: data.readingType || 'public',
  520. uploadHide: data.uploadHide
  521. })
  522. }
  523. },
  524. })
  525. } else {
  526. this.uploadAudio()
  527. }
  528. },
  529. // 挑战录音上传
  530. uploadAudio() {
  531. this.setData({
  532. uploadState: true
  533. })
  534. let uploadTask = wx.uploadFile({
  535. url: 'https://reader-api.ai160.com//file/upload',
  536. filePath: this.data.tempFilePath,
  537. name: '朗读录音',
  538. header: {
  539. uid: wx.getStorageSync('uid')
  540. },
  541. success: async (res) => {
  542. let formateRes = JSON.parse(res.data);
  543. let audioPath = formateRes.data;
  544. let uploadRes = await publishWorks({
  545. exampleId: this.data.pkData.exampleId,
  546. audioPath,
  547. })
  548. let _data = this.data.readDetail
  549. let scoreRes = await postWorksScore({
  550. "userReadId": uploadRes.id,
  551. "complete": _data.integrity,
  552. "accuracy": _data.accuracy,
  553. "speed": _data.fluency,
  554. "intonation": _data.tone,
  555. "score": _data.myOverall
  556. }).finally(() => {
  557. this.setData({
  558. uploadState: false
  559. })
  560. })
  561. console.log({
  562. "userReadId": uploadRes.id,
  563. "complete": _data.integrity,
  564. "accuracy": _data.accuracy,
  565. "speed": _data.fluency,
  566. "intonation": _data.tone,
  567. "score": _data.myOverall
  568. }, 'score', scoreRes, 'scoreRes');
  569. let data = {}
  570. if (_data.businessType != 2) {
  571. data = {
  572. challengerUserReadId: uploadRes.id,
  573. userReadId: this.data.pkData.id,
  574. winnerUId: this.data.pkData.score > _data.myOverall ? this.data.pkData.uid : this.data.pkData.score == _data.myOverall ? '' : wx.getStorageSync('uid')
  575. }
  576. } else {
  577. data = {
  578. challengerUserReadId: uploadRes.id,
  579. userReadId: this.data.pkData.id,
  580. winnerUId: ''
  581. }
  582. }
  583. let result = await uploadPk(data)
  584. await userEvent({
  585. action: 'WXPKUPLOAD',
  586. })
  587. wx.redirectTo({
  588. url: `/pages/pkResult/index?id=${result.id}`
  589. })
  590. },
  591. fail: (res) => {
  592. this.setData({
  593. uploadState: false
  594. })
  595. }
  596. });
  597. uploadTask.onProgressUpdate((res) => {
  598. this.setData({
  599. percent: res.progress
  600. })
  601. })
  602. },
  603. // 字体换行
  604. startRecording() {
  605. setTimeout(() => {
  606. if (this.data.currentRow == null) {
  607. this.setData({
  608. currentRow: 0
  609. })
  610. }
  611. let row = this.data.article[this.data.currentRow]
  612. if (!row.readTime) {
  613. return
  614. }
  615. this.setTimeoutObj = setTimeout(() => {
  616. this.setData({
  617. currentRow: ++this.data.currentRow
  618. })
  619. this.setData({
  620. scrollTop: this.rowH * this.data.currentRow
  621. })
  622. this.startRecording()
  623. },
  624. row.readTime);
  625. }, 100)
  626. },
  627. // 视频播放结束
  628. videoEnd() {
  629. this.resetReading()
  630. },
  631. videoPlay() {
  632. if (this.data.state) {
  633. return
  634. }
  635. if (this.data.videoInfo.userReadExtend.resourcesType == 1) {
  636. if (this.data.exampleState) {
  637. this.setData({
  638. exampleState: false
  639. })
  640. return this.resultAudioContext.stop()
  641. }
  642. this.resultAudioContext.src = this.data.readingReset ? this.data.readDetail.tempFilePath : this.data.videoInfo.userRead.audioPath;
  643. setTimeout(() => {
  644. this.resultAudioContext.play();
  645. }, 200)
  646. this.setData({
  647. exampleState: true
  648. })
  649. } else {
  650. if (this.data.readingReset) {
  651. this.resultAudioContext.src = this.data.readDetail.tempFilePath;
  652. this.resultAudioContext.play();
  653. this.setData({
  654. muted: true,
  655. exampleState: true
  656. })
  657. } else {
  658. this.setData({
  659. muted: false,
  660. exampleState: true
  661. })
  662. }
  663. this.setData({
  664. videoPath: this.data.videoInfo.userRead.videoPath
  665. })
  666. wx.nextTick(() => {
  667. this.videoContext.play()
  668. })
  669. }
  670. this.startRecording()
  671. submitPlayLog({
  672. userReadId: this.data.videoInfo.userRead.exampleId,
  673. playStopTime: 1000
  674. })
  675. },
  676. // 控制视频或音频的播放状态
  677. async playMediaState() {
  678. this.setData({
  679. muted: false
  680. })
  681. if (this.data.videoInfo.userReadExtend.resourcesType == 0) {
  682. this.setData({
  683. videoPath: this.data.videoInfo.userRead.originVideo
  684. })
  685. wx.nextTick(() => {
  686. this.videoContext.play()
  687. })
  688. } else {
  689. this.innerAudioContext.play();
  690. }
  691. userEvent({
  692. action: 'WXREADING',
  693. readId: this.data.videoInfo.userRead.id
  694. })
  695. },
  696. // 重置一切状态
  697. resetReading() {
  698. clearTimeout(this.setTimeoutObj)
  699. clearInterval(this.stl)
  700. // 重置视频
  701. if (this.data.videoInfo.userReadExtend.resourcesType == 0) {
  702. this.videoContext.stop()
  703. this.videoContext.seek(0)
  704. }
  705. // 重置试听音频
  706. if (this.data.exampleState) {
  707. this.resultAudioContext.stop()
  708. // 重置录音时的背景音乐
  709. this.innerAudioContext.stop();
  710. console.log('是我暂停了');
  711. }
  712. if (this.data.state) {
  713. // 重置录音时的背景音乐
  714. this.innerAudioContext.stop();
  715. /*微信录音结束*/
  716. recorderManager.stop();
  717. }
  718. this.setData({
  719. exampleState: false,
  720. state: false,
  721. currentRow: null,
  722. scrollTop: 0,
  723. ["silderData.sliderValue"]: 0,
  724. ["silderData.currentTime"]: '00:00'
  725. })
  726. },
  727. // 阻止作品上传时返回
  728. beforeleave() {
  729. this.setData({
  730. uploadState: true
  731. })
  732. },
  733. // 获取设备高度与行高度
  734. getHeight() {
  735. var query = wx.createSelectorQuery();
  736. query.select('.content').boundingClientRect((rect) => {
  737. this.setData({
  738. contentH: rect.height
  739. })
  740. }).exec()
  741. query.select('.row').boundingClientRect((rect) => {
  742. this.rowH = rect.height
  743. }).exec()
  744. },
  745. // 进度条
  746. slider({
  747. detail
  748. }) {
  749. this.resultAudioContext.pause();
  750. this.resultAudioContext.seek(detail.value / 100 * this.data.videoInfo.userRead.duration)
  751. setTimeout(() => {
  752. this.resultAudioContext.play()
  753. }, 300)
  754. },
  755. onHide() {
  756. console.log('onhide');
  757. // #if MP
  758. wsEngine.reset()
  759. this.resetReading()
  760. // #endif
  761. },
  762. onUnload() {
  763. wsEngine.reset()
  764. this.resetReading()
  765. this.storeBindings.destroyStoreBindings()
  766. },
  767. backReading() {
  768. wx.navigateBack({
  769. delta: 1
  770. })
  771. },
  772. otherWork() {
  773. wx.navigateTo({
  774. url: `/pages/otherWork/index?exampleId=${this.data.videoInfo.userRead.exampleId}`
  775. })
  776. },
  777. async toBuy({
  778. detail
  779. }) {
  780. this.selectComponent('#donutBuy').open(detail)
  781. },
  782. paySuccess() {
  783. this.selectComponent('#vipModal').open()
  784. this.setData({
  785. isVip: true
  786. })
  787. setTimeout(() => {
  788. this.getVipInfo()
  789. }, 1500)
  790. userEvent({
  791. action: 'ANDROID_PAY_SUCCESS',
  792. })
  793. },
  794. // 收藏课程
  795. async collect() {
  796. let {
  797. id,
  798. type,
  799. uid
  800. } = this.data.videoInfo.userRead
  801. if (wx.getStorageSync('uid') == uid) {
  802. return wx.showToast({
  803. title: '不能收藏自己作品哦!',
  804. icon: "none"
  805. })
  806. }
  807. await collectVideo({
  808. targetCode: id,
  809. favoritesType: type
  810. })
  811. this.setData({
  812. ['videoInfo.isFavorites']: !this.data.videoInfo.isFavorites
  813. })
  814. },
  815. // 点赞
  816. async likeVideo() {
  817. if (this.data.videoInfo.isLike) {
  818. return
  819. }
  820. let {
  821. id
  822. } = this.data.videoInfo.userRead
  823. await likeVideo(id)
  824. this.setData({
  825. ['videoInfo.isLike']: true,
  826. ['videoInfo.userRead.likeAmount']: this.data.videoInfo.userRead.likeAmount + 1
  827. })
  828. },
  829. //评论
  830. openComment() {
  831. this.selectComponent('#comment').open('', this.data.videoInfo.userRead.id)
  832. },
  833. addCommentNum() {
  834. this.setData({
  835. ['videoInfo.userRead.commentAmount']: ++this.data.videoInfo.userRead.commentAmount
  836. })
  837. },
  838. })