移动端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

116 line
3.5 KiB

  1. import axios from 'axios'
  2. // import { Notification, MessageBox, Message } from 'element-ui'
  3. import { Notify, Dialog, Toast } from 'vant';
  4. import store from '@/store'
  5. import { getToken } from '@/utils/auth'
  6. import errorCode from '@/utils/errorCode'
  7. axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
  8. // 创建axios实例
  9. const service = axios.create({
  10. // axios中请求配置有baseURL选项,表示请求URL公共部分
  11. baseURL: process.env.VUE_APP_BASE_API,
  12. // 超时
  13. timeout: 100000
  14. })
  15. // request拦截器
  16. service.interceptors.request.use(config => {
  17. Toast.loading({
  18. message: '加载中...',
  19. forbidClick: true,
  20. duration: 0
  21. });
  22. // 是否需要设置 token
  23. const isToken = (config.headers || {}).isToken === false
  24. if (getToken() && !isToken) {
  25. config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
  26. }
  27. // get请求映射params参数
  28. if (config.method === 'get' && config.params) {
  29. let url = config.url + '?';
  30. for (const propName of Object.keys(config.params)) {
  31. const value = config.params[propName];
  32. var part = encodeURIComponent(propName) + "=";
  33. if (value !== null && typeof (value) !== "undefined") {
  34. if (typeof value === 'object') {
  35. for (const key of Object.keys(value)) {
  36. let params = propName + '[' + key + ']';
  37. var subPart = encodeURIComponent(params) + "=";
  38. url += subPart + encodeURIComponent(value[key]) + "&";
  39. }
  40. } else {
  41. url += part + encodeURIComponent(value) + "&";
  42. }
  43. }
  44. }
  45. url = url.slice(0, -1);
  46. config.params = {};
  47. config.url = url;
  48. }
  49. return config
  50. }, error => {
  51. console.log(error)
  52. Promise.reject(error)
  53. })
  54. // 响应拦截器
  55. service.interceptors.response.use(res => {
  56. Toast.clear();
  57. // 未设置状态码则默认成功状态
  58. const code = res.data.code || 200;
  59. // 获取错误信息
  60. const msg = errorCode[code] || res.data.msg || errorCode['default']
  61. if (code === 401) {
  62. Dialog.confirm({
  63. title: '系统提示',
  64. message: '登录状态已过期,您可以继续留在该页面,或者重新登录',
  65. confirmButtonText: '重新登录',
  66. cancelButtonText: '取消'
  67. })
  68. .then(() => {
  69. store.dispatch('LogOut').then(() => {
  70. if ('/authenticRight/index'.indexOf(to.path) !== -1) {
  71. location.href = '/authenticRight/login';
  72. } else if ('/homestead/index'.indexOf(to.path) !== -1) {
  73. location.href = '/homestead/login';
  74. } else {
  75. location.href = '/index';
  76. }
  77. })
  78. })
  79. } else if (code === 500) {
  80. Dialog.alert({ type: 'warning', message: msg });
  81. return Promise.reject(new Error(msg))
  82. } else if (code !== 200) {
  83. Dialog.alert({ type: 'warning', message: msg });
  84. return Promise.reject('error')
  85. } else {
  86. return res.data
  87. }
  88. },
  89. error => {
  90. console.log('err' + error)
  91. let { message } = error;
  92. if (message == "Network Error") {
  93. message = "后端接口连接异常";
  94. }
  95. else if (message.includes("timeout")) {
  96. message = "系统接口请求超时";
  97. }
  98. else if (message.includes("Request failed with status code")) {
  99. message = "系统接口" + message.substr(message.length - 3) + "异常";
  100. }
  101. Toast.clear();
  102. // Message({
  103. // message: message,
  104. // type: 'error',
  105. // duration: 5 * 1000
  106. // })
  107. Dialog.alert({ type: 'warning', message: message });
  108. return Promise.reject(error)
  109. }
  110. )
  111. export default service