var url = "请求域名"
export default class api {
syncRequest = function(option) {
option.method = option.method ? option.method : "POST"
option.header = option.header ? option.header : {}
option.data = option.data ? option.data : {}
option.loading = option.loading ? true : false,
option.loadtext = option.loadtext ? option.loadtext : '加载中...'
if (option.method == "GET") {
option.header['Content-Type'] = 'application/json;charset=UTF-8'
} else {
option.header['Content-Type'] = 'application/x-www-form-urlencoded'
}
if (option.loading) {
uni.showLoading({
mask: true,
title: option.loadtext
})
}
return new Promise((resolve, reject) => {
uni.request({
url: url + option.url,
method: option.method,
header: option.header,
data: option.data,
success: (res) => {
if (option.loading) {
uni.hideLoading()
}
if (res.statusCode == 200) {
resolve(res);
} else {
if (option.msg) {
uni.showToast({
icon: "none",
title: option.msg
})
}
}
},
fail: (err) => {
if (option.loading) {
uni.hideLoading()
}
if (option.msg) {
uni.showToast({
icon: "none",
title: option.msg
})
}
reject(err)
}
});
})
}
}
import Request from './common/request.js'
let request = new Request()
Vue.prototype.$api = request
<button type="default" @tap="request">点击</button>
async request(){
var data = await this.$api.syncRequest({
url:'/index/index/index',
loading:true,
})
console.log(data);
},