http.js 2.6 KB

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