u
This commit is contained in:
1 parent
4aaa93df7d
commit
c330e52fd3
66 files changed
+7386
-4
No files matched your search
@@ -0,0 +1,100 @@
|
||||
import { httpPost } from ".";
|
||||
|
||||
// 登录
|
||||
export function loginApi(orgid, userid, password) {
|
||||
return httpPost(`/auth/login`, {
|
||||
orgid,
|
||||
userid,
|
||||
password,
|
||||
});
|
||||
}
|
||||
|
||||
// ============================= data =============================
|
||||
export function loadDataApi(viewName, searchCondition = "", orderField = "", searchColumns = []) {
|
||||
return httpPost("/data/loadData", {
|
||||
viewName,
|
||||
searchCondition,
|
||||
orderField,
|
||||
searchColumns,
|
||||
});
|
||||
}
|
||||
|
||||
export function loadDataBySqlApi(sql) {
|
||||
return httpPost("/data/loadDataBySql", {
|
||||
sql,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页加载表格数据
|
||||
* @param {Object} params 参数对象
|
||||
* @param {number} params.page 当前页
|
||||
* @param {number} params.size 每页条数
|
||||
* @param {string} params.viewName 视图名称
|
||||
* @param {string} [params.searchCondition] 搜索条件
|
||||
* @param {string} [params.orderField] 排序字段
|
||||
* @param {string[]} [params.searchColumns] 搜索列
|
||||
* @returns {Promise<{
|
||||
* success: boolean,
|
||||
* data: {
|
||||
* rows: Array<Record<string, any>>,
|
||||
* total: number,
|
||||
* page: number,
|
||||
* size: number
|
||||
* },
|
||||
* code: number,
|
||||
* message: string
|
||||
* }>} 返回 Promise 对象
|
||||
*/
|
||||
export function loadPageDataApi(
|
||||
{ page, size, viewName, searchCondition = "", orderField = "", searchColumns = [] },
|
||||
config = {},
|
||||
) {
|
||||
return httpPost(
|
||||
"/data/page",
|
||||
{
|
||||
page,
|
||||
size,
|
||||
viewName,
|
||||
searchCondition,
|
||||
orderField,
|
||||
searchColumns,
|
||||
},
|
||||
config,
|
||||
);
|
||||
}
|
||||
|
||||
export function saveObjectApi(data) {
|
||||
return httpPost("/data/saveObject", data);
|
||||
}
|
||||
|
||||
export function saveObjectKeysApi(data) {
|
||||
return httpPost("/data/saveObjectKeys", data);
|
||||
}
|
||||
// ============================= data =============================
|
||||
|
||||
// ============================= column =============================
|
||||
export function loadViewColumnsApi(viewName) {
|
||||
return httpPost("/column/view", {
|
||||
viewName,
|
||||
});
|
||||
}
|
||||
|
||||
export function loadEditColumnsApi(viewName) {
|
||||
return httpPost("/column/edit", {
|
||||
viewName,
|
||||
});
|
||||
}
|
||||
|
||||
export function loadAllColumnsApi(viewName) {
|
||||
return httpPost("/column/all", {
|
||||
viewName,
|
||||
});
|
||||
}
|
||||
|
||||
export function checkColumnsApi(viewName) {
|
||||
return httpPost("/column/check", {
|
||||
viewName,
|
||||
});
|
||||
}
|
||||
// ============================= column =============================
|
||||
@@ -0,0 +1,116 @@
|
||||
import axios from "axios";
|
||||
import { ElMessage } from "element-plus";
|
||||
import { useUserStore } from "@/store/user";
|
||||
import router from "@/router";
|
||||
|
||||
let hasShown401 = false;
|
||||
|
||||
const axiosInstance = axios.create({
|
||||
// baseURL: "http://127.0.0.1:8084/api",
|
||||
baseURL: process.env.PUBLIC_API_BASE_URL,
|
||||
timeout: 30000,
|
||||
});
|
||||
|
||||
axiosInstance.interceptors.request.use(
|
||||
(config) => {
|
||||
const userStore = useUserStore();
|
||||
const token = userStore.userInfo.token;
|
||||
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
return config;
|
||||
},
|
||||
(error) => Promise.reject(error),
|
||||
);
|
||||
|
||||
axiosInstance.interceptors.response.use(
|
||||
(response) => {
|
||||
const resData = response.data;
|
||||
|
||||
resData.success = resData.code === 200;
|
||||
|
||||
if (resData.code === 401) {
|
||||
if (!hasShown401) {
|
||||
hasShown401 = true;
|
||||
const userStore = useUserStore();
|
||||
ElMessage.warning("登录已过期,请重新登录");
|
||||
userStore.resetUserInfo();
|
||||
router.replace("/login");
|
||||
}
|
||||
} else if (resData.code === 500) {
|
||||
// 系统异常
|
||||
ElMessage.error(resData.message || "系统异常,请稍后再试");
|
||||
} else if (resData.code === 1000) {
|
||||
// 业务异常
|
||||
ElMessage.error(resData.message || "业务异常,请稍后再试");
|
||||
}
|
||||
|
||||
return response;
|
||||
},
|
||||
(error) => {
|
||||
let message = "请求失败";
|
||||
|
||||
if (error.code === "ECONNABORTED") {
|
||||
message = "请求超时,请稍后重试";
|
||||
} else if (error.message === "Network Error") {
|
||||
message = "网络异常,请检查网络连接";
|
||||
} else if (error.response) {
|
||||
const status = error.response.status;
|
||||
|
||||
switch (status) {
|
||||
case 400:
|
||||
message = "请求参数错误";
|
||||
break;
|
||||
case 404:
|
||||
message = "请求接口不存在";
|
||||
break;
|
||||
case 500:
|
||||
message = "服务器异常";
|
||||
break;
|
||||
default:
|
||||
message = error.response.data?.msg || `请求失败 (${status})`;
|
||||
}
|
||||
}
|
||||
|
||||
// ElMessage.error(message);
|
||||
|
||||
return Promise.reject(error);
|
||||
},
|
||||
);
|
||||
|
||||
/**
|
||||
* 通用 API 响应类型
|
||||
* @template T
|
||||
* @typedef {Object} ApiResponse
|
||||
* @property {boolean} success - 请求是否成功
|
||||
* @property {number} code - 状态码
|
||||
* @property {string} message - 消息
|
||||
* @property {T} data - 数据
|
||||
*/
|
||||
|
||||
/**
|
||||
* 通用 HTTP POST 请求
|
||||
* @template T
|
||||
* @param {string} url
|
||||
* @param {any} data
|
||||
* @param {any} config
|
||||
* @returns {Promise<ApiResponse<T>>}
|
||||
*/
|
||||
export function httpPost(url, data, config) {
|
||||
return axiosInstance
|
||||
.post(url, data, {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
...config,
|
||||
})
|
||||
.then((res) => res.data);
|
||||
}
|
||||
|
||||
export function reset401Flag() {
|
||||
hasShown401 = false;
|
||||
}
|
||||
|
||||
export default axiosInstance;
|
||||
Reference in new issue
Block a user