hengmei-two/jsFile/requst.js

173 lines
4.4 KiB
JavaScript
Raw Normal View History

// const ENV = process.env.NODE_ENV;
const ENV = process.env;
console.log(ENV,2);
// 清理所有缓存并前往授权页
2021-08-19 06:40:59 +00:00
const goLogin = () => {
uni.clearStorageSync();
uni.navigateTo({
url: '/pages/login/login'
})
2021-08-19 06:40:59 +00:00
}
// 请求错误处理
const checkError = (e) => {
2021-08-19 06:40:59 +00:00
// console.error("----接口错误----", e)
if (e.data) {
if (e.data.code) {
switch (Number(e.data.code)) {
case 4003:
// 参数错误
console.log('4003参数错误');
2021-08-19 06:40:59 +00:00
break;
case 4004:
// 记录不存在
console.log('4004记录不存在');
break;
case 5001:
// xxx错误
console.log('5001xxx错误');
break;
case 5050:
// 服务器错误,请稍后重试
console.log('5050服务器错误请稍后重试');
break;
case 5051:
// 未知错误
console.log('5051未知错误');
break;
case 6001:
// token验证失败或已失效
console.log('6001token验证失败或已失效');
goLogin();
break;
2021-08-19 06:40:59 +00:00
}
}
}
2021-08-19 06:40:59 +00:00
}
// 封装请求
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({
2022-02-17 03:49:34 +00:00
url: `${uni.getStorageSync('hostapi')}${url}`,
2021-08-19 06:40:59 +00:00
method: methods,
data: options,
2021-08-19 06:40:59 +00:00
header: headers,
success: res => {
console.log(`${url}返的结果===>`,res);
2021-08-19 06:40:59 +00:00
if (res.statusCode == 200) {
if (res.data.code == 0) {
resolve(res.data)
} else {
2022-03-04 08:48:40 +00:00
uni.showToast({
title:res.data.msg,
icon:'none'
})
checkError(res)
2021-08-19 06:40:59 +00:00
}
} else {
checkError(res)
2021-08-19 06:40:59 +00:00
}
},
fail: e => {
checkError(e)
2021-08-19 06:40:59 +00:00
},
2021-11-02 10:23:53 +00:00
complete: rest => {
// console.log(rest);
2021-08-19 06:40:59 +00:00
}
})
})
}
// 上传文件 封装请求
const uploadFile = (url, options) => {
let tempData = options || {}
return new Promise((resolve, reject) => {
uni.uploadFile({
2022-02-17 03:49:34 +00:00
url: `${uni.getStorageSync('hostapi')}${url}`,
2021-08-19 06:40:59 +00:00
filePath: tempData.file,
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)
} else {
reject(temp)
uni.showToast({
title: temp.msg || '接口错误(' + temp.code + ')',
icon: 'none'
})
}
} else {
uni.showToast({
title: `未知错误(${res.statusCode})`,
icon: 'none'
})
}
},
fail(e) {
// uni.showToast({
// title: '接口请求超时',
// icon: 'none'
// })
// reject(e.data)
},
complete: () => {
uni.hideLoading()
}
});
})
}
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)
}
}