231 lines
5.0 KiB
JavaScript
231 lines
5.0 KiB
JavaScript
const preCacheKeyClearFetch = 'storage:clear:fetch:'
|
||
import Api from '@/common/js/api.js'
|
||
|
||
uni.pro.interceptor('request', {
|
||
config(paramOptions) {
|
||
let options = Object.assign({}, paramOptions)
|
||
options.url = uni.conf.baseUrl + paramOptions.url
|
||
this.options = options
|
||
return options
|
||
},
|
||
success(res) {
|
||
if (res.code >= 400) {
|
||
return res.data
|
||
} else {
|
||
return res.data
|
||
}
|
||
}
|
||
})
|
||
|
||
function requestrequest(options) {
|
||
return new Promise((resolve, reject) => {
|
||
uni.request({
|
||
...options,
|
||
success: res => {
|
||
// resolve(res.data) //异步操作执行成功
|
||
if (res.data.code == 1) {
|
||
resolve(res.data) //异步操作执行成功
|
||
} else {
|
||
resolve(res.data) //异步操作执行失败
|
||
}
|
||
}
|
||
})
|
||
})
|
||
}
|
||
|
||
async function request(options) {
|
||
let networkType = ''
|
||
uni.getNetworkType({
|
||
success: (res) => {
|
||
networkType = res.networkType
|
||
}
|
||
});
|
||
if (networkType == 'none') {
|
||
uni.showToast({
|
||
title: '网络异常,请检查网络',
|
||
icon: 'none'
|
||
})
|
||
return false;
|
||
}
|
||
try {
|
||
if (options.toast) {
|
||
uni.showLoading({
|
||
title: '加载中',
|
||
mask: true
|
||
})
|
||
}
|
||
options.header = {
|
||
userId: uni.getStorageSync('userId'),
|
||
minitype: uni.getStorageSync('Type'),
|
||
fatoken: uni.getStorageSync('token'),
|
||
token: uni.getStorageSync('logintoken'),
|
||
isnew: 'isnew',
|
||
'content-type': 'application/json'
|
||
}
|
||
// #ifdef MP-WEIXIN || APP-PLUS
|
||
options.url = uni.conf.baseUrl + options.url
|
||
let res = await requestrequest(options);
|
||
// #endif
|
||
// #ifdef H5
|
||
let res = await uni.pro.request(options);
|
||
// #endif
|
||
if (res.code != 1) {
|
||
if (options.toast) {
|
||
uni.showToast({
|
||
title: res.message || res.msg,
|
||
icon: "none",
|
||
})
|
||
setTimeout(res => {
|
||
uni.hideLoading()
|
||
}, 1000)
|
||
}
|
||
if (res.code == 400) {
|
||
return Promise.reject(res.message)
|
||
} else if (res.code == 401) {
|
||
uni.showToast({
|
||
title: res.message || res.msg,
|
||
icon: "none",
|
||
success: () => {
|
||
uni.removeStorageSync('logintoken');
|
||
uni.removeStorageSync('token');
|
||
uni.reLaunch({
|
||
url: '/pages/login/login'
|
||
})
|
||
}
|
||
})
|
||
} else if (res.code == 405) {
|
||
let token = await Api.h5encryption({
|
||
store_id: uni.cache.get('userId')
|
||
})
|
||
if (token.code == 1) {
|
||
uni.cache.set('token', token.data.token);
|
||
}
|
||
} else if (res.code == 482) {
|
||
let nowTime = new Date() / 1000 | 0
|
||
let offset = parseInt(res.message) - parseInt(nowTime);
|
||
uni.cache.set('storage:offset-time', offset, -1)
|
||
return await request(options)
|
||
} else if (res.code == 422) {
|
||
return Promise.reject(res.message)
|
||
}
|
||
} else {
|
||
if (!options.toast) {
|
||
// uni.showToast({
|
||
// title: res.message || res.msg,
|
||
// icon: "none",
|
||
// success() {
|
||
// setTimeout(res => {
|
||
// // if (options.toast) {
|
||
// uni.hideLoading()
|
||
// // }
|
||
// }, 2000)
|
||
// }
|
||
// })
|
||
}
|
||
return res
|
||
|
||
}
|
||
} catch (err) {
|
||
console.warn('uni.request fail [network]', options, err)
|
||
throw err;
|
||
} finally {
|
||
if (options.toast) {
|
||
setTimeout(res => {
|
||
uni.hideLoading()
|
||
}, 1000)
|
||
}
|
||
}
|
||
}
|
||
|
||
function isExpire(url) {
|
||
return uni.cache.get(preCacheKeyClearFetch + url)
|
||
}
|
||
/**
|
||
* 标记fetch过期,会重新请求
|
||
* @author NanQi
|
||
* @param {String} url 标记的URL
|
||
*/
|
||
function markFetch(url) {
|
||
uni.cache.set(preCacheKeyClearFetch + url, true)
|
||
}
|
||
|
||
/**
|
||
* 拉取数据(get请求,带缓存)
|
||
* @author NanQi
|
||
* @param {String} url 请求的URL
|
||
* @param {Object} data 请求参数
|
||
* @param {Boolean} toast 是否显示toast
|
||
* @param {Number} expire 缓存过期时间(秒)
|
||
* @return {Promise} Promise对象
|
||
*/
|
||
async function fetch(url, data, toast = true, expire = uni.conf.default_expire) {
|
||
let param = ''
|
||
if (data) {
|
||
param += ':' + uni.utils.md5(uni.utils.sortTransform(data));
|
||
}
|
||
const cacheKey = 'memory:fetch:' + url + param;
|
||
const cacheVal = uni.cache.get(cacheKey);
|
||
if (!isExpire(url) && cacheVal) {
|
||
return Promise.resolve(cacheVal);
|
||
} else {
|
||
if (isExpire(url)) {
|
||
uni.cache.remove(preCacheKeyClearFetch + url)
|
||
}
|
||
try {
|
||
const res = await get(url, data, toast);
|
||
uni.cache.remove(cacheKey);
|
||
uni.cache.set(cacheKey, res, expire);
|
||
return res;
|
||
} catch (err) {
|
||
const res = uni.cache.getStorageData(cacheKey);
|
||
if (res) {
|
||
return Promise.resolve(res);
|
||
} else {
|
||
throw err;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取数据(get请求,不带缓存)
|
||
* @author NanQi
|
||
* @param {String} url 请求的URL
|
||
* @param {Object} data 请求参数
|
||
* @param {Boolean} toast 是否显示toast
|
||
* @return {Promise} Promise对象
|
||
*/
|
||
function get(url, data, toast = true) {
|
||
return request({
|
||
url,
|
||
data,
|
||
toast,
|
||
})
|
||
}
|
||
|
||
|
||
/**
|
||
* post请求
|
||
* @author NanQi
|
||
* @param {String} url 请求的URL
|
||
* @param {Object} data 请求参数
|
||
* @param {Boolean} toast 是否显示toast
|
||
* @param {String} method 请求方式,默认POST
|
||
* @return {Promise} Promise对象
|
||
*/
|
||
function post(url, data, toast = true, method = 'POST') {
|
||
return request({
|
||
url,
|
||
method,
|
||
data,
|
||
toast
|
||
})
|
||
}
|
||
|
||
uni.api = {
|
||
request,
|
||
markFetch,
|
||
fetch,
|
||
get,
|
||
post
|
||
} |