@sweetui/sweet-mobile
Advanced tools
Comparing version 2.3.1 to 2.3.2
{ | ||
"name": "@sweetui/sweet-mobile", | ||
"version": "2.3.1", | ||
"version": "2.3.2", | ||
"description": "Sweet Mobile Core", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
178
xhr/index.js
import axios from 'axios'; | ||
const SWXHR = function (Vue, options, store, tool) { | ||
this.Vue = Vue; | ||
// 注册 | ||
this._reg(store, tool); | ||
var that = this, | ||
deOptions = { | ||
config: { | ||
// baseURL: '', | ||
timeout: 0, | ||
headers: {}, | ||
maxStatus: 500 | ||
}, | ||
intercept: { | ||
// 请求前 | ||
request(p) { | ||
return p; | ||
}, | ||
// 请求后 | ||
response(response) { | ||
return response; | ||
} | ||
} | ||
}; | ||
// this.OPTIONS = { | ||
// config: Object.assign(deOptions.config, options.config), | ||
// intercept: Object.assign(deOptions.intercept, options.intercept) | ||
// }; | ||
this.OPTIONS = this.SWTOOL.object.extend(true, deOptions, options); | ||
that.axios = axios.create(this.OPTIONS.config); | ||
// 添加请求拦截器 | ||
that._setInterceptors(); | ||
const SWXHR = function(Vue, options, store, tool) { | ||
this.Vue = Vue; | ||
// 注册 | ||
this._reg(store, tool); | ||
var that = this, | ||
deOptions = { | ||
config: { | ||
// baseURL: '', | ||
timeout: 0, | ||
headers: {}, | ||
maxStatus: 500 | ||
}, | ||
intercept: { | ||
// 请求前 | ||
request(p) { | ||
return p; | ||
}, | ||
// 请求后 | ||
response(response) { | ||
return response; | ||
} | ||
} | ||
}; | ||
// this.OPTIONS = { | ||
// config: Object.assign(deOptions.config, options.config), | ||
// intercept: Object.assign(deOptions.intercept, options.intercept) | ||
// }; | ||
this.OPTIONS = this.SWTOOL.object.extend(true, deOptions, options); | ||
that.axios = axios.create(this.OPTIONS.config); | ||
// 添加请求拦截器 | ||
that._setInterceptors(); | ||
}; | ||
SWXHR.prototype = { | ||
install(Vue) { | ||
if (this.install.installed) return; | ||
this.install.installed = true; | ||
Vue.prototype.SWXHR = this; | ||
}, | ||
_reg(store, tool) { | ||
// 注册工具类 | ||
if (tool) { | ||
this.SWTOOL = tool.SWTOOL; | ||
} | ||
if (store) { | ||
store.SWXHR = this; | ||
store.SWTOOL = this.SWTOOL; | ||
} | ||
}, | ||
_setInterceptors() { | ||
var intercept = this.OPTIONS.intercept, | ||
request = intercept.request, | ||
response = intercept.response, | ||
defaultError = function (error) { | ||
// 对请求错误做些什么 | ||
return Promise.reject(error); | ||
}; | ||
install(Vue) { | ||
if (this.install.installed) return; | ||
this.install.installed = true; | ||
Vue.prototype.SWXHR = this; | ||
}, | ||
_reg(store, tool) { | ||
// 注册工具类 | ||
if (tool) { | ||
this.SWTOOL = tool.SWTOOL; | ||
} | ||
if (store) { | ||
store.SWXHR = this; | ||
store.SWTOOL = this.SWTOOL; | ||
} | ||
}, | ||
_setInterceptors() { | ||
var intercept = this.OPTIONS.intercept, | ||
request = intercept.request, | ||
response = intercept.response, | ||
defaultError = function(error) { | ||
// 对请求错误做些什么 | ||
return Promise.reject(error); | ||
}; | ||
if (request && typeof request === 'function') | ||
this.axios.interceptors.request.use(request, defaultError); | ||
if (request && typeof request === 'function') | ||
this.axios.interceptors.request.use(request, defaultError); | ||
if (response && typeof response === 'function') | ||
this.axios.interceptors.response.use(response, defaultError); | ||
}, | ||
_getAxios(type, args) { | ||
var url = args[0], | ||
data = args[1] ? args[1] : {}; | ||
if (!url) return; | ||
// 在server.config.js中寻找匹配前缀的代理 | ||
for (const [prefix, target] of Object.entries(this.OPTIONS.config.proxy || {})) { | ||
if (url.startsWith(prefix)) { | ||
url = url.replace(prefix, '') | ||
break | ||
} | ||
if (response && typeof response === 'function') | ||
this.axios.interceptors.response.use(response, defaultError); | ||
}, | ||
_getAxios(type, args) { | ||
var url = args[0], | ||
data = args[1] ? args[1] : {}; | ||
if (!url) return; | ||
// 在server.config.js中寻找匹配前缀的代理 | ||
for (const [prefix, target] of Object.entries(this.OPTIONS.config.proxy || {})) { | ||
if (url.startsWith(prefix)) { | ||
url = target + url.replace(process.env.NODE_ENV !== 'development' ? prefix : '', '') | ||
break | ||
} | ||
} | ||
return this.axios[type.toLowerCase()](url, data); | ||
}, | ||
GET() { | ||
return this._getAxios('GET', arguments); | ||
}, | ||
POST() { | ||
return this._getAxios('POST', arguments); | ||
}, | ||
DELETE() { | ||
return this._getAxios('DELETE', arguments); | ||
}, | ||
PUT() { | ||
return this._getAxios('PUT', arguments); | ||
}, | ||
ALL() { | ||
return this.axios.all(arguments); | ||
}, | ||
SPREAD() { | ||
return this.axios.spread(arguments); | ||
} | ||
return this.axios[type.toLowerCase()](url, data); | ||
}, | ||
GET() { | ||
return this._getAxios('GET', arguments); | ||
}, | ||
POST() { | ||
return this._getAxios('POST', arguments); | ||
}, | ||
DELETE() { | ||
return this._getAxios('DELETE', arguments); | ||
}, | ||
PUT() { | ||
return this._getAxios('PUT', arguments); | ||
}, | ||
ALL() { | ||
return this.axios.all(arguments); | ||
}, | ||
SPREAD() { | ||
return this.axios.spread(arguments); | ||
} | ||
}; | ||
export default SWXHR; |
155866
2