消毒机设备
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.1 KiB

  1. import { HEADER_TOKEN_KEY } from '@/libs/constant'
  2. import axios from 'axios'
  3. import { FtMessage } from 'libs/message'
  4. import { getToken } from 'libs/token'
  5. const http = axios.create({
  6. baseURL: import.meta.env.FT_API_BASE,
  7. timeout: 1000 * 60,
  8. })
  9. // 请求拦截器
  10. http.interceptors.request.use(
  11. (config) => {
  12. if (getToken()) {
  13. config.headers![HEADER_TOKEN_KEY] = getToken()
  14. }
  15. return config
  16. },
  17. (error: any) => {
  18. return Promise.reject(error)
  19. },
  20. )
  21. // 响应拦截器
  22. http.interceptors.response.use(
  23. (response) => {
  24. if (
  25. response.status === 200
  26. && response.data.code !== '0'
  27. ) {
  28. // 返回错误拦截
  29. FtMessage.error(response.data.msg)
  30. return Promise.reject(response)
  31. }
  32. else if (
  33. response.config.url?.includes('/files/download')
  34. || response.config.url?.includes('downloadStream')
  35. ) {
  36. return response.data
  37. }
  38. else if (response.data instanceof Blob) {
  39. return response.data
  40. }
  41. return response.data.data // 返回数据体
  42. },
  43. (error: any) => {
  44. console.log(error)
  45. if (error.response && error.response.status === 401) {
  46. FtMessage.error('账号权限过期')
  47. // TODO 登出
  48. }
  49. else {
  50. if (error.message.includes('timeout')) {
  51. FtMessage.error('请求超时')
  52. }
  53. else if (error.message.includes('Network')) {
  54. FtMessage.error('网络连接错误')
  55. }
  56. else {
  57. FtMessage.error('接口请求失败')
  58. }
  59. error.response = {
  60. data: {
  61. res: false,
  62. },
  63. }
  64. return Promise.reject(error.response)
  65. }
  66. },
  67. )
  68. // 封装 GET 请求
  69. export function get<T>(url: string, params?: any): Promise<T> {
  70. return http.get(url, { params })
  71. }
  72. // 封装 POST 请求
  73. export function post<T>(url: string, data?: any): Promise<T> {
  74. return http.post(url, data)
  75. }
  76. // 封装 PUT 请求
  77. export function put<T>(url: string, data?: any): Promise<T> {
  78. return http.put(url, data)
  79. }
  80. // 封装 DELETE 请求
  81. export function del<T>(url: string, params?: any): Promise<T> {
  82. return http.delete(url, { params })
  83. }
  84. export default http