api中的 请求方式封装:get和post

function apiHttpGet(url, config) { //封装后的get请求
return new Promise((resolve, reject) => {
let _store = false;
let _res = null;
let key = url;
// console.log(_Vue.$store.state.ResponseList.responseList)
if ((typeof (config) == 'object' && config.isStore)) { //判断是否需要缓存
let _list = _Vue.$store.state.ResponseList.responseList;
if (_list.length > 0) {
if (config.params) {
let furl = formatParams(config.params);
key += furl;
}
let _time = 2000 * 60;//默认缓存2分钟
if (config.storeTime && config.storeTime != null && config != '' && !isNaN(config.storeTime) && Number(config.storeTime) >= 0) {
_time = Number(config.storeTime);
}
_list.map((item) => {
if (item.key == key) {
let now = (new Date()).valueOf();
if ((now - item.time) < _time) {
_res = item.value;
_store = true;
resolve(_res); //如果已缓存,且处于缓存时间内,取缓存数据
}
}
})
}
}
if (!_store) {
Axios.get(url, config).then((res) => {
if ((typeof (config) == 'object' && config.isStore) && typeof (res) != 'undefined' && res != null) {
if ((typeof (res) == 'object' && res.code == 1) || (typeof (res) != 'object')) {
key = url;
if (config.params) {
let furl = formatParams(config.params);
key += furl;
}
let isadd = false;
_Vue.$store.state.ResponseList.responseList.map(item => {
if (item.key == key) {
isadd = true;
item.value = JSON.parse(JSON.stringify(res));
let now = (new Date()).valueOf();
item.time = now;
}
})
if (!isadd) {
let now = (new Date()).valueOf();
_Vue.$store.state.ResponseList.responseList.push({ key: key, value: JSON.parse(JSON.stringify(res)) , time: now });
}
}
}

resolve(res);
})
}
})

}
function apiHttpPost(url, config) {
return new Promise((resolve, reject) => {
let _store = false;
let _res = null;
let key = url;
if ((typeof (config) == 'object' && config.isStore)) {
let _list = _Vue.$store.state.ResponseList.responseList;
if (_list.length > 0) {
if (config.params) {
let furl = formatParams(config.params);
key += furl;
}
let _time = 2000 * 60;//默认缓存2分钟
if (config.storeTime && config.storeTime != null && config != '' && !isNaN(config.storeTime) && Number(config.storeTime) >= 0) {
_time = Number(config.storeTime);
}
_list.map((item) => {
if (item.key == key) {
let now = (new Date()).valueOf();
if ((now - item.time) < _time) {
_res = item.value;
_store = true;
resolve(_res);
}
}
})
}
}
if (!_store) {
Axios.post(url, config).then((res) => {
if ((typeof (config) == 'object' && config.isStore) && typeof (res) != 'undefined' && res != null) {
if ((typeof (res) == 'object' && res.code == 1) || (typeof (res) != 'object')) {
key = url;
if (config.params) {
let furl = formatParams(config.params);
key += furl;
}
let isadd = false;
_Vue.$store.state.ResponseList.responseList.map(item => {
if (item.key == key) {
isadd = true;
item.value = JSON.parse(JSON.stringify(res));
let now = (new Date()).valueOf();
item.time = now;
}
})
if (!isadd) {
let now = (new Date()).valueOf();
_Vue.$store.state.ResponseList.responseList.push({ key: key, value: JSON.parse(JSON.stringify(res)), time: now });
}
}
}
resolve(res);
})
}
})
}

猜你喜欢

转载自www.cnblogs.com/xiaoxiaocheng/p/9878256.html