import Vue from "vue";
|
|
function http(uri, data, callback, method = 'GET', showLoading, title){
|
|
if(showLoading){
|
|
uni.showLoading({
|
|
title: title || '正在提交...'
|
|
});
|
|
}
|
|
|
|
uni.request({
|
|
url: Vue.prototype.VITE_GLOB_API + uri,
|
|
data: enhanceData(data),
|
|
method: method,
|
|
header: {
|
|
// 'X-Access-Token': localStorage.getItem('token'),
|
|
'Content-Type' : method == 'POST' ? 'application/x-www-form-urlencoded' : 'application/json'
|
|
},
|
|
success: (res) => {
|
|
|
|
if(showLoading){
|
|
uni.hideLoading();
|
|
}
|
|
|
|
|
|
if(res.statusCode == 401){
|
|
// localStorage.removeItem('token')
|
|
// localStorage.removeItem('userInfo')
|
|
// uni.navigateTo({
|
|
// url: '/pages/login/mobile'
|
|
// })
|
|
}
|
|
|
|
if(res.statusCode == 200 && res.data.code != 200){
|
|
uni.showToast({
|
|
icon: 'none',
|
|
title: res.data.message,
|
|
});
|
|
}
|
|
|
|
callback(res.data)
|
|
},
|
|
fail: () => {
|
|
uni.showLoading({})
|
|
setTimeout(()=>{
|
|
uni.hideLoading()
|
|
uni.showToast({icon:"none", title:"网络异常"})
|
|
}, 2000)
|
|
|
|
callback({
|
|
code : 600,
|
|
message : ''
|
|
})
|
|
|
|
if(showLoading){
|
|
uni.hideLoading();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
function deleted(uri, data, callback){
|
|
http(uri, data, callback, 'DELETE')
|
|
}
|
|
|
|
function post(uri, data, callback){
|
|
http(uri, data, callback, 'POST')
|
|
}
|
|
|
|
function get(uri, data, callback){
|
|
http(uri, data, callback, 'GET')
|
|
}
|
|
|
|
function enhanceData(data){
|
|
const userid = uni.getStorageSync("userid")
|
|
const enter_id = uni.getStorageSync("enter_id")
|
|
if (!data){
|
|
data = {}
|
|
}
|
|
if (userid){
|
|
data.userid = userid
|
|
}
|
|
if (enter_id){
|
|
data.enter_id = enter_id
|
|
}
|
|
return data
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function sync(method, uri, data){
|
|
return new Promise((resolve, reject) => {
|
|
uni.request({
|
|
url: uri,
|
|
data: data,
|
|
method: method,
|
|
header: { 'auth': '1AS9F1HPC4FBC9EN00J7KX2L5RJ99XHZ' },
|
|
success: (res) => {
|
|
resolve(res.data)
|
|
},
|
|
fail: (err) => {
|
|
reject(err);
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
|
|
let cache = null
|
|
function async(method, uri, data){
|
|
const promise = sync(method, uri, data).then(res => {
|
|
cache = res
|
|
}).catch(err => {
|
|
|
|
})
|
|
}
|
|
|
|
|
|
function syncHttp(uri, data, method = 'GET'){
|
|
async(method, uri, data)
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default {
|
|
http: http,
|
|
delete: deleted,
|
|
post: post,
|
|
get: get,
|
|
syncHttp: syncHttp
|
|
}
|
|
|