problem_report.js 9.9 KB

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