200 lines
5.4 KiB
JavaScript
200 lines
5.4 KiB
JavaScript
|
// 清理所有缓存并前往登录授权页
|
|||
|
const goLogin = () => {
|
|||
|
uni.clearStorageSync();
|
|||
|
uni.navigateTo({
|
|||
|
url: '/pages/login/login'
|
|||
|
})
|
|||
|
}
|
|||
|
let flag = true;
|
|||
|
|
|||
|
// 刷新token并跳转到当前页面
|
|||
|
var authTimer = null;
|
|||
|
const refreshTokenPage = () => {
|
|||
|
clearTimeout(authTimer);
|
|||
|
// 获取当前页面路径
|
|||
|
let currentRoutes = getCurrentPages(); // 获取当前打开过的页面路由数组
|
|||
|
let currentRoute = currentRoutes[currentRoutes.length - 1].route //获取当前页面路由
|
|||
|
let currentParam = currentRoutes[currentRoutes.length - 1].options; //获取路由参数
|
|||
|
// 拼接参数
|
|||
|
let param = ''
|
|||
|
for (let key in currentParam) {
|
|||
|
param += '?' + key + '=' + currentParam[key]
|
|||
|
}
|
|||
|
let localRoute = '/'+ currentRoute + param;
|
|||
|
if(localRoute !== '/pages/login/login'){
|
|||
|
uni.login({
|
|||
|
success: (res) => {
|
|||
|
uni.request({
|
|||
|
url:`${getApp().globalData.hostapi}/api/v1/user/login`,
|
|||
|
data:{
|
|||
|
code:res.code,
|
|||
|
},
|
|||
|
success: (result) => {
|
|||
|
if(result.data.code == 0) {
|
|||
|
uni.setStorageSync('userId',result.data.account_id);
|
|||
|
uni.setStorageSync('token',result.data.data.token);//缓存token
|
|||
|
uni.setStorageSync('openid',result.data.data.openid);//缓存openid
|
|||
|
uni.setStorageSync('expire',result.data.data.expire);//缓存失效时间(时间戳格式)
|
|||
|
uni.setStorageSync('phone_active',result.data.data.phone_active);//是否授权手机号
|
|||
|
uni.setStorageSync('is_active',result.data.data.is_active);//是否授权头像和昵称
|
|||
|
// 重新进入当前页面
|
|||
|
uni.reLaunch({
|
|||
|
url:localRoute
|
|||
|
})
|
|||
|
}
|
|||
|
}
|
|||
|
})
|
|||
|
}
|
|||
|
})
|
|||
|
}
|
|||
|
flag = true;
|
|||
|
}
|
|||
|
|
|||
|
// 请求错误处理
|
|||
|
const checkError = (e) => {
|
|||
|
console.log('500接口错误');
|
|||
|
// console.error("----接口错误----", e)
|
|||
|
if (e.data) {
|
|||
|
if (e.data.code) {
|
|||
|
switch (Number(e.data.code)) {
|
|||
|
case 500:
|
|||
|
// 接口错误
|
|||
|
console.log('500接口错误');
|
|||
|
case 4003:
|
|||
|
// 参数错误
|
|||
|
console.log('4003参数错误');
|
|||
|
break;
|
|||
|
case 4004:
|
|||
|
// 记录不存在
|
|||
|
console.log('4004记录不存在');
|
|||
|
break;
|
|||
|
case 5001:
|
|||
|
// xxx错误
|
|||
|
console.log('5001xxx错误');
|
|||
|
break;
|
|||
|
case 5050:
|
|||
|
// 服务器错误,请稍后重试
|
|||
|
console.log('5050服务器错误,请稍后重试');
|
|||
|
// 调用到登录页
|
|||
|
goLogin();
|
|||
|
break;
|
|||
|
case 5051:
|
|||
|
// 未知错误
|
|||
|
console.log('5051未知错误');
|
|||
|
break;
|
|||
|
case 6001:
|
|||
|
// token验证失败或已失效
|
|||
|
console.log('6001token验证失败或已失效');
|
|||
|
if(flag) {
|
|||
|
flag = false;
|
|||
|
// 调用刷新token事件并跳转到当前页面
|
|||
|
refreshTokenPage();
|
|||
|
}
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// 封装request的(GET、POST)请求
|
|||
|
const request = (method, url, options) => {
|
|||
|
let methods = '';
|
|||
|
let headers = {};
|
|||
|
switch (method) {
|
|||
|
case 'get':
|
|||
|
methods = 'GET'
|
|||
|
headers = {
|
|||
|
'Content-Type': 'application/json; charset=UTF-8',
|
|||
|
'Authorization': 'Bearer '+uni.getStorageSync('token') || ''
|
|||
|
}
|
|||
|
break;
|
|||
|
case 'post':
|
|||
|
methods = 'POST'
|
|||
|
headers = {
|
|||
|
'Content-Type': 'application/json; charset=UTF-8',
|
|||
|
'Authorization': 'Bearer '+uni.getStorageSync('token') || ''
|
|||
|
}
|
|||
|
break;
|
|||
|
case 'postForm':
|
|||
|
methods = 'POST'
|
|||
|
headers = {
|
|||
|
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
|
|||
|
'Authorization': 'Bearer '+uni.getStorageSync('token') || ''
|
|||
|
}
|
|||
|
break;
|
|||
|
}
|
|||
|
return new Promise((resolve, reject) => {
|
|||
|
uni.request({
|
|||
|
url: `${getApp().globalData.hostapi}${url}`,
|
|||
|
method: methods,
|
|||
|
data: options,
|
|||
|
header: headers,
|
|||
|
success: res => {
|
|||
|
console.log(`${url}返的结果===>`,res);
|
|||
|
if (res.statusCode == 200) {
|
|||
|
resolve(res.data);
|
|||
|
if(res.data.code !== 0){
|
|||
|
// 接口返回错误信息
|
|||
|
checkError(res);
|
|||
|
}
|
|||
|
} else {
|
|||
|
// 接口返回错误信息
|
|||
|
checkError(res);
|
|||
|
}
|
|||
|
},
|
|||
|
fail: e => {
|
|||
|
// 接口请求错误
|
|||
|
checkError(e, reject);
|
|||
|
},
|
|||
|
complete: rest => {
|
|||
|
// 是否成功,都会执行
|
|||
|
console.log(rest,100);
|
|||
|
}
|
|||
|
})
|
|||
|
})
|
|||
|
}
|
|||
|
|
|||
|
// 上传文件 封装请求
|
|||
|
const uploadFile = (url, options) => {
|
|||
|
let tempData = options || {}
|
|||
|
return new Promise((resolve, reject) => {
|
|||
|
uni.uploadFile({
|
|||
|
url: `${getApp().globalData.hostapi}${url}`,
|
|||
|
filePath: tempData.path,
|
|||
|
name: 'image',
|
|||
|
fileType:'image',
|
|||
|
formData: tempData,
|
|||
|
header: {
|
|||
|
'Content-Type': 'multipart/form-data;charset=UTF-8',
|
|||
|
'Authorization': 'Bearer '+uni.getStorageSync('token') || ''
|
|||
|
},
|
|||
|
success: res => {
|
|||
|
if (res.statusCode == 200) {
|
|||
|
let temp = JSON.parse(res.data)
|
|||
|
if (temp.code == 0) {
|
|||
|
resolve(temp)
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
});
|
|||
|
})
|
|||
|
}
|
|||
|
|
|||
|
export default {
|
|||
|
get: (url, options) => {
|
|||
|
return request('get', url, options)
|
|||
|
},
|
|||
|
// JOSN格式
|
|||
|
post: (url, options) => {
|
|||
|
return request('post', url, options)
|
|||
|
},
|
|||
|
// form-data格式
|
|||
|
postForm: (url, options) => {
|
|||
|
return request('postForm', url, options)
|
|||
|
},
|
|||
|
// 上传
|
|||
|
upload: (url, options) => {
|
|||
|
return uploadFile(url, options)
|
|||
|
}
|
|||
|
}
|