index.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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. buyVip,
  12. getVipInfo
  13. } from '~/api/user'
  14. import {
  15. userEvent
  16. } from '~/api/global'
  17. import {
  18. createStoreBindings
  19. } from 'mobx-miniprogram-bindings'
  20. import {
  21. store
  22. } from '~/store/index'
  23. import {
  24. setDuration
  25. } from '~/utils/util'
  26. const aiengine = require('~/utils/ChivoxAiEngine')
  27. const sha1 = require('~/utils/sha1');
  28. // 文章行高
  29. let rowH = 0
  30. let videoContext = null
  31. // 滚动变色定时器
  32. let stl = null
  33. // 倒计时
  34. let setTimeoutObj = null
  35. // 录音
  36. let innerAudioContext = null
  37. // 试听
  38. let resultAudioContext = null
  39. /*创建基础引擎*/
  40. let wsEngine = aiengine.createWsEngine({});
  41. /*微信录音*/
  42. let recorderManager = wx.getRecorderManager();
  43. Page({
  44. data: {
  45. videoInfo: {},
  46. videoPath: '',
  47. currentRow: null,
  48. state: false,
  49. // 示例播放状态
  50. exampleState: false,
  51. // 是否静音播放视频
  52. muted: false,
  53. countDown: {
  54. state: false,
  55. num: 3,
  56. },
  57. contentH: 0,
  58. percent: 0,
  59. scrollTop: 0,
  60. //如果readingReset为true就是重读
  61. readingReset: false,
  62. //readingType为public是普通阅读,为pk是pk逻辑,readMatch为朗读赛
  63. readingType: 'public',
  64. uploadState: false,
  65. article: [],
  66. silderData: {
  67. currentTime: '00:00',
  68. endTime: '00:00',
  69. silderValue: 0
  70. },
  71. // 朗读赛的id
  72. activityId: '',
  73. // 0免费1收费
  74. free: 1,
  75. isVip: false
  76. },
  77. onLoad(options) {
  78. console.log(options);
  79. let videoId = options.videoId
  80. wx.setNavigationBarTitle({
  81. title: options.navBarTitle
  82. })
  83. this.getreadInfo(videoId, options.reset).then(res => {
  84. wx.nextTick(() => {
  85. if (options.voluntarily && this.data.isVip) {
  86. this.setCountDown()
  87. }
  88. if (options.autoPlay) {
  89. this.videoPlay()
  90. }
  91. })
  92. })
  93. this.setData({
  94. readingReset: options.reset || false,
  95. readingType: options.readingType || 'public',
  96. uploadHide: options.uploadHide,
  97. activityId: options.activityId || '',
  98. free: Number(options.free)
  99. })
  100. // 手工绑定
  101. this.storeBindings = createStoreBindings(this, {
  102. store,
  103. fields: {
  104. userInfo: 'userInfo',
  105. readDetail: 'readDetail',
  106. pkData: 'pkData'
  107. },
  108. actions: {
  109. setUser: 'setUser',
  110. setReadDetail: 'setReadDetail'
  111. }
  112. })
  113. // 录音授权
  114. wx.getSetting({
  115. success(res) {
  116. if (!res.authSetting['scope.record']) {
  117. wx.authorize({
  118. scope: 'scope.record',
  119. success() {
  120. // 用户已经同意小程序使用录音功能,后续调用接口不会弹窗询问
  121. },
  122. fail() {
  123. wx.showModal({
  124. title: '授权提示',
  125. content: '请先开启录音功能',
  126. success(res) {
  127. wx.openSetting({
  128. success(res) {}
  129. })
  130. }
  131. })
  132. }
  133. })
  134. }
  135. }
  136. })
  137. /*监听评测结果:必须在基础引擎创建后,调用任何评测接口前设置监听,否则有可能收不到相关事件。*/
  138. wsEngine.onResult((res) => {
  139. this.getRecordScore(res)
  140. });
  141. wsEngine.onErrorResult(async (res) => {
  142. console.log("===收到错误结果=============", res)
  143. await userEvent({
  144. action: 'WXSCORE',
  145. targetContent: res
  146. })
  147. });
  148. this.innerAudioContext = wx.createInnerAudioContext();
  149. this.innerAudioContext.onTimeUpdate(res => {
  150. this.setData({
  151. ["silderData.sliderValue"]: Math.round(this.innerAudioContext.currentTime / this.innerAudioContext.duration * 100),
  152. ["silderData.currentTime"]: setDuration(this.innerAudioContext.currentTime)
  153. })
  154. })
  155. this.resultAudioContext = wx.createInnerAudioContext();
  156. this.resultAudioContext.onTimeUpdate(res => {
  157. this.setData({
  158. ["silderData.sliderValue"]: Math.round(this.resultAudioContext.currentTime / this.resultAudioContext.duration * 100),
  159. ["silderData.currentTime"]: setDuration(this.resultAudioContext.currentTime)
  160. })
  161. })
  162. this.resultAudioContext.onError(res => {
  163. console.log(res, 'resultAudioContext');
  164. })
  165. this.innerAudioContext.onError(res => {
  166. console.log(res, 'bbbb');
  167. })
  168. this.resultAudioContext.onEnded(res => {
  169. console.log('102-resultAudioContext.ended');
  170. this.setData({
  171. exampleState: false
  172. })
  173. if (this.data.videoInfo.userReadExtend.resourcesType == 0) {
  174. this.videoContext.stop()
  175. this.videoContext.seek(0)
  176. }
  177. })
  178. this.resultAudioContext.onStop((res) => {
  179. console.log('109-resultAudioContext.onStop');
  180. this.setData({
  181. exampleState: false
  182. })
  183. if (this.data.videoInfo.userReadExtend.resourcesType == 0) {
  184. this.videoContext.stop()
  185. this.videoContext.seek(0)
  186. }
  187. });
  188. this.resultAudioContext.onEnded(res => {
  189. this.setData({
  190. ["silderData.sliderValue"]: 100
  191. })
  192. })
  193. },
  194. onShow() {
  195. this.getVipInfo()
  196. },
  197. // 获取是否vip
  198. async getVipInfo() {
  199. let vipTime = await getVipInfo()
  200. this.setData({
  201. isVip: vipTime != ''
  202. })
  203. },
  204. // 获取阅读内容
  205. getreadInfo(videoId, reset = false) {
  206. return new Promise(async (resolve, reject) => {
  207. let videoInfo = await getreadInfo(videoId)
  208. let data = JSON.parse(videoInfo.userReadExtend.lessonText)
  209. data = data.map((item, index) => {
  210. item.time = Number(item.time)
  211. item.readTime = data[index + 1] ? data[index + 1].time - item.time : ''
  212. return item
  213. })
  214. this.setData({
  215. videoPath: videoInfo.userRead.originVideo,
  216. article: data,
  217. videoInfo,
  218. ["silderData.endTime"]: setDuration(videoInfo.userRead.duration)
  219. })
  220. if (!reset) {
  221. this.getHeight()
  222. }
  223. if (this.data.videoInfo.userReadExtend.resourcesType == 0) {
  224. this.videoContext = wx.createVideoContext('myVideo')
  225. } else {
  226. this.innerAudioContext.src = videoInfo.userRead.originVideo
  227. this.innerAudioContext.onEnded(res => {
  228. console.log("138innerAudioContext触发的");
  229. this.resetReading()
  230. })
  231. this.innerAudioContext.onStop((res) => {
  232. console.log("143innerAudioContext触发的");
  233. });
  234. }
  235. resolve()
  236. })
  237. },
  238. // 开始录制
  239. setCountDown() {
  240. if (!this.data.isVip && !!this.data.free) {
  241. this.resetReading()
  242. return this.selectComponent('#buyVip').open({
  243. isVip: this.data.isVip
  244. })
  245. }
  246. if (this.data.state) {
  247. this.resetReading()
  248. return
  249. }
  250. if (!this.data.readingReset) {
  251. this.getHeight()
  252. }
  253. this.resetReading()
  254. this.setData({
  255. readingReset: false,
  256. 'countDown.state': true
  257. })
  258. this.stl = setInterval(async () => {
  259. if (this.data.countDown.num == 0) {
  260. clearInterval(this.stl)
  261. this.setData({
  262. state: true,
  263. countDown: {
  264. state: false,
  265. num: 3
  266. }
  267. })
  268. await this.playMediaState()
  269. await this.soundRecording()
  270. this.startRecording()
  271. } else {
  272. this.setData({
  273. 'countDown.num': --this.data.countDown.num
  274. })
  275. }
  276. }, 1000)
  277. },
  278. // 录音
  279. soundRecording() {
  280. /*调用微信开始录音接口,并启动语音评测*/
  281. let timeStamp = new Date().getTime()
  282. let sig = sha1(`16075689600000da${timeStamp}caa8e60da6042731c230fe431ac9c7fd`)
  283. let app = {
  284. applicationId: '16075689600000da',
  285. sig, //签名字符串
  286. alg: 'sha1',
  287. timestamp: timeStamp + '',
  288. userId: wx.getStorageSync('uid')
  289. }
  290. let lessonText = JSON.parse(this.data.videoInfo.userReadExtend.lessonText).map((item) => {
  291. return item.text
  292. }).join('')
  293. console.log(lessonText);
  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}&free=${this.data.free}`,
  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. console.log('data----', this.data.pkData.score, _data.myOverall, data);
  476. let result = await uploadPk(data)
  477. await userEvent({
  478. action: 'WXPKUPLOAD',
  479. })
  480. wx.redirectTo({
  481. url: `/pages/pkResult/index?id=${result.id}`
  482. })
  483. },
  484. fail: (res) => {
  485. this.setData({
  486. uploadState: false
  487. })
  488. }
  489. });
  490. uploadTask.onProgressUpdate((res) => {
  491. console.log('作品上传进度', res.progress);
  492. this.setData({
  493. percent: res.progress
  494. })
  495. })
  496. },
  497. // 字体换行
  498. startRecording() {
  499. setTimeout(() => {
  500. if (this.data.currentRow == null) {
  501. this.setData({
  502. currentRow: 0
  503. })
  504. }
  505. let row = this.data.article[this.data.currentRow]
  506. if (!row.readTime) {
  507. return
  508. }
  509. this.setTimeoutObj = setTimeout(() => {
  510. this.setData({
  511. currentRow: ++this.data.currentRow
  512. })
  513. this.setData({
  514. scrollTop: this.rowH * this.data.currentRow
  515. })
  516. this.startRecording()
  517. },
  518. row.readTime);
  519. }, 100)
  520. },
  521. // 视频播放结束
  522. videoEnd() {
  523. this.resetReading()
  524. },
  525. videoPlay() {
  526. if (this.data.state) {
  527. return
  528. }
  529. if (this.data.videoInfo.userReadExtend.resourcesType == 1) {
  530. if (this.data.exampleState) {
  531. this.setData({
  532. exampleState: false
  533. })
  534. return this.resultAudioContext.stop()
  535. }
  536. this.resultAudioContext.src = this.data.readingReset ? this.data.readDetail.tempFilePath : this.data.videoInfo.userRead.audioPath;
  537. setTimeout(() => {
  538. this.resultAudioContext.play();
  539. }, 200)
  540. this.setData({
  541. exampleState: true
  542. })
  543. } else {
  544. if (this.data.readingReset) {
  545. this.resultAudioContext.src = this.data.readDetail.tempFilePath;
  546. this.resultAudioContext.play();
  547. this.setData({
  548. muted: true,
  549. exampleState: true
  550. })
  551. } else {
  552. this.setData({
  553. muted: false,
  554. exampleState: true
  555. })
  556. }
  557. this.setData({
  558. videoPath: this.data.videoInfo.userRead.videoPath
  559. })
  560. wx.nextTick(() => {
  561. this.videoContext.play()
  562. })
  563. }
  564. this.startRecording()
  565. submitPlayLog({
  566. userReadId: this.data.videoInfo.userRead.exampleId,
  567. playStopTime: 1000
  568. })
  569. },
  570. // 控制视频或音频的播放状态
  571. async playMediaState() {
  572. this.setData({
  573. muted: false
  574. })
  575. if (this.data.videoInfo.userReadExtend.resourcesType == 0) {
  576. this.setData({
  577. videoPath: this.data.videoInfo.userRead.originVideo
  578. })
  579. wx.nextTick(() => {
  580. this.videoContext.play()
  581. })
  582. } else {
  583. this.innerAudioContext.play();
  584. }
  585. await userEvent({
  586. action: 'WXREADING',
  587. readId: this.data.videoInfo.userRead.id
  588. })
  589. },
  590. // 重置一切状态
  591. resetReading() {
  592. clearTimeout(this.setTimeoutObj)
  593. clearInterval(this.stl)
  594. // 重置视频
  595. if (this.data.videoInfo.userReadExtend.resourcesType == 0) {
  596. this.videoContext.stop()
  597. this.videoContext.seek(0)
  598. }
  599. // 重置试听音频
  600. if (this.data.exampleState) {
  601. this.resultAudioContext.stop()
  602. // 重置录音时的背景音乐
  603. this.innerAudioContext.stop();
  604. console.log('是我暂停了');
  605. }
  606. if (this.data.state) {
  607. // 重置录音时的背景音乐
  608. this.innerAudioContext.stop();
  609. /*微信录音结束*/
  610. recorderManager.stop();
  611. }
  612. this.setData({
  613. exampleState: false,
  614. state: false,
  615. currentRow: null,
  616. scrollTop: 0,
  617. ["silderData.sliderValue"]: 0,
  618. ["silderData.currentTime"]: '00:00'
  619. })
  620. },
  621. // 阻止作品上传时返回
  622. beforeleave() {
  623. this.setData({
  624. uploadState: true
  625. })
  626. },
  627. // 获取设备高度与行高度
  628. getHeight() {
  629. var query = wx.createSelectorQuery();
  630. query.select('.content').boundingClientRect((rect) => {
  631. this.setData({
  632. contentH: rect.height
  633. })
  634. }).exec()
  635. query.select('.row').boundingClientRect((rect) => {
  636. this.rowH = rect.height
  637. }).exec()
  638. },
  639. // 进度条
  640. slider({
  641. detail
  642. }) {
  643. this.resultAudioContext.pause();
  644. this.resultAudioContext.seek(detail.value / 100 * this.data.videoInfo.userRead.duration)
  645. setTimeout(() => {
  646. this.resultAudioContext.play()
  647. }, 300)
  648. },
  649. onHide() {
  650. wsEngine.reset()
  651. this.resetReading()
  652. },
  653. onUnload() {
  654. wsEngine.reset()
  655. this.resetReading()
  656. this.storeBindings.destroyStoreBindings()
  657. },
  658. backReading() {
  659. wx.navigateBack({
  660. delta: 1
  661. })
  662. },
  663. otherWork() {
  664. wx.navigateTo({
  665. url: `/pages/otherWork/index?exampleId=${this.data.videoInfo.userRead.exampleId}`
  666. })
  667. },
  668. async toBuy({
  669. detail
  670. }) {
  671. console.log(detail);
  672. wx.showLoading({
  673. title: '提交中',
  674. mask: true
  675. })
  676. let res = await buyVip({
  677. productId: detail.id
  678. }).finally(() => {
  679. wx.hideLoading()
  680. })
  681. let {
  682. timeStamp,
  683. nonceStr,
  684. signType,
  685. paySign
  686. } = res
  687. // package保留字
  688. wx.requestPayment({
  689. timeStamp,
  690. nonceStr,
  691. package: res.package,
  692. signType,
  693. paySign,
  694. success: (res) => {
  695. this.selectComponent('#buyVip').closeModal()
  696. this.selectComponent('#vipModal').open({
  697. type: detail.payType == 'LIFELONG' ? 'sip' : 'vip'
  698. })
  699. setTimeout(() => {
  700. this.getVipInfo()
  701. }, 1500)
  702. },
  703. fail(res) {
  704. wx.showToast({
  705. title: "支付失败",
  706. icon: "none",
  707. duration: 3000
  708. })
  709. }
  710. })
  711. },
  712. creatShare() {
  713. return new Promise((resolve, reject) => {
  714. let video = this.data.videoInfo
  715. let context = wx.createSelectorQuery();
  716. context
  717. .select('#share')
  718. .fields({
  719. node: true,
  720. size: true
  721. }).exec((res) => {
  722. const canvas = res[0].node;
  723. const ctx = canvas.getContext('2d');
  724. const dpr = wx.getSystemInfoSync().pixelRatio;
  725. canvas.width = res[0].width * dpr;
  726. canvas.height = res[0].height * dpr;
  727. ctx.scale(dpr, dpr);
  728. ctx.font = '14px PingFang';
  729. let pic = canvas.createImage();
  730. pic.src = video.userReadExtend && video.userReadExtend.resourcesType == 1 ? video.userReadExtend.backgroundVirtualImg : video.userRead.coverImg;
  731. pic.onload = () => {
  732. ctx.drawImage(pic, 0, 0, 375, 211);
  733. let peiyin = canvas.createImage();
  734. peiyin.src = '/static/peiyin.jpg';
  735. peiyin.onload = () => {
  736. ctx.drawImage(peiyin, 0, 211, 375, 89);
  737. //分享
  738. let fx = canvas.createImage();
  739. fx.src = '/static/share.png'
  740. fx.onload = () => {
  741. ctx.drawImage(fx, 12, 220, 20, 20)
  742. ctx.fillText('分享', 36, 238)
  743. // 收藏,一个一个渲染
  744. let sc = canvas.createImage();
  745. sc.src = '/static/no_collect.png'
  746. sc.onload = () => {
  747. ctx.drawImage(sc, 110, 220, 19, 19)
  748. ctx.fillText('收藏', 134, 238)
  749. //点赞
  750. let dz = canvas.createImage();
  751. dz.src = '/static/heart.png'
  752. dz.onload = () => {
  753. ctx.drawImage(dz, 318, 222, 22, 22)
  754. ctx.fillText(0, 254, 238)
  755. // 评论
  756. let pl = canvas.createImage();
  757. pl.src = '/static/comment.png'
  758. pl.onload = () => {
  759. ctx.drawImage(pl, 228, 222, 22, 22)
  760. ctx.fillText(0, 340, 238)
  761. if (video.userReadExtend.resourcesType == 1) {
  762. let aBg = canvas.createImage();
  763. aBg.src = '/static/shareAudioBg.png';
  764. aBg.onload = () => {
  765. ctx.drawImage(aBg, 127.5, 38, 120, 120);
  766. let rate = 0.5
  767. ctx.arc(
  768. Math.floor(375 * rate),
  769. 98,
  770. Math.floor(100 * rate),
  771. 0,
  772. 2 * Math.PI
  773. );
  774. ctx.clip() //裁剪
  775. let coverImg = canvas.createImage();
  776. coverImg.src = video.userRead.coverImg;
  777. coverImg.onload = () => {
  778. ctx.drawImage( //定位在圆圈范围内便会出现
  779. coverImg, //图片暂存路径
  780. 129, 42,
  781. 110, 110,
  782. );
  783. ctx.restore()
  784. }
  785. }
  786. }
  787. setTimeout(() => {
  788. console.log(wx.getStorageSync('shareVideoId'), 'reading-wx.getStorageSync');
  789. wx.canvasToTempFilePath({
  790. canvas: canvas,
  791. success(res) {
  792. resolve({
  793. title: '我的新作品发布啦,快来捧场点赞!',
  794. path: `/pages/pkPage/index?videoId=${wx.getStorageSync('shareVideoId')}&uid=${wx.getStorageSync('uid')}&isShare=true`,
  795. imageUrl: res.tempFilePath
  796. })
  797. },
  798. fail(res) {
  799. reject()
  800. }
  801. }, this)
  802. }, 500)
  803. }
  804. }
  805. }
  806. }
  807. }
  808. }
  809. })
  810. })
  811. },
  812. onShareAppMessage({
  813. from,
  814. target
  815. }) {
  816. if (from == 'button') {
  817. const promise = new Promise(resolve => {
  818. this.creatShare().then(res => {
  819. resolve(res)
  820. })
  821. })
  822. return {
  823. title: '我的新作品发布啦,快来捧场点赞!',
  824. path: `/pages/index/index?uid=${wx.getStorageSync('uid')}`,
  825. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/375-300-1.jpg',
  826. promise
  827. }
  828. } else {
  829. return {
  830. title: '自从用了它,家里朗朗书声,美妙极了!你家孩子也快来试试!',
  831. path: `/pages/index/index?uid=${wx.getStorageSync('uid')}`,
  832. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/375-300-1.jpg'
  833. }
  834. }
  835. },
  836. })