index.js 23 KB

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