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