http.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import axios from 'axios';
  2. import store from '@/store';
  3. import router from '@/router';
  4. import { getToken } from '@/utils/auth';
  5. import { Message } from 'element-ui';
  6. const service = axios.create({
  7. baseURL: process.env.VUE_APP_BASE_API,
  8. timeout: 30000,
  9. });
  10. // 请求拦截器
  11. service.interceptors.request.use(
  12. (config) => {
  13. config.headers['Content-Type'] = 'application/json';
  14. return config;
  15. },
  16. (error) => {
  17. return Promise.reject(error);
  18. },
  19. );
  20. // 响应拦截器
  21. service.interceptors.response.use(
  22. (response) => {
  23. const res = response.data;
  24. const { code, status, message, error } = res;
  25. if (code === 404) {
  26. Message({
  27. message: '请求的资源不存在',
  28. type: 'error',
  29. duration: 3 * 1000,
  30. });
  31. return Promise.reject(new Error(message || 'Error'));
  32. }
  33. // 返回数据失败
  34. if (status === 0) {
  35. Message({
  36. message: `${error}`,
  37. type: 'error',
  38. duration: 3 * 1000,
  39. });
  40. return Promise.reject(new Error(`${error}` || 'Error'));
  41. }
  42. // 无效的操作用户
  43. if (status === -1) {
  44. Message({
  45. message: error,
  46. type: 'error',
  47. duration: 3 * 1000,
  48. });
  49. store.dispatch('user/signOut');
  50. router.push('/login');
  51. }
  52. return res;
  53. },
  54. (error) => {
  55. if (error.code === 'ERR_CANCELED') {
  56. return Message.success('取消上传成功');
  57. }
  58. Message({
  59. message: error.message,
  60. type: 'error',
  61. duration: 3 * 1000,
  62. });
  63. return Promise.reject(error);
  64. },
  65. );
  66. /**
  67. * 得到必需的请求参数
  68. * @returns {object} 返回必需的请求参数
  69. * */
  70. function getRequestParams() {
  71. const token = getToken();
  72. return {
  73. AccessToken: token?.access_token ?? '',
  74. UserCode: token?.user_code ?? '',
  75. UserType: token?.user_type ?? '',
  76. SessionID: token?.session_id ?? '',
  77. };
  78. }
  79. /**
  80. * @description http 请求封装
  81. */
  82. export const http = {
  83. /**
  84. * @param {String} url 请求地址
  85. * @param {object} config 请求配置
  86. */
  87. get: (url, config) => service.get(url, config),
  88. /**
  89. * @param {string} url 请求地址
  90. * @param {object} data 请求数据
  91. * @param {object} config 请求配置
  92. */
  93. post: (url, data = {}, config = {}) => {
  94. config.params = {
  95. ...config.params,
  96. ...getRequestParams(),
  97. };
  98. return service.post(url, data, config);
  99. },
  100. postForm: (url, data = {}, config = {}) => {
  101. config.params = {
  102. ...config.params,
  103. ...getRequestParams(),
  104. };
  105. return service.postForm(url, data, config);
  106. },
  107. put: (url, data, config) => service.put(url, data, config),
  108. delete: (url, data, config) => service.delete(url, data, config),
  109. };