From 0270903d6891a8181a2abbee17a3b9ba47c6648d Mon Sep 17 00:00:00 2001 From: hbrwang Date: Mon, 23 Nov 2020 17:24:43 +0800 Subject: [PATCH] refactor requests --- web/frps/package.json | 3 +- web/frps/src/App.vue | 10 +- web/frps/src/components/Overview.vue | 121 +++++++++++------------ web/frps/src/components/ProxiesHttp.vue | 49 ++++----- web/frps/src/components/ProxiesHttps.vue | 50 ++++------ web/frps/src/components/ProxiesStcp.vue | 26 ++--- web/frps/src/components/ProxiesTcp.vue | 36 +++---- web/frps/src/components/ProxiesUdp.vue | 28 ++---- web/frps/src/components/Traffic.vue | 31 ++---- web/frps/src/main.js | 8 +- web/frps/src/store/index.js | 24 +++++ web/frps/src/utils/fetch.js | 20 ++++ web/frps/vue.config.js | 2 +- 13 files changed, 200 insertions(+), 208 deletions(-) create mode 100644 web/frps/src/store/index.js create mode 100644 web/frps/src/utils/fetch.js diff --git a/web/frps/package.json b/web/frps/package.json index 6fa17146..b777b71f 100644 --- a/web/frps/package.json +++ b/web/frps/package.json @@ -15,7 +15,8 @@ "core-js": "^3.7.0", "vue": "^2.6.12", "vue-router": "^3.4.9", - "whatwg-fetch": "^3.5.0" + "whatwg-fetch": "^3.5.0", + "vuex": "^3.5.1" }, "devDependencies": { "@vue/cli-plugin-babel": "~4.5.9", diff --git a/web/frps/src/App.vue b/web/frps/src/App.vue index 43372ad4..3ebd41f9 100644 --- a/web/frps/src/App.vue +++ b/web/frps/src/App.vue @@ -24,7 +24,7 @@
- +
@@ -34,6 +34,14 @@ - - diff --git a/web/frps/src/components/ProxiesHttps.vue b/web/frps/src/components/ProxiesHttps.vue index af7b2a07..cb772291 100644 --- a/web/frps/src/components/ProxiesHttps.vue +++ b/web/frps/src/components/ProxiesHttps.vue @@ -67,11 +67,13 @@ export default { subdomain_host: '' } }, - watch: { - $route: 'fetchData' + computed: { + serverInfo() { + return this.$store.state.serverInfo + } }, - created() { - this.fetchData() + mounted() { + this.initData() }, methods: { formatTrafficIn(row, column) { @@ -80,33 +82,21 @@ export default { formatTrafficOut(row, column) { return Humanize.fileSize(row.traffic_out) }, - fetchData() { - fetch('/api/serverinfo', { credentials: 'include' }) - .then(res => { - return res.json() - }) - .then(json => { - this.vhost_https_port = json.vhost_https_port - this.subdomain_host = json.subdomain_host - if (this.vhost_https_port == null || this.vhost_https_port === 0) { - return - } else { - fetch('/api/proxy/https', { credentials: 'include' }) - .then(res => { - return res.json() - }) - .then(json => { - this.proxies = [] - for (const proxyStats of json.proxies) { - this.proxies.push(new HttpsProxy(proxyStats, this.vhost_https_port, this.subdomain_host)) - } - }) - } - }) + async initData() { + if (!this.serverInfo) return + + this.vhost_https_port = this.serverInfo.vhost_https_port + this.subdomain_host = this.serverInfo.subdomain_host + if (this.vhost_https_port == null || this.vhost_https_port === 0) return + + const json = await this.$fetch('proxy/https') + if (!json) return + + this.proxies = [] + for (const proxyStats of json.proxies) { + this.proxies.push(new HttpsProxy(proxyStats, this.vhost_https_port, this.subdomain_host)) + } } } } - - diff --git a/web/frps/src/components/ProxiesStcp.vue b/web/frps/src/components/ProxiesStcp.vue index ce3c9e03..fca0f3e8 100644 --- a/web/frps/src/components/ProxiesStcp.vue +++ b/web/frps/src/components/ProxiesStcp.vue @@ -60,11 +60,8 @@ export default { proxies: [] } }, - watch: { - $route: 'fetchData' - }, - created() { - this.fetchData() + mounted() { + this.initData() }, methods: { formatTrafficIn(row, column) { @@ -73,17 +70,14 @@ export default { formatTrafficOut(row, column) { return Humanize.fileSize(row.traffic_out) }, - fetchData() { - fetch('/api/proxy/stcp', { credentials: 'include' }) - .then(res => { - return res.json() - }) - .then(json => { - this.proxies = [] - for (const proxyStats of json.proxies) { - this.proxies.push(new StcpProxy(proxyStats)) - } - }) + async initData() { + const json = await this.$fetch('proxy/stcp') + if (!json) return + + this.proxies = [] + for (const proxyStats of json.proxies) { + this.proxies.push(new StcpProxy(proxyStats)) + } } } } diff --git a/web/frps/src/components/ProxiesTcp.vue b/web/frps/src/components/ProxiesTcp.vue index b7bc4403..f69189ed 100644 --- a/web/frps/src/components/ProxiesTcp.vue +++ b/web/frps/src/components/ProxiesTcp.vue @@ -3,13 +3,13 @@