首页+登录页
This commit is contained in:
@@ -0,0 +1,161 @@
|
||||
// 将res的key对应的值复制给dzdata的同名key
|
||||
/**
|
||||
* 将源对象res的属性复制到目标对象dzdata中。
|
||||
* @param {Object} res - 源对象,其属性将被复制。
|
||||
* @param {Object} dzdata - 目标对象,将接收源对象的属性。
|
||||
* @example
|
||||
* ObjectCopy({a: 1, b: 2}, {c: 3}); // dzdata 变为 {a: 1, b: 2, c: 3}
|
||||
*/
|
||||
export function ObjectCopy<T extends Record<string, any>>(res: Partial<T>, dzdata: T): void {
|
||||
Object.keys(dzdata).forEach(key => {
|
||||
if (res.hasOwnProperty(key)) {
|
||||
(dzdata as Record<string, any>)[key] = res[key];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 时间格式化
|
||||
* @param {*} time 传入时间参数,支持字符串和时间戳
|
||||
* @param {*} f 对应格式 "YYYY-MM-DD hh:mm:ss" "YYYY年MM月DD日hh时mm分ss秒"
|
||||
* 格式可以自定义,对应的字符串对应的时间会更新,输入单个字符表示可以不补零
|
||||
* @return {*} 返回格式化的日期
|
||||
*/
|
||||
export function formatTime(time: string | number | Date, f: string): string | undefined {
|
||||
const date = new Date(time);
|
||||
if (!(date instanceof Date && !isNaN(date.getTime()))) {
|
||||
console.error("不合法的日期!");
|
||||
return;
|
||||
}
|
||||
|
||||
const year = date.getFullYear();
|
||||
const month = date.getMonth() + 1;
|
||||
const day = date.getDate();
|
||||
const hour = date.getHours();
|
||||
const minute = date.getMinutes();
|
||||
const second = date.getSeconds();
|
||||
|
||||
const pad = (num: number): string => num.toString().padStart(2, "0");
|
||||
const monthAdd0 = pad(month);
|
||||
const dayAdd0 = pad(day);
|
||||
const hourAdd0 = pad(hour);
|
||||
const minuteAdd0 = pad(minute);
|
||||
const secondAdd0 = pad(second);
|
||||
|
||||
let str = f.toString();
|
||||
str = str.replace("YYYY", year.toString());
|
||||
|
||||
if (f.includes("M")) {
|
||||
str = f.includes("MM") ? str.replace("MM", monthAdd0) : str.replace("M", month.toString());
|
||||
}
|
||||
if (f.includes("D")) {
|
||||
str = f.includes("DD") ? str.replace("DD", dayAdd0) : str.replace("D", day.toString());
|
||||
}
|
||||
if (f.includes("h")) {
|
||||
str = f.includes("hh") ? str.replace("hh", hourAdd0) : str.replace("h", hour.toString());
|
||||
}
|
||||
if (f.includes("m")) {
|
||||
str = f.includes("mm") ? str.replace("mm", minuteAdd0) : str.replace("m", minute.toString());
|
||||
}
|
||||
if (f.includes("s")) {
|
||||
str = f.includes("ss") ? str.replace("ss", secondAdd0) : str.replace("s", second.toString());
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description: 补零函数 为时间服务 不足两位的自动在数字前面加上0
|
||||
* @param {*} n 待补零数字
|
||||
* @return {*} 补零后数字
|
||||
*/
|
||||
function timeAdd0(n: number): string {
|
||||
return n.toString().padStart(2, "0");
|
||||
}
|
||||
|
||||
export function deepclone<T>(obj: T): T {
|
||||
if (obj === null || typeof obj !== "object") {
|
||||
return obj;
|
||||
}
|
||||
|
||||
let newobj: any = obj instanceof Array ? [] : {};
|
||||
|
||||
if (window.JSON) {
|
||||
newobj = JSON.parse(JSON.stringify(obj));
|
||||
} else {
|
||||
for (const i in obj) {
|
||||
newobj[i] = typeof obj[i] === "object" ? deepclone(obj[i]) : obj[i];
|
||||
}
|
||||
}
|
||||
return newobj as T;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据日期格式化时间。
|
||||
* @param {Date} date - 需要格式化的日期对象。
|
||||
* @param {string} format - 时间格式字符串。
|
||||
* @returns {string} 格式化后的时间字符串。
|
||||
* @example
|
||||
* formatTimeBydate(new Date(), 'yyyy-MM-dd HH:mm:ss');
|
||||
*/
|
||||
export function formatTimeBydate(this: Date, f: string): string | undefined {
|
||||
return formatTime(this, f);
|
||||
}
|
||||
|
||||
export function debounce<T extends (...args: any[]) => any>(fn: T, delay: number): (...args: Parameters<T>) => void {
|
||||
let timer: ReturnType<typeof setTimeout> | null = null;
|
||||
return function (this: ThisParameterType<T>, ...args: Parameters<T>): void {
|
||||
const _this = this;
|
||||
if (timer) {
|
||||
clearTimeout(timer);
|
||||
timer = null;
|
||||
}
|
||||
timer = setTimeout(() => {
|
||||
fn.apply(_this, args);
|
||||
}, delay);
|
||||
};
|
||||
}
|
||||
|
||||
export function throttle<T extends (...args: any[]) => any>(fn: T, delay: number): (...args: Parameters<T>) => void {
|
||||
let timer: ReturnType<typeof setTimeout> | null = null;
|
||||
return function (this: ThisParameterType<T>, ...args: Parameters<T>): void {
|
||||
const _this = this;
|
||||
if (!timer) {
|
||||
timer = setTimeout(() => {
|
||||
fn.apply(_this, args);
|
||||
timer = null;
|
||||
}, delay);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// 字典查询 通个一个字段返回另一个字段的值
|
||||
/**
|
||||
* @description 字典查询
|
||||
* @param {*} dict 字典数组
|
||||
* @param {*} ckey 查询字段
|
||||
* @param {*} cvalue 查询值
|
||||
* @param {*} rkey 返回字段
|
||||
* @return {*} 返回值
|
||||
*/
|
||||
|
||||
export function getDictValue<T extends Record<string, any>>(dict: T[], ckey: keyof T, cvalue: T[keyof T], rkey: keyof T): T[keyof T] | string {
|
||||
let result = "";
|
||||
dict.forEach(item => {
|
||||
if (item[ckey] === cvalue) {
|
||||
result = item[rkey];
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
// 分割数组
|
||||
export function chunkArrayInGroups<T>(arr: T[], size: number): T[][] {
|
||||
return Array.from({ length: Math.ceil(arr.length / size) }, (_, i) => arr.slice(i * size, i * size + size));
|
||||
}
|
||||
// 重置对象
|
||||
export function resetObject<T extends Record<string, any>>(obj: T): void {
|
||||
Object.keys(obj).forEach(key => {
|
||||
(obj as Record<string, any>)[key] = null;
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
import type { AxiosError, AxiosResponse, InternalAxiosRequestConfig } from "axios";
|
||||
import axios from "axios";
|
||||
import { useCookies } from "vue3-cookies";
|
||||
|
||||
const { cookies } = useCookies();
|
||||
|
||||
// const baseURL: string = "http://127.0.0.1:7777" + "/api";
|
||||
const baseURL: string = "https://www.hxyouzi.com" + "/api";
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
};
|
||||
|
||||
const request = axios.create({
|
||||
baseURL,
|
||||
headers,
|
||||
});
|
||||
|
||||
// 添加请求拦截器
|
||||
request.interceptors.request.use(
|
||||
(config: InternalAxiosRequestConfig) => {
|
||||
const token = cookies.get("token");
|
||||
if (token && typeof token === "string" && token.trim() !== "") {
|
||||
config.headers["Authorization"] = "Bearer " + token;
|
||||
} else delete config.headers["Authorization"];
|
||||
return config;
|
||||
},
|
||||
function (error: AxiosError): string {
|
||||
// 对请求错误做些什么
|
||||
return error.message || "Request error";
|
||||
}
|
||||
);
|
||||
|
||||
// 添加响应拦截器
|
||||
request.interceptors.response.use(
|
||||
function (response: AxiosResponse): any {
|
||||
// 2xx 范围内的状态码都会触发该函数。
|
||||
// 对响应数据做点什么
|
||||
return response.data;
|
||||
},
|
||||
async function (error: AxiosError): Promise<string> {
|
||||
// 超出 2xx 范围的状态码都会触发该函数。
|
||||
// 对响应错误做点什么
|
||||
console.log("Response error", error);
|
||||
|
||||
// if (error.response?.status === 401) {
|
||||
// window.$msg.warning("无效的token");
|
||||
// cookies.remove("token");
|
||||
// cookies.remove("userinfo");
|
||||
// router.replace("/login");
|
||||
// return "Unauthorized";
|
||||
// }
|
||||
|
||||
return error.message || "Response error";
|
||||
}
|
||||
);
|
||||
|
||||
export default request;
|
||||
Reference in New Issue
Block a user