index.js 34 KB

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