index.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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. console.log(options);
  88. let videoId = options.videoId
  89. wx.setNavigationBarTitle({
  90. title: options.navBarTitle
  91. })
  92. this.setData({
  93. readingReset: options.reset || false,
  94. readingType: options.readingType || 'public',
  95. uploadHide: options.uploadHide,
  96. activityId: options.activityId || '',
  97. free: options.free ? Number(options.free) : 1
  98. })
  99. this.getreadInfo(videoId, options.reset).then(res => {
  100. wx.nextTick(() => {
  101. /* if (options.voluntarily && this.data.isVip) {
  102. this.setCountDown()
  103. }
  104. if (options.autoPlay) {
  105. this.videoPlay()
  106. } */
  107. })
  108. })
  109. // 手工绑定
  110. this.storeBindings = createStoreBindings(this, {
  111. store,
  112. fields: {
  113. userInfo: 'userInfo',
  114. readDetail: 'readDetail',
  115. pkData: 'pkData'
  116. },
  117. actions: {
  118. setUser: 'setUser',
  119. setReadDetail: 'setReadDetail'
  120. }
  121. })
  122. // 录音授权
  123. wx.getSetting({
  124. success(res) {
  125. if (!res.authSetting['scope.record']) {
  126. wx.authorize({
  127. scope: 'scope.record',
  128. success() {
  129. // 用户已经同意小程序使用录音功能,后续调用接口不会弹窗询问
  130. },
  131. fail() {
  132. wx.showModal({
  133. title: '授权提示',
  134. content: '请先开启录音功能',
  135. success(res) {
  136. wx.openSetting({
  137. success(res) {}
  138. })
  139. }
  140. })
  141. }
  142. })
  143. }
  144. }
  145. })
  146. /*监听评测结果:必须在基础引擎创建后,调用任何评测接口前设置监听,否则有可能收不到相关事件。*/
  147. wsEngine.onResult((res) => {
  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. wsEngine.start({
  311. request: {
  312. coreType: businessType == 0 ? "cn.pred.raw" : "en.pred.score",
  313. refText: lessonText,
  314. rank: 100,
  315. result: {
  316. details: {
  317. gop_adjust: 0.5 //评测系数
  318. }
  319. }
  320. },
  321. app,
  322. audio: {
  323. audioType: "mp3",
  324. channel: 1,
  325. sampleBytes: 2,
  326. sampleRate: 16000
  327. },
  328. success: (res) => {
  329. /*引擎启动成功,可以启动录音机开始录音,并将音频片传给引擎*/
  330. //开始录音,在开始录音回调中feed音频片
  331. recorderManager.start(this.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. //开始录音,在开始录音回调中feed音频片
  375. recorderManager.start(this.options);
  376. recorderManager.onError(res => {
  377. console.log(res, 'songError');
  378. })
  379. //监听录音开始事件
  380. recorderManager.onStart(() => {});
  381. //监听录音结束事件
  382. recorderManager.onStop(async (res) => {
  383. this.setData({
  384. tempFilePath: res.tempFilePath,
  385. });
  386. let detail = {
  387. integrity: 80,
  388. tone: 80,
  389. accuracy: 80,
  390. fluency: 80,
  391. myOverall: 80,
  392. businessType: this.data.videoInfo.userReadExtend.businessType,
  393. tempFilePath: this.data.tempFilePath,
  394. title: this.data.videoInfo.userRead.title,
  395. id: this.data.videoInfo.userRead.exampleId,
  396. coverImg: this.data.videoInfo.userRead.coverImg,
  397. resourcesType: this.data.videoInfo.userReadExtend.resourcesType,
  398. aBg: this.data.videoInfo.userReadExtend.resourcesType == 1 ? this.data.videoInfo.userReadExtend.backgroundVirtualImg : '',
  399. originVideo: this.data.videoInfo.userRead.originVideo
  400. }
  401. this.setReadDetail(detail)
  402. await userEvent({
  403. action: 'WXSCORE',
  404. })
  405. if (this.data.readingType == 'public' || this.data.readingType == 'readMatch') {
  406. wx.navigateTo({
  407. url: `/pages/score/index?readingType=${this.data.readingType}&activityId=${this.data.activityId}&free=${this.data.free}`,
  408. events: {
  409. // 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据
  410. goback: (data) => {
  411. this.setData({
  412. readingReset: data.reset || false,
  413. readingType: data.readingType || 'public',
  414. uploadHide: data.uploadHide
  415. })
  416. }
  417. },
  418. })
  419. } else {
  420. this.uploadAudio()
  421. }
  422. });
  423. },
  424. // 直接跳转的时候用的,勿动
  425. // util() {
  426. // wx.navigateTo({
  427. // url: `/pages/score/index?readingType=${this.data.readingType}`,
  428. // events: {
  429. // // 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据
  430. // someEvent: (data) => {
  431. // console.log(data)
  432. // this.setData({
  433. // readingReset: data.reset || false,
  434. // readingType: data.readingType || 'public',
  435. // uploadHide: data.uploadHide
  436. // })
  437. // console.log(this.data, 'ggggggggg');
  438. // }
  439. // },
  440. // })
  441. // },
  442. // 获取测评结果
  443. async getRecordScore(res) {
  444. let result = res.result;
  445. let businessType = this.data.videoInfo.userReadExtend.businessType
  446. let integrity = Math.floor(result.integrity); //完成度
  447. let accuracy = Math.floor(result.accuracy); // 准确度 发音分
  448. let fluency = Math.floor(result.fluency.overall); //流利度
  449. let tone = 0 // 语调声调
  450. let myOverall = 0;
  451. if (businessType == 0) {
  452. tone = Math.floor(result.tone);
  453. myOverall = Math.floor(integrity * 0.45 + accuracy * 0.35 + fluency * 0.1 + tone * 0.1);
  454. } else if (businessType == 1) {
  455. myOverall = Math.floor(integrity * 0.55 + accuracy * 0.35 + fluency * 0.1);
  456. }
  457. let detail = {
  458. integrity,
  459. tone,
  460. accuracy,
  461. fluency,
  462. myOverall,
  463. businessType: this.data.videoInfo.userReadExtend.businessType,
  464. tempFilePath: this.data.tempFilePath,
  465. title: this.data.videoInfo.userRead.title,
  466. id: this.data.videoInfo.userRead.exampleId,
  467. coverImg: this.data.videoInfo.userRead.coverImg,
  468. resourcesType: this.data.videoInfo.userReadExtend.resourcesType,
  469. aBg: this.data.videoInfo.userReadExtend.resourcesType == 1 ? this.data.videoInfo.userReadExtend.backgroundVirtualImg : '',
  470. originVideo: this.data.videoInfo.userRead.originVideo
  471. }
  472. console.log('评测结果2', detail);
  473. this.setReadDetail(detail)
  474. await userEvent({
  475. action: 'WXSCORE',
  476. })
  477. if (this.data.readingType == 'public' || this.data.readingType == 'readMatch') {
  478. wx.navigateTo({
  479. url: `/pages/score/index?readingType=${this.data.readingType}&activityId=${this.data.activityId}&free=${this.data.free}`,
  480. events: {
  481. // 为指定事件添加一个监听器,获取被打开页面传送到当前页面的数据
  482. goback: (data) => {
  483. this.setData({
  484. readingReset: data.reset || false,
  485. readingType: data.readingType || 'public',
  486. uploadHide: data.uploadHide
  487. })
  488. }
  489. },
  490. })
  491. } else {
  492. this.uploadAudio()
  493. }
  494. },
  495. // 挑战录音上传
  496. uploadAudio() {
  497. this.setData({
  498. uploadState: true
  499. })
  500. let uploadTask = wx.uploadFile({
  501. url: 'https://reader-api.ai160.com//file/upload',
  502. filePath: this.data.tempFilePath,
  503. name: '朗读录音',
  504. header: {
  505. uid: wx.getStorageSync('uid')
  506. },
  507. success: async (res) => {
  508. let formateRes = JSON.parse(res.data);
  509. let audioPath = formateRes.data;
  510. let uploadRes = await publishWorks({
  511. exampleId: this.data.pkData.exampleId,
  512. audioPath,
  513. })
  514. let _data = this.data.readDetail
  515. let scoreRes = await postWorksScore({
  516. "userReadId": uploadRes.id,
  517. "complete": _data.integrity,
  518. "accuracy": _data.accuracy,
  519. "speed": _data.fluency,
  520. "intonation": _data.tone,
  521. "score": _data.myOverall
  522. }).finally(() => {
  523. this.setData({
  524. uploadState: false
  525. })
  526. })
  527. console.log({
  528. "userReadId": uploadRes.id,
  529. "complete": _data.integrity,
  530. "accuracy": _data.accuracy,
  531. "speed": _data.fluency,
  532. "intonation": _data.tone,
  533. "score": _data.myOverall
  534. }, 'score', scoreRes, 'scoreRes');
  535. let data = {}
  536. if (_data.businessType != 2) {
  537. data = {
  538. challengerUserReadId: uploadRes.id,
  539. userReadId: this.data.pkData.id,
  540. winnerUId: this.data.pkData.score > _data.myOverall ? this.data.pkData.uid : this.data.pkData.score == _data.myOverall ? '' : wx.getStorageSync('uid')
  541. }
  542. } else {
  543. data = {
  544. challengerUserReadId: uploadRes.id,
  545. userReadId: this.data.pkData.id,
  546. winnerUId: ''
  547. }
  548. }
  549. let result = await uploadPk(data)
  550. console.log(result,'rrrr');
  551. await userEvent({
  552. action: 'WXPKUPLOAD',
  553. })
  554. wx.redirectTo({
  555. url: `/pages/pkResult/index?id=${result.id}`
  556. })
  557. },
  558. fail: (res) => {
  559. this.setData({
  560. uploadState: false
  561. })
  562. }
  563. });
  564. uploadTask.onProgressUpdate((res) => {
  565. console.log('作品上传进度', res.progress);
  566. this.setData({
  567. percent: res.progress
  568. })
  569. })
  570. },
  571. // 字体换行
  572. startRecording() {
  573. setTimeout(() => {
  574. if (this.data.currentRow == null) {
  575. this.setData({
  576. currentRow: 0
  577. })
  578. }
  579. let row = this.data.article[this.data.currentRow]
  580. if (!row.readTime) {
  581. return
  582. }
  583. this.setTimeoutObj = setTimeout(() => {
  584. this.setData({
  585. currentRow: ++this.data.currentRow
  586. })
  587. this.setData({
  588. scrollTop: this.rowH * this.data.currentRow
  589. })
  590. this.startRecording()
  591. },
  592. row.readTime);
  593. }, 100)
  594. },
  595. // 视频播放结束
  596. videoEnd() {
  597. this.resetReading()
  598. },
  599. videoPlay() {
  600. if (this.data.state) {
  601. return
  602. }
  603. if (this.data.videoInfo.userReadExtend.resourcesType == 1) {
  604. if (this.data.exampleState) {
  605. this.setData({
  606. exampleState: false
  607. })
  608. return this.resultAudioContext.stop()
  609. }
  610. this.resultAudioContext.src = this.data.readingReset ? this.data.readDetail.tempFilePath : this.data.videoInfo.userRead.audioPath;
  611. setTimeout(() => {
  612. this.resultAudioContext.play();
  613. }, 200)
  614. this.setData({
  615. exampleState: true
  616. })
  617. } else {
  618. if (this.data.readingReset) {
  619. this.resultAudioContext.src = this.data.readDetail.tempFilePath;
  620. this.resultAudioContext.play();
  621. this.setData({
  622. muted: true,
  623. exampleState: true
  624. })
  625. } else {
  626. this.setData({
  627. muted: false,
  628. exampleState: true
  629. })
  630. }
  631. this.setData({
  632. videoPath: this.data.videoInfo.userRead.videoPath
  633. })
  634. wx.nextTick(() => {
  635. this.videoContext.play()
  636. })
  637. }
  638. this.startRecording()
  639. submitPlayLog({
  640. userReadId: this.data.videoInfo.userRead.exampleId,
  641. playStopTime: 1000
  642. })
  643. },
  644. // 控制视频或音频的播放状态
  645. async playMediaState() {
  646. this.setData({
  647. muted: false
  648. })
  649. if (this.data.videoInfo.userReadExtend.resourcesType == 0) {
  650. this.setData({
  651. videoPath: this.data.videoInfo.userRead.originVideo
  652. })
  653. wx.nextTick(() => {
  654. this.videoContext.play()
  655. })
  656. } else {
  657. this.innerAudioContext.play();
  658. }
  659. await userEvent({
  660. action: 'WXREADING',
  661. readId: this.data.videoInfo.userRead.id
  662. })
  663. },
  664. // 重置一切状态
  665. resetReading() {
  666. clearTimeout(this.setTimeoutObj)
  667. clearInterval(this.stl)
  668. // 重置视频
  669. if (this.data.videoInfo.userReadExtend.resourcesType == 0) {
  670. this.videoContext.stop()
  671. this.videoContext.seek(0)
  672. }
  673. // 重置试听音频
  674. if (this.data.exampleState) {
  675. this.resultAudioContext.stop()
  676. // 重置录音时的背景音乐
  677. this.innerAudioContext.stop();
  678. console.log('是我暂停了');
  679. }
  680. if (this.data.state) {
  681. // 重置录音时的背景音乐
  682. this.innerAudioContext.stop();
  683. /*微信录音结束*/
  684. recorderManager.stop();
  685. }
  686. this.setData({
  687. exampleState: false,
  688. state: false,
  689. currentRow: null,
  690. scrollTop: 0,
  691. ["silderData.sliderValue"]: 0,
  692. ["silderData.currentTime"]: '00:00'
  693. })
  694. },
  695. // 阻止作品上传时返回
  696. beforeleave() {
  697. this.setData({
  698. uploadState: true
  699. })
  700. },
  701. // 获取设备高度与行高度
  702. getHeight() {
  703. var query = wx.createSelectorQuery();
  704. query.select('.content').boundingClientRect((rect) => {
  705. this.setData({
  706. contentH: rect.height
  707. })
  708. }).exec()
  709. query.select('.row').boundingClientRect((rect) => {
  710. this.rowH = rect.height
  711. }).exec()
  712. },
  713. // 进度条
  714. slider({
  715. detail
  716. }) {
  717. this.resultAudioContext.pause();
  718. this.resultAudioContext.seek(detail.value / 100 * this.data.videoInfo.userRead.duration)
  719. setTimeout(() => {
  720. this.resultAudioContext.play()
  721. }, 300)
  722. },
  723. onHide() {
  724. wsEngine.reset()
  725. this.resetReading()
  726. },
  727. onUnload() {
  728. wsEngine.reset()
  729. this.resetReading()
  730. this.storeBindings.destroyStoreBindings()
  731. },
  732. backReading() {
  733. wx.navigateBack({
  734. delta: 1
  735. })
  736. },
  737. otherWork() {
  738. wx.navigateTo({
  739. url: `/pages/otherWork/index?exampleId=${this.data.videoInfo.userRead.exampleId}`
  740. })
  741. },
  742. async toBuy({
  743. detail
  744. }) {
  745. console.log(detail);
  746. wx.showLoading({
  747. title: '提交中',
  748. mask: true
  749. })
  750. let res = await buyVip({
  751. productId: detail.id
  752. }).finally(() => {
  753. wx.hideLoading()
  754. })
  755. let {
  756. timeStamp,
  757. nonceStr,
  758. signType,
  759. paySign
  760. } = res
  761. // package保留字
  762. wx.requestPayment({
  763. timeStamp,
  764. nonceStr,
  765. package: res.package,
  766. signType,
  767. paySign,
  768. success: (res) => {
  769. this.selectComponent('#buyVip').closeModal()
  770. this.selectComponent('#vipModal').open({
  771. type: detail.payType == 'LIFELONG' ? 'sip' : 'vip'
  772. })
  773. setTimeout(() => {
  774. this.getVipInfo()
  775. }, 1500)
  776. },
  777. fail(res) {
  778. wx.showToast({
  779. title: "支付失败",
  780. icon: "none",
  781. duration: 3000
  782. })
  783. }
  784. })
  785. },
  786. creatShare() {
  787. return new Promise((resolve, reject) => {
  788. let video = this.data.videoInfo
  789. let context = wx.createSelectorQuery();
  790. context
  791. .select('#share')
  792. .fields({
  793. node: true,
  794. size: true
  795. }).exec((res) => {
  796. let canvas = res[0].node;
  797. let ctx = canvas.getContext('2d');
  798. let dpr = wx.getSystemInfoSync().pixelRatio;
  799. canvas.width = res[0].width * dpr;
  800. canvas.height = res[0].height * dpr;
  801. ctx.scale(dpr, dpr);
  802. ctx.font = '14px PingFang';
  803. let pic = canvas.createImage();
  804. pic.src = video.userReadExtend && video.userReadExtend.resourcesType == 1 ? video.userReadExtend.backgroundVirtualImg : video.userRead.coverImg;
  805. pic.onload = () => {
  806. ctx.drawImage(pic, 0, 0, 375, 211);
  807. let peiyin = canvas.createImage();
  808. peiyin.src = '/static/peiyin.jpg';
  809. peiyin.onload = () => {
  810. ctx.drawImage(peiyin, 0, 211, 375, 89);
  811. //分享
  812. let fx = canvas.createImage();
  813. fx.src = '/static/share.png'
  814. fx.onload = () => {
  815. ctx.drawImage(fx, 12, 220, 20, 20)
  816. ctx.fillText('分享', 36, 238)
  817. // 收藏,一个一个渲染
  818. let sc = canvas.createImage();
  819. sc.src = '/static/no_collect.png'
  820. sc.onload = () => {
  821. ctx.drawImage(sc, 110, 220, 19, 19)
  822. ctx.fillText('收藏', 134, 238)
  823. //点赞
  824. let dz = canvas.createImage();
  825. dz.src = '/static/heart.png'
  826. dz.onload = () => {
  827. ctx.drawImage(dz, 318, 222, 22, 22)
  828. ctx.fillText(0, 254, 238)
  829. // 评论
  830. let pl = canvas.createImage();
  831. pl.src = '/static/comment.png'
  832. pl.onload = () => {
  833. ctx.drawImage(pl, 228, 222, 22, 22)
  834. ctx.fillText(0, 340, 238)
  835. if (video.userReadExtend.resourcesType == 1) {
  836. let aBg = canvas.createImage();
  837. aBg.src = '/static/shareAudioBg.png';
  838. aBg.onload = () => {
  839. ctx.drawImage(aBg, 127.5, 38, 120, 120);
  840. let rate = 0.5
  841. ctx.arc(
  842. Math.floor(375 * rate),
  843. 98,
  844. Math.floor(100 * rate),
  845. 0,
  846. 2 * Math.PI
  847. );
  848. ctx.clip() //裁剪
  849. let coverImg = canvas.createImage();
  850. coverImg.src = video.userRead.coverImg;
  851. coverImg.onload = () => {
  852. ctx.drawImage( //定位在圆圈范围内便会出现
  853. coverImg, //图片暂存路径
  854. 129, 42,
  855. 110, 110,
  856. );
  857. ctx.restore()
  858. }
  859. }
  860. }
  861. setTimeout(() => {
  862. console.log(wx.getStorageSync('shareVideoId'), 'reading-wx.getStorageSync');
  863. wx.canvasToTempFilePath({
  864. canvas: canvas,
  865. success(res) {
  866. resolve({
  867. title: '我的新作品发布啦,快来捧场点赞!',
  868. path: `/pages/pkPage/index?videoId=${wx.getStorageSync('shareVideoId')}&uid=${wx.getStorageSync('uid')}&isShare=true`,
  869. imageUrl: res.tempFilePath
  870. })
  871. },
  872. fail(res) {
  873. reject()
  874. }
  875. }, this)
  876. }, 500)
  877. }
  878. }
  879. }
  880. }
  881. }
  882. }
  883. })
  884. })
  885. },
  886. onShareAppMessage({
  887. from,
  888. target
  889. }) {
  890. if (from == 'button') {
  891. let promise = new Promise(resolve => {
  892. this.creatShare().then(res => {
  893. resolve(res)
  894. })
  895. })
  896. return {
  897. title: '我的新作品发布啦,快来捧场点赞!',
  898. path: `/pages/index/index?uid=${wx.getStorageSync('uid')}`,
  899. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/375-300-1.jpg',
  900. promise
  901. }
  902. } else {
  903. return {
  904. title: '自从用了它,家里朗朗书声,美妙极了!你家孩子也快来试试!',
  905. path: `/pages/index/index?uid=${wx.getStorageSync('uid')}`,
  906. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/375-300-1.jpg'
  907. }
  908. }
  909. },
  910. })