index.js 31 KB

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