problem_report.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. // pages/peoblem_report/peoblem_report.js
  2. import { routers, viewImage, sharePage, isFn, getGlobalVal, formatDateTime, formateNumber, formatDate } from '../../utils/util.js'
  3. import { FindProblemPictureDetail } from '../../utils/api.js'
  4. const { navigateTo, redirectTo, navigateBack } = routers()
  5. const { globalData } = getApp()
  6. const { baseImgUrl, thumbnail } = globalData
  7. const app = getApp()
  8. const { formatNumber } = require('../../utils/util.js')
  9. Page({
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. baseImgUrl,
  15. thumbnail,
  16. voiceKeyIndex:-1,
  17. imgNum:3,
  18. isPlay:0,
  19. times:0,
  20. isMusic:0,
  21. isEnd:0,
  22. voiceKeyList:[]
  23. },
  24. /**
  25. * 生命周期函数--监听页面加载
  26. */
  27. onLoad: function (options) {
  28. this.setData(options);
  29. this.getData();
  30. },
  31. /**
  32. * 生命周期函数--监听页面初次渲染完成
  33. */
  34. onReady: function () {
  35. },
  36. /**
  37. * 生命周期函数--监听页面显示
  38. */
  39. onShow: function () {
  40. },
  41. /**
  42. * 生命周期函数--监听页面隐藏
  43. */
  44. onHide: function () {
  45. let pages = getCurrentPages();
  46. const {isMusic,voiceKeyList, voiceKeyIndex}=this.data
  47. console.log( '生命周期函数--监听页面隐藏'+isMusic)
  48. // this.setData({isEnd:1})
  49. // this.innerAudioContext.destroy();
  50. if(isMusic){
  51. this.stopPlay()
  52. // this.innerAudioContext.onStop(() => {
  53. this.innerAudioContext.stop();
  54. clearInterval(this.setIntervalAudio)
  55. this.pauseAudio(voiceKeyList, voiceKeyIndex)
  56. // })
  57. }
  58. },
  59. /**
  60. * 生命周期函数--监听页面卸载
  61. */
  62. onUnload: function () {
  63. const {isMusic,voiceKeyList, voiceKeyIndex}=this.data
  64. console.log( '生命周期函数--监听页面卸载'+isMusic)
  65. // this.setData({isEnd:1})
  66. // this.innerAudioContext.destroy();
  67. if(isMusic){
  68. this.stopPlay()
  69. // this.innerAudioContext.onStop(() => {
  70. this.innerAudioContext.stop();
  71. clearInterval(this.setIntervalAudio)
  72. this.pauseAudio(voiceKeyList, voiceKeyIndex)
  73. // })
  74. }
  75. },
  76. /**
  77. * 页面相关事件处理函数--监听用户下拉动作
  78. */
  79. onPullDownRefresh: function () {
  80. },
  81. /**
  82. * 页面上拉触底事件的处理函数
  83. */
  84. onReachBottom: function () {
  85. },
  86. /**
  87. * 用户点击右上角分享
  88. */
  89. onShareAppMessage: function () {
  90. if (app.sharePageDefaultCtl) {
  91. return app.sharePageDefaultCtl()
  92. }
  93. },
  94. //查看图片
  95. viewImageCtl: app.viewImageCtl,
  96. // 获取难题详情
  97. // getPuzzleReport:function(){
  98. // const postData = { fn: this.getPuzzleReport, param: { ...arguments } }
  99. // const { problemPictureId, nodes, answerNodes } = this.data;
  100. // const fn = res => {
  101. // const { derverFileKey, problemFeedbackVideoPath, problemPictureId, problemPicturePath, recommendedQuestions, similarQuestions, teacherMessage} = res.data;
  102. // const correctListTemp = (res => {
  103. // for (let i in res) {
  104. // if (res[i].question.indexOf("img") != -1){
  105. // res[i].question = res[i].question.replace(/\<img/gi, '<img style="max-width:100%;height:auto" ')
  106. // }
  107. // }
  108. // return res
  109. // })(similarQuestions)
  110. // const listTemp = (res => {
  111. // for (let i in res) {
  112. // if (res[i].analysis.indexOf("img") != -1) {
  113. // res[i].analysis = res[i].analysis.replace(/\<img/gi, '<img style="max-width:100%;height:auto" ')
  114. // }
  115. // if (res[i].question.indexOf("img") != -1) {
  116. // res[i].question = res[i].question.replace(/\<img/gi, '<img style="max-width:100%;height:auto" ')
  117. // }
  118. // }
  119. // return res
  120. // })(recommendedQuestions)
  121. // this.setData({ derverFileKey, problemFeedbackVideoPath, problemPictureId, problemPicturePath, recommendedQuestions: listTemp, similarQuestions: correctListTemp, teacherMessage});
  122. // }
  123. // app.post('/api/parents/parents/V2/findProblemPictureDetail', { problemPictureId}, 0, postData).then(fn)
  124. // },
  125. /**
  126. * 获取数据
  127. */
  128. getData: function (cb) {
  129. const continuousFn = { fn: this.getData, param: { ...arguments } }
  130. const { problemPictureId } = this.data
  131. FindProblemPictureDetail({ data: { problemPictureId }, continuousFn }).then(res => {
  132. const { derverFileKey, problemFeedbackVideoPath, problemPictureId, problemPicturePath, recommendedQuestions, similarQuestions, teacherMessage,fileKeys,voiceKeyList } = res.data;
  133. const voiceListTemp = (res => {
  134. for (let i in res) {
  135. res[i].voiceType = 0
  136. res[i].durationNum = 0;
  137. res[i].progress = 0;
  138. res[i].isHands = false;
  139. // res[i].audioTotal = formatNumber(Math.floor(voiceList[i].duration / 60)) + ':' + formatNumber(Math.floor(voiceList[i].duration % 60));
  140. }
  141. return res
  142. })(voiceKeyList)
  143. const correctListTemp = (res => {
  144. for (let i in res) {
  145. if (res[i].question.indexOf("img") != -1) {
  146. res[i].question = res[i].question.replace(/\<img/gi, '<img style="max-width:100%;height:auto" ')
  147. }
  148. }
  149. return res
  150. })(similarQuestions)
  151. const listTemp = (res => {
  152. for (let i in res) {
  153. if (res[i].analysis.indexOf("img") != -1) {
  154. res[i].analysis = res[i].analysis.replace(/\<img/gi, '<img style="max-width:100%;height:auto" ')
  155. }
  156. if (res[i].question.indexOf("img") != -1) {
  157. res[i].question = res[i].question.replace(/\<img/gi, '<img style="max-width:100%;height:auto" ')
  158. }
  159. }
  160. return res
  161. })(recommendedQuestions)
  162. this.setData({ derverFileKey, problemFeedbackVideoPath, problemPictureId, problemPicturePath, recommendedQuestions: listTemp, similarQuestions: correctListTemp, teacherMessage,voiceKeyList:voiceListTemp,fileKeys });
  163. if (isFn(cb)) cb()
  164. }).catch(res => {
  165. this.setData({ isLoaded: true })
  166. if (isFn(cb)) cb()
  167. })
  168. },
  169. /**
  170. * 开始计时
  171. */
  172. startSetInter: function () {
  173. let time = 1000;
  174. var that = this;
  175. let { times,isEnd} = this.data;
  176. // console.log(times)
  177. var setTime = this.data.times + 1
  178. console.log(setTime)
  179. that.setData({ times: setTime,imgNum:setTime })
  180. that.data.setInter = setTimeout(this.startSetInter, time);
  181. if (setTime>2) {
  182. that.endSetInter();
  183. if(setTime==3&&!isEnd){
  184. setTimeout(
  185. function () {
  186. that.startSetInter()
  187. }, 1000)
  188. }
  189. }
  190. },
  191. /**
  192. * 结束计时
  193. */
  194. endSetInter:function(){
  195. var that = this;
  196. clearTimeout(that.data.setInter)
  197. that.setData({ times: 0})
  198. },
  199. /**
  200. * 结束计时
  201. */
  202. stopPlay:function(){
  203. var that = this;
  204. that.endSetInter();
  205. that.setData({ imgNum:3})
  206. console.log('结束')
  207. },
  208. /**
  209. * 音频控制按钮
  210. */
  211. audioCtl: function (e) {
  212. const { index } = e.currentTarget.dataset
  213. const { voiceKeyList, voiceKeyIndex} = this.data
  214. const { voiceType } = voiceKeyList[index];
  215. this.setData({isMusic:1})
  216. var that=this
  217. // console.log(index,voiceKeyIndex,voiceType)
  218. if (index == voiceKeyIndex) {
  219. if (voiceType == 1) {
  220. console.log('1---停止')
  221. that.stopPlay()
  222. if (this.innerAudioContext) this.innerAudioContext.stop()
  223. } else if (voiceType == 2) {
  224. console.log('1---播放')
  225. if (this.innerAudioContext) this.innerAudioContext.play()
  226. } else {
  227. this.audioManager()
  228. }
  229. } else {
  230. const temp = {}
  231. temp['voiceKeyIndex'] = index
  232. this.setData(temp)
  233. this.audioManager()
  234. }
  235. },
  236. /**
  237. * 开始播放语音
  238. */
  239. playAudio: function (voiceKeyList, voiceKeyIndex) {
  240. const { voiceKey, voiceType, durationNum, duration, progress } = voiceKeyList[voiceKeyIndex];
  241. console.log('开始播放语音')
  242. this.startSetInter()
  243. const temp = {}
  244. temp[`voiceKeyList[${voiceKeyIndex}].voiceType`] = 1;
  245. temp[`voiceKeyList[${voiceKeyIndex}].isHands`] = true;
  246. this.setData(temp)
  247. let d = voiceType == 2 ? durationNum - 1 : duration
  248. if (this.setIntervalAudio) {
  249. clearInterval(this.setIntervalAudio)
  250. }
  251. // console.log(d)
  252. this.setIntervalAudio = setInterval(() => {
  253. if (d > 0) {
  254. d--
  255. temp[`voiceKeyList[${voiceKeyIndex}].durationNum`] = d
  256. temp[`voiceKeyList[${voiceKeyIndex}].progress`] = Math.floor((duration - d) / duration * 100)
  257. this.setData(temp)
  258. } else {
  259. clearInterval(this.setIntervalAudio)
  260. }
  261. }, 1000)
  262. },
  263. /**
  264. * 暂停播放语音
  265. */
  266. pauseAudio: function (voiceKeyList, voiceKeyIndex) {
  267. console.log('2---停止')
  268. const {isEnd}=this.data
  269. this.setData({isEnd:!isEnd})
  270. const temp = {}
  271. temp[`voiceKeyList[${voiceKeyIndex}].voiceType`] = 2
  272. this.setData(temp)
  273. },
  274. /**
  275. * 结束播放语音
  276. */
  277. endAudio: function (voiceKeyList, voiceKeyIndex, listIndex) {
  278. console.log('3---停止')
  279. const {isEnd}=this.data
  280. this.setData({isEnd:!isEnd})
  281. const { voiceKey, voiceType, durationNum, duration, progress } = voiceKeyList[voiceKeyIndex]
  282. this.stopPlay()
  283. const temp = {}
  284. temp[`voiceKeyList[${voiceKeyIndex}].voiceType`] = 0
  285. temp[`voiceKeyList[${voiceKeyIndex}].duration`] = duration
  286. temp[`voiceKeyList[${voiceKeyIndex}].progress`] = 0
  287. temp[`voiceKeyList[${voiceKeyIndex}].isHands`] = false;
  288. this.setData(temp)
  289. },
  290. /**
  291. * 语音播放管理器
  292. */
  293. audioManager: function () {
  294. const { voiceKeyList, voiceKeyIndex, baseImgUrl } = this.data
  295. if (this.innerAudioContext) {
  296. this.innerAudioContext.destroy()
  297. clearInterval(this.setIntervalAudio)
  298. for (const i in voiceKeyList) {
  299. const temp = {};
  300. temp[`voiceKeyList[${i}].voiceType`] = 0;
  301. this.setData(temp)
  302. }
  303. }
  304. const { voiceKey } = voiceKeyList[voiceKeyIndex]
  305. if (!voiceKey) return
  306. this.innerAudioContext = wx.createInnerAudioContext()
  307. this.innerAudioContext.autoplay = true
  308. this.innerAudioContext.src = baseImgUrl+voiceKey
  309. console.log(baseImgUrl + voiceKey)
  310. this.innerAudioContext.onPlay(() => {
  311. this.playAudio(voiceKeyList, voiceKeyIndex)
  312. })
  313. this.innerAudioContext.onStop(() => {
  314. clearInterval(this.setIntervalAudio)
  315. this.pauseAudio(voiceKeyList, voiceKeyIndex)
  316. })
  317. this.innerAudioContext.onEnded(() => {
  318. this.innerAudioContext.destroy()
  319. clearInterval(this.setIntervalAudio)
  320. this.endAudio(voiceKeyList, voiceKeyIndex)
  321. })
  322. this.innerAudioContext.onError(res => {
  323. console.log(res)
  324. wx.showToast({
  325. title: '播放语音失败',
  326. icon: 'none'
  327. })
  328. })
  329. },
  330. /**
  331. * 结束语音
  332. */
  333. nextAudio: function () {
  334. const { voiceKeyList, voiceKeyIndex} = this.data;
  335. this.stopPlay()
  336. if (voiceKeyIndex != -1 && listIndex != -1) {
  337. const { voiceKey, voiceType, durationNum, duration, progress } = voiceKeyList[voiceKeyIndex]
  338. const temp = {}
  339. temp[`voiceKeyList[${voiceKeyIndex}].voiceType`] = 0
  340. temp[`voiceKeyList[${voiceKeyIndex}].duration`] = duration
  341. temp[`voiceKeyList[${voiceKeyIndex}].progress`] = 0
  342. if (this.innerAudioContext) this.innerAudioContext.pause()
  343. this.setData(temp)
  344. }
  345. }
  346. })