index.js 31 KB

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