index.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871
  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. return this.selectComponent('#buyVip').open({
  269. isVip: this.data.isVip
  270. })
  271. }
  272. if (this.data.state) {
  273. this.resetReading()
  274. return
  275. }
  276. if (!this.data.readingReset) {
  277. this.getHeight()
  278. }
  279. this.resetReading()
  280. this.setData({
  281. readingReset: false,
  282. 'countDown.state': true
  283. })
  284. this.stl = setInterval(async () => {
  285. if (this.data.countDown.num == 0) {
  286. clearInterval(this.stl)
  287. this.setData({
  288. state: true,
  289. countDown: {
  290. state: false,
  291. num: 3
  292. }
  293. })
  294. await this.playMediaState()
  295. if (this.data.videoInfo.userReadExtend.businessType != 2) {
  296. await this.soundRecording()
  297. } else {
  298. await this.songRecording()
  299. }
  300. this.startRecording()
  301. } else {
  302. this.setData({
  303. 'countDown.num': --this.data.countDown.num
  304. })
  305. }
  306. }, 1000)
  307. },
  308. // 录音
  309. soundRecording() {
  310. /*调用微信开始录音接口,并启动语音评测*/
  311. let timeStamp = new Date().getTime()
  312. let sig = sha1(`16075689600000da${timeStamp}caa8e60da6042731c230fe431ac9c7fd`)
  313. let app = {
  314. applicationId: '16075689600000da',
  315. sig, //签名字符串
  316. alg: 'sha1',
  317. timestamp: timeStamp + '',
  318. userId: wx.getStorageSync('uid')
  319. }
  320. let lessonText = JSON.parse(this.data.videoInfo.userReadExtend.lessonText).map((item) => {
  321. return item.text
  322. }).join('\n')
  323. // userReadExtend 中 businessType 0:中文/ 1: 英文 / 2: 歌曲
  324. let businessType = this.data.videoInfo.userReadExtend.businessType
  325. // https://www.chivox.com/opendoc/#/ChineseDoc/coreCn/Chinese/cn.sent.raw?id=%e5%8f%82%e6%95%b0%e8%af%b4%e6%98%8e <----参数说明
  326. console.log('启动了', businessType == 0 ? "cn.pred.raw" : "en.pred.score");
  327. wsEngine.start({
  328. request: {
  329. coreType: businessType == 0 ? "cn.pred.raw" : "en.pred.score",
  330. refText: lessonText,
  331. rank: 100,
  332. result: {
  333. details: {
  334. gop_adjust: 0.5 //评测系数
  335. }
  336. }
  337. },
  338. app,
  339. audio: {
  340. audioType: "mp3",
  341. channel: 1,
  342. sampleBytes: 2,
  343. sampleRate: 16000
  344. },
  345. success: (res) => {
  346. /*引擎启动成功,可以启动录音机开始录音,并将音频片传给引擎*/
  347. let recorderOptions = {
  348. duration: 600000,
  349. sampleRate: 44100, //采样率
  350. numberOfChannels: 1, //录音通道数
  351. encodeBitRate: 192000, //编码码率
  352. format: 'mp3', //音频格式,有效值aac/mp3
  353. frameSize: 50 //指定帧大小,单位 KB
  354. };
  355. recorderManager.start(recorderOptions);
  356. },
  357. fail: (res) => {
  358. console.log("fail============= ", res);
  359. },
  360. });
  361. recorderManager.onError(res => {
  362. console.log(res, 'recorderManagerError');
  363. })
  364. //监听录音开始事件
  365. recorderManager.onStart(() => {});
  366. //监听录音结束事件
  367. recorderManager.onStop((res) => {
  368. console.log('录音结束', res);
  369. this.setData({
  370. tempFilePath: res.tempFilePath,
  371. });
  372. //录音机结束后,驰声引擎执行结束操作,等待评测返回结果
  373. wsEngine.stop({
  374. success: () => {
  375. console.log('====== wsEngine stop success ======');
  376. },
  377. fail: (res) => {
  378. console.log('录音结束报错', res);
  379. },
  380. });
  381. });
  382. //监听已录制完指定帧大小的文件事件。如果设置了 frameSize,则会回调此事件。
  383. recorderManager.onFrameRecorded((res) => {
  384. let {
  385. frameBuffer
  386. } = res
  387. //TODO 调用feed接口传递音频片给驰声评测引擎
  388. wsEngine.feed({
  389. data: frameBuffer, // frameBuffer为微信录音机回调的音频数据
  390. success: () => {},
  391. fail: (res) => {
  392. console.log('监听已录制完指定帧大小报错', res)
  393. },
  394. });
  395. });
  396. },
  397. songRecording() {
  398. //开始录音,在开始录音回调中feed音频片
  399. let recorderOptions = {
  400. duration: 600000,
  401. sampleRate: 44100, //采样率
  402. numberOfChannels: 1, //录音通道数
  403. encodeBitRate: 192000, //编码码率
  404. format: 'mp3', //音频格式,有效值aac/mp3
  405. frameSize: 50 //指定帧大小,单位 KB
  406. };
  407. recorderManager.start(recorderOptions);
  408. recorderManager.onError(res => {
  409. console.log(res, 'songError');
  410. })
  411. //监听录音开始事件
  412. recorderManager.onStart(() => {});
  413. //监听录音结束事件
  414. recorderManager.onStop(async (res) => {
  415. this.setData({
  416. tempFilePath: res.tempFilePath,
  417. });
  418. let detail = {
  419. integrity: 80,
  420. tone: 80,
  421. accuracy: 80,
  422. fluency: 80,
  423. myOverall: 80,
  424. businessType: this.data.videoInfo.userReadExtend.businessType,
  425. tempFilePath: this.data.tempFilePath,
  426. title: this.data.videoInfo.userRead.title,
  427. id: this.data.videoInfo.userRead.exampleId,
  428. coverImg: this.data.videoInfo.userRead.coverImg,
  429. resourcesType: this.data.videoInfo.userReadExtend.resourcesType,
  430. aBg: this.data.videoInfo.userReadExtend.resourcesType == 1 ? this.data.videoInfo.userReadExtend.backgroundVirtualImg : '',
  431. originVideo: this.data.videoInfo.userRead.originVideo
  432. }
  433. this.setReadDetail(detail)
  434. await userEvent({
  435. action: 'WXSCORE',
  436. })
  437. if (this.data.readingType == 'public' || this.data.readingType == 'readMatch') {
  438. wx.navigateTo({
  439. url: `/pages/score/index?readingType=${this.data.readingType}&activityId=${this.data.activityId}&free=${this.data.free}`,
  440. events: {
  441. // 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据
  442. goback: (data) => {
  443. this.setData({
  444. readingReset: data.reset || false,
  445. readingType: data.readingType || 'public',
  446. uploadHide: data.uploadHide
  447. })
  448. }
  449. },
  450. })
  451. } else {
  452. this.uploadAudio()
  453. }
  454. });
  455. },
  456. // 直接跳转的时候用的,勿动
  457. // util() {
  458. // wx.navigateTo({
  459. // url: `/pages/score/index?readingType=${this.data.readingType}`,
  460. // events: {
  461. // // 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据
  462. // someEvent: (data) => {
  463. // console.log(data)
  464. // this.setData({
  465. // readingReset: data.reset || false,
  466. // readingType: data.readingType || 'public',
  467. // uploadHide: data.uploadHide
  468. // })
  469. // console.log(this.data, 'ggggggggg');
  470. // }
  471. // },
  472. // })
  473. // },
  474. // 获取测评结果
  475. async getRecordScore(res) {
  476. let result = res.result;
  477. //0是中文,1是英文
  478. let businessType = this.data.videoInfo.userReadExtend.businessType
  479. let integrity = Math.floor(result.integrity); //完成度
  480. let accuracy = Math.floor(result.accuracy); // 准确度 发音分
  481. let fluency = Math.floor(result.fluency.overall); //流利度
  482. let tone = 0 // 语调声调
  483. let myOverall = 0;
  484. if (businessType == 0) {
  485. tone = Math.floor(result.tone);
  486. myOverall = Math.floor(integrity * 0.5 + accuracy * 0.3 + fluency * 0.1 + tone * 0.1);
  487. } else if (businessType == 1) {
  488. myOverall = Math.floor(integrity * 0.5 + accuracy * 0.3 + fluency * 0.2);
  489. }
  490. let detail = {
  491. integrity,
  492. tone,
  493. accuracy,
  494. fluency,
  495. myOverall,
  496. businessType: this.data.videoInfo.userReadExtend.businessType,
  497. tempFilePath: this.data.tempFilePath,
  498. title: this.data.videoInfo.userRead.title,
  499. id: this.data.videoInfo.userRead.exampleId,
  500. coverImg: this.data.videoInfo.userRead.coverImg,
  501. resourcesType: this.data.videoInfo.userReadExtend.resourcesType,
  502. aBg: this.data.videoInfo.userReadExtend.resourcesType == 1 ? this.data.videoInfo.userReadExtend.backgroundVirtualImg : '',
  503. originVideo: this.data.videoInfo.userRead.originVideo
  504. }
  505. console.log('评测结果2', detail);
  506. this.setReadDetail(detail)
  507. await userEvent({
  508. action: 'WXSCORE',
  509. })
  510. if (this.data.readingType == 'public' || this.data.readingType == 'readMatch') {
  511. wx.navigateTo({
  512. url: `/pages/score/index?readingType=${this.data.readingType}&activityId=${this.data.activityId}&free=${this.data.free}`,
  513. events: {
  514. // 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据
  515. goback: (data) => {
  516. this.setData({
  517. readingReset: data.reset || false,
  518. readingType: data.readingType || 'public',
  519. uploadHide: data.uploadHide
  520. })
  521. }
  522. },
  523. })
  524. } else {
  525. this.uploadAudio()
  526. }
  527. },
  528. // 挑战录音上传
  529. uploadAudio() {
  530. this.setData({
  531. uploadState: true
  532. })
  533. let uploadTask = wx.uploadFile({
  534. url: 'https://reader-api.ai160.com//file/upload',
  535. filePath: this.data.tempFilePath,
  536. name: '朗读录音',
  537. header: {
  538. uid: wx.getStorageSync('uid')
  539. },
  540. success: async (res) => {
  541. let formateRes = JSON.parse(res.data);
  542. let audioPath = formateRes.data;
  543. let uploadRes = await publishWorks({
  544. exampleId: this.data.pkData.exampleId,
  545. audioPath,
  546. })
  547. let _data = this.data.readDetail
  548. let scoreRes = await postWorksScore({
  549. "userReadId": uploadRes.id,
  550. "complete": _data.integrity,
  551. "accuracy": _data.accuracy,
  552. "speed": _data.fluency,
  553. "intonation": _data.tone,
  554. "score": _data.myOverall
  555. }).finally(() => {
  556. this.setData({
  557. uploadState: false
  558. })
  559. })
  560. console.log({
  561. "userReadId": uploadRes.id,
  562. "complete": _data.integrity,
  563. "accuracy": _data.accuracy,
  564. "speed": _data.fluency,
  565. "intonation": _data.tone,
  566. "score": _data.myOverall
  567. }, 'score', scoreRes, 'scoreRes');
  568. let data = {}
  569. if (_data.businessType != 2) {
  570. data = {
  571. challengerUserReadId: uploadRes.id,
  572. userReadId: this.data.pkData.id,
  573. winnerUId: this.data.pkData.score > _data.myOverall ? this.data.pkData.uid : this.data.pkData.score == _data.myOverall ? '' : wx.getStorageSync('uid')
  574. }
  575. } else {
  576. data = {
  577. challengerUserReadId: uploadRes.id,
  578. userReadId: this.data.pkData.id,
  579. winnerUId: ''
  580. }
  581. }
  582. let result = await uploadPk(data)
  583. await userEvent({
  584. action: 'WXPKUPLOAD',
  585. })
  586. wx.redirectTo({
  587. url: `/pages/pkResult/index?id=${result.id}`
  588. })
  589. },
  590. fail: (res) => {
  591. this.setData({
  592. uploadState: false
  593. })
  594. }
  595. });
  596. uploadTask.onProgressUpdate((res) => {
  597. this.setData({
  598. percent: res.progress
  599. })
  600. })
  601. },
  602. // 字体换行
  603. startRecording() {
  604. setTimeout(() => {
  605. if (this.data.currentRow == null) {
  606. this.setData({
  607. currentRow: 0
  608. })
  609. }
  610. let row = this.data.article[this.data.currentRow]
  611. if (!row.readTime) {
  612. return
  613. }
  614. this.setTimeoutObj = setTimeout(() => {
  615. this.setData({
  616. currentRow: ++this.data.currentRow
  617. })
  618. this.setData({
  619. scrollTop: this.rowH * this.data.currentRow
  620. })
  621. this.startRecording()
  622. },
  623. row.readTime);
  624. }, 100)
  625. },
  626. // 视频播放结束
  627. videoEnd() {
  628. this.resetReading()
  629. },
  630. videoPlay() {
  631. if (this.data.state) {
  632. return
  633. }
  634. if (this.data.videoInfo.userReadExtend.resourcesType == 1) {
  635. if (this.data.exampleState) {
  636. this.setData({
  637. exampleState: false
  638. })
  639. return this.resultAudioContext.stop()
  640. }
  641. this.resultAudioContext.src = this.data.readingReset ? this.data.readDetail.tempFilePath : this.data.videoInfo.userRead.audioPath;
  642. setTimeout(() => {
  643. this.resultAudioContext.play();
  644. }, 200)
  645. this.setData({
  646. exampleState: true
  647. })
  648. } else {
  649. if (this.data.readingReset) {
  650. this.resultAudioContext.src = this.data.readDetail.tempFilePath;
  651. this.resultAudioContext.play();
  652. this.setData({
  653. muted: true,
  654. exampleState: true
  655. })
  656. } else {
  657. this.setData({
  658. muted: false,
  659. exampleState: true
  660. })
  661. }
  662. this.setData({
  663. videoPath: this.data.videoInfo.userRead.videoPath
  664. })
  665. wx.nextTick(() => {
  666. this.videoContext.play()
  667. })
  668. }
  669. this.startRecording()
  670. submitPlayLog({
  671. userReadId: this.data.videoInfo.userRead.exampleId,
  672. playStopTime: 1000
  673. })
  674. },
  675. // 控制视频或音频的播放状态
  676. async playMediaState() {
  677. this.setData({
  678. muted: false
  679. })
  680. if (this.data.videoInfo.userReadExtend.resourcesType == 0) {
  681. this.setData({
  682. videoPath: this.data.videoInfo.userRead.originVideo
  683. })
  684. wx.nextTick(() => {
  685. this.videoContext.play()
  686. })
  687. } else {
  688. this.innerAudioContext.play();
  689. }
  690. userEvent({
  691. action: 'WXREADING',
  692. readId: this.data.videoInfo.userRead.id
  693. })
  694. },
  695. // 重置一切状态
  696. resetReading() {
  697. clearTimeout(this.setTimeoutObj)
  698. clearInterval(this.stl)
  699. // 重置视频
  700. if (this.data.videoInfo.userReadExtend.resourcesType == 0) {
  701. this.videoContext.stop()
  702. this.videoContext.seek(0)
  703. }
  704. // 重置试听音频
  705. if (this.data.exampleState) {
  706. this.resultAudioContext.stop()
  707. // 重置录音时的背景音乐
  708. this.innerAudioContext.stop();
  709. console.log('是我暂停了');
  710. }
  711. if (this.data.state) {
  712. // 重置录音时的背景音乐
  713. this.innerAudioContext.stop();
  714. /*微信录音结束*/
  715. recorderManager.stop();
  716. }
  717. this.setData({
  718. exampleState: false,
  719. state: false,
  720. currentRow: null,
  721. scrollTop: 0,
  722. ["silderData.sliderValue"]: 0,
  723. ["silderData.currentTime"]: '00:00'
  724. })
  725. },
  726. // 阻止作品上传时返回
  727. beforeleave() {
  728. this.setData({
  729. uploadState: true
  730. })
  731. },
  732. // 获取设备高度与行高度
  733. getHeight() {
  734. var query = wx.createSelectorQuery();
  735. query.select('.content').boundingClientRect((rect) => {
  736. this.setData({
  737. contentH: rect.height
  738. })
  739. }).exec()
  740. query.select('.row').boundingClientRect((rect) => {
  741. this.rowH = rect.height
  742. }).exec()
  743. },
  744. // 进度条
  745. slider({
  746. detail
  747. }) {
  748. this.resultAudioContext.pause();
  749. this.resultAudioContext.seek(detail.value / 100 * this.data.videoInfo.userRead.duration)
  750. setTimeout(() => {
  751. this.resultAudioContext.play()
  752. }, 300)
  753. },
  754. onHide() {
  755. console.log('onhide');
  756. // #if MP
  757. wsEngine.reset()
  758. this.resetReading()
  759. // #endif
  760. },
  761. onUnload() {
  762. wsEngine.reset()
  763. this.resetReading()
  764. this.storeBindings.destroyStoreBindings()
  765. },
  766. backReading() {
  767. wx.navigateBack({
  768. delta: 1
  769. })
  770. },
  771. otherWork() {
  772. wx.navigateTo({
  773. url: `/pages/otherWork/index?exampleId=${this.data.videoInfo.userRead.exampleId}`
  774. })
  775. },
  776. async toBuy({
  777. detail
  778. }) {
  779. wx.showLoading({
  780. title: '提交中',
  781. mask: true
  782. })
  783. let res = await buyVip({
  784. productId: detail.id
  785. }).finally(() => {
  786. wx.hideLoading()
  787. })
  788. userEvent({
  789. action: 'ANDROID_PAY_ACTIVITY',
  790. })
  791. let {
  792. timeStamp,
  793. nonceStr,
  794. signType,
  795. paySign
  796. } = res
  797. // package保留字
  798. wx.requestPayment({
  799. timeStamp,
  800. nonceStr,
  801. package: res.package,
  802. signType,
  803. paySign,
  804. success: (res) => {
  805. this.selectComponent('#buyVip').closeModal()
  806. this.selectComponent('#vipModal').open()
  807. this.setData({
  808. isVip: true
  809. })
  810. setTimeout(() => {
  811. this.getVipInfo()
  812. }, 1500)
  813. userEvent({
  814. action: 'ANDROID_PAY_SUCCESS',
  815. })
  816. },
  817. fail(res) {
  818. wx.showToast({
  819. title: "支付失败",
  820. icon: "none",
  821. duration: 3000
  822. })
  823. }
  824. })
  825. },
  826. // 收藏课程
  827. async collect() {
  828. let {
  829. id,
  830. type,
  831. uid
  832. } = this.data.videoInfo.userRead
  833. if (wx.getStorageSync('uid') == uid) {
  834. return wx.showToast({
  835. title: '不能收藏自己作品哦!',
  836. icon: "none"
  837. })
  838. }
  839. await collectVideo({
  840. targetCode: id,
  841. favoritesType: type
  842. })
  843. this.setData({
  844. ['videoInfo.isFavorites']: !this.data.videoInfo.isFavorites
  845. })
  846. },
  847. // 点赞
  848. async likeVideo() {
  849. if (this.data.videoInfo.isLike) {
  850. return
  851. }
  852. let {
  853. id
  854. } = this.data.videoInfo.userRead
  855. await likeVideo(id)
  856. this.setData({
  857. ['videoInfo.isLike']: true,
  858. ['videoInfo.userRead.likeAmount']: this.data.videoInfo.userRead.likeAmount + 1
  859. })
  860. },
  861. //评论
  862. openComment() {
  863. this.selectComponent('#comment').open('', this.data.videoInfo.userRead.id)
  864. },
  865. addCommentNum() {
  866. this.setData({
  867. ['videoInfo.userRead.commentAmount']: ++this.data.videoInfo.userRead.commentAmount
  868. })
  869. },
  870. })