request.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import type { AxiosRequestConfig } from 'axios'
  2. import axios from 'axios'
  3. import { showFailToast } from 'vant'
  4. import { user } from '~/store/user'
  5. import router from '../router/index'
  6. const token = user.value?.token
  7. export interface Response {
  8. code: string
  9. data: any
  10. msg: string
  11. status: any
  12. statusText: any
  13. headers: any
  14. config: any
  15. }
  16. const instance = axios.create({
  17. baseURL: (window as any).GLOBAL_CONFIG.api,
  18. timeout: 60 * 1000,
  19. headers: {
  20. 'Content-Type': 'application/x-www-form-urlencoded',
  21. },
  22. method: 'post',
  23. })
  24. instance.interceptors.request.use(
  25. async (config) => {
  26. // config.params = Object.assign({ token }, config.params)
  27. if (config.method?.toLocaleLowerCase() === 'get' || config?.data?.file) {
  28. config.params = Object.assign({ token }, config.params)
  29. }
  30. else {
  31. config.data = Object.assign(
  32. {
  33. token: user.value?.token,
  34. client: 'web',
  35. api: 'json',
  36. site: 'zjzs',
  37. issubmit: (config.url?.endsWith('add') || config.url?.endsWith('edit')) ? '1' : undefined,
  38. },
  39. config.data)
  40. }
  41. return config
  42. },
  43. (error) => {
  44. console.error('request error: ', error)
  45. return Promise.reject(error)
  46. },
  47. )
  48. // response interceptor
  49. instance.interceptors.response.use(
  50. (response: { data: Response }) => {
  51. if (response.config.url == "/openapi/wx/wx.php" && response.data.code == '401') {
  52. return response.data
  53. }
  54. if (response.data.code === '10000') {
  55. showFailToast('登录信息已过期,请重新登录')
  56. return
  57. }
  58. if (response.data.code === undefined)
  59. return response.data
  60. response.data.code = response.data?.code?.toString()
  61. response.data.msg = response.data.msg.replaceAll(/<.*?>/g, ' ')
  62. const { code, msg } = response.data
  63. if (code !== '1') {
  64. showFailToast(msg)
  65. return Promise.reject(response.data)
  66. }
  67. return response.data
  68. },
  69. (error) => {
  70. console.error(`response error: ${error}`)
  71. return Promise.reject(error)
  72. },
  73. )
  74. export default (config: AxiosRequestConfig): Promise<Response> => instance.request(config)
  75. function obj2form(data: { [key: string]: any }) {
  76. const formData = new FormData()
  77. Object.keys(data).forEach(key => formData.append(key, data[key]))
  78. return formData
  79. }
  80. export const REQUEST = {
  81. empty: axios,
  82. default: instance,
  83. import: (c: Partial<AxiosRequestConfig>) => instance({
  84. timeout: 10 * 60 * 1000,
  85. transformRequest: [obj2form],
  86. ...c,
  87. }),
  88. upload: (c: Partial<AxiosRequestConfig>) => instance({
  89. timeout: 3 * 60 * 1000,
  90. transformRequest: [obj2form],
  91. ...c,
  92. }),
  93. download: (c: Partial<AxiosRequestConfig>) => instance({
  94. timeout: 1 * 60 * 1000,
  95. method: 'get',
  96. params: { token, limit: 10000, page: 1, api: 'xls', ...c },
  97. }),
  98. }
  99. export function download(url: string, data?: object | null) {
  100. const params = Object.assign({ token, limit: 10000, page: 1, api: 'xls' }, data)
  101. const paramsStr = Object.entries(params).map(([k, v]) => `${k}=${v}`).join('&')
  102. const el = document.createElement('a')
  103. const href = `${(window as any).GLOBAL_CONFIG.api}${url}?${paramsStr}`
  104. el.setAttribute('href', href)
  105. el.click()
  106. }