From 9e2fcf431ad4ec6494a4955e6a3095b56dc16eb2 Mon Sep 17 00:00:00 2001 From: Kerwin Date: Wed, 26 Apr 2023 21:06:06 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E5=A4=8D=E5=88=B6?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/index.ts | 1 - config/proxy.ts | 16 ----- src/components/common/Setting/About.vue | 3 + src/utils/copy.ts | 18 ++++++ src/utils/crypto/index.ts | 18 ------ src/utils/format/index.ts | 44 ------------- src/utils/storage/index.ts | 58 ++++++++++++++++- src/utils/storage/local.ts | 70 --------------------- src/views/chat/components/Message/Text.vue | 40 +++++++++++- src/views/chat/components/Message/index.vue | 18 +++++- src/views/chat/hooks/useCopyCode.ts | 24 ------- src/views/chat/index.vue | 3 - 12 files changed, 130 insertions(+), 183 deletions(-) delete mode 100644 config/index.ts delete mode 100644 config/proxy.ts create mode 100644 src/utils/copy.ts delete mode 100644 src/utils/crypto/index.ts delete mode 100644 src/utils/format/index.ts delete mode 100644 src/utils/storage/local.ts delete mode 100644 src/views/chat/hooks/useCopyCode.ts diff --git a/config/index.ts b/config/index.ts deleted file mode 100644 index e739ac85..00000000 --- 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 d724238b..00000000 --- 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/components/common/Setting/About.vue b/src/components/common/Setting/About.vue index 994622f0..ddf186e0 100644 --- a/src/components/common/Setting/About.vue +++ b/src/components/common/Setting/About.vue @@ -88,6 +88,9 @@ onMounted(() => {
+

+ Get Token +

{{ $t('setting.reverseProxy') }} diff --git a/src/utils/copy.ts b/src/utils/copy.ts new file mode 100644 index 00000000..6fd32d8b --- /dev/null +++ b/src/utils/copy.ts @@ -0,0 +1,18 @@ +export function copyToClip(text: string) { + return new Promise((resolve, reject) => { + try { + const input: HTMLTextAreaElement = document.createElement('textarea') + input.setAttribute('readonly', 'readonly') + input.value = text + document.body.appendChild(input) + input.select() + if (document.execCommand('copy')) + document.execCommand('copy') + document.body.removeChild(input) + resolve(text) + } + catch (error) { + reject(error) + } + }) +} diff --git a/src/utils/crypto/index.ts b/src/utils/crypto/index.ts deleted file mode 100644 index 6c57c8d3..00000000 --- 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 dbd5a08f..00000000 --- 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 87c2c0ad..136ac918 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 215d22c2..00000000 --- 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 }) diff --git a/src/views/chat/components/Message/Text.vue b/src/views/chat/components/Message/Text.vue index 1f82bfc0..f356f472 100644 --- a/src/views/chat/components/Message/Text.vue +++ b/src/views/chat/components/Message/Text.vue @@ -1,5 +1,5 @@