axios.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. /* eslint-disable */
  3. import axios from "axios"
  4. // import router from '../router/index'
  5. import { browser } from '@/utils/common'
  6. import { Toast } from 'vant'
  7. // Full config: https://github.com/axios/axios#request-config
  8. // axios.defaults.baseURL = process.env.baseURL || process.env.apiUrl || '';
  9. // axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
  10. // axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
  11. let config = {
  12. // baseURL: process.env.baseURL || process.env.apiUrl || ""
  13. // timeout: 60 * 1000, // Timeout
  14. // withCredentials: true, // Check cross-site Access-Control
  15. // transformRequest: [function(data) {
  16. // console.log(data)
  17. // if(!data.qStringify) {
  18. // data = qs.stringify({
  19. // ...data
  20. // })
  21. // }
  22. // return data
  23. // }]
  24. };
  25. const _axios = axios.create(config);
  26. _axios.interceptors.request.use(
  27. function(config) {
  28. // Do something before request is sent
  29. // 判断用户是否登录
  30. if(browser().android || browser().iPhone) { // app里面
  31. let userInfo = localStorage.getItem('Authorization')
  32. if(userInfo) {
  33. config.headers['Authorization'] = userInfo
  34. }
  35. } else { // 网页里面
  36. let auth = localStorage.getItem('userInfo')
  37. if(auth) {
  38. config.headers['Authorization'] = auth
  39. }
  40. }
  41. // config.headers['Authorization'] ='bearer 8ffb419c-0d24-46b8-8faf-135ff24fa1d9'
  42. return config;
  43. },
  44. function(error) {
  45. Toast.clear()
  46. Toast('网络错误')
  47. // Do something with request error
  48. return Promise.reject(error);
  49. }
  50. );
  51. // Add a response interceptor
  52. _axios.interceptors.response.use(
  53. function(response) {
  54. // Do something with response data
  55. if(response.data.code == 403 || response.data.code == 403) {
  56. window.localStorage.removeItem('userInfo') // 删除用户信息
  57. window.localStorage.removeItem('Authorization') // 删除用户信息
  58. if(browser().android) {
  59. DAYA.postMessage(JSON.stringify({api: 'login'}))
  60. } else if(browser().iPhone) {
  61. window.webkit.messageHandlers.DAYA.postMessage(JSON.stringify({api: 'login'}))
  62. } else {
  63. // router.push('/login')
  64. }
  65. }
  66. return response;
  67. },
  68. function(error) {
  69. Toast.clear()
  70. Toast('网络错误')
  71. // Do something with response error
  72. return Promise.reject(error);
  73. }
  74. );
  75. // Plugin.install = function(Vue) {
  76. // Vue.axios = _axios;
  77. // window.axios = _axios;
  78. // Object.defineProperties(Vue.prototype, {
  79. // axios: {
  80. // get() {
  81. // return _axios;
  82. // }
  83. // },
  84. // $axios: {
  85. // get() {
  86. // return _axios;
  87. // }
  88. // },
  89. // });
  90. // };
  91. // Vue.use(Plugin)
  92. export default _axios