All checks were successful
Build / build-and-test (push) Successful in 27s
[Style] 为主视图增加 16px 间距 更新 mdui-card 的右边距问题 (老问题了, 还是没有解决) [Base] 引入 VueUse 分离 Axios HTML 部分紧凑化
57 lines
1.4 KiB
JavaScript
57 lines
1.4 KiB
JavaScript
import { useAxios } from '@vueuse/integrations/useAxios'
|
|
import axios from 'axios'
|
|
|
|
function getEndpoint() {
|
|
const apiMapping = {
|
|
'': ['http://localhost:28001/', '/api/'],
|
|
}
|
|
let host = import.meta.env.SSR ? 'ssr' : window.location.host
|
|
let entry = apiMapping[host] ?? apiMapping['']
|
|
return import.meta.env.SSR ? entry[0] : replaceUrl(entry[1])
|
|
}
|
|
|
|
function replaceUrl(url) {
|
|
return url
|
|
.replace('{{hostname}}', window.location.hostname)
|
|
.replace('{{port}}', window.location.port)
|
|
.replace('{{protocol}}', window.location.protocol)
|
|
}
|
|
|
|
export function useApiRequest(method, url, data, config = {}) {
|
|
const start = Date.now()
|
|
const baseURL = getEndpoint()
|
|
const fullURL = `${baseURL}${url}`
|
|
const {
|
|
response,
|
|
error,
|
|
isFinished,
|
|
isLoading,
|
|
execute,
|
|
} = useAxios(
|
|
fullURL,
|
|
{
|
|
method,
|
|
...(method === 'POST' ? { data } : {}),
|
|
...(config || {})
|
|
},
|
|
{
|
|
immediate: false,
|
|
axios,
|
|
}
|
|
)
|
|
const exec = async () => {
|
|
await execute()
|
|
const stop = Date.now()
|
|
return {
|
|
status: response.value?.status || (error.value?.response?.status ?? -1),
|
|
data: response.value?.data || error.value?.response?.data || null,
|
|
duration: stop - start,
|
|
start,
|
|
stop,
|
|
error: error.value,
|
|
isSSR: import.meta.env.SSR,
|
|
}
|
|
}
|
|
return { execute: exec, isFinished, isLoading }
|
|
}
|