封装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 => { }).catch(err => {
exeFun.call(this, 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 }
})
})
},
} }
+25 -16
View File
@@ -124,26 +124,35 @@ export default {
}, },
methods: { methods: {
getDate(){ getDate(){
this.loading = true // this.loading = true
let form = this.form let form = this.form
form.userId = this.$store.getters.userInfo.account form.userId = this.$store.getters.userInfo.account
form.pageSize = this.pageSize form.pageSize = this.pageSize
form.page = this.currentPage form.page = this.currentPage
return new Promise((resolve,reject)=>{ // return new Promise((resolve,reject)=>{
this.$http.get('lawss/AskQuestions/getAskQuestionsPage', form, {_this: this }, res => { // this.$http.get('lawss/AskQuestions/getAskQuestionsPage', form, {_this: this }, res => {
if(res.data){ // if(res.data){
const data = res.data // const data = res.data
this.data = data.records // this.data = data.records
this.total = data.total // this.total = data.total
this.currentPage = data.current // this.currentPage = data.current
this.pageSize = data.size // this.pageSize = data.size
} // }
this.loading = false // this.loading = false
resolve(res) // resolve(res)
}, e => { // }, e => {
this.loading = false // this.loading = false
reject(e) // reject(e)
}) // })
// })
this.$http._getData('lawss/AskQuestions/getAskQuestionsPage', form, {_this: this, loading: 'loading' }).then(res => {
if(res.data){
const data = res.data
this.data = data.records
this.total = data.total
this.currentPage = data.current
this.pageSize = data.size
}
}) })
}, },
onSubmit(){ onSubmit(){
+18 -11
View File
@@ -1984,17 +1984,24 @@ export default {
{ {
_this: this _this: this
}, val => { }, val => {
this.$http.post('lawss/activiti/completeTask',{ // this.$http.post('lawss/activiti/completeTask',{
taskIds: val.data, // taskIds: val.data,
userId : this.$store.getters.userInfo.userId, // userId : this.$store.getters.userInfo.userId,
json: JSON.stringify(res.data), // json: JSON.stringify(res.data),
}, // },
{ // {
_this: this // _this: this
}, res => { // }, res => {
this.submitGetData(); // this.submitGetData();
}, e => { // }, e => {
}) // })
this.$http._postData('lawss/activiti/completeTask',{
taskIds: val.data,
userId : this.$store.getters.userInfo.userId,
json: JSON.stringify(res.data),
}).then(res => {
this.submitGetData();
})
}, e => { }, e => {
}) })
}, e => { }, e => {