request.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import Vue from 'vue'
  2. import uni_request from './uni_request.js'
  3. const request = uni_request({
  4. timeout: 123456,
  5. baseURL: 'http://fangw.jiuweiyun.cn/api', //线上
  6. // baseURL: 'http://192.168.0.4:8114/api', //本地
  7. // baseURL: 'http://192.168.0.15:8086/api', //本地
  8. header: {
  9. 'content-type': 'application/json'
  10. }
  11. })
  12. // 请求前拦截器
  13. request.interceptors.request.use(config => {
  14. uni.showLoading({
  15. title: '加载中',
  16. mask: true
  17. })
  18. config.header.Authorization = 'Bearer ' + uni.getStorageSync("token")
  19. return config
  20. })
  21. // 请求后拦截器
  22. request.interceptors.response.use(async (response, ...args) => { // 拦截器
  23. uni.hideLoading()
  24. if (response.data.code === 50000 || response.data.code === 40029) { //50000token过期 40029 code已使用
  25. uni.removeStorageSync('token');
  26. window.location = 'http://api.app.jiuweiyun.cn/api/fangwei/auth'
  27. } else {
  28. return response
  29. }
  30. });
  31. request.onerror = (...args) => { // 请求失败统一处理方法
  32. console.log(args)
  33. uni.hideLoading()
  34. uni.showModal({
  35. title: '请求失败',
  36. content: '网络出错',
  37. // content: JSON.stringify(args),
  38. showCancel: false
  39. })
  40. return Promise.reject(args)
  41. }
  42. export default request