index.js 34 KB

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