import Vue from 'vue' import utils from '../utils/utils.js' function http(uri, data, callback, method = 'GET', showLoading, title) { if(showLoading){ uni.showLoading({ title: title || '加载中...' }); } uni.request({ url: Vue.prototype.$config.baseUrl + uri, data: enhanceData(data), method: method, header: { 'X-Access-Token': uni.getStorageSync('token'), 'Content-Type' : 'application/x-www-form-urlencoded' }, success: (res) => { if(showLoading){ uni.hideLoading(); } if(res.statusCode == 401 || res.data.message == '操作失败,token非法无效!' || res.data.message == '操作失败,用户不存在!'){ uni.removeStorageSync('token') console.error('登录过期'); utils.toLogin() } if(res.statusCode == 200 && res.data.code != 200 && res.data.code != 902){ uni.showToast({ mask: true, duration: 1000, title: res.data.message, icon:'none' }); } callback(res.data) }, fail: () => { uni.showLoading({}) setTimeout(()=>{ uni.hideLoading() uni.showToast({icon:"none", title:"网络异常"}) }, 3000) 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") if (!data) { data = {} } if (userid) { data.userid = userid } 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 }