request.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import axios from "axios";
  2. import { showMessage } from "./errorStatusCode"; // 引入状态码文件
  3. import type { AxiosRequestConfig, AxiosResponse, AxiosError } from "axios";
  4. import { ElNotification } from "element-plus";
  5. import { useStore } from "../store/index";
  6. const baseURL = "/api";
  7. // 创建axios实例
  8. const instance = axios.create({
  9. baseURL, // 设置API的基础URL
  10. });
  11. // 请求拦截器
  12. instance.interceptors.request.use(
  13. (config: AxiosRequestConfig): any => {
  14. // 可在请求发送前对config进行修改,如添加请求头等
  15. const { getToken } = useStore();
  16. const token = getToken;
  17. const headers = config.headers || {};
  18. headers["Token"] = token;
  19. config.headers = headers;
  20. return config;
  21. },
  22. (error: AxiosError) => {
  23. // 处理请求错误
  24. return Promise.reject(error);
  25. }
  26. );
  27. // 响应拦截器
  28. instance.interceptors.response.use(
  29. (response: AxiosResponse) => {
  30. // 对响应数据进行处理
  31. if (response.status !== 200 || response.data.code === 'error') {
  32. ElNotification.closeAll()
  33. ElNotification({
  34. message: response.status !== 200 ? showMessage(response.status) : response.data.msg,
  35. type: "error",
  36. });
  37. }
  38. return response;
  39. },
  40. (error: AxiosError) => {
  41. ElNotification.closeAll()
  42. ElNotification({
  43. message: showMessage(error.request.status), // 传入响应码,匹配响应码对应信息,
  44. type: "error",
  45. });
  46. // 处理响应错误
  47. return Promise.reject(error);
  48. }
  49. );
  50. // 封装GET请求
  51. export async function get(url: string, params?: any, file: boolean = false): Promise<any> {
  52. return new Promise((resolve, reject) => {
  53. instance
  54. .get(url, { params })
  55. .then(({ data }: any) => {
  56. const { code } = data;
  57. if (code === "ok" || file) {
  58. resolve(data);
  59. return;
  60. }
  61. reject(data);
  62. })
  63. .catch((error) => {
  64. reject(error);
  65. });
  66. });
  67. }
  68. // 封装POST请求
  69. export async function post(url: string, data?: any): Promise<any> {
  70. return new Promise((resolve, reject) => {
  71. instance
  72. .post(url, data, {
  73. headers: {
  74. "Content-type": " application/x-www-form-urlencoded; charset=UTF-8",
  75. },
  76. })
  77. .then(({ data }: any) => {
  78. const { code } = data;
  79. if (code === "ok") {
  80. resolve(data);
  81. return;
  82. }
  83. reject(data);
  84. })
  85. .catch((error) => {
  86. reject(error);
  87. });
  88. });
  89. }
  90. // 封装文件上传请求
  91. export async function uploadFile(url: string, data?: any): Promise<any> {
  92. return new Promise((resolve, reject) => {
  93. instance
  94. .post(url, data, {
  95. headers: {
  96. "Content-type": "multipart/form-data",
  97. },
  98. })
  99. .then(({ data }: any) => {
  100. const { code } = data;
  101. if (code === "ok") {
  102. resolve(data);
  103. return;
  104. }
  105. reject(data);
  106. })
  107. .catch((error) => {
  108. reject(error);
  109. });
  110. });
  111. }