questionMixin.js 15 KB

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