reading.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. import httpRequestApi from '../../utils/APIClient';
  2. const aiengine = require('../../utils/ChivoxAiEngine')
  3. const sha1 = require('../../utils/sha1');
  4. import {
  5. getUserInfo,
  6. getVipInfo
  7. } from '~/api/user'
  8. const app = getApp()
  9. import {
  10. formatDate
  11. } from '../../utils/util';
  12. Page({
  13. data: {
  14. userInfo: {},
  15. isVip: false,
  16. title: '',
  17. id: '',
  18. img: '',
  19. fullScreenBtn: false,
  20. playBtn: true,
  21. gesture: true,
  22. muted: false,
  23. gesture: false,
  24. centerBtn: true,
  25. recordFlag: 0,
  26. recordSource: '',
  27. videoCtr: 'recordingVideoEnd',
  28. btnFlag: false,
  29. btnImgFlag: false,
  30. microphonePng: '../../static/index/microphone.png',
  31. recordingGif: '../../static/index/readingNow.gif',
  32. videoUrl: '',
  33. readingText: '',
  34. videoList: [],
  35. pageNo: 1,
  36. totalSize: 0,
  37. nextMargin: getApp().globalData.nextMargin,
  38. lowerThresHold: 100,
  39. isVideoListShow: true,
  40. overall: '', // 评测总分
  41. integrity: '', //完成度
  42. tone: '', //语调声调
  43. fluency: '', //流利度
  44. accuracy: '', // 正确分,发音分
  45. star: [0, 0, 0, 0, 0],
  46. ifTextShow: true,
  47. ifScoreDialogShow: false,
  48. ifScoreShow: false,
  49. statusbarobj: {
  50. isshowbtn: false, //是否显示按钮
  51. title: "朗读小咖秀", //标题
  52. },
  53. isScroll: true,
  54. noMoreWork: false,
  55. authHide: true,
  56. authImg: '',
  57. isIOS: app.globalData.isIOS,
  58. payPrice: ''
  59. },
  60. onLoad: function(option) {
  61. console.log(option);
  62. this.videoCtx = null;
  63. const uid = wx.getStorageSync('uid')
  64. this.getClassInfo(option.id);
  65. // this.getMicAuth()
  66. },
  67. getClassInfo: function(id) {
  68. httpRequestApi.getClassDetail(id).success(res => {
  69. wx.setNavigationBarTitle({
  70. title: res.data.data.userRead.title,
  71. })
  72. console.log('课程信息', res)
  73. let reg = /\\n/g
  74. this.setData({
  75. title: res.data.data.userRead.title,
  76. videoUrl: res.data.data.userRead.videoPath,
  77. videoPathTemp: res.data.data.userRead.videoPath,
  78. originVideo: res.data.data.userRead.originVideo,
  79. img: res.data.data.userRead.iconImg,
  80. id: res.data.data.userRead.id,
  81. readingText: res.data.data.userRead.lessonText,
  82. grade: res.data.data.userRead.grade,
  83. exampleId: res.data.data.userRead.exampleId,
  84. summary: res.data.data.userRead.summary,
  85. coverImg: res.data.data.userRead.coverImg,
  86. shareImg: res.data.data.userRead.shareImg,
  87. })
  88. this.getReadInfo(id)
  89. httpRequestApi.userIntoPage('pages/reading/reading', '朗读页面').success((res) => {
  90. })
  91. })
  92. },
  93. onHide: function() {
  94. console.log('onhide')
  95. if (this.data.btnImgFlag) {
  96. this.recorderManager.stop();
  97. }
  98. if (this.innerAudioContext) {
  99. this.innerAudioContext.stop();
  100. }
  101. // this.ss.destroyEngine();
  102. this.setData({
  103. recordFlag: 0
  104. })
  105. },
  106. onUnload: function() {
  107. console.log('onUnload')
  108. if (this.data.btnImgFlag) {
  109. this.recorderManager.stop();
  110. }
  111. if (this.innerAudioContext) {
  112. this.innerAudioContext.stop();
  113. }
  114. },
  115. onShow: function() {
  116. // this.getUserInfo()
  117. this.videoCtx = wx.createVideoContext('myVideo', this);
  118. let data = requirePlugin("myPlugin");
  119. const obj = {
  120. appid: 'a415',
  121. userid: wx.getStorageSync('uid'),
  122. getEvalMessage: (res) => {
  123. this.getRecordScore(res)
  124. // console.log('评测结果',res)
  125. },
  126. recorderCallback: (type, data) => {
  127. // 录音测评监控回调
  128. this.ssRecorderCallback(type, data)
  129. }
  130. }
  131. // this.ss = new data.ssEngine(obj);
  132. this.recorderManager = wx.getRecorderManager();
  133. this.recorderManager.onStop((res) => {
  134. this.videoCtx.seek(0);
  135. this.videoCtx.stop();
  136. console.log('recorder stop', res)
  137. const recordFile = res.tempFilePath;
  138. this.setData({
  139. recordFlag: 0,
  140. recordSource: recordFile,
  141. btnFlag: true,
  142. btnImgFlag: false
  143. })
  144. this.wsEngine.stop({
  145. success: () => {
  146. console.log('====== wsEngine stop success ======');
  147. wx.showLoading({
  148. title: '作品评测中',
  149. mask: true
  150. })
  151. // TODO: add your code here
  152. },
  153. fail: (res) => {
  154. console.log("====== wsEngine stop fail ======");
  155. console.log(JSON.stringify(res)); //请关注res.errId, res.error
  156. // TODO: add your code here
  157. },
  158. complete: () => {
  159. // TODO: add your code here
  160. // clearInterval(recordTimeInterval);
  161. console.log("====== wsEngine stop complete ======");
  162. }
  163. });
  164. })
  165. this.recorderManager.onStart(() => {
  166. // this.saveVideo();
  167. this.setData({
  168. btnImgFlag: true,
  169. btnFlag: false
  170. })
  171. console.log('recorder start')
  172. });
  173. //监听已录制完指定帧大小的文件事件。如果设置了 frameSize,则会回调此事件。
  174. this.recorderManager.onFrameRecorded((res) => {
  175. const {
  176. frameBuffer
  177. } = res
  178. console.log('frameBuffer.byteLength', frameBuffer.byteLength)
  179. //TODO 调用feed接口传递音频片给驰声评测引擎
  180. this.wsEngine.feed({
  181. data: frameBuffer, // frameBuffer为微信录音机回调的音频数据
  182. success: () => {
  183. // feed 成功
  184. console.log('feed success.')
  185. },
  186. fail: (res) => {
  187. // feed 失败, 请关注res.errId, res.error
  188. console.log('feed fail:', JSON.stringify(res))
  189. },
  190. complete: () => {
  191. // feed 完成
  192. }
  193. });
  194. });
  195. },
  196. async getUserInfo() {
  197. /* let userInfo = await getUserInfo()
  198. let vip = await getVipInfo()
  199. console.log(userInfo);
  200. this.setData({
  201. userInfo,
  202. isVip: vip ? true : false
  203. }) */
  204. },
  205. ssRecorderCallback: function(type, data) {
  206. /* 录音开始 */
  207. if (type === 'onStart') {
  208. this.setData({
  209. btnImgFlag: true,
  210. btnFlag: false
  211. })
  212. console.log('recorder start')
  213. }
  214. if (type === 'onStop') {
  215. this.videoCtx.stop();
  216. this.setData({
  217. recordFlag: 0,
  218. recordSource: data.tempFilePath,
  219. btnFlag: true,
  220. btnImgFlag: false
  221. })
  222. console.log('recorder start')
  223. }
  224. },
  225. // 录音中视频播放结束 (控制录音同时结束)
  226. recordingVideoEnd: function() {
  227. console.log(this.data.videoCtr)
  228. console.log('recordingVideoEnd');
  229. //
  230. if (this.data.recordFlag === 0) {
  231. // this.recordStop();
  232. this.playingVideoEnd();
  233. return;
  234. }
  235. // 录音结束
  236. if (this.data.recordFlag === 1) {
  237. this.recordStop();
  238. }
  239. },
  240. // 播放中视频播放结束 (控制录音同时结束)
  241. playingVideoEnd: function() {
  242. console.log('playingVideoEnd')
  243. // this.ss.stopPlay();
  244. },
  245. /***
  246. * recordFlag:
  247. * 0 初始状态
  248. * 1 录音中
  249. * 2 录音结束
  250. ***/
  251. audioRecord: function() {
  252. let child = this.selectComponent('#readingTips').data
  253. // 判断是否有权限朗读 不是vip并且没有朗读机会
  254. const isVip = child.vipTime ? true : false
  255. if (!isVip && child.userInfo.experienceAmount == 0) {
  256. this.selectComponent('#readingTips').showModal();
  257. } else {
  258. if (this.recorderManager) {
  259. this.recorderManager.stop();
  260. }
  261. if (this.innerAudioContext) {
  262. this.innerAudioContext.stop();
  263. }
  264. if (this.data.recordFlag === 0) {
  265. this.wsEngine = aiengine.createWsEngine({});
  266. this.wsEngine.onResult(res => {
  267. wx.hideLoading();
  268. this.getRecordScore(res)
  269. })
  270. this.wsEngine.onErrorResult(res => {
  271. wx.hideLoading();
  272. console.log('驰声createWsEngine失败', res);
  273. })
  274. this.videoComplete();
  275. if (this.data.btnFlag) {
  276. httpRequestApi.userEvent('RERECORDING');
  277. } else {
  278. httpRequestApi.userEvent('READING', this.data.exampleId);
  279. }
  280. // this.getMicAuth()
  281. return;
  282. }
  283. // 录音结束后
  284. if (this.data.recordFlag === 1) {
  285. wx.showLoading({
  286. title: '作品转码中',
  287. mask: true
  288. })
  289. this.recordStop();
  290. }
  291. }
  292. },
  293. // 录音开始
  294. /**
  295. * duration: 时长 最长10分钟
  296. sampleRate: 44100, 采样率
  297. numberOfChannels: 1, 录音通道
  298. encodeBitRate: 192000, 码率
  299. format: 'mp3', 格式
  300. frameSize: 50 制定帧大小
  301. */
  302. recordStart: function() {
  303. console.log('录音开始');
  304. const options = {
  305. duration: 600000,
  306. sampleRate: 44100,
  307. numberOfChannels: 1,
  308. encodeBitRate: 192000,
  309. format: 'mp3',
  310. frameSize: 50
  311. }
  312. this.recorderManager.start(options);
  313. // this.ss.startRecord({
  314. // coreType: 'cn.sent.score', //测评题型
  315. // evalTime: 300000, //测评的录音时间,时间到自动停止测评
  316. // refText: this.data.readingText, //测评文本
  317. // warrantId: 'c11163aa6c834a028da4a4b30955be96', //鉴权id
  318. // })
  319. // 驰声测评
  320. let timeStamp = new Date().getTime()
  321. let sig = sha1(`16075689600000da${timeStamp}caa8e60da6042731c230fe431ac9c7fd`)
  322. console.log('加密串', sig)
  323. // return
  324. let app = {
  325. applicationId: '16075689600000da',
  326. sig, //签名字符串 通过签名算法alg(appkey + timestamp + secretKey)生成
  327. alg: 'sha1',
  328. timestamp: timeStamp + '',
  329. userId: wx.getStorageSync('uid')
  330. }
  331. console.log('app数据', app)
  332. let lessonText = this.data.readingText;
  333. console.log('课文课文课文', lessonText)
  334. this.wsEngine.start({
  335. app,
  336. request: {
  337. coreType: "cn.pred.raw",
  338. refText: lessonText,
  339. rank: 100,
  340. attachAudioUrl: 1,
  341. result: {
  342. details: {
  343. gop_adjust: 1
  344. }
  345. }
  346. },
  347. audio: {
  348. audioType: "mp3",
  349. channel: 1,
  350. sampleBytes: 2,
  351. sampleRate: 16000
  352. },
  353. success: (res) => {
  354. // 调用成功
  355. console.log('驰声start成功', res)
  356. },
  357. fail: (res) => {
  358. // start失败,请关注res.errId, res.error
  359. console.log('驰声start失败', res)
  360. }
  361. })
  362. },
  363. getMicAuth() {
  364. let _this = this;
  365. wx.getSetting({
  366. success(res) {
  367. if (res.authSetting['scope.record']) {
  368. _this.videoComplete();
  369. } else {
  370. _this.getMicSetAuth();
  371. return;
  372. }
  373. }
  374. })
  375. },
  376. getMicSetAuth() {
  377. let _this = this;
  378. wx.authorize({
  379. scope: 'scope.record',
  380. success() {
  381. // 用户已经同意小程序使用录音功能,后续调用 wx.startRecord 接口不会弹窗询问
  382. _this.audioRecord();
  383. },
  384. fail() {
  385. wx.showModal({
  386. title: '录音前请打开麦克风权限',
  387. content: '',
  388. confirmText: '我知道了',
  389. showCancel: false,
  390. success(res) {
  391. // console.log('用户点击确定')
  392. wx.openSetting({
  393. success(res) {
  394. console.log('跳转到设置页', res.authSetting)
  395. // res.authSetting = {
  396. // "scope.userInfo": true,
  397. // "scope.userLocation": true
  398. // }
  399. }
  400. })
  401. }
  402. })
  403. }
  404. })
  405. },
  406. // 录音结束
  407. recordStop: function() {
  408. this.setData({
  409. muted: false
  410. })
  411. // this.ss.stopRecord();
  412. console.log('录音结束')
  413. this.recorderManager.stop();
  414. wx.hideLoading()
  415. },
  416. // 获取测评结果
  417. getRecordScore(res) {
  418. console.log('测评结果', JSON.stringify(res))
  419. const result = res.result;
  420. const overall = result.overall; // 总分
  421. const integrity = result.integrity; //完成度
  422. const tone = result.tone; // 语调声调
  423. // const accuracy = result.accuracy; // 准确度 发音分
  424. const accuracy = result.overall; // 准确度 发音分
  425. const fluency = result.fluency.overall; //流利度
  426. let starArray = [];
  427. let myOverall = integrity * 0.3 + accuracy * 0.5 + fluency * 0.1 + tone * 0.1;
  428. // let myOverall = 80;
  429. console.log('我的总分', myOverall)
  430. if (myOverall < 5) {
  431. starArray = [0, 0, 0, 0, 0]
  432. } else if (5 <= myOverall && myOverall < 30) {
  433. starArray = [1, 0, 0, 0, 0]
  434. } else if (30 <= myOverall && myOverall < 50) {
  435. starArray = [1, 1, 0, 0, 0]
  436. } else if (50 <= myOverall && myOverall < 70) {
  437. starArray = [1, 1, 1, 0, 0]
  438. } else if (70 <= myOverall && myOverall < 90) {
  439. starArray = [1, 1, 1, 1, 0]
  440. } else if (90 <= myOverall) {
  441. starArray = [1, 1, 1, 1, 1]
  442. }
  443. // let score = myOverall / 20;
  444. // if (score <= 0) {
  445. // starArray = [0, 0, 0, 0, 0]
  446. // } else {
  447. // for (let i = 1; i < 5; i += 0.9) {
  448. // if (i <= score) {
  449. // starArray.push(1);
  450. // } else {
  451. // starArray.push(0)
  452. // }
  453. // }
  454. // }
  455. this.setData({
  456. overall,
  457. integrity,
  458. tone,
  459. accuracy,
  460. fluency,
  461. ifScoreDialogShow: true,
  462. star: starArray
  463. })
  464. },
  465. closeScoreDialog() {
  466. this.setData({
  467. ifScoreDialogShow: false,
  468. ifScoreShow: true,
  469. ifTextShow: false
  470. })
  471. },
  472. // 播放录音
  473. audioPlay: function() {
  474. /* 用了先声智能以后录音播放由先声只能接管 */
  475. if (this.innerAudioContext) {
  476. this.innerAudioContext.stop();
  477. this.videoCtx.stop();
  478. }
  479. this.innerAudioContext = wx.createInnerAudioContext();
  480. this.innerAudioContext.src = this.data.recordSource; // 这里可以是录音的临时路径
  481. console.log('音频路径', this.data.recordSource)
  482. this.setData({
  483. videoUrl: this.data.videoUrl,
  484. muted: true
  485. })
  486. console.log(this.innerAudioContext.src);
  487. this.videoCtx.play();
  488. this.innerAudioContext.play();
  489. },
  490. exampleVideoPlay: function() {
  491. if (this.data.videoPathTemp == this.data.videoUrl) {
  492. // 播放记录增加一条
  493. httpRequestApi.playLogReport({
  494. userReadId: this.data.id,
  495. playStopTime: 1000
  496. }).success(res => {
  497. console.log('播放记录', res)
  498. })
  499. }
  500. httpRequestApi.userEvent('EXAMPLE');
  501. },
  502. videoComplete: function() {
  503. this.setData({
  504. recordFlag: 1,
  505. videoUrl: this.data.originVideo,
  506. centerBtn: false,
  507. playBtn: false,
  508. isVideoListShow: false,
  509. muted: false,
  510. ifTextShow: true,
  511. ifScoreShow: false
  512. }, () => {
  513. console.log('播放视频播放视频播放视频')
  514. // this.videoCtx.seek(1);
  515. this.videoCtx.stop();
  516. this.videoCtx.play();
  517. this.recordStart();
  518. })
  519. },
  520. // 上传
  521. upload: function() {
  522. if (this.videoCtx) {
  523. this.videoCtx.stop();
  524. }
  525. if (this.data.audioPlaying) {
  526. // this.ss.stopPlay();
  527. this.setData({
  528. audioPlaying: false
  529. })
  530. }
  531. wx.showLoading({
  532. title: '作品上传中',
  533. mask: true
  534. })
  535. const recordSource = this.data.recordSource;
  536. wx.uploadFile({
  537. url: 'https://reader-api.ai160.com//file/upload',
  538. filePath: recordSource,
  539. name: '朗读录音',
  540. header: {
  541. uid: wx.getStorageSync('uid')
  542. },
  543. success: (res) => {
  544. const formateRes = JSON.parse(res.data);
  545. let audioPath = formateRes.data;
  546. this.shareWorks(audioPath);
  547. }
  548. })
  549. },
  550. shareWorks: function(audio) {
  551. const data = {
  552. "lessonId": this.data.id,
  553. "originVideo": this.data.videoUrl,
  554. "audioPath": audio,
  555. "title": this.data.title,
  556. "iconImg": this.data.img,
  557. "summary": this.data.summary,
  558. "productId": this.data.productId,
  559. "grade": this.data.grade,
  560. "exampleId": this.data.exampleId,
  561. "coverImg": this.data.coverImg,
  562. "shareImg": this.data.shareImg
  563. };
  564. httpRequestApi.postWork(data).success(res => {
  565. wx.hideLoading({
  566. success: () => {
  567. wx.showToast({
  568. title: '作品已上传正在审核中',
  569. icon: 'none',
  570. duration: 1000,
  571. success: () => {
  572. console.log('上传成功', res);
  573. if (res.data.count > 0) {
  574. this.setData({
  575. flowerNum: res.data.count
  576. })
  577. }
  578. const _data = this.data;
  579. const scoreData = {
  580. "userReadId": res.data.data.id,
  581. "complete": _data.integrity,
  582. "accuracy": _data.accuracy,
  583. "speed": _data.fluency,
  584. "intonation": _data.tone,
  585. "score": _data.overall
  586. }
  587. // 上传评分
  588. httpRequestApi.postWorksScore(scoreData).success(res => {
  589. console.log(res)
  590. });
  591. const pages = getCurrentPages();
  592. const prevPage = pages[pages.length - 2];
  593. prevPage.setData({
  594. // workId: res.data.data.id, // 有id就塞到第一位
  595. fromReading: true
  596. }, () => {
  597. wx.redirectTo({
  598. url: '/pages/userWorks/index'
  599. })
  600. })
  601. }
  602. })
  603. }
  604. });
  605. }).fail(res => {
  606. wx.hideLoading({
  607. success: () => {
  608. wx.showToast({
  609. title: '上传超时',
  610. icon: 'fail',
  611. duration: 1000
  612. })
  613. }
  614. });
  615. })
  616. httpRequestApi.userEvent('UPLOAD');
  617. },
  618. // 获取本课朗读内容
  619. getReadInfo: function(id, pageNo, pageSize) {
  620. // const uid = wx.getStorageSync('uid');
  621. const data = {
  622. exampleId: this.data.id || id,
  623. pageNo: this.data.pageNo,
  624. pageSize: 3,
  625. type: 'READ'
  626. };
  627. httpRequestApi.getClassRead(data).success(res => {
  628. const readInfo = res.data.data.list;
  629. readInfo.forEach(item => {
  630. const temp = {};
  631. temp.title = item.userRead ? item.userRead.title : '';
  632. temp.img = item.userRead.iconImg;
  633. temp.plays = item.userRead.playAmount ? item.userRead.playAmount : 0;
  634. temp.likes = item.userRead.likeAmount ? item.userRead.likeAmount : 0;
  635. temp.shareAmount = item.userRead.shareAmount;
  636. temp.favoritesAmount = item.userRead.favoritesAmount;
  637. temp.commentAmount = item.userRead.commentAmount ? item.userRead.commentAmount : 0;
  638. temp.classId = item.userRead.exampleId;
  639. temp.time = formatDate(item.userRead.gmtCreated, 3);
  640. temp.avatar = item.user ? item.user.avatar : '';
  641. temp.uid = item.user ? item.user.uid : '';
  642. temp.url = item.userRead.videoPath;
  643. temp.isLike = item.isLike;
  644. // temp.avatar = item.user.avatar;
  645. temp.nickName = item.user ? item.user.nickName : '';
  646. temp.id = item.userRead.id;
  647. temp.noReading = true;
  648. temp.isFans = item.isFans ? true : item.user.uid === this.uid ? true : false;
  649. temp.coverImg = item.userRead.coverImg;
  650. temp.videoShow = false;
  651. temp.grade = item.userRead.grade;
  652. temp.type = item.userRead.type;
  653. temp.shareImg = item.userRead.shareImg;
  654. item.isActivity && (temp.activity = true);
  655. // recommendWorks.push(temp);
  656. // that.data.hotData.hotWorks.push(temp);
  657. this.data.videoList.push(temp);
  658. });
  659. this.setData({
  660. videoList: this.data.videoList,
  661. totalSize: res.data.data.totalSize,
  662. noMoreWork: readInfo.length <= 0 ? true : false
  663. })
  664. });
  665. },
  666. // 评论区点击
  667. commentTap: function(e) {
  668. console.log('点击评论区', e)
  669. if (e.target.dataset.type === 'blank') {
  670. if (this.data.commentShow && this.data.commentId) {
  671. httpRequestApi.getClassDetail(this.data.commentId).success(res => {
  672. console.log('评论回显', res.data.data.userRead.commentAmount)
  673. let str = `videoList[${this.data.commentIndex}].commentAmount`;
  674. this.setData({
  675. [str]: res.data.data.userRead.commentAmount
  676. })
  677. })
  678. }
  679. this.setData({
  680. commentShow: false
  681. })
  682. }
  683. },
  684. scrollToLower: function(e) {
  685. console.log('滑动到底部', e)
  686. this.setData({
  687. pageNo: this.data.pageNo + 1
  688. }, () => {
  689. this.getReadInfo()
  690. })
  691. },
  692. onReachBottom: function(e) {
  693. console.log('滑动到底部', e)
  694. this.setData({
  695. pageNo: this.data.pageNo + 1
  696. }, () => {
  697. this.getReadInfo()
  698. })
  699. },
  700. scrollToUpper: function(e) {
  701. console.log('滑动到顶部', e)
  702. },
  703. // 打开评论
  704. openComment: function(e) {
  705. console.log('id', e.detail.activeId)
  706. this.setData({
  707. commentShow: !this.data.commentShow,
  708. commentId: e.detail.activeId,
  709. commentIndex: e.detail.activeIndex
  710. });
  711. },
  712. goToReading: function(e) {
  713. this.setData({
  714. pageNo: 1,
  715. videoList: []
  716. })
  717. const id = e.detail.activeId ? e.detail.activeId : e.currentTarget.dataset.id;
  718. this.getClassInfo(id)
  719. },
  720. creatShare(video) {
  721. return new Promise((resolve, reject) => {
  722. let context = wx.createSelectorQuery();
  723. context
  724. .select('#share')
  725. .fields({
  726. node: true,
  727. size: true
  728. }).exec((res) => {
  729. const canvas = res[0].node;
  730. const ctx = canvas.getContext('2d');
  731. const dpr = wx.getSystemInfoSync().pixelRatio;
  732. canvas.width = res[0].width * dpr;
  733. canvas.height = res[0].height * dpr;
  734. ctx.scale(dpr, dpr);
  735. ctx.font = '14px PingFang';
  736. let pic = canvas.createImage();
  737. pic.src = video.coverImg; //可以是本地,也可以是网络图片
  738. pic.onload = () => {
  739. ctx.drawImage(pic, 0, 0, 375, 211);
  740. }
  741. let peiyin = canvas.createImage();
  742. peiyin.src = '/static/image/peiyin.jpg';
  743. peiyin.onload = () => {
  744. ctx.drawImage(peiyin, 0, 211, 375, 89);
  745. // 收藏,一个一个渲染
  746. let sc = canvas.createImage();
  747. sc.src = '/static/image/no_collect.png'
  748. sc.onload = () => {
  749. ctx.drawImage(sc, 12, 220, 20, 20)
  750. ctx.fillText('收藏', 36, 238)
  751. //分享
  752. let fx = canvas.createImage();
  753. fx.src = '/static/index/share.png'
  754. fx.onload = () => {
  755. ctx.drawImage(fx, 78, 220, 22, 22)
  756. ctx.fillText('分享', 104, 238)
  757. //点赞
  758. let dz = canvas.createImage();
  759. dz.src = video.isLike ? '/static/index/heart_colored.png' : '/static/index/heart.png'
  760. dz.onload = () => {
  761. ctx.drawImage(dz, 258, 222, 22, 22)
  762. ctx.fillText(video.likes, 284, 238)
  763. //评论
  764. let pl = canvas.createImage();
  765. pl.src = '/static/index/comment.png'
  766. pl.onload = () => {
  767. ctx.drawImage(pl, 318, 222, 22, 22)
  768. ctx.fillText(video.commentAmount, 340, 238)
  769. setTimeout(() => {
  770. wx.canvasToTempFilePath({
  771. canvas: canvas,
  772. success(res) {
  773. resolve({
  774. title: '请欣赏我的课文朗读作品,点赞+评论。',
  775. path: `/pages/index/index?readId=${video.id}&uid=${wx.getStorageSync('uid')}`,
  776. imageUrl: res.tempFilePath
  777. })
  778. },
  779. fail(res) {
  780. reject()
  781. }
  782. }, this)
  783. }, 500)
  784. }
  785. }
  786. }
  787. }
  788. }
  789. })
  790. })
  791. },
  792. /**
  793. * 用户点击右上角分享
  794. */
  795. onShareAppMessage({
  796. from,
  797. target
  798. }) {
  799. if (from == 'button') {
  800. let video = target.dataset.info
  801. const promise = new Promise(resolve => {
  802. this.creatShare(video).then(res => {
  803. resolve(res)
  804. })
  805. })
  806. console.log(video);
  807. return {
  808. title: '请欣赏我的课文朗读作品,点赞+评论。',
  809. path: `/pages/index/index?readId=${video.id}&uid=${wx.getStorageSync('uid')}`,
  810. imageUrl: video.coverImg,
  811. promise
  812. }
  813. } else {
  814. return {
  815. title: '课文朗读,从未如此有趣。',
  816. path: `/pages/index/index?&uid=${wx.getStorageSync('uid')}`,
  817. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/shareContent.png'
  818. }
  819. }
  820. },
  821. touchMove: function() {
  822. return
  823. },
  824. openShare: function(e) {
  825. console.log('用户点击分享按钮回调', e)
  826. this.setData({
  827. shareTitle: e.detail.currentTarget.dataset.title,
  828. shareId: e.detail.currentTarget.dataset.id,
  829. shareImg: e.detail.currentTarget.dataset.shareimg,
  830. ifTapActivity: e.detail.currentTarget.dataset.activity
  831. })
  832. },
  833. onPlay: function(e) {
  834. // 下边视频列表onplay
  835. console.log('视频播放视频播放')
  836. if (this.videoCtx) {
  837. this.videoCtx.stop();
  838. } else {
  839. this.videoCtx = wx.createVideoContext('myVideo', this);
  840. this.videoCtx.stop();
  841. }
  842. },
  843. collectTap: function(e) {
  844. const index = e.detail.index;
  845. let str = `videoList[${index}].isFavorite`;
  846. let str2 = `videoList[${index}].favoritesAmount`;
  847. let favoritesAmount = e.detail.isCollect ? this.data.videoList[index].favoritesAmount + 1 : this.data.videoList[index].favoritesAmount - 1
  848. this.setData({
  849. [str]: e.detail.isCollect,
  850. [str2]: favoritesAmount
  851. })
  852. },
  853. likeTap: function(e) {
  854. console.log('点赞', e)
  855. const index = e.detail.index;
  856. let likeStr = `videoList[${index}].isLike`;
  857. let likeNumStr = `videoList[${index}].likes`;
  858. this.setData({
  859. [likeStr]: true,
  860. [likeNumStr]: this.data.videoList[index].likes + 1
  861. })
  862. },
  863. // 获取焦点事件
  864. inputFocus(e) {
  865. this.setData({
  866. isScroll: false
  867. })
  868. this.triggerEvent('inputFocus');
  869. },
  870. // 失去焦点事件
  871. inputBlur(e) {
  872. this.setData({
  873. isScroll: true
  874. })
  875. this.triggerEvent('inputBlur');
  876. },
  877. addShareAmount: function(e) {
  878. console.log('+++++1', e)
  879. let str = `videoList[${e.detail.index}].shareAmount`;
  880. this.setData({
  881. [str]: this.data.videoList[e.detail.index].shareAmount + 1
  882. })
  883. },
  884. // 去鉴权
  885. goToAuth() {
  886. if (!wx.getStorageSync('message')) {
  887. wx.requestSubscribeMessage({
  888. tmplIds: ['KJ0YtcAacJNSXDBsE27JXqoaFrcJ1-N6Jcu85yTtQuY',
  889. '-2ZZpWFoyKvAtX1HwEIQLQ92LnN8cryamB94LqLGo98'
  890. ],
  891. success: (res) => {
  892. console.log(res)
  893. if (res['KJ0YtcAacJNSXDBsE27JXqoaFrcJ1-N6Jcu85yTtQuY'] === 'reject') {
  894. console.log('用户不同意订阅')
  895. wx.setStorageSync('message', false)
  896. } else if (res['KJ0YtcAacJNSXDBsE27JXqoaFrcJ1-N6Jcu85yTtQuY'] === 'accept') {
  897. console.log('订阅成功')
  898. wx.setStorageSync('message', true)
  899. }
  900. this.userAuth()
  901. },
  902. fail: () => {
  903. this.userAuth()
  904. }
  905. })
  906. } else {
  907. this.userAuth()
  908. }
  909. },
  910. userAuth() {
  911. if (this.data.isIOS) {
  912. wx.navigateTo({
  913. url: `../vipActivity/vipActivity`
  914. });
  915. } else {
  916. const productId = wx.getStorageSync('productId');
  917. httpRequestApi.createOrder(productId).success(res => {
  918. console.log(res.data.data.package);
  919. const timeStamp = res.data.data.timeStamp;
  920. const nonceStr = res.data.data.nonceStr;
  921. const packages = res.data.data.package;
  922. const paySign = res.data.data.sign;
  923. wx.requestPayment({
  924. timeStamp,
  925. nonceStr,
  926. package: packages,
  927. signType: 'MD5',
  928. paySign,
  929. success: (res) => {
  930. this.setData({
  931. authHide: !this.data.authHide
  932. })
  933. httpRequestApi.getAuthActivity().success(res => {
  934. if (!res.data.data) {
  935. wx.setStorageSync('vip', false)
  936. wx.setStorageSync('date', '')
  937. } else {
  938. wx.setStorageSync('vip', true)
  939. wx.setStorageSync('date', res.data.data)
  940. }
  941. }).fail(error => {
  942. console.log(error)
  943. })
  944. },
  945. fail(res) {}
  946. })
  947. })
  948. }
  949. this.hideAuth();
  950. },
  951. hideAuth() {
  952. this.setData({
  953. authHide: !this.data.authHide
  954. })
  955. },
  956. onShareTimeline: function() {
  957. return {
  958. title: '终于找到适合孩子的朗读神器了!动画配音,边玩边学!',
  959. query: `uid=${wx.getStorageSync('uid')}`,
  960. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/yuwen.jpg'
  961. }
  962. },
  963. })