martial-arts/jsFile/requst.js

193 lines
5.1 KiB
JavaScript
Raw Normal View History

2022-08-01 09:03:10 +00:00
// 清理所有缓存并前往登录授权页
const goLogin = () => {
uni.clearStorageSync();
uni.navigateTo({
url: '/pages/login/login'
})
}
let flag = true;
// 刷新token并跳转到当前页面
const refreshTokenPage = () => {
uni.login({
provider: 'weixin',
success: (result)=> {
uni.request({
url: `${getApp().globalData.hostapi}/api/user/login`,
2022-08-01 09:03:10 +00:00
method: 'post',
data: {code:result.code},
success: res => {
if(res.data.data.token!=''){
flag = true;
uni.setStorageSync('userId',res.data.data.account_id)//用户id
uni.setStorageSync('token',res.data.data.token)//缓存token
2022-08-01 09:03:10 +00:00
uni.setStorageSync('openid',res.data.data.openid)//缓存openid
uni.setStorageSync('expire',res.data.data.expire)//缓存失效时间(时间戳格式)
uni.setStorageSync('phone_active',res.data.data.phone_active)//是否授权手机号
uni.setStorageSync('is_active',res.data.data.is_active)//是否授权头像和昵称
uni.setStorageSync('invite_code',res.data.data.invite_code)//缓存邀请码
2022-08-01 09:03:10 +00:00
uni.reLaunch({ // 重新进入当前页面
url:uni.getStorageSync('page-path-options')
})
}
}
})
},
});
}
// 请求错误处理
const checkError = (e) => {
// console.error("----接口错误----", e)
if (e.data) {
if (e.data.code) {
if(flag) {
flag = false;
switch (Number(e.data.code)) {
case 500:
2022-08-11 10:27:47 +00:00
case 4000:// 参数错误
2023-02-22 02:39:55 +00:00
case 4001:// 参数错误
case 4002:// 参数错误
2022-08-01 09:03:10 +00:00
case 4003:// 参数错误
case 4004:// 记录不存在
case 5000:// xxx错误
2022-08-01 09:03:10 +00:00
case 5001:// xxx错误
case 5050:// 服务器错误,请稍后重试
case 5051:// 未知错误
uni.showToast({
title:e.data.msg,
icon:'none'
})
if(Number(e.data.code)==5050){
2022-08-11 10:27:47 +00:00
// 跳转到登录页
2022-08-01 09:03:10 +00:00
setTimeout(()=>{
goLogin();
},1000)
}
break;
case 6001:
// token验证失败或已失效
console.log('6001token验证失败或已失效');
// 调用刷新token事件并跳转到当前页面
refreshTokenPage();
break;
}
2022-08-16 10:06:46 +00:00
setTimeout(()=>{
flag = true;
},2000)
2022-08-01 09:03:10 +00:00
}
}
}
}
// 封装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) => {
2022-08-12 10:14:13 +00:00
console.log(`${url}的参数===>`,options);
2022-08-01 09:03:10 +00:00
uni.request({
url: `${getApp().globalData.hostapi}${url}`,
method: methods,
data: options,
header: headers,
success: res => {
if (res.statusCode == 200) {
if (res.data.code == 0) {
// 接口调用成功
resolve(res.data);
} else {
2022-08-16 10:06:46 +00:00
reject(res);
2022-08-01 09:03:10 +00:00
// 接口返回错误信息
checkError(res);
}
} else {
// 接口返回错误信息
checkError(res);
}
},
fail: e => {
// 接口请求错误
checkError(e, reject);
},
complete: rest => {
// 是否成功,都会执行
console.log(`${url}返的结果===>`,rest.data);
2022-08-01 09:03:10 +00:00
}
})
})
}
/**
* 上传文件 封装请求
* @param {接口地址} url
* @param {传递参数} options
* @param {文件类型image\file} type
*/
const uploadFile = (url, options, type) => {
let tempData = options || {}
return new Promise((resolve, reject) => {
uni.uploadFile({
url: `${getApp().globalData.hostapi}${url}`,
filePath: tempData.path,
name: type,
fileType:type,//支付宝小程序必传
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)
}
2022-08-12 10:14:13 +00:00
} else {
// 接口返回错误信息
checkError(res);
}
2022-08-01 09:03:10 +00:00
}
});
})
}
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, type='image') => {
return uploadFile(url, options, type)
}
}