index.js 34 KB

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