|
@@ -1,102 +1,135 @@
|
|
|
import axios from 'axios'
|
|
|
-import type { AxiosRequestConfig } from 'axios'
|
|
|
-import { ElMessage } from 'element-plus'
|
|
|
-import { user } from '~/store/index'
|
|
|
+import { showFailToast } from 'vant'
|
|
|
|
|
|
-const token = user.value?.token
|
|
|
-export interface Response {
|
|
|
- code: string
|
|
|
- data: any
|
|
|
- msg: string
|
|
|
- status: any
|
|
|
- statusText: any
|
|
|
- headers: any
|
|
|
- config: any
|
|
|
-}
|
|
|
+import { userInfo } from '~/store/user'
|
|
|
+
|
|
|
+const { token } = userInfo
|
|
|
|
|
|
-const instance = axios.create({
|
|
|
- baseURL: (window as any).GLOBAL_CONFIG.api,
|
|
|
- timeout: 60 * 1000,
|
|
|
+const service = axios.create({
|
|
|
+ baseURL: window.globalVariables.api,
|
|
|
+ method: 'post',
|
|
|
+ timeout: 60000,
|
|
|
headers: {
|
|
|
'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
},
|
|
|
- method: 'post',
|
|
|
})
|
|
|
|
|
|
-instance.interceptors.request.use(
|
|
|
- async (config) => {
|
|
|
- if (config.method?.toLocaleLowerCase() === 'get') {
|
|
|
- config.params = Object.assign({ token }, config.params)
|
|
|
- }
|
|
|
- else {
|
|
|
- config.data = Object.assign(
|
|
|
+service.interceptors.request.use(
|
|
|
+ (config) => {
|
|
|
+ // do something before request is sent
|
|
|
+
|
|
|
+ config.data = Object.assign(
|
|
|
+ {
|
|
|
+ token,
|
|
|
+ site: 'jnmf',
|
|
|
+ client: 'web',
|
|
|
+ api: 'json',
|
|
|
+ // from: 'backend',
|
|
|
+ issubmit: (config.url.endsWith('add') || config.url.endsWith('edit')) ? 1 : undefined,
|
|
|
+ },
|
|
|
+ config.data || {},
|
|
|
+ )
|
|
|
+ if (config.method === 'get') {
|
|
|
+ config.params = Object.assign(
|
|
|
{
|
|
|
- token: user.value?.token,
|
|
|
- client: 'web',
|
|
|
- api: 'json',
|
|
|
- site: 'aimk',
|
|
|
- issubmit: (config.url?.endsWith('add') || config.url?.endsWith('edit')) ? '1' : undefined,
|
|
|
+ token,
|
|
|
},
|
|
|
- config.data)
|
|
|
+ config.params || {},
|
|
|
+ )
|
|
|
}
|
|
|
+ // 短时间多个请求会冲突
|
|
|
+ // console.groupCollapsed('axios: ' + config.url)
|
|
|
+ // console.log('request', config.data)
|
|
|
return config
|
|
|
},
|
|
|
(error) => {
|
|
|
- console.error('request error: ', error)
|
|
|
+ // do something with request error
|
|
|
+ console.log(error) // for debug
|
|
|
return Promise.reject(error)
|
|
|
},
|
|
|
)
|
|
|
|
|
|
// response interceptor
|
|
|
-instance.interceptors.response.use(
|
|
|
- (response: { data: Response }) => {
|
|
|
- response.data.code = response.data?.code?.toString()
|
|
|
- response.data.msg = response.data.msg.replaceAll(/<.*?>/g, ' ')
|
|
|
+let isRelogin = false
|
|
|
+service.interceptors.response.use(
|
|
|
+ (response, c) => {
|
|
|
+ const { code, msg, data } = response.data
|
|
|
+ if (code === '10001') {
|
|
|
+ if (!isRelogin) {
|
|
|
+ isRelogin = true
|
|
|
+ showFailToast(msg)
|
|
|
+ request({
|
|
|
+ url: '/user/main/login',
|
|
|
+ data: {
|
|
|
+ token: data.token,
|
|
|
+ },
|
|
|
+ }).then((res) => {
|
|
|
+ if (res.code == 1) {
|
|
|
+ localStorage.setItem('userInfo', JSON.stringify(res.data))
|
|
|
+ isRelogin = false
|
|
|
+ // 视情况打开
|
|
|
+ location.reload()
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (code !== '1') {
|
|
|
+ showFailToast(msg)
|
|
|
+ }
|
|
|
+ // console.log('response', response.data)
|
|
|
+ // console.groupEnd()
|
|
|
+ return response.data
|
|
|
+ },
|
|
|
+ (error) => {
|
|
|
+ console.error(`err${error}`) // for debug
|
|
|
+ return Promise.reject(error)
|
|
|
+ },
|
|
|
+)
|
|
|
+
|
|
|
+// 上传
|
|
|
+export const request = axios.create({
|
|
|
+ baseURL: window.globalVariables.api,
|
|
|
+ method: 'post',
|
|
|
+ timeout: 5000,
|
|
|
+ headers: {
|
|
|
+ 'Content-Type': 'application/x-www-form-urlencoded',
|
|
|
+ },
|
|
|
+})
|
|
|
+request.interceptors.response.use(
|
|
|
+ (response) => {
|
|
|
const { code, msg } = response.data
|
|
|
if (code !== '1')
|
|
|
- ElMessage.error(msg)
|
|
|
+ showFailToast(msg)
|
|
|
|
|
|
return response.data
|
|
|
},
|
|
|
(error) => {
|
|
|
- console.error(`response error: ${error}`)
|
|
|
+ console.log(`err${error}`) // for debug
|
|
|
return Promise.reject(error)
|
|
|
},
|
|
|
)
|
|
|
|
|
|
-export default (config: AxiosRequestConfig): Promise<Response> => instance.request(config)
|
|
|
-
|
|
|
-function obj2form(data: { [key: string]: any }) {
|
|
|
- const formData = new FormData()
|
|
|
- Object.keys(data).forEach(key => formData.append(key, data[key]))
|
|
|
- return formData
|
|
|
-}
|
|
|
-
|
|
|
-export const REQUEST = {
|
|
|
- empty: axios,
|
|
|
- default: instance,
|
|
|
- import: (c: Partial<AxiosRequestConfig>) => instance({
|
|
|
- timeout: 10 * 60 * 1000,
|
|
|
- transformRequest: [obj2form],
|
|
|
- ...c,
|
|
|
- }),
|
|
|
- upload: (c: Partial<AxiosRequestConfig>) => instance({
|
|
|
- timeout: 3 * 60 * 1000,
|
|
|
- transformRequest: [obj2form],
|
|
|
- ...c,
|
|
|
- }),
|
|
|
- download: (c: Partial<AxiosRequestConfig>) => instance({
|
|
|
- timeout: 1 * 60 * 1000,
|
|
|
- method: 'get',
|
|
|
- params: { token, limit: 10000, page: 1, api: 'xls', ...c },
|
|
|
- }),
|
|
|
-}
|
|
|
-
|
|
|
-export function download(url: string, data?: object | null) {
|
|
|
+export function download(url, data, name) {
|
|
|
const params = Object.assign({ token, limit: 10000, page: 1, api: 'xls' }, data)
|
|
|
const paramsStr = Object.entries(params).map(([k, v]) => `${k}=${v}`).join('&')
|
|
|
const el = document.createElement('a')
|
|
|
- const href = `${(window as any).GLOBAL_CONFIG.api}${url}?${paramsStr}`
|
|
|
+ const href = `${window.globalVariables.api}${url}?${paramsStr}`
|
|
|
+ el.setAttribute('href', href)
|
|
|
+ name && el.setAttribute('download', name)
|
|
|
+ el.click()
|
|
|
+}
|
|
|
+
|
|
|
+export function download2(url, data, name) {
|
|
|
+ const el = document.createElement('a')
|
|
|
+ const href = `${window.globalVariables.api}/openapi/download.php?path=${url}&name=${name}`
|
|
|
el.setAttribute('href', href)
|
|
|
+ name && el.setAttribute('download', name)
|
|
|
el.click()
|
|
|
}
|
|
|
+
|
|
|
+export default (...args) => {
|
|
|
+ if (!isRelogin)
|
|
|
+ return service(...args)
|
|
|
+ else
|
|
|
+ return Promise.reject({})
|
|
|
+}
|