index.js 36 KB

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