request.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import Vue from 'vue'
  2. import $store from '@/store'
  3. import uni_request from './uni_request.js'
  4. const host = 'http://api.app.jiuweiyun.cn'
  5. // const host = 'http://api.woaidakele.cn'
  6. // const host = 'http://192.168.0.4'
  7. // const host = 'http://192.168.0.11'
  8. const port = 80
  9. // const port = 443
  10. // const url = '/dwbsapp'
  11. const url = '/api'
  12. // uni.onNetworkStatusChange(res => { $store.commit('device/NETWORKTYPE_CHANGE', res.networkType) }) // 网络类型变化监听
  13. // uni.getNetworkType({ success(res) { $store.commit('device/NETWORKTYPE_CHANGE', res.networkType) }}) // 获取网络类型
  14. const request = uni_request({
  15. timeout: 123456,
  16. baseURL: `${host}:${port}${url}`,
  17. })
  18. request.interceptors.request.use(config => {
  19. config.header.Authorization = 'Bearer ' + $store.state.app.token
  20. return config
  21. })
  22. request.interceptors.response.use((response, ...args) => { // 拦截器
  23. uni.hideLoading()
  24. uni.$emit('HIDELOADING') // 隐藏加载
  25. uni.stopPullDownRefresh() // 停止下拉刷新
  26. if (response.statusCode === 200) {
  27. if (+response.data.code === 202) {
  28. uni.$emit('TOAST', response.data.message)
  29. }
  30. } else if (response.statusCode === 401) { // 服务器响应不为 200 的统一处理方法
  31. uni.$emit('TOAST', '登录信息过期,请重新登录')
  32. uni.$emit('RELAUNCH')
  33. } else {
  34. // uni.$emit('TOAST', `网络好像出了点问题:${response.statusCode}/${args[1]}`)
  35. }
  36. return response
  37. })
  38. request.onerror = (...args) => { // 请求失败统一处理方法
  39. uni.hideLoading()
  40. uni.$emit('HIDELOADING') // 隐藏加载
  41. uni.stopPullDownRefresh() // 停止下拉刷新
  42. // uni.$emit('TOAST', `网络好像出了点问题`)
  43. }
  44. export default request
  45. export const FlashScreenUrl = 'https://api.jiuweiyun.cn/public/uploads/images/topics/420.jpg' // app 闪屏固定url
  46. export const ArticleURL = `${host}:${port}${url}/article/show_article` // 文章固定链接
  47. // export const ArticleURL = `https://mp.weixin.qq.com/s/O5Q00WQhCYXI6Mky_ExPOw` // 文章固定链接