questionMixin.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* 问答社区 公共逻辑 */
  2. import {
  3. apiQuestionList
  4. } from '@/api/question'
  5. import { apiApplyPermission, apiUserInfo } from '@/api/user'
  6. const moment = require('@/utils/moment-with-locales.min')
  7. moment.locale('zh-cn');
  8. export default {
  9. data() {
  10. return {
  11. moment: moment,
  12. innerAudio: null, //该页面的音频
  13. currentAudioMsg: {
  14. id: '',
  15. audioCurrentTime: 0, //音频播放实时时间
  16. audioTime: 0, //当前音频时间
  17. audioCurrentUrl: '', //当前音频地址
  18. }, //当前正在播放音频的一些信息
  19. pupData: {
  20. show: false,
  21. content: '', //弹窗html字符串
  22. type: '',
  23. mobile: "",
  24. customer_info: {}
  25. },
  26. page: 1,
  27. pageSize: 20,
  28. finished: false,
  29. selectId: -1,
  30. }
  31. },
  32. onLoad() {
  33. this.initAudio()
  34. },
  35. onUnload() {
  36. this.destroyAudio()
  37. },
  38. methods: {
  39. //初始化audio
  40. initAudio() {
  41. this.innerAudio = uni.createInnerAudioContext()
  42. this.handleAudioFun()
  43. },
  44. //销毁audio
  45. destroyAudio() {
  46. if (this.innerAudio) {
  47. this.innerAudio.destroy()
  48. }
  49. },
  50. //audio事件
  51. handleAudioFun() {
  52. this.innerAudio.onPlay(() => {
  53. console.log('开始了')
  54. this.questionList.map(i => {
  55. if (i.id === this.currentAudioMsg.id) {
  56. i.loading = false
  57. }
  58. })
  59. })
  60. this.innerAudio.onTimeUpdate(() => {
  61. //console.log('时间更新')
  62. this.currentAudioMsg.audioCurrentTime = this.innerAudio.currentTime * 1000
  63. //console.log('duration',this.innerAudio.duration)
  64. })
  65. this.innerAudio.onPause(() => {
  66. console.log("暂停");
  67. })
  68. this.innerAudio.onEnded(() => {
  69. console.log('音频播放完毕')
  70. const {
  71. id
  72. } = this.currentAudioMsg
  73. this.questionList.map(i => {
  74. if (i.id === id) {
  75. i.answer.isplay = false
  76. i.answer.ispause = false
  77. }
  78. })
  79. this.changeCurrentAudio({
  80. id: '',
  81. answer: {
  82. source: '',
  83. audioTime: 0
  84. }
  85. })
  86. })
  87. },
  88. //播放音频
  89. handleAudioPlay() {
  90. this.innerAudio.onCanplay(() => {
  91. this.innerAudio.play()
  92. })
  93. },
  94. //切换当前播放音频
  95. changeCurrentAudio(item) {
  96. const {
  97. id
  98. } = item
  99. const {
  100. source,
  101. audioTime
  102. } = item.answer
  103. this.currentAudioMsg = {
  104. id: id,
  105. audioCurrentTime: 0 * 1000,
  106. audioTime: audioTime,
  107. audioCurrentUrl: source
  108. }
  109. this.questionList.map(i => {
  110. if (i.id === item.id) {
  111. i.loading = true
  112. }
  113. })
  114. },
  115. //获取问答列表
  116. async getQuestionList(status) {
  117. let questionData = []
  118. const res = await apiQuestionList({
  119. page_index: this.page,
  120. page_size: this.pageSize,
  121. chart_permission_id: this.selectId === -1 ? '' : this.selectId,
  122. reply_status: status
  123. })
  124. if (res.code === 200) {
  125. if (res.data) {
  126. questionData = res.data
  127. } else {
  128. this.finished = true
  129. }
  130. }
  131. let tempArr = []
  132. questionData.forEach(item => {
  133. let temp = item
  134. let audio_url = '',
  135. audio_play_seconds = 0;
  136. //问题状态为已回答,取audio_list第一项为音频
  137. if (status === 3) {
  138. audio_url = item.audio_list[0].audio_url
  139. audio_play_seconds = item.audio_list[0].audio_play_seconds
  140. }
  141. //问题状态不为已回答,取默认值
  142. temp.answer = {
  143. source: audio_url,
  144. audioTime: parseInt(audio_play_seconds) * 1000,
  145. isplay: false,
  146. ispause: false
  147. }
  148. temp.id = item.community_question_id
  149. temp.loading = false
  150. tempArr.push(temp)
  151. })
  152. if (this.page > 1) {
  153. this.questionList = this.questionList.concat(tempArr)
  154. } else {
  155. this.questionList = tempArr
  156. }
  157. },
  158. //点击某条音频
  159. handleAudio(item) {
  160. //如果没有权限,弹窗并return
  161. /* if (!item.auth_ok) {
  162. this.initPupData(item)
  163. return
  164. } */
  165. const {
  166. source,
  167. isplay
  168. } = item.answer
  169. if (isplay) {
  170. //说明是播放->暂停
  171. this.innerAudio.pause()
  172. } else if (item.id === this.currentAudioMsg.id) {
  173. //说明是暂停->播放
  174. this.innerAudio.play()
  175. } else {
  176. console.log('aaa', source, this.innerAudio.src)
  177. //说明是第一次播放或点击其他播放项
  178. this.changeCurrentAudio(item)
  179. this.innerAudio.stop()
  180. this.innerAudio.src = source
  181. /* this.innerAudio.play() */
  182. this.handleAudioPlay()
  183. }
  184. this.questionList.map((i) => {
  185. if (i.id === item.id) {
  186. if (i.answer.isplay) {
  187. i.answer.ispause = true
  188. }
  189. i.answer.isplay = !i.answer.isplay
  190. } else {
  191. i.answer.isplay = false
  192. i.answer.ispause = false
  193. i.loading = false
  194. }
  195. })
  196. },
  197. //初始化无权限弹窗
  198. initPupData(item) {
  199. let str = '<p>您暂无权限查看语音回复</p>'
  200. const {
  201. type,
  202. mobile,
  203. name,
  204. customer_info
  205. } = item.permission_info
  206. if (type === 'apply') {
  207. this.pupData.type = 'apply'
  208. str += '<p>若想查看可以申请开通</p>'
  209. }
  210. if (type === 'contact') {
  211. this.pupData.mobile = mobile + ''
  212. this.pupData.saleName = name
  213. this.pupData.type = 'contact'
  214. str += `<p>若想查看可以联系对口销售</p>`
  215. }
  216. this.pupData.customer_info = customer_info
  217. this.pupData.content = str
  218. this.pupData.show = true
  219. },
  220. //拨号
  221. handleCallPhone(tel) {
  222. uni.makePhoneCall({
  223. phoneNumber: tel ? tel : '123456',
  224. success: () => {
  225. this.pupData.show = false
  226. }
  227. });
  228. },
  229. //申请权限
  230. async handleApply() {
  231. if (this.pupData.customer_info.has_apply) { //已经申请过
  232. this.pupData.content = `<p>您已提交过申请,请耐心等待</p>`
  233. this.pupData.type = ''
  234. } else {
  235. if (!this.pupData.customer_info.status || this.pupData.customer_info.status != '流失') {
  236. uni.navigateTo({
  237. url: "/pages-applyPermission/applyPermission?source=5"
  238. })
  239. } else { //主动调一次申请权限接口
  240. const res = await apiApplyPermission({
  241. company_name: this.pupData.customer_info.company_name,
  242. real_name: this.pupData.customer_info.name,
  243. source: 2,
  244. from_page: '活动列表'
  245. })
  246. if (res.code === 200) {}
  247. this.pupData.content = `<p>申请已提交</p><p>请等待销售人员与您联系</p>`
  248. this.pupData.type = ''
  249. }
  250. }
  251. },
  252. }
  253. }