index.js 23 KB

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