index.js 29 KB

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