questionMixin.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /* 问答社区 公共逻辑 */
  2. import {
  3. apiQuestionList
  4. } from '@/api/question'
  5. import { apiApplyPermission, apiUserInfo } from '@/api/user'
  6. import {apiCountAudioClick} from '@/api/question'
  7. const moment = require('@/utils/moment-with-locales.min')
  8. moment.locale('zh-cn');
  9. export default {
  10. data() {
  11. return {
  12. moment: moment,
  13. innerAudio: null, //该页面的音频
  14. currentAudioMsg: {
  15. id: '',
  16. audioCurrentTime: 0, //音频播放实时时间
  17. audioTime: 0, //当前音频时间
  18. audioCurrentUrl: '', //当前音频地址
  19. }, //当前正在播放音频的一些信息
  20. pupData: {
  21. show: false,
  22. content: '', //弹窗html字符串
  23. type: '',
  24. mobile: "",
  25. customer_info: {}
  26. },
  27. page: 1,
  28. pageSize: 20,
  29. finished: false,
  30. selectId: -1,
  31. noAuth:['潜在','流失','冻结'],
  32. hasAuth:true,//是否有该页面权限
  33. noAuthInfo:null,/* {
  34. name:'梁娜',
  35. mobile:123456,
  36. type:'apply',//apply||contact||''
  37. customer_info:{
  38. company_name:'',
  39. name:'客户a',
  40. mobile:'',
  41. status:'冻结',
  42. is_suspend:0,
  43. has_apply:false
  44. }
  45. }, *///无权限用户信息
  46. }
  47. },
  48. computed:{
  49. userAuth(){
  50. //暂停试用
  51. if(this.userInfo.status==='试用'&&this.userInfo.is_suspend===1){
  52. return false
  53. }
  54. //潜在流失冻结
  55. if(this.noAuth.includes(this.userInfo.status)){
  56. return false
  57. }
  58. //有权限的
  59. return true
  60. },
  61. isUserResearcher(){
  62. //内部人员+研究员
  63. if(this.userInfo.is_inner===1&&this.userInfo.is_researcher===1){
  64. return true
  65. }
  66. return false
  67. }
  68. },
  69. onLoad() {
  70. //this.initAudio()
  71. },
  72. onShow(){
  73. this.initAudio()
  74. },
  75. onHide(){
  76. this.resetAudio()
  77. this.destroyAudio()
  78. },
  79. onUnload() {
  80. //this.destroyAudio()
  81. },
  82. methods: {
  83. //初始化audio
  84. initAudio() {
  85. this.innerAudio = uni.createInnerAudioContext()
  86. this.handleAudioFun()
  87. },
  88. //销毁audio
  89. destroyAudio() {
  90. if (this.innerAudio) {
  91. this.innerAudio.destroy()
  92. }
  93. },
  94. //重置音频播放信息
  95. resetAudio(){
  96. this.innerAudio.pause();
  97. this.questionList.map((i) => {
  98. if(i.id===this.currentAudioMsg.id){
  99. i.answer.isplay = false
  100. i.answer.ispause = false
  101. }
  102. })
  103. this.changeCurrentAudio({
  104. id: '',
  105. answer: {
  106. source: '',
  107. audioTime: 0
  108. }
  109. })
  110. },
  111. //audio事件
  112. handleAudioFun() {
  113. this.innerAudio.onPlay(() => {
  114. console.log('开始了')
  115. this.questionList.map(i => {
  116. if (i.id === this.currentAudioMsg.id) {
  117. i.loading = false
  118. }
  119. })
  120. })
  121. this.innerAudio.onTimeUpdate(() => {
  122. //console.log('时间更新')
  123. this.currentAudioMsg.audioCurrentTime = this.innerAudio.currentTime * 1000
  124. })
  125. this.innerAudio.onPause(() => {
  126. console.log("暂停");
  127. })
  128. this.innerAudio.onEnded(() => {
  129. console.log('音频播放完毕')
  130. const {
  131. id
  132. } = this.currentAudioMsg
  133. this.questionList.map(i => {
  134. if (i.id === id) {
  135. i.answer.isplay = false
  136. i.answer.ispause = false
  137. }
  138. })
  139. this.changeCurrentAudio({
  140. id: '',
  141. answer: {
  142. source: '',
  143. audioTime: 0
  144. }
  145. })
  146. })
  147. },
  148. //播放音频
  149. handleAudioPlay() {
  150. this.innerAudio.onCanplay(() => {
  151. this.innerAudio.play()
  152. })
  153. },
  154. //切换当前播放音频
  155. changeCurrentAudio(item) {
  156. const {
  157. id
  158. } = item
  159. const {
  160. source,
  161. audioTime
  162. } = item.answer
  163. this.currentAudioMsg = {
  164. id: id,
  165. audioCurrentTime: 0 * 1000,
  166. audioTime: audioTime,
  167. audioCurrentUrl: source
  168. }
  169. this.questionList.map(i => {
  170. if (i.id === item.id) {
  171. i.loading = true
  172. }
  173. })
  174. },
  175. //获取问答列表:status(问题状态) only_mine(只看我的)
  176. async getQuestionList(status,onlyMine=0) {
  177. let questionData = []
  178. const res = await apiQuestionList({
  179. page_index: this.page,
  180. page_size: this.pageSize,
  181. /* chart_permission_id: this.selectId === -1 ? '' : this.selectId, */
  182. group_id: this.selectId === -1 ? '' : this.selectId,
  183. reply_status: status,
  184. only_mine:onlyMine
  185. })
  186. if (res.code === 200) {
  187. if (res.data) {
  188. questionData = res.data
  189. } else {
  190. this.finished = true
  191. }
  192. }
  193. //没权限
  194. if(res.code ===403){
  195. this.hasAuth = false
  196. this.noAuthInfo = res.data
  197. }
  198. let tempArr = []
  199. questionData.forEach(item => {
  200. let temp = item
  201. let audio_url = '',
  202. audio_play_seconds = 0;
  203. //问题状态为已回答,取audio_list第一项为音频
  204. if (item.reply_status === 3) {
  205. audio_url = item.audio_list[0].audio_url
  206. audio_play_seconds = item.audio_list[0].audio_play_seconds
  207. }
  208. //问题状态不为已回答,取默认值
  209. temp.answer = {
  210. source: audio_url,
  211. audioTime: parseInt(audio_play_seconds) * 1000,
  212. isplay: false,
  213. ispause: false
  214. }
  215. temp.id = item.community_question_id
  216. temp.loading = false
  217. tempArr.push(temp)
  218. })
  219. if (this.page > 1) {
  220. this.questionList = this.questionList.concat(tempArr)
  221. } else {
  222. this.questionList = tempArr
  223. }
  224. },
  225. //点击某条音频
  226. handleAudio(item) {
  227. //如果没有权限,弹窗并return
  228. /* if (!item.auth_ok) {
  229. this.initPupData(item)
  230. return
  231. } */
  232. const {
  233. source,
  234. isplay
  235. } = item.answer
  236. if (isplay) {
  237. //说明是播放->暂停
  238. this.innerAudio.pause()
  239. } else if (item.id === this.currentAudioMsg.id) {
  240. //说明是暂停->播放
  241. this.innerAudio.play()
  242. } else {
  243. //console.log('aaa', source, this.innerAudio.src)
  244. if(!source) return
  245. //说明是第一次播放或点击其他播放项
  246. this.changeCurrentAudio(item)
  247. this.innerAudio.stop()
  248. this.innerAudio.src = source
  249. /* this.innerAudio.play() */
  250. this.handleAudioPlay()
  251. //音频点击次数+1
  252. const audio_id = item.audio_list[0].community_question_audio_id
  253. apiCountAudioClick({
  254. community_question_audio_id:audio_id,
  255. source_agent:1
  256. }).then((res)=>{
  257. if(res.code===200){
  258. console.log('音频id为'+audio_id+'点击次数+1')
  259. }
  260. })
  261. }
  262. this.questionList.map((i) => {
  263. if (i.id === item.id) {
  264. if (i.answer.isplay) {
  265. i.answer.ispause = true
  266. }
  267. i.answer.isplay = !i.answer.isplay
  268. } else {
  269. i.answer.isplay = false
  270. i.answer.ispause = false
  271. i.loading = false
  272. }
  273. })
  274. },
  275. //初始化无权限弹窗
  276. initPupData(item) {
  277. let str = '<p>您暂无权限查看语音回复</p>'
  278. const {
  279. type,
  280. mobile,
  281. name,
  282. customer_info
  283. } = item.permission_info
  284. if (type === 'apply') {
  285. this.pupData.type = 'apply'
  286. str += '<p>若想查看可以申请开通</p>'
  287. }
  288. if (type === 'contact') {
  289. this.pupData.mobile = mobile + ''
  290. this.pupData.saleName = name
  291. this.pupData.type = 'contact'
  292. str += `<p>若想查看可以联系对口销售</p>`
  293. }
  294. this.pupData.customer_info = customer_info
  295. this.pupData.content = str
  296. this.pupData.show = true
  297. },
  298. //拨号
  299. handleCallPhone(tel) {
  300. uni.makePhoneCall({
  301. phoneNumber: tel ? tel : ' ',
  302. success: () => {
  303. this.pupData.show = false
  304. }
  305. });
  306. },
  307. //弹窗申请权限
  308. async handleApply() {
  309. if (this.pupData.customer_info.has_apply) { //已经申请过
  310. this.pupData.content = `<p>您已提交过申请,请耐心等待</p>`
  311. this.pupData.type = ''
  312. } else {
  313. if (!this.pupData.customer_info.status || this.pupData.customer_info.status != '流失') {
  314. uni.navigateTo({
  315. url: "/pages-applyPermission/applyPermission?source=5&form_page=问答社区"
  316. })
  317. } else { //主动调一次申请权限接口
  318. const res = await apiApplyPermission({
  319. company_name: this.pupData.customer_info.company_name,
  320. real_name: this.pupData.customer_info.name,
  321. source: 5,
  322. from_page: '问答社区'
  323. })
  324. if (res.code === 200) {
  325. //重新获取页面数据
  326. const pages = getCurrentPages();
  327. const page = pages[pages.length - 1];
  328. /* page.onLoad(); */
  329. page.onShow();
  330. }
  331. this.pupData.content = `<p>申请已提交</p><p>请等待销售人员与您联系</p>`
  332. this.pupData.type = ''
  333. }
  334. }
  335. },
  336. //申请权限
  337. async handleGoApply(){
  338. const {customer_info} = this.noAuthInfo
  339. if (customer_info.has_apply) { //已经申请过
  340. uni.showToast({
  341. title:'您已提交过申请,请耐心等待',
  342. icon:'none'
  343. })
  344. } else {
  345. if (!customer_info.status || customer_info.status != '流失') {
  346. uni.navigateTo({
  347. url: "/pages-applyPermission/applyPermission?source=5&form_page=问答社区"
  348. })
  349. } else { //主动调一次申请权限接口
  350. const res = await apiApplyPermission({
  351. company_name: customer_info.company_name,
  352. real_name: customer_info.name,
  353. source: 5,
  354. from_page: '问答社区'
  355. })
  356. if (res.code === 200) {
  357. uni.showToast({
  358. title:'您已提交过申请,请耐心等待',
  359. icon:'none'
  360. })
  361. const pages = getCurrentPages();
  362. const page = pages[pages.length - 1];
  363. page.onShow();
  364. }
  365. }
  366. }
  367. },
  368. }
  369. }