request.js 4.1 KB

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