封装axios

This commit is contained in:
zyn
2023-04-17 16:09:17 +08:00
parent 35baf683e1
commit 8f779cd47a
3 changed files with 92 additions and 28 deletions
+49 -1
View File
@@ -536,5 +536,53 @@ export default {
}).catch(err => {
exeFun.call(this, err)
})
}
},
_getData (url, params, config = {}) {
return new Promise((resolve,reject)=>{
// 参数包含this
let _this = config._this || false
// 参数包含loading
let loading = config.loading || false
if (_this && loading) {
_this[loading] = true
}
_axios.get(api() + url + '?_t=' + new Date().getTime(), { params }).then(res => {
resolve(res.data)
}).catch(err => {
reject(err)
}).finally(()=>{
if (_this && loading) { _this[loading] = false }
})
})
},
_postData (url, param, config = {}) {
return new Promise((resolve,reject) => {
var _formData = formData(param)
// 参数包含this
let _this = config._this || false
// 参数包含loading
let loading = config.loading || false
// 是否显示操作提示
let showTips = config.showTips === undefined ? true : config.showTips
if (_this && loading) { _this[loading] = true }
_axios.post(api() + url + '?' + new Date().getTime(), _formData).then(res => {
if (res.data.ok !== undefined && showTips) {
let type = res.data.ok ? 'success' : 'warning'
if (_this && res.data.message !== '' && res.data.message !== null) {
Message[type](res.data.message)
}
}
resolve(res.data)
}).catch(err => {
reject(err)
}).finally(()=>{
if (_this && loading) { _this[loading] = false }
})
})
},
}