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