From b6e764b0dd25471537807aff65504a88bda5414c Mon Sep 17 00:00:00 2001 From: CareyWong Date: Wed, 19 Aug 2020 12:55:22 +0800 Subject: [PATCH] =?UTF-8?q?Feat=EF=BC=9AAdd=20sub=20url=20cache?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 4 ++++ src/views/Subconverter.vue | 41 +++++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/.env b/.env index 21fb037..b6de37a 100644 --- a/.env +++ b/.env @@ -14,3 +14,7 @@ VUE_APP_MYURLS_DEFAULT_BACKEND = "https://suo.yt" # 文本托管后端 VUE_APP_CONFIG_UPLOAD_BACKEND = "https://api.wcc.best" + +# 页面配置 +VUE_APP_USE_STORAGE = true +VUE_APP_CACHE_TTL = 86400 diff --git a/src/views/Subconverter.vue b/src/views/Subconverter.vue index b3c031a..1deadda 100644 --- a/src/views/Subconverter.vue +++ b/src/views/Subconverter.vue @@ -21,6 +21,7 @@ type="textarea" rows="3" placeholder="支持订阅或ss/ssr/vmess单链接。多个链接请每行一个或用 | 分隔" + @blur="saveSubUrl" /> @@ -390,6 +391,11 @@ export default { created() { document.title = "Subscription Converter"; this.isPC = this.$getOS().isPc; + + // 获取 url cache + if (process.env.VUE_APP_USE_STORAGE === 'true') { + this.form.sourceSubUrl = this.getLocalStorageItem('sourceSubUrl') + } }, mounted() { this.form.clientType = "clash"; @@ -623,7 +629,40 @@ export default { this.backendVersion = res.data.replace(/backend\n$/gm, ""); this.backendVersion = this.backendVersion.replace("subconverter", ""); }); + }, + saveSubUrl() { + if (this.form.sourceSubUrl !== '') { + this.setLocalStorageItem('sourceSubUrl', this.form.sourceSubUrl) + } + }, + getLocalStorageItem(itemKey) { + const now = +new Date() + let ls = localStorage.getItem(itemKey) + + let itemValue = '' + if (ls !== null) { + let data = JSON.parse(ls) + if (data.expire > now) { + itemValue = data.value + } else { + localStorage.removeItem(itemKey) + } + } + + return itemValue + }, + setLocalStorageItem(itemKey, itemValue) { + const ttl = process.env.VUE_APP_CACHE_TTL + const now = +new Date() + + let data = { + setTime: now, + ttl: parseInt(ttl), + expire: now + ttl * 1000, + value: itemValue + } + localStorage.setItem(itemKey, JSON.stringify(data)) } - } + }, };