index.js 30 KB

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