index.js 24 KB

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