import store from './store' const baseUrl = "http://192.168.1.101:8000"; const baseApiUrl = baseUrl + "/api"; function initPramas() { arguments[0].url = baseApiUrl + arguments[0].url; arguments[0].timeout = 10000 // console.log(typeof arguments[0].fail) if (typeof arguments[0].fail === "undefined") { arguments[0].fail = (res) => { console.log(res) let msg = '请求失败' if (res.data && res.data.msg) { msg = res.data.msg } uni.showToast({ title: msg, icon: "none" }) } } const success = arguments[0].success arguments[0].success = (res) => { console.log(res) if(typeof res.data === "string"){ try{ res.data = JSON.parse(res.data) }catch(err){} } if (res.data.code === 0) { uni.showToast({ title: res.data.msg, icon: "none" }) arguments[0].fail(res) } if (res.data.code === 1) { success(res) } if (res.data.code === 401 || res.statusCode ===401 ) { uni.navigateTo({ url: "/pages/user/login" }) } } if (typeof arguments[0].header === "undefined") { arguments[0].header = {} } if (store.state.user.token) { arguments[0].header["token"] = store.state.user.token; } return arguments[0]; } export default { baseUrl: baseUrl, get: function() { let obj = initPramas(arguments[0]); obj.method = "GET"; uni.request(obj); }, post: function() { let obj = initPramas(arguments[0]); obj.method = "POST"; obj.header["Content-type"] = "application/x-www-form-urlencoded" console.log(obj); uni.request(obj); }, upload: function() { let obj = initPramas(arguments[0]); obj.url = baseApiUrl + '/common/upload'; uni.uploadFile(obj) } }