reading.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. import httpRequestApi from '../../utils/APIClient';
  2. import {
  3. formatDate
  4. } from '../../utils/util';
  5. Page({
  6. data: {
  7. title: '',
  8. id: '',
  9. img: '',
  10. fullScreenBtn: false,
  11. playBtn: true,
  12. gesture: true,
  13. muted: false,
  14. gesture: false,
  15. centerBtn: true,
  16. recordFlag: 0,
  17. recordSource: '',
  18. videoCtr: 'recordingVideoEnd',
  19. btnFlag: false,
  20. btnImgFlag: false,
  21. microphonePng: '../../static/image/microphone.png',
  22. recordingGif: '../../static/image/readingNow.gif',
  23. videoUrl: '',
  24. readingText: '',
  25. videoList: [],
  26. pageNo: 1,
  27. nextMargin: getApp().globalData.nextMargin,
  28. lowerThresHold: 100,
  29. isVideoListShow: true,
  30. overall: '', // 评测总分
  31. integrity: '', //完成度
  32. tone: '', //语调声调
  33. fluency: '', //流利度
  34. accuracy: '', // 正确分,发音分
  35. star: [0, 0, 0, 0, 0],
  36. ifTextShow: true,
  37. ifScoreDialogShow: false,
  38. ifScoreShow: false
  39. },
  40. onLoad: function (option) {
  41. console.log(option);
  42. this.videoCtx = null;
  43. const uid = wx.getStorageSync('uid')
  44. this.getClassInfo(option.id)
  45. },
  46. getClassInfo: function (id) {
  47. httpRequestApi.getClassDetail(id).success(res => {
  48. console.log('课程信息', res)
  49. let reg = /\\n/g
  50. this.setData({
  51. title: res.data.data.userRead.title,
  52. videoUrl: res.data.data.userRead.videoPath,
  53. originVideo: res.data.data.userRead.originVideo,
  54. img: res.data.data.userRead.iconImg,
  55. id: res.data.data.userRead.id,
  56. readingText: res.data.data.userRead.lessonText,
  57. grade: res.data.data.userRead.grade,
  58. exampleId: res.data.data.userRead.exampleId,
  59. summary: res.data.data.userRead.summary
  60. })
  61. this.getReadInfo(id)
  62. httpRequestApi.userIntoPage('pages/reading/reading', '朗读页面').success((res) => {
  63. })
  64. })
  65. },
  66. onHide: function () {
  67. console.log('onhide')
  68. if (this.data.btnImgFlag) {
  69. this.ss.stopRecord();
  70. }
  71. this.ss.destroyEngine();
  72. this.setData({
  73. recordFlag: 0
  74. })
  75. },
  76. onUnload: function () {
  77. console.log('onUnload')
  78. if (this.data.btnImgFlag) {
  79. this.ss.stopRecord();
  80. }
  81. this.ss.destroyEngine();
  82. },
  83. onShow: function () {
  84. this.setData({
  85. statusbarobj: {
  86. isshowbtn: false, //是否显示按钮
  87. title: "小学语文课文朗读", //标题
  88. },
  89. })
  90. this.videoCtx = wx.createVideoContext('myVideo', this);
  91. let data = requirePlugin("myPlugin");
  92. const obj = {
  93. appid: 'a415',
  94. userid: wx.getStorageSync('uid'),
  95. getEvalMessage: (res) => {
  96. this.getRecordScore(res)
  97. // console.log('评测结果',res)
  98. },
  99. recorderCallback: (type, data) => {
  100. // 录音测评监控回调
  101. this.ssRecorderCallback(type, data)
  102. }
  103. }
  104. this.ss = new data.ssEngine(obj);
  105. },
  106. ssRecorderCallback: function (type, data) {
  107. console.log('录音测评监控回调', type)
  108. console.log('录音测评监控回调', data)
  109. /* 录音开始 */
  110. if (type === 'onStart') {
  111. this.setData({
  112. btnImgFlag: true,
  113. btnFlag: false
  114. })
  115. console.log('recorder start')
  116. }
  117. if (type === 'onStop') {
  118. this.videoCtx.stop();
  119. this.setData({
  120. recordFlag: 0,
  121. recordSource: data.tempFilePath,
  122. btnFlag: true,
  123. btnImgFlag: false
  124. })
  125. console.log('recorder start')
  126. }
  127. },
  128. // 录音中视频播放结束 (控制录音同时结束)
  129. recordingVideoEnd: function () {
  130. console.log(this.data.videoCtr)
  131. console.log('recordingVideoEnd');
  132. //
  133. if (this.data.recordFlag === 0) {
  134. // this.recordStop();
  135. this.playingVideoEnd();
  136. return;
  137. }
  138. // 录音结束
  139. if (this.data.recordFlag === 1) {
  140. this.recordStop();
  141. }
  142. },
  143. // 播放中视频播放结束 (控制录音同时结束)
  144. playingVideoEnd: function () {
  145. console.log('playingVideoEnd')
  146. this.ss.stopPlay();;
  147. },
  148. /***
  149. * recordFlag:
  150. * 0 初始状态
  151. * 1 录音中
  152. * 2 录音结束
  153. ***/
  154. audioRecord: function () {
  155. /* wx.getSetting({
  156. success(res) {
  157. if (res.authSetting['scope.record']) {
  158. that.videoComplete();
  159. }else {
  160. wx.showToast({
  161. title: 'onError',
  162. icon: 'fail',
  163. duration: 500
  164. })
  165. }
  166. }
  167. }) */
  168. console.log('recordFlag', this.data.recordFlag)
  169. if (this.data.recordFlag === 0) {
  170. // this.recordStart();
  171. // this.saveVideo();
  172. console.log('是否有视频', this.videoCtx)
  173. if (this.videoCtx) {
  174. this.videoCtx.stop();
  175. this.videoCtx.seek(0);
  176. }
  177. // if (this.recorderManager) {
  178. // this.recorderManager.stop();
  179. // }
  180. // if (this.innerAudioContext) {
  181. // this.innerAudioContext.stop();
  182. // }
  183. const that = this;
  184. that.videoComplete();
  185. // wx.getSetting({
  186. // success(res) {
  187. // if (res.authSetting['scope.record']) {
  188. // that.videoComplete();
  189. // }
  190. // }
  191. // })
  192. return;
  193. }
  194. // 录音结束后
  195. if (this.data.recordFlag === 1) {
  196. wx.showLoading({
  197. title: '作品转码中',
  198. mask: true
  199. })
  200. this.recordStop();
  201. }
  202. },
  203. // 录音开始
  204. /**
  205. * duration: 时长 最长10分钟
  206. sampleRate: 44100, 采样率
  207. numberOfChannels: 1, 录音通道
  208. encodeBitRate: 192000, 码率
  209. format: 'mp3', 格式
  210. frameSize: 50 制定帧大小
  211. */
  212. recordStart: function () {
  213. console.log('录音开始');
  214. const options = {
  215. duration: 600000,
  216. sampleRate: 44100,
  217. numberOfChannels: 1,
  218. encodeBitRate: 192000,
  219. format: 'mp3',
  220. frameSize: 50
  221. }
  222. this.ss.startRecord({
  223. coreType: 'cn.sent.score', //测评题型
  224. evalTime: 300000, //测评的录音时间,时间到自动停止测评
  225. refText: this.data.readingText, //测评文本
  226. warrantId: 'c11163aa6c834a028da4a4b30955be96', //鉴权id
  227. })
  228. },
  229. // 录音结束
  230. recordStop: function () {
  231. this.setData({
  232. muted: false
  233. })
  234. this.ss.stopRecord();
  235. console.log('录音结束')
  236. wx.hideLoading()
  237. },
  238. // 获取测评结果
  239. getRecordScore(res) {
  240. console.log('测评结果', res)
  241. const result = res.result;
  242. const overall = result.overall; // 总分
  243. const integrity = result.integrity; //完成度
  244. const tone = result.tone; // 语调声调
  245. const accuracy = result.accuracy; // 发音分
  246. const fluency = result.fluency.overall; //流利度
  247. let starArray = [];
  248. let score = overall / 20;
  249. if (score <= 0) {
  250. starArray = [0, 0, 0, 0, 0]
  251. } else {
  252. for (let i = 1; i < 5; i += 0.9) {
  253. if (i < score) {
  254. starArray.push(1);
  255. } else {
  256. starArray.push(0)
  257. }
  258. }
  259. }
  260. this.setData({
  261. overall,
  262. integrity,
  263. tone,
  264. accuracy,
  265. fluency,
  266. ifScoreDialogShow: true,
  267. star: starArray
  268. })
  269. },
  270. closeScoreDialog() {
  271. this.setData({
  272. ifScoreDialogShow: false,
  273. ifScoreShow: true,
  274. ifTextShow: false
  275. })
  276. },
  277. // 播放录音
  278. audioPlay: function () {
  279. /* 用了先声智能以后录音播放由先声只能接管 */
  280. this.setData({
  281. videoUrl: this.data.videoUrl
  282. })
  283. this.videoCtx.seek(0)
  284. const recordSource = this.data.recordSource
  285. wx.uploadFile({
  286. url: 'https://reader-test.efunbox.cn/readerBase/file/upload',
  287. filePath: recordSource,
  288. name: '朗读录音',
  289. header: {
  290. uid: wx.getStorageSync('uid')
  291. },
  292. success: (res) => {
  293. const formateRes = JSON.parse(res.data);
  294. let audioPath = formateRes.data;
  295. this.setData({
  296. audioPath,
  297. audioPlaying: true
  298. })
  299. this.ss.startPlay(audioPath);
  300. this.videoCtx.play();
  301. }
  302. })
  303. },
  304. videoComplete: function () {
  305. // let videoUrl = 'http://efunimgs.ai160.com/ott/test/002tPr2Xlx07oP7B4ro40104120022hP0k010.mp4';
  306. this.setData({
  307. recordFlag: 1,
  308. videoUrl: this.data.originVideo,
  309. centerBtn: false,
  310. playBtn: false,
  311. isVideoListShow: false,
  312. muted: true
  313. }, () => {
  314. this.videoCtx.play();
  315. this.recordStart();
  316. })
  317. },
  318. // 上传
  319. upload: function () {
  320. if (this.videoCtx) {
  321. this.videoCtx.stop();
  322. }
  323. if (this.data.audioPlaying) {
  324. this.ss.stopPlay();
  325. this.setData({
  326. audioPlaying: false
  327. })
  328. }
  329. wx.showLoading({
  330. title: '作品分享中',
  331. mask: true
  332. })
  333. if (this.data.audioPath) {
  334. this.shareWorks(this.data.audioPath)
  335. } else {
  336. const recordSource = this.data.recordSource;
  337. wx.uploadFile({
  338. url: 'https://reader-test.efunbox.cn/readerBase/file/upload',
  339. filePath: recordSource,
  340. name: '朗读录音',
  341. header: {
  342. uid: wx.getStorageSync('uid')
  343. },
  344. success: (res) => {
  345. const formateRes = JSON.parse(res.data);
  346. let audioPath = formateRes.data;
  347. this.shareWorks(audioPath);
  348. }
  349. })
  350. }
  351. },
  352. shareWorks: function (audio) {
  353. const data = {
  354. "lessonId": this.data.id,
  355. "originVideo": this.data.videoUrl,
  356. "audioPath": audio,
  357. "title": this.data.title,
  358. "iconImg": this.data.img,
  359. "summary": this.data.summary,
  360. "productId": this.data.productId,
  361. "grade": this.data.grade,
  362. "exampleId": this.data.exampleId
  363. };
  364. httpRequestApi.postWork(data).success(res => {
  365. wx.hideLoading({
  366. success: () => {
  367. wx.showToast({
  368. title: '上传成功',
  369. icon: 'success',
  370. duration: 1000,
  371. success: () => {
  372. console.log('上传成功', res);
  373. const _data = this.data;
  374. const scoreData = {
  375. "userReadId": res.data.data.id,
  376. "complete": _data.integrity,
  377. "accuracy": _data.accuracy,
  378. "speed": _data.fluency,
  379. "intonation": _data.tone,
  380. "score": _data.overall
  381. }
  382. // 上传评分
  383. httpRequestApi.postWorksScore(scoreData).success(res => {
  384. console.log(res)
  385. });
  386. const pages = getCurrentPages();
  387. const prevPage = pages[pages.length - 2];
  388. console.log('上一个页面', prevPage);
  389. console.log('上一个页面', res.data.data.id);
  390. prevPage.setData({
  391. workId: res.data.data.id, // 有id就塞到第一位
  392. }, () => {
  393. console.log('上一个页面', prevPage);
  394. wx.navigateBack({
  395. delta: 1
  396. })
  397. })
  398. }
  399. })
  400. }
  401. });
  402. });
  403. },
  404. // 获取本课朗读内容
  405. getReadInfo: function (id, pageNo, pageSize) {
  406. // const uid = wx.getStorageSync('uid');
  407. const data = {
  408. exampleId: this.data.id || id,
  409. pageNo: this.data.pageNo,
  410. pageSize: 3,
  411. type: 'READ'
  412. };
  413. httpRequestApi.getClassRead(data).success(res => {
  414. const readInfo = res.data.data.list;
  415. console.log(res)
  416. readInfo.forEach(item => {
  417. const temp = {};
  418. temp.title = item.userRead ? item.userRead.title : '';
  419. temp.img = item.userRead.iconImg;
  420. temp.plays = item.userRead.playAmount ? item.userRead.playAmount : 0;
  421. temp.likes = item.userRead.likeAmount ? item.userRead.likeAmount : 0;
  422. temp.classId = item.userRead.exampleId;
  423. temp.time = formatDate(item.userRead.gmtCreated, 3);
  424. temp.avatar = item.user ? item.user.avatar : '';
  425. temp.uid = item.user ? item.user.uid : '';
  426. temp.url = item.userRead.originVideo;
  427. // temp.avatar = item.user.avatar;
  428. temp.nickName = item.user ? item.user.wechatName : '';
  429. temp.id = item.userRead.id;
  430. item.noReading = true;
  431. // recommendWorks.push(temp);
  432. // that.data.hotData.hotWorks.push(temp);
  433. this.data.videoList.push(temp);
  434. });
  435. this.setData({
  436. videoList: this.data.videoList
  437. })
  438. });
  439. },
  440. // 评论区点击
  441. commentTap: function (e) {
  442. console.log('点击评论区', e)
  443. if (e.target.dataset.type === 'blank') {
  444. this.setData({
  445. commentShow: false
  446. })
  447. }
  448. },
  449. scrollToLower: function (e) {
  450. console.log('滑动到底部', e)
  451. this.setData({
  452. pageNo: this.data.pageNo + 1
  453. }, () => {
  454. this.getReadInfo()
  455. })
  456. },
  457. scrollToUpper: function (e) {
  458. console.log('滑动到顶部', e)
  459. },
  460. // 打开评论
  461. openComment: function (e) {
  462. console.log('id', e.detail.activeId)
  463. this.setData({
  464. commentShow: !this.data.commentShow,
  465. commentId: e.detail.activeId,
  466. });
  467. },
  468. goToReading: function(e){
  469. this.setData({
  470. pageNo: 1,
  471. videoList:[]
  472. })
  473. const id = e.detail.activeId ? e.detail.activeId : e.currentTarget.dataset.id;
  474. this.getClassInfo(id)
  475. }
  476. })