index.js 34 KB

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