Fix endpoint issues

This commit is contained in:
liangping 2022-02-06 10:17:18 +08:00
parent 85ca2e3989
commit 1a348f6d3e
2 changed files with 40 additions and 41 deletions

View File

@ -54,7 +54,6 @@
<small id="data-provider"> <small id="data-provider">
{{ currentApi }} ({{ selected_chain.sdk_version || '-' }}) {{ currentApi }} ({{ selected_chain.sdk_version || '-' }})
<b-dropdown <b-dropdown
v-if="apiOptions.length > 1"
class="ml-0" class="ml-0"
variant="flat-primary" variant="flat-primary"
no-caret no-caret
@ -163,7 +162,6 @@ import DarkToggler from '@core/layouts/components/app-navbar/components/DarkTogg
import Locale from '@core/layouts/components/app-navbar/components/Locale.vue' import Locale from '@core/layouts/components/app-navbar/components/Locale.vue'
import SearchBar from '@core/layouts/components/app-navbar/components/SearchBar.vue' import SearchBar from '@core/layouts/components/app-navbar/components/SearchBar.vue'
// import CartDropdown from '@core/layouts/components/app-navbar/components/CartDropdown.vue' // import CartDropdown from '@core/layouts/components/app-navbar/components/CartDropdown.vue'
import store from '@/store'
import { getLocalAccounts, timeIn, toDay } from '@/libs/utils' import { getLocalAccounts, timeIn, toDay } from '@/libs/utils'
// import UserDropdown from '@core/layouts/components/app-navbar/components/UserDropdown.vue' // import UserDropdown from '@core/layouts/components/app-navbar/components/UserDropdown.vue'
@ -197,12 +195,10 @@ export default {
}, },
}, },
data() { data() {
const conf = store.state.chains.selected
const s = localStorage.getItem(`${conf.chain_name}-api-index`)
return { return {
variant: 'success', variant: 'success',
tips: 'Synced', tips: 'Synced',
index: s || 0, index: 0,
} }
}, },
computed: { computed: {
@ -212,16 +208,16 @@ export default {
}, },
selected_chain() { selected_chain() {
this.block() this.block()
return store.state.chains.selected return this.$store.state.chains.selected
}, },
chainVariant() { chainVariant() {
return this.variant return this.variant
}, },
currentApi() { currentApi() {
return this.apiOptions[Number(this.index)] return this.index + 1 > this.apiOptions.length ? this.apiOptions[0] : this.apiOptions[this.index]
}, },
apiOptions() { apiOptions() {
const conf = store.state.chains.selected const conf = this.$store.state.chains.selected
if (Array.isArray(conf.api)) { if (Array.isArray(conf.api)) {
return conf.api return conf.api
} }
@ -237,16 +233,19 @@ export default {
methods: { methods: {
change(v) { change(v) {
this.index = v this.index = v
const conf = store.state.chains.selected const conf = this.$store.state.chains.selected
localStorage.setItem(`${conf.chain_name}-api-index`, v) localStorage.setItem(`${conf.chain_name}-api-index`, v)
}, },
block() { block() {
store.commit('setHeight', 0) const conf = this.$store.state.chains.selected
const s = localStorage.getItem(`${conf.chain_name}-api-index`) || 0
this.index = Number(s)
this.$store.commit('setHeight', 0)
this.$http.getLatestBlock().then(block => { this.$http.getLatestBlock().then(block => {
store.commit('setHeight', Number(block.block.header.height)) this.$store.commit('setHeight', Number(block.block.header.height))
if (timeIn(block.block.header.time, 1, 'm')) { if (timeIn(block.block.header.time, 1, 'm')) {
this.variant = 'danger' this.variant = 'danger'
this.tips = `Halted ${toDay(block.block.header.time, 'from')}, Height: ${store.state.chains.height} ` this.tips = `Halted ${toDay(block.block.header.time, 'from')}, Height: ${this.$store.state.chains.height} `
} else { } else {
this.variant = 'success' this.variant = 'success'
this.tips = 'Synced' this.tips = 'Synced'

View File

@ -251,33 +251,6 @@ export default class ChainFetch {
}) })
} }
async get(url, config = null) {
let host = ''
if (!config) {
this.getSelectedConfig()
}
host = (config ? config.api : this.config.api)
const finalurl = (Array.isArray(host) ? host[this.getApiIndex(config)] : host) + url
// finalurl = finalurl.replaceAll('v1beta1', this.getEndpointVersion())
const ret = await fetch(finalurl).then(response => response.json())
return ret
}
getApiIndex(config = null) {
const conf = config || this.config
return localStorage.getItem(`${conf.chain_name}-api-index`) || 0
}
async getUrl(url) {
this.getSelectedConfig()
return fetch(url).then(res => res.json())
}
static fetch(host, url) {
const ret = fetch((Array.isArray(host) ? host[0] : host) + url).then(response => response.json())
return ret
}
async getAuthAccount(address, config = null) { async getAuthAccount(address, config = null) {
return this.get('/auth/accounts/'.concat(address), config).then(data => { return this.get('/auth/accounts/'.concat(address), config).then(data => {
const result = commonProcess(data) const result = commonProcess(data)
@ -420,9 +393,9 @@ export default class ChainFetch {
this.getSelectedConfig() this.getSelectedConfig()
} }
const conf = config || this.config const conf = config || this.config
const index = localStorage.getItem(`${conf.chain_name}-api-index`) || 0 const index = this.getApiIndex(config)
// Default options are marked with * // Default options are marked with *
const response = await fetch((Array.isArray(conf.api) ? conf.api[Number(index)] : conf.api) + url, { const response = await fetch((Array.isArray(conf.api) ? conf.api[index] : conf.api) + url, {
method: 'POST', // *GET, POST, PUT, DELETE, etc. method: 'POST', // *GET, POST, PUT, DELETE, etc.
// mode: 'cors', // no-cors, *cors, same-origin // mode: 'cors', // no-cors, *cors, same-origin
// credentials: 'same-origin', // redirect: 'follow', // manual, *follow, error // credentials: 'same-origin', // redirect: 'follow', // manual, *follow, error
@ -437,6 +410,33 @@ export default class ChainFetch {
// const response = axios.post((config ? config.api : this.config.api) + url, data) // const response = axios.post((config ? config.api : this.config.api) + url, data)
return response.json() // parses JSON response into native JavaScript objects return response.json() // parses JSON response into native JavaScript objects
} }
async get(url, config = null) {
if (!config) {
this.getSelectedConfig()
}
const conf = config || this.config
const finalurl = (Array.isArray(conf.api) ? conf.api[this.getApiIndex(config)] : conf.api) + url
// finalurl = finalurl.replaceAll('v1beta1', this.getEndpointVersion())
const ret = await fetch(finalurl).then(response => response.json())
return ret
}
getApiIndex(config = null) {
const conf = config || this.config
const index = Number(localStorage.getItem(`${conf.chain_name}-api-index`) || 0)
return index < conf.api.length ? index : 0
}
async getUrl(url) {
this.getSelectedConfig()
return fetch(url).then(res => res.json())
}
static fetch(host, url) {
const ret = fetch((Array.isArray(host) ? host[0] : host) + url).then(response => response.json())
return ret
}
} }
// export default chainAPI // export default chainAPI