water_xcx/packageD/utils/fetchData.js

63 lines
2.0 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

function fetchData (url, params='', data={}, method='GET' ,callback, errback, timeout = 60000) {
const CONTENT_TYPE = 'application/x-www-form-urlencoded';
const HOST = 'https://open.ys7.com'; // http://10.10.34.51:8081 https://open.ys7.com
const OPEN_DOMAIN = ''; // /api/v3/console
wx.getStorage({
key: 'accessToken',
success(res) {
console.log('发送请求前先获取缓存accesstoken',res.data);
if (res.data) {
const ACCESSTOKEN = res.data;
wx.request({
url: `${HOST}${OPEN_DOMAIN}${url}${params}`, //仅为示例,并非真实的接口地址
method: method,
data: data,
header: {
'content-type': CONTENT_TYPE,
'accessToken': ACCESSTOKEN
},
timeout: timeout,
success: (res) => {
console.log(res.data);
// accesstoken过期统一跳转至登录页
if ((res.data.code && res.data.code == 10002) || (res.data.meta && res.data.meta.code == 10002) || (res.data.result && res.data.result.code == 10002)) {
wx.showToast({
title: 'accesstoken过期或异常',
})
} else {
if (res.data) {
callback && callback(res.data)
} else {
wx.showToast({
title: '网络异常请稍后重试',
icon: 'error'
})
}
}
},
error:(err)=>{
console.log(err);
wx.showToast({
title: '网络异常请稍后重试',
icon: 'error'
});
errback && errback()
},
})
}
},
fail() {
wx.navigateTo({
url: '/packageD/pages/login/index?url=/packageD/pages/queryDevice/index',
})
}
})
}
module.exports = fetchData;