index.js 33 KB

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