diff --git a/config/index.ts b/config/index.ts deleted file mode 100644 index e739ac8..0000000 --- a/config/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './proxy' diff --git a/config/proxy.ts b/config/proxy.ts deleted file mode 100644 index d724238..0000000 --- a/config/proxy.ts +++ /dev/null @@ -1,16 +0,0 @@ -import type { ProxyOptions } from 'vite' - -export function createViteProxy(isOpenProxy: boolean, viteEnv: ImportMetaEnv) { - if (!isOpenProxy) - return - - const proxy: Record = { - '/api': { - target: viteEnv.VITE_APP_API_BASE_URL, - changeOrigin: true, - rewrite: path => path.replace('/api/', '/'), - }, - } - - return proxy -} diff --git a/src/utils/crypto/index.ts b/src/utils/crypto/index.ts deleted file mode 100644 index 6c57c8d..0000000 --- a/src/utils/crypto/index.ts +++ /dev/null @@ -1,18 +0,0 @@ -import CryptoJS from 'crypto-js' - -const CryptoSecret = '__CRYPTO_SECRET__' - -export function enCrypto(data: any) { - const str = JSON.stringify(data) - return CryptoJS.AES.encrypt(str, CryptoSecret).toString() -} - -export function deCrypto(data: string) { - const bytes = CryptoJS.AES.decrypt(data, CryptoSecret) - const str = bytes.toString(CryptoJS.enc.Utf8) - - if (str) - return JSON.parse(str) - - return null -} diff --git a/src/utils/format/index.ts b/src/utils/format/index.ts deleted file mode 100644 index dbd5a08..0000000 --- a/src/utils/format/index.ts +++ /dev/null @@ -1,44 +0,0 @@ -/** - * 转义 HTML 字符 - * @param source - */ -export function encodeHTML(source: string) { - return source - .replace(/&/g, '&') - .replace(//g, '>') - .replace(/"/g, '"') - .replace(/'/g, ''') -} - -/** - * 判断是否为代码块 - * @param text - */ -export function includeCode(text: string | null | undefined) { - const regexp = /^(?:\s{4}|\t).+/gm - return !!(text?.includes(' = ') || text?.match(regexp)) -} - -/** - * 复制文本 - * @param options - */ -export function copyText(options: { text: string; origin?: boolean }) { - const props = { origin: true, ...options } - - let input: HTMLInputElement | HTMLTextAreaElement - - if (props.origin) - input = document.createElement('textarea') - else - input = document.createElement('input') - - input.setAttribute('readonly', 'readonly') - input.value = props.text - document.body.appendChild(input) - input.select() - if (document.execCommand('copy')) - document.execCommand('copy') - document.body.removeChild(input) -} diff --git a/src/utils/storage/index.ts b/src/utils/storage/index.ts index 87c2c0a..136ac91 100644 --- a/src/utils/storage/index.ts +++ b/src/utils/storage/index.ts @@ -1 +1,57 @@ -export * from './local' +interface StorageData { + data: T + expire: number | null +} + +export function createLocalStorage(options?: { expire?: number | null }) { + const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7 + + const { expire } = Object.assign({ expire: DEFAULT_CACHE_TIME }, options) + + function set(key: string, data: T) { + const storageData: StorageData = { + data, + expire: expire !== null ? new Date().getTime() + expire * 1000 : null, + } + + const json = JSON.stringify(storageData) + window.localStorage.setItem(key, json) + } + + function get(key: string) { + const json = window.localStorage.getItem(key) + if (json) { + let storageData: StorageData | null = null + + try { + storageData = JSON.parse(json) + } + catch { + // Prevent failure + } + + if (storageData) { + const { data, expire } = storageData + if (expire === null || expire >= Date.now()) + return data + } + + remove(key) + return null + } + } + + function remove(key: string) { + window.localStorage.removeItem(key) + } + + function clear() { + window.localStorage.clear() + } + + return { set, get, remove, clear } +} + +export const ls = createLocalStorage() + +export const ss = createLocalStorage({ expire: null }) diff --git a/src/utils/storage/local.ts b/src/utils/storage/local.ts deleted file mode 100644 index 215d22c..0000000 --- a/src/utils/storage/local.ts +++ /dev/null @@ -1,70 +0,0 @@ -import { deCrypto, enCrypto } from '../crypto' - -interface StorageData { - data: T - expire: number | null -} - -export function createLocalStorage(options?: { expire?: number | null; crypto?: boolean }) { - const DEFAULT_CACHE_TIME = 60 * 60 * 24 * 7 - - const { expire, crypto } = Object.assign( - { - expire: DEFAULT_CACHE_TIME, - crypto: true, - }, - options, - ) - - function set(key: string, data: T) { - const storageData: StorageData = { - data, - expire: expire !== null ? new Date().getTime() + expire * 1000 : null, - } - - const json = crypto ? enCrypto(storageData) : JSON.stringify(storageData) - window.localStorage.setItem(key, json) - } - - function get(key: string) { - const json = window.localStorage.getItem(key) - if (json) { - let storageData: StorageData | null = null - - try { - storageData = crypto ? deCrypto(json) : JSON.parse(json) - } - catch { - // Prevent failure - } - - if (storageData) { - const { data, expire } = storageData - if (expire === null || expire >= Date.now()) - return data - } - - remove(key) - return null - } - } - - function remove(key: string) { - window.localStorage.removeItem(key) - } - - function clear() { - window.localStorage.clear() - } - - return { - set, - get, - remove, - clear, - } -} - -export const ls = createLocalStorage() - -export const ss = createLocalStorage({ expire: null, crypto: false })