request.js 4.0 KB

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