questionMixin.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. onShow(){
  36. this.initAudio()
  37. },
  38. onHide(){
  39. this.resetAudio()
  40. this.destroyAudio()
  41. },
  42. onUnload() {
  43. //this.destroyAudio()
  44. },
  45. methods: {
  46. //初始化audio
  47. initAudio() {
  48. this.innerAudio = uni.createInnerAudioContext()
  49. this.handleAudioFun()
  50. },
  51. //销毁audio
  52. destroyAudio() {
  53. if (this.innerAudio) {
  54. this.innerAudio.destroy()
  55. }
  56. },
  57. //重置音频播放信息
  58. resetAudio(){
  59. this.innerAudio.pause();
  60. this.questionList.map((i) => {
  61. if(i.id===this.currentAudioMsg.id){
  62. i.answer.isplay = false
  63. i.answer.ispause = false
  64. }
  65. })
  66. this.changeCurrentAudio({
  67. id: '',
  68. answer: {
  69. source: '',
  70. audioTime: 0
  71. }
  72. })
  73. },
  74. //audio事件
  75. handleAudioFun() {
  76. this.innerAudio.onPlay(() => {
  77. console.log('开始了')
  78. this.questionList.map(i => {
  79. if (i.id === this.currentAudioMsg.id) {
  80. i.loading = false
  81. }
  82. })
  83. })
  84. this.innerAudio.onTimeUpdate(() => {
  85. //console.log('时间更新')
  86. this.currentAudioMsg.audioCurrentTime = this.innerAudio.currentTime * 1000
  87. })
  88. this.innerAudio.onPause(() => {
  89. console.log("暂停");
  90. })
  91. this.innerAudio.onEnded(() => {
  92. console.log('音频播放完毕')
  93. const {
  94. id
  95. } = this.currentAudioMsg
  96. this.questionList.map(i => {
  97. if (i.id === id) {
  98. i.answer.isplay = false
  99. i.answer.ispause = false
  100. }
  101. })
  102. this.changeCurrentAudio({
  103. id: '',
  104. answer: {
  105. source: '',
  106. audioTime: 0
  107. }
  108. })
  109. })
  110. },
  111. //播放音频
  112. handleAudioPlay() {
  113. this.innerAudio.onCanplay(() => {
  114. this.innerAudio.play()
  115. })
  116. },
  117. //切换当前播放音频
  118. changeCurrentAudio(item) {
  119. const {
  120. id
  121. } = item
  122. const {
  123. source,
  124. audioTime
  125. } = item.answer
  126. this.currentAudioMsg = {
  127. id: id,
  128. audioCurrentTime: 0 * 1000,
  129. audioTime: audioTime,
  130. audioCurrentUrl: source
  131. }
  132. this.questionList.map(i => {
  133. if (i.id === item.id) {
  134. i.loading = true
  135. }
  136. })
  137. },
  138. //获取问答列表:status(问题状态) only_mine(只看我的)
  139. async getQuestionList(status,onlyMine=0) {
  140. let questionData = []
  141. const res = await apiQuestionList({
  142. page_index: this.page,
  143. page_size: this.pageSize,
  144. chart_permission_id: this.selectId === -1 ? '' : this.selectId,
  145. reply_status: status,
  146. only_mine:onlyMine
  147. })
  148. if (res.code === 200) {
  149. if (res.data) {
  150. questionData = res.data
  151. } else {
  152. this.finished = true
  153. }
  154. }
  155. let tempArr = []
  156. questionData.forEach(item => {
  157. let temp = item
  158. let audio_url = '',
  159. audio_play_seconds = 0;
  160. //问题状态为已回答,取audio_list第一项为音频
  161. if (item.reply_status === 3) {
  162. audio_url = item.audio_list[0].audio_url
  163. audio_play_seconds = item.audio_list[0].audio_play_seconds
  164. }
  165. //问题状态不为已回答,取默认值
  166. temp.answer = {
  167. source: audio_url,
  168. audioTime: parseInt(audio_play_seconds) * 1000,
  169. isplay: false,
  170. ispause: false
  171. }
  172. temp.id = item.community_question_id
  173. temp.loading = false
  174. tempArr.push(temp)
  175. })
  176. if (this.page > 1) {
  177. this.questionList = this.questionList.concat(tempArr)
  178. } else {
  179. this.questionList = tempArr
  180. }
  181. },
  182. //点击某条音频
  183. handleAudio(item) {
  184. //如果没有权限,弹窗并return
  185. if (!item.auth_ok) {
  186. this.initPupData(item)
  187. return
  188. }
  189. const {
  190. source,
  191. isplay
  192. } = item.answer
  193. if (isplay) {
  194. //说明是播放->暂停
  195. this.innerAudio.pause()
  196. } else if (item.id === this.currentAudioMsg.id) {
  197. //说明是暂停->播放
  198. this.innerAudio.play()
  199. } else {
  200. //console.log('aaa', source, this.innerAudio.src)
  201. //说明是第一次播放或点击其他播放项
  202. this.changeCurrentAudio(item)
  203. this.innerAudio.stop()
  204. this.innerAudio.src = source
  205. /* this.innerAudio.play() */
  206. this.handleAudioPlay()
  207. }
  208. this.questionList.map((i) => {
  209. if (i.id === item.id) {
  210. if (i.answer.isplay) {
  211. i.answer.ispause = true
  212. }
  213. i.answer.isplay = !i.answer.isplay
  214. } else {
  215. i.answer.isplay = false
  216. i.answer.ispause = false
  217. i.loading = false
  218. }
  219. })
  220. },
  221. //初始化无权限弹窗
  222. initPupData(item) {
  223. let str = '<p>您暂无权限查看语音回复</p>'
  224. const {
  225. type,
  226. mobile,
  227. name,
  228. customer_info
  229. } = item.permission_info
  230. if (type === 'apply') {
  231. this.pupData.type = 'apply'
  232. str += '<p>若想查看可以申请开通</p>'
  233. }
  234. if (type === 'contact') {
  235. this.pupData.mobile = mobile + ''
  236. this.pupData.saleName = name
  237. this.pupData.type = 'contact'
  238. str += `<p>若想查看可以联系对口销售</p>`
  239. }
  240. this.pupData.customer_info = customer_info
  241. this.pupData.content = str
  242. this.pupData.show = true
  243. },
  244. //拨号
  245. handleCallPhone(tel) {
  246. uni.makePhoneCall({
  247. phoneNumber: tel ? tel : '123456',
  248. success: () => {
  249. this.pupData.show = false
  250. }
  251. });
  252. },
  253. //申请权限
  254. async handleApply() {
  255. if (this.pupData.customer_info.has_apply) { //已经申请过
  256. this.pupData.content = `<p>您已提交过申请,请耐心等待</p>`
  257. this.pupData.type = ''
  258. } else {
  259. if (!this.pupData.customer_info.status || this.pupData.customer_info.status != '流失') {
  260. uni.navigateTo({
  261. url: "/pages-applyPermission/applyPermission?source=5&form_page=问答社区"
  262. })
  263. } else { //主动调一次申请权限接口
  264. const res = await apiApplyPermission({
  265. company_name: this.pupData.customer_info.company_name,
  266. real_name: this.pupData.customer_info.name,
  267. source: 5,
  268. from_page: '问答社区'
  269. })
  270. if (res.code === 200) {
  271. //重新获取页面数据
  272. const pages = getCurrentPages();
  273. const page = pages[pages.length - 1];
  274. /* page.onLoad(); */
  275. page.onShow();
  276. }
  277. this.pupData.content = `<p>申请已提交</p><p>请等待销售人员与您联系</p>`
  278. this.pupData.type = ''
  279. }
  280. }
  281. },
  282. }
  283. }