mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-09-22 21:06:32 +00:00
开源准备
This commit is contained in:
@@ -0,0 +1,113 @@
|
||||
import axios from "axios";
|
||||
import storage from "store";
|
||||
import router from "@/router";
|
||||
import { getNewToken } from "@/api/system/auth";
|
||||
import { save_token } from "@/utils/util";
|
||||
import notification from "ant-design-vue/es/notification";
|
||||
|
||||
// 创建 axios 实例
|
||||
const request = axios.create({
|
||||
baseURL: import.meta.env.VITE_API_BASE_URL,
|
||||
timeout: import.meta.env.VITE_TIMEOUT, // 请求超时时间
|
||||
});
|
||||
|
||||
// 异常拦截处理器
|
||||
const errorHandler = async (error) => {
|
||||
console.log(error);
|
||||
|
||||
if (!error.response) {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
||||
const data = error.response.data;
|
||||
// 从 localstorage 获取 access_token
|
||||
const access_token = storage.get("Access-Token");
|
||||
// || data.msg === "用户未登录或登录已过期"
|
||||
if (data.status_code === 403) {
|
||||
storage.remove("Access-Token");
|
||||
storage.remove("Refresh-Token");
|
||||
|
||||
notification.error({
|
||||
message: "错误",
|
||||
description: data.msg,
|
||||
});
|
||||
router.push("/login");
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
||||
if (data.status_code === 401) {
|
||||
if (!access_token) {
|
||||
notification.error({
|
||||
message: "错误",
|
||||
description: data.msg,
|
||||
});
|
||||
router.push("/login");
|
||||
return Promise.reject(error);
|
||||
}
|
||||
|
||||
const refresh_token = storage.get("Refresh-Token");
|
||||
storage.remove("Access-Token");
|
||||
storage.remove("Refresh-Token");
|
||||
|
||||
return getNewToken({ refresh_token: refresh_token })
|
||||
.then((response) => {
|
||||
const result = response.data;
|
||||
if (result.status_code === 200) {
|
||||
save_token(
|
||||
result.data.access_token,
|
||||
result.data.refresh_token,
|
||||
result.data.expires_in
|
||||
);
|
||||
return request(error.response.config).then((response) => response);
|
||||
}
|
||||
|
||||
notification.error({
|
||||
message: "错误",
|
||||
description: result.msg,
|
||||
});
|
||||
router.push("/login");
|
||||
return Promise.reject(error);
|
||||
})
|
||||
.catch((error) => {
|
||||
notification.error({
|
||||
message: "错误",
|
||||
description: data.msg,
|
||||
});
|
||||
return Promise.reject(error);
|
||||
});
|
||||
} else {
|
||||
notification.error({
|
||||
message: "错误",
|
||||
description: data.msg,
|
||||
});
|
||||
return Promise.reject(error);
|
||||
}
|
||||
};
|
||||
|
||||
// 请求拦截器
|
||||
request.interceptors.request.use((config) => {
|
||||
const token = storage.get("Access-Token");
|
||||
// 如果 token 存在
|
||||
// 让每个请求携带自定义 token 请根据实际情况自行修改
|
||||
if (token) {
|
||||
config.headers["Authorization"] = "Bearer " + token;
|
||||
}
|
||||
return config;
|
||||
}, errorHandler);
|
||||
|
||||
// 响应拦截器
|
||||
request.interceptors.response.use((response) => {
|
||||
// 打印全局响应
|
||||
console.log(response);
|
||||
if (response.data.status_code !== 200) {
|
||||
notification.error({
|
||||
message: "错误",
|
||||
description: response.data.msg,
|
||||
});
|
||||
// 对请求错误做些什么
|
||||
return Promise.reject(response);
|
||||
}
|
||||
return response;
|
||||
}, errorHandler);
|
||||
|
||||
export default request;
|
||||
@@ -0,0 +1,131 @@
|
||||
import storage from "store";
|
||||
|
||||
export function timeFix() {
|
||||
const time = new Date();
|
||||
const hour = time.getHours();
|
||||
return hour < 9
|
||||
? "早上好"
|
||||
: hour <= 11
|
||||
? "上午好"
|
||||
: hour <= 13
|
||||
? "中午好"
|
||||
: hour < 20
|
||||
? "下午好"
|
||||
: "晚上好";
|
||||
}
|
||||
|
||||
export function getRangeDate(startDate, endDate) {
|
||||
let targetArr = [];
|
||||
let start = new Date(startDate);
|
||||
let end = new Date(endDate);
|
||||
let startDateInfo = {
|
||||
year: start.getFullYear(),
|
||||
month: start.getMonth() + 1,
|
||||
day: start.getDate(),
|
||||
};
|
||||
let endDateInfo = {
|
||||
year: end.getFullYear(),
|
||||
month: end.getMonth() + 1,
|
||||
day: end.getDate(),
|
||||
};
|
||||
if (startDateInfo.year === endDateInfo.year) {
|
||||
//同年
|
||||
if (startDateInfo.month !== endDateInfo.month) {
|
||||
//同年,不同月份
|
||||
//获取开始时间所在月的月底日期
|
||||
let startMax = new Date(
|
||||
startDateInfo.year,
|
||||
startDateInfo.month,
|
||||
0
|
||||
).getDate();
|
||||
let endNum = startMax - startDateInfo.day + endDateInfo.day;
|
||||
for (let i = startDateInfo.day; i <= startDateInfo.day + endNum; i++) {
|
||||
if (i > startMax) {
|
||||
targetArr.push(
|
||||
`${endDateInfo.year}-${
|
||||
endDateInfo.month < 10
|
||||
? "0" + endDateInfo.month
|
||||
: endDateInfo.month
|
||||
}-${i - startMax < 10 ? "0" + (i - startMax) : i - startMax}`
|
||||
);
|
||||
} else {
|
||||
targetArr.push(
|
||||
`${startDateInfo.year}-${
|
||||
startDateInfo.month < 10
|
||||
? "0" + startDateInfo.month
|
||||
: startDateInfo.month
|
||||
}-${i < 10 ? "0" + i : i}`
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//同年同月
|
||||
for (let i = startDateInfo.day; i <= endDateInfo.day; i++) {
|
||||
targetArr.push(
|
||||
`${startDateInfo.year}-${
|
||||
startDateInfo.month < 10
|
||||
? "0" + startDateInfo.month
|
||||
: startDateInfo.month
|
||||
}-${i < 10 ? "0" + i : i}`
|
||||
);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//不同年 【既然不同年那肯定也不同月】
|
||||
let startMax = new Date(
|
||||
startDateInfo.year,
|
||||
startDateInfo.month,
|
||||
0
|
||||
).getDate();
|
||||
let endNum = startMax - startDateInfo.day + endDateInfo.day;
|
||||
for (let i = startDateInfo.day; i <= startDateInfo.day + endNum; i++) {
|
||||
if (i > startMax) {
|
||||
targetArr.push(
|
||||
`${endDateInfo.year}-${
|
||||
endDateInfo.month < 10 ? "0" + endDateInfo.month : endDateInfo.month
|
||||
}-${i - startMax < 10 ? "0" + (i - startMax) : i - startMax}`
|
||||
);
|
||||
} else {
|
||||
targetArr.push(
|
||||
`${startDateInfo.year}-${
|
||||
startDateInfo.month < 10
|
||||
? "0" + startDateInfo.month
|
||||
: startDateInfo.month
|
||||
}-${i < 10 ? "0" + i : i}`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return targetArr;
|
||||
}
|
||||
|
||||
export function save_token(access_token, refresh_token, expires_in) {
|
||||
storage.set("Access-Token", access_token, new Date().getTime() + expires_in);
|
||||
storage.set("Refresh-Token", refresh_token);
|
||||
}
|
||||
|
||||
export function listToTree(list) {
|
||||
let resultList = list.filter((item) => {
|
||||
let children = list.filter((child) => {
|
||||
return item.id === child.parent_id;
|
||||
});
|
||||
if (children.length > 0) {
|
||||
item.children = children;
|
||||
}
|
||||
return item.parent_id === null;
|
||||
});
|
||||
return cloneDeep(resultList);
|
||||
}
|
||||
|
||||
export function cloneDeep(obj) {
|
||||
return JSON.parse(JSON.stringify(obj));
|
||||
}
|
||||
|
||||
export function isEmpty(obj) {
|
||||
if (obj === undefined || obj === null || obj === "") {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user