web init
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
import axios, { AxiosRequestConfig } from "axios";
|
||||
import { ElLoading } from "element-plus";
|
||||
|
||||
const request = axios.create({
|
||||
baseURL: "/api",
|
||||
timeout: 30000,
|
||||
headers: {
|
||||
'Content-Type': 'application/json;charset=UTF-8',
|
||||
}
|
||||
})
|
||||
|
||||
let loadingInstance: ReturnType<typeof ElLoading.service> | null = null;
|
||||
|
||||
request.interceptors.request.use(config => {
|
||||
if (config.showLoading) {
|
||||
loadingInstance = ElLoading.service({
|
||||
lock: true,
|
||||
text: "加载中...",
|
||||
background: "rgba(0, 0, 0, 0.7)",
|
||||
});
|
||||
}
|
||||
return config;
|
||||
});
|
||||
|
||||
request.interceptors.response.use(response => {
|
||||
if (loadingInstance) {
|
||||
loadingInstance.close();
|
||||
loadingInstance = null;
|
||||
}
|
||||
return response;
|
||||
});
|
||||
|
||||
export const api = {
|
||||
get: function(url: string, config = {} as AxiosRequestConfig) { return request.get(url, config) },
|
||||
post: function(url: string, data = {}, config = {} as AxiosRequestConfig) { return request.post(url, data, config) },
|
||||
put: function(url: string, data = {}, config = {} as AxiosRequestConfig) { return request.put(url, data, config) },
|
||||
delete: function(url: string, config = {} as AxiosRequestConfig) { return request.delete(url, config) },
|
||||
upload: function(url: string, formData: FormData, config = {} as AxiosRequestConfig) {
|
||||
return request.post(url, formData, {
|
||||
...config,
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
...(config.headers as Record<string, string>)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export default request;
|
||||
2
schoolNewsWeb/src/domain/index.ts
Normal file
2
schoolNewsWeb/src/domain/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
// 导出通用类型
|
||||
export * from "./types/common";
|
||||
19
schoolNewsWeb/src/domain/types/common.ts
Normal file
19
schoolNewsWeb/src/domain/types/common.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
// 通用响应类型
|
||||
interface ApiResponse<T> {
|
||||
code: number;
|
||||
message: string;
|
||||
success: boolean;
|
||||
failure: boolean;
|
||||
auth: boolean;
|
||||
data?: T;
|
||||
dataList?: T[];
|
||||
}
|
||||
|
||||
// 扩展 axios 配置类型
|
||||
declare module "axios" {
|
||||
interface AxiosRequestConfig {
|
||||
showLoading?: boolean;
|
||||
}
|
||||
}
|
||||
|
||||
export { ApiResponse };
|
||||
@@ -1,10 +1,13 @@
|
||||
import { createApp } from "vue";
|
||||
import ElementPlus from "element-plus";
|
||||
import "element-plus/dist/index.css";
|
||||
import App from "./App.vue";
|
||||
import "./registerServiceWorker";
|
||||
import router from "./router";
|
||||
import store from "./store";
|
||||
|
||||
const app = createApp(App);
|
||||
app.use(ElementPlus);
|
||||
app.use(store);
|
||||
app.use(router);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user