初始化仓库
This commit is contained in:
15
src/utils/auth.ts
Normal file
15
src/utils/auth.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import Cookies from 'js-cookie';
|
||||
|
||||
const TokenKey = 'Admin-Token'
|
||||
|
||||
export function getToken() {
|
||||
return Cookies.get(TokenKey)
|
||||
}
|
||||
|
||||
export function setToken(token: string) {
|
||||
return Cookies.set(TokenKey, token)
|
||||
}
|
||||
|
||||
export function removeToken() {
|
||||
return Cookies.remove(TokenKey)
|
||||
}
|
||||
71
src/utils/dateUtils.ts
Normal file
71
src/utils/dateUtils.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { ref, onMounted, onBeforeUnmount } from 'vue';
|
||||
|
||||
/**
|
||||
* 格式化日期时间
|
||||
* @param date 日期对象或时间戳
|
||||
* @param format 格式字符串,默认 'YYYY-MM-DD HH:mm:ss'
|
||||
* @returns 格式化后的时间字符串
|
||||
*/
|
||||
export function formatDateTime(date: Date | number | string, format = 'YYYY-MM-DD HH:mm:ss'): string {
|
||||
const d = new Date(date);
|
||||
|
||||
if (isNaN(d.getTime())) {
|
||||
return '';
|
||||
}
|
||||
|
||||
const year = d.getFullYear();
|
||||
const month = String(d.getMonth() + 1).padStart(2, '0');
|
||||
const day = String(d.getDate()).padStart(2, '0');
|
||||
const hours = String(d.getHours()).padStart(2, '0');
|
||||
const minutes = String(d.getMinutes()).padStart(2, '0');
|
||||
const seconds = String(d.getSeconds()).padStart(2, '0');
|
||||
|
||||
return format
|
||||
.replace('YYYY', String(year))
|
||||
.replace('MM', month)
|
||||
.replace('DD', day)
|
||||
.replace('HH', hours)
|
||||
.replace('mm', minutes)
|
||||
.replace('ss', seconds);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前时间字符串
|
||||
* @param format 格式字符串
|
||||
* @returns 当前时间字符串
|
||||
*/
|
||||
export function getCurrentTime(format = 'YYYY-MM-DD HH:mm:ss'): string {
|
||||
return formatDateTime(new Date(), format);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取实时时间(用于显示)
|
||||
* @param format 格式字符串
|
||||
* @returns 响应式时间字符串
|
||||
*/
|
||||
export function useRealTime(format = 'YYYY-MM-DD HH:mm:ss') {
|
||||
const currentTime = ref(getCurrentTime(format));
|
||||
let timer: NodeJS.Timeout | null = null;
|
||||
|
||||
const startTimer = () => {
|
||||
timer = setInterval(() => {
|
||||
currentTime.value = getCurrentTime(format);
|
||||
}, 1000);
|
||||
};
|
||||
|
||||
const stopTimer = () => {
|
||||
if (timer) {
|
||||
clearInterval(timer);
|
||||
timer = null;
|
||||
}
|
||||
};
|
||||
|
||||
onMounted(startTimer);
|
||||
onBeforeUnmount(stopTimer);
|
||||
|
||||
return {
|
||||
currentTime,
|
||||
startTimer,
|
||||
stopTimer
|
||||
};
|
||||
}
|
||||
33
src/utils/useDialog.ts
Normal file
33
src/utils/useDialog.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { ref } from 'vue';
|
||||
|
||||
/**
|
||||
* Dialog控制Hook
|
||||
* @returns visible(显隐状态)、show(显示)、hide(隐藏)、toggle(切换)
|
||||
*/
|
||||
export function useDialog(initialVisible:boolean = false): {
|
||||
visible: import('vue').Ref<boolean>,
|
||||
show: () => void,
|
||||
hide: () => void,
|
||||
toggle: () => void
|
||||
} {
|
||||
const visible = ref(initialVisible);
|
||||
|
||||
const show = () => {
|
||||
visible.value = true;
|
||||
};
|
||||
|
||||
const hide = () => {
|
||||
visible.value = false;
|
||||
};
|
||||
|
||||
const toggle = () => {
|
||||
visible.value = !visible.value;
|
||||
};
|
||||
|
||||
return {
|
||||
visible,
|
||||
show,
|
||||
hide,
|
||||
toggle
|
||||
};
|
||||
}
|
||||
41
src/utils/useLoading.ts
Normal file
41
src/utils/useLoading.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { ref } from 'vue';
|
||||
|
||||
/**
|
||||
* Loading控制Hook
|
||||
* @returns loading(加载状态)、startLoading(开始加载)、stopLoading(停止加载)、withLoading(包装异步函数)
|
||||
*/
|
||||
export function useLoading(initialLoading = false) {
|
||||
const loading = ref(initialLoading);
|
||||
|
||||
const startLoading = () => {
|
||||
loading.value = true;
|
||||
};
|
||||
|
||||
const stopLoading = () => {
|
||||
loading.value = false;
|
||||
};
|
||||
|
||||
/**
|
||||
* 包装异步函数,自动控制loading状态
|
||||
* @param fn 异步函数
|
||||
* @returns 包装后的函数
|
||||
*/
|
||||
const withLoading = <T extends (...args: any[]) => Promise<any>>(fn: T): T => {
|
||||
return (async (...args: any[]) => {
|
||||
startLoading();
|
||||
try {
|
||||
const result = await fn(...args);
|
||||
return result;
|
||||
} finally {
|
||||
stopLoading();
|
||||
}
|
||||
}) as T;
|
||||
};
|
||||
|
||||
return {
|
||||
loading,
|
||||
startLoading,
|
||||
stopLoading,
|
||||
withLoading
|
||||
};
|
||||
}
|
||||
11
src/utils/uuidUtils.ts
Normal file
11
src/utils/uuidUtils.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
/**
|
||||
* 生成一个随机的UUID v4
|
||||
* @returns 生成的UUID字符串
|
||||
*/
|
||||
export function generateUUID(): string {
|
||||
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
||||
const r = Math.random() * 16 | 0;
|
||||
const v = c === 'x' ? r : (r & 0x3 | 0x8);
|
||||
return v.toString(16);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user