index.js 29 KB

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