index.js 29 KB

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