@sweetui/sweet
Advanced tools
Comparing version 2.1.12 to 2.1.13
{ | ||
"name": "@sweetui/sweet", | ||
"version": "2.1.12", | ||
"version": "2.1.13", | ||
"description": "Sweet Core", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
const browserLang = | ||
window.navigator.language || window.navigator.browserLanguage; | ||
window.navigator.language || window.navigator.browserLanguage, | ||
browserLangReal = browserLang.match(/\S*(?=-)/) ? browserLang.match(/\S*(?=-)/)[0] : browserLang; | ||
const language = | ||
window.localStorage.getItem('userLang') || | ||
(browserLang.match(/\S*(?=-)/)[0] === 'en' ? 'en' : browserLang); | ||
(browserLangReal === 'zh' ? 'zh-CN' : 'en'); | ||
@@ -7,0 +8,0 @@ export default { |
@@ -51,2 +51,4 @@ import Vue from 'vue'; | ||
this.auto = true; | ||
this.isCancel = false; | ||
this.source = ''; | ||
@@ -65,7 +67,18 @@ for (let name in options) { | ||
getAxios(type, options) { | ||
if (this.loading) return; | ||
else this.switchLoading(true); | ||
var ax = this.SWXHR._getAxios(type, options); | ||
if (this.loading && !this.isCancel) { | ||
return; | ||
} else if (this.loading && this.isCancel) { | ||
this.source.cancel('手动取消请求'); | ||
} else this.switchLoading(true); | ||
var object = this.SWXHR._getAxios(type, options, this.isCancel); | ||
var ax = object.axios; | ||
this.source = object.source; | ||
ax.then(res => { | ||
if (this.auto) this.switchLoading(false); | ||
}).catch(thrown => { | ||
if (axios.isCancel(thrown)) { | ||
console.log('Request canceled', thrown.message); | ||
} else { | ||
// 处理错误 | ||
} | ||
}) | ||
@@ -170,7 +183,21 @@ return ax; | ||
}, | ||
_getAxios(type, args) { | ||
_setCancelToken() { | ||
var CancelToken = axios.CancelToken, | ||
source = CancelToken.source(); | ||
this.axios.defaults.cancelToken = source.token; | ||
return source; | ||
}, | ||
_getAxios(type, args, bool) { | ||
var url = args[0], | ||
data = args[1] ? args[1] : {}; | ||
if (!url) return; | ||
return this.axios[type.toLowerCase()](url, data); | ||
if (!bool) return this.axios[type.toLowerCase()](url, data); | ||
else { | ||
var source = this._setCancelToken(); | ||
return { | ||
axios: this.axios[type.toLowerCase()](url, data), | ||
source: source | ||
} | ||
} | ||
}, | ||
@@ -177,0 +204,0 @@ create(options) { |
20424
407