2022-03-14 09:09:10 +00:00
|
|
|
|
// 判断当前环境
|
|
|
|
|
const ENV = process.env.NODE_ENV;
|
|
|
|
|
console.log(ENV,'当前环境'); // development:开发环境 test:测试环境 production:生产环境
|
|
|
|
|
// 清理所有缓存并前往授权页
|
|
|
|
|
const goLogin = () => {
|
|
|
|
|
uni.clearStorageSync();
|
|
|
|
|
uni.navigateTo({
|
|
|
|
|
url: '/pages/login/login'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
// 请求错误处理
|
|
|
|
|
const checkError = (e) => {
|
|
|
|
|
// console.error("----接口错误----", e)
|
|
|
|
|
if (e.data) {
|
|
|
|
|
if (e.data.code) {
|
|
|
|
|
switch (Number(e.data.code)) {
|
|
|
|
|
case 4003:
|
|
|
|
|
// 参数错误
|
|
|
|
|
console.log('4003参数错误');
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 封装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',
|
2022-03-31 10:34:17 +00:00
|
|
|
|
'Authorization': 'Bearer '+uni.getStorageSync('token') || '',
|
2022-03-31 03:15:53 +00:00
|
|
|
|
'token':uni.getStorageSync('token') || ''
|
2022-03-14 09:09:10 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'post':
|
|
|
|
|
methods = 'POST'
|
|
|
|
|
headers = {
|
|
|
|
|
'Content-Type': 'application/json; charset=UTF-8',
|
2022-03-31 10:34:17 +00:00
|
|
|
|
'Authorization': 'Bearer '+uni.getStorageSync('token') || '',
|
|
|
|
|
'token':uni.getStorageSync('token') || ''
|
2022-03-14 09:09:10 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'postForm':
|
|
|
|
|
methods = 'POST'
|
|
|
|
|
headers = {
|
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
|
2022-03-31 10:34:17 +00:00
|
|
|
|
'Authorization': 'Bearer '+uni.getStorageSync('token') || '',
|
|
|
|
|
'token':uni.getStorageSync('token') || ''
|
2022-03-14 09:09:10 +00:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-03-21 06:53:40 +00:00
|
|
|
|
let params = {};
|
|
|
|
|
if(options!=undefined) params = options;
|
2022-03-31 09:58:34 +00:00
|
|
|
|
// params.token = uni.getStorageSync('token');
|
2022-03-14 09:09:10 +00:00
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
uni.request({
|
2022-04-22 10:39:36 +00:00
|
|
|
|
url: `${getApp().globalData.hostapi}${url}`,
|
2022-03-14 09:09:10 +00:00
|
|
|
|
method: methods,
|
2022-03-21 06:53:40 +00:00
|
|
|
|
data: params,
|
2022-03-14 09:09:10 +00:00
|
|
|
|
header: headers,
|
2022-04-02 01:17:26 +00:00
|
|
|
|
// sslVerify:false,//验证 ssl 证书 仅App安卓端支持
|
|
|
|
|
// withCredentials:true,//跨域请求时是否携带凭证(cookies)仅H5支持
|
2022-03-14 09:09:10 +00:00
|
|
|
|
success: res => {
|
|
|
|
|
console.log(`${url}返的结果===>`,res);
|
|
|
|
|
if (res.statusCode == 200) {
|
2022-03-21 06:53:40 +00:00
|
|
|
|
// 接口调用成功
|
|
|
|
|
resolve(res.data);
|
2022-03-14 09:09:10 +00:00
|
|
|
|
} else {
|
|
|
|
|
// 接口返回错误信息
|
|
|
|
|
checkError(res);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
fail: e => {
|
|
|
|
|
// 接口请求错误
|
|
|
|
|
checkError(e, reject);
|
|
|
|
|
},
|
|
|
|
|
complete: rest => {
|
|
|
|
|
// 是否成功,都会执行
|
2022-04-18 09:08:12 +00:00
|
|
|
|
console.log(rest,115);
|
2022-03-14 09:09:10 +00:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
// 上传文件 封装请求
|
|
|
|
|
const uploadFile = (url, options) => {
|
|
|
|
|
let tempData = options || {}
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
uni.uploadFile({
|
2022-04-22 10:39:36 +00:00
|
|
|
|
url: `${getApp().globalData.hostapi}${url}`,
|
2022-04-06 03:24:55 +00:00
|
|
|
|
filePath: tempData.path,
|
|
|
|
|
name: 'file',
|
2022-03-14 09:09:10 +00:00
|
|
|
|
fileType:'image',
|
|
|
|
|
formData: tempData,
|
2022-04-02 01:17:26 +00:00
|
|
|
|
// sslVerify:false,//验证 ssl 证书 仅App安卓端支持
|
|
|
|
|
// withCredentials:true,//跨域请求时是否携带凭证(cookies)仅H5支持
|
2022-03-14 09:09:10 +00:00
|
|
|
|
header: {
|
2022-04-06 03:24:55 +00:00
|
|
|
|
// 'Content-Type': 'multipart/form-data;charset=UTF-8',
|
|
|
|
|
// 'Authorization': 'Bearer '+uni.getStorageSync('token') || '',
|
|
|
|
|
token:uni.getStorageSync('token') || ''
|
2022-03-14 09:09:10 +00:00
|
|
|
|
},
|
|
|
|
|
success: res => {
|
2022-04-18 09:08:12 +00:00
|
|
|
|
console.log(res,138);
|
2022-03-14 09:09:10 +00:00
|
|
|
|
if (res.statusCode == 200) {
|
|
|
|
|
let temp = JSON.parse(res.data)
|
2022-04-06 03:24:55 +00:00
|
|
|
|
if (temp.code == 1) {
|
2022-03-14 09:09:10 +00:00
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
}
|