From 519b62d43f70a1e21d2cda7f971b326e521cbd61 Mon Sep 17 00:00:00 2001 From: "hubery.wang" Date: Fri, 4 Dec 2020 01:19:56 +0800 Subject: [PATCH] edit fetch --- web/frps/src/components/Traffic.vue | 8 ++++++-- web/frps/src/store/modules/server.js | 10 +++++++--- web/frps/src/utils/fetch.js | 14 ++++---------- web/frps/src/views/Overview.vue | 1 + web/frps/src/views/ProxiesHttp.vue | 7 ++++++- web/frps/src/views/ProxiesHttps.vue | 7 ++++++- web/frps/src/views/ProxiesStcp.vue | 7 ++++++- web/frps/src/views/ProxiesTcp.vue | 7 ++++++- web/frps/src/views/ProxiesUdp.vue | 7 ++++++- 9 files changed, 48 insertions(+), 20 deletions(-) diff --git a/web/frps/src/components/Traffic.vue b/web/frps/src/components/Traffic.vue index 9b44b81c..46adef8a 100644 --- a/web/frps/src/components/Traffic.vue +++ b/web/frps/src/components/Traffic.vue @@ -16,8 +16,12 @@ export default { }, methods: { async initData() { - const json = await this.$fetch(`traffic/${this.proxyName}`) - if (!json) return + const res = await this.$fetch(`traffic/${this.proxyName}`) + if (!res.ok) { + this.$message.warning('Get traffic info from frps failed!') + return + } + const json = await res.json() DrawProxyTrafficChart(this.proxyName, json.traffic_in, json.traffic_out) } diff --git a/web/frps/src/store/modules/server.js b/web/frps/src/store/modules/server.js index 6ef4ee7d..9c547cd5 100644 --- a/web/frps/src/store/modules/server.js +++ b/web/frps/src/store/modules/server.js @@ -15,9 +15,13 @@ const mutations = { const actions = { async fetchServerInfo({ commit }) { - const json = await fetch('serverinfo') - commit('SET_SERVER_INFO', json || null) - return json + const res = await fetch('serverinfo') + if (!res.ok) { + this.$message.warning('Get server info from frps failed!') + commit('SET_SERVER_INFO', null) + } + + commit('SET_SERVER_INFO', (await res.json()) || null) } } diff --git a/web/frps/src/utils/fetch.js b/web/frps/src/utils/fetch.js index 4fc354aa..4be2e1e1 100644 --- a/web/frps/src/utils/fetch.js +++ b/web/frps/src/utils/fetch.js @@ -1,20 +1,14 @@ import { Message } from 'element-ui' export default function(api, init = {}) { - return new Promise(resolve => { + return new Promise((resolve, reject) => { fetch(`/api/${api}`, Object.assign({ credentials: 'include' }, init)) .then(res => { - if (res.status < 200 || res.status >= 300) { - Message.warning('Get server info from frps failed!') - resolve() - return - } - - resolve(res ? res.json() : undefined) + resolve(res) }) .catch(err => { - this.$message.error(err.message) - resolve() + Message.error(err.message) + reject() }) }) } diff --git a/web/frps/src/views/Overview.vue b/web/frps/src/views/Overview.vue index c377601f..b1ef5303 100644 --- a/web/frps/src/views/Overview.vue +++ b/web/frps/src/views/Overview.vue @@ -82,6 +82,7 @@ export default { methods: { initData() { if (!this.serverInfo) return + console.log('serverInfo', this.serverInfo) this.version = this.serverInfo.version this.bind_port = this.serverInfo.bind_port diff --git a/web/frps/src/views/ProxiesHttp.vue b/web/frps/src/views/ProxiesHttp.vue index d9a2f98b..f285526c 100644 --- a/web/frps/src/views/ProxiesHttp.vue +++ b/web/frps/src/views/ProxiesHttp.vue @@ -95,7 +95,12 @@ export default { this.subdomain_host = this.serverInfo.subdomain_host if (this.vhost_http_port == null || this.vhost_http_port === 0) return - const json = await this.$fetch('proxy/http') + const res = await this.$fetch('proxy/http') + if (!res.ok) { + this.$message.warning('Get proxy info from frps failed!') + return + } + const json = await res.json() if (!json) return this.proxies = [] diff --git a/web/frps/src/views/ProxiesHttps.vue b/web/frps/src/views/ProxiesHttps.vue index 84fe7abb..a584f8e0 100644 --- a/web/frps/src/views/ProxiesHttps.vue +++ b/web/frps/src/views/ProxiesHttps.vue @@ -90,7 +90,12 @@ export default { 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') + const res = await this.$fetch('proxy/https') + if (!res.ok) { + this.$message.warning('Get proxy info from frps failed!') + return + } + const json = await res.json() if (!json) return this.proxies = [] diff --git a/web/frps/src/views/ProxiesStcp.vue b/web/frps/src/views/ProxiesStcp.vue index 41cf97ef..05dd5dec 100644 --- a/web/frps/src/views/ProxiesStcp.vue +++ b/web/frps/src/views/ProxiesStcp.vue @@ -71,7 +71,12 @@ export default { return Humanize.fileSize(row.traffic_out) }, async initData() { - const json = await this.$fetch('proxy/stcp') + const res = await this.$fetch('proxy/stcp') + if (!res.ok) { + this.$message.warning('Get proxy info from frps failed!') + return + } + const json = await res.json() if (!json) return this.proxies = [] diff --git a/web/frps/src/views/ProxiesTcp.vue b/web/frps/src/views/ProxiesTcp.vue index be12ded2..3d127ef6 100644 --- a/web/frps/src/views/ProxiesTcp.vue +++ b/web/frps/src/views/ProxiesTcp.vue @@ -75,7 +75,12 @@ export default { return Humanize.fileSize(row.traffic_out) }, async initData() { - const json = await this.$fetch('proxy/tcp') + const res = await this.$fetch('proxy/tcp') + if (!res.ok) { + this.$message.warning('Get proxy info from frps failed!') + return + } + const json = await res.json() if (!json) return this.proxies = [] diff --git a/web/frps/src/views/ProxiesUdp.vue b/web/frps/src/views/ProxiesUdp.vue index a9ea2975..6302c8ea 100644 --- a/web/frps/src/views/ProxiesUdp.vue +++ b/web/frps/src/views/ProxiesUdp.vue @@ -73,7 +73,12 @@ export default { return Humanize.fileSize(row.traffic_out) }, async initData() { - const json = await this.$fetch('proxy/udp') + const res = await this.$fetch('proxy/udp') + if (!res.ok) { + this.$message.warning('Get proxy info from frps failed!') + return + } + const json = await res.json() if (!json) return this.proxies = []