const BASE_URL = '/api' //发布上线时需要更换
export const http = (method,url,data) => {
//console.log(data)
return new Promise((resolve,reject) => {
uni.showLoading({
title: '加载中',
mask: true
})
uni.request({
method: method,
url: BASE_URL + url,
data:data,
header: {
"Content-Type": method == "GET"? "json": "application/x-www-form-urlencoded",
"token": token()
},
success:(res) => {
resolve(res.data);
},
fail:(err) => {
reject(err);
//这里可以写一些状态码判断
// let code = err.data.code;
// console.log(code)
// switch(code){
// case 500:
// ……
// break
// }
},
complete:() => {
uni.hideLoading();
}
})
})
return promise
}
使用方法:
//1、在页面引用
import {http} from '../../common/js/request.js'
//2、发起请求
//要传给后台的参数
let params = {
admin_id: this.adminId,
clean_log_id: this.cleanlogId
}
http('POST','请求的url',params).then(res => {
if(res){
uni.showToast({
title: res.value,
icon: 'none',
duration: 1500
})
}
this.showSure = false;
})
网友评论