chore(projects): 添加判断和存储的工具函数

This commit is contained in:
‘chen.home’ 2022-08-06 20:17:31 +08:00
parent 6d8fda1f4f
commit 8f0adf25c9
6 changed files with 192 additions and 9 deletions

2
.env
View File

@ -4,3 +4,5 @@ VITE_BASE_URL=/
VITE_APP_TITLE = Ench Admin
# 路由模式
VITE_HASH_ROUTE = Y
# 存储前缀
VITE_STORAGE_PREFIX = ""

2
src/types/env.d.ts vendored
View File

@ -13,6 +13,8 @@ interface ImportMetaEnv {
readonly VITE_COMPRESS_TYPE?: 'gzip' | 'brotliCompress' | 'deflate' | 'deflateRaw';
/** hash路由模式 */
readonly VITE_HASH_ROUTE?: 'Y' | 'N';
/** 本地存储前缀 */
readonly VITE_STORAGE_PREFIX?: string;
}
interface ImportMeta {

97
src/utils/is.ts Normal file
View File

@ -0,0 +1,97 @@
/* eslint-disable */
const toString = Object.prototype.toString;
export function is(val: unknown, type: string) {
return toString.call(val) === `[object ${type}]`;
}
export function isDef<T = unknown>(val?: T): val is T {
return typeof val !== 'undefined';
}
export function isUnDef<T = unknown>(val?: T): val is T {
return !isDef(val);
}
export function isObject(val: any): val is Record<any, any> {
return val !== null && is(val, 'Object');
}
export function isEmpty<T = unknown>(val: T): val is T {
if (isArray(val) || isString(val)) {
return val.length === 0;
}
if (val instanceof Map || val instanceof Set) {
return val.size === 0;
}
if (isObject(val)) {
return Object.keys(val).length === 0;
}
return false;
}
export function isDate(val: unknown): val is Date {
return is(val, 'Date');
}
export function isNull(val: unknown): val is null {
return val === null;
}
export function isNullAndUnDef(val: unknown): val is null | undefined {
return isUnDef(val) && isNull(val);
}
export function isNullOrUnDef(val: unknown): val is null | undefined {
return isUnDef(val) || isNull(val);
}
export function isNumber(val: unknown): val is number {
return is(val, 'Number');
}
export function isPromise<T = any>(val: unknown): val is Promise<T> {
return is(val, 'Promise') && isObject(val) && isFunction(val.then) && isFunction(val.catch);
}
export function isString(val: unknown): val is string {
return is(val, 'String');
}
export function isFunction(val: unknown): val is Function {
return typeof val === 'function';
}
export function isBoolean(val: unknown): val is boolean {
return is(val, 'Boolean');
}
export function isRegExp(val: unknown): val is RegExp {
return is(val, 'RegExp');
}
export function isArray(val: any): val is Array<any> {
return val && Array.isArray(val);
}
export function isWindow(val: any): val is Window {
return typeof window !== 'undefined' && is(val, 'Window');
}
export function isElement(val: unknown): val is Element {
return isObject(val) && !!val.tagName;
}
export const isServer = typeof window === 'undefined';
export const isClient = !isServer;
export function isUrl<T>(path: T): boolean {
const reg =
/(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;
// @ts-expect-error
return reg.test(path);
}

76
src/utils/storage.ts Normal file
View File

@ -0,0 +1,76 @@
// 默认缓存期限为7天
const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7;
// 读取缓存前缀
const prefix = import.meta.env.VITE_STORAGE_PREFIX as string;
interface StorageData {
value: any;
expire: number | null;
}
/**
* LocalStorage部分操作
*/
export const setLocal = (key: string, value: unknown, expire: number | null = DEFAULT_CACHE_TIME): void => {
const storageData: StorageData = {
value,
expire: expire !== null ? new Date().getTime() + expire * 1000 : null,
};
const json = JSON.stringify(storageData);
localStorage.setItem(prefix + key, json);
};
export const getLocal = (key: string) => {
const json = localStorage.getItem(prefix + key);
if (!json) return null;
let storageData: StorageData | null = null;
storageData = JSON.parse(json as string);
if (storageData) {
const { value, expire } = storageData;
if (expire === null || expire >= Date.now()) {
return value;
}
}
removeLocal(key);
return null;
};
export const removeLocal = (key: string): void => {
localStorage.removeItem(prefix + key);
};
export const clearLocal = (): void => {
localStorage.clear();
};
/**
* sessionStorage部分操作
*/
export function setSession(key: string, value: unknown) {
const json = JSON.stringify(value);
sessionStorage.setItem(prefix + key, json);
}
export function getSession<T>(key: string) {
const json = sessionStorage.getItem(prefix + key);
let data: T | null = null;
if (json) {
try {
data = JSON.parse(json);
} catch {
// 防止解析失败
}
}
return data;
}
export function removeSession(key: string) {
window.sessionStorage.removeItem(prefix + key);
}
export function clearSession() {
window.sessionStorage.clear();
}

15
src/utils/token.ts Normal file
View File

@ -0,0 +1,15 @@
import { setLocal, getLocal, removeLocal } from './storage';
const TOKEN_CODE = 'access_token';
const DURATION = 6 * 60 * 60;
export function getToken() {
return getLocal(TOKEN_CODE);
}
export function setToken(token: any) {
setLocal(TOKEN_CODE, token, DURATION);
}
export function removeToken() {
removeLocal(TOKEN_CODE);
}

View File

@ -1,14 +1,5 @@
<template>
<div text-center c-blue>I prove that you have made the jump test1.</div>
<n-space m-50>
<n-button>Default</n-button>
<n-button type="tertiary">Tertiary</n-button>
<n-button type="primary">Primary</n-button>
<n-button type="info">Info</n-button>
<n-button type="success">Success</n-button>
<n-button type="warning">Warning</n-button>
<n-button type="error">Error</n-button>
</n-space>
</template>
<script setup lang="ts"></script>