2022-02-12 11:33:47 +00:00
|
|
|
|
// 统一给参数
|
|
|
|
|
const dataObj = (url, params) => {
|
|
|
|
|
let options = params
|
|
|
|
|
// #ifdef APP-PLUS
|
|
|
|
|
let data = null; //业务数据
|
|
|
|
|
let terminal = 1 //终端类型,web:0,app:1
|
|
|
|
|
options = {
|
|
|
|
|
...params,
|
|
|
|
|
data,
|
|
|
|
|
terminal
|
|
|
|
|
}
|
|
|
|
|
// #endif
|
|
|
|
|
return options
|
|
|
|
|
}
|
|
|
|
|
const goLogin = () => {
|
2022-02-18 08:26:42 +00:00
|
|
|
|
console.log('token失效,request.js的 goLogin 方法输出');
|
|
|
|
|
uni.reLaunch({
|
|
|
|
|
url: '/pages/login/login'
|
|
|
|
|
})
|
2022-02-12 11:33:47 +00:00
|
|
|
|
}
|
|
|
|
|
// 请求错误处理
|
|
|
|
|
const checkError = (e, reject) => {
|
2022-02-18 08:26:42 +00:00
|
|
|
|
console.log(e,`未请求成功的接口,request.js的 checkError 方法输出`);
|
2022-02-12 11:33:47 +00:00
|
|
|
|
if (e.data) {
|
|
|
|
|
if (e.data.code) {
|
|
|
|
|
switch (Number(e.data.code)) {
|
|
|
|
|
case 4001:
|
|
|
|
|
goLogin()
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
reject(e.data)
|
2022-02-18 08:26:42 +00:00
|
|
|
|
} else reject({msg:`接口错误,request.js的 checkError 方法输出`})
|
2022-02-12 11:33:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 封装请求
|
|
|
|
|
const request = (method, url, options) => {
|
2022-02-18 08:26:42 +00:00
|
|
|
|
// console.log(`请求方法:${method},请求接口:${url},38行输出`);
|
2022-02-12 11:33:47 +00:00
|
|
|
|
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-13 13:38:30 +00:00
|
|
|
|
url: `${uni.getStorageSync('hostapi')}${url}`,
|
2022-02-12 11:33:47 +00:00
|
|
|
|
method: methods,
|
|
|
|
|
data: dataObj(url, options),
|
|
|
|
|
header: headers,
|
|
|
|
|
success: res => {
|
|
|
|
|
// console.log('反的结果===>',res);
|
|
|
|
|
if (res.statusCode == 200) {
|
|
|
|
|
if (res.data.code == 0) {
|
|
|
|
|
resolve(res.data)
|
|
|
|
|
} else {
|
|
|
|
|
resolve(res.data)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
reject(res.data)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
fail: e => {
|
|
|
|
|
checkError(e, reject)
|
|
|
|
|
},
|
|
|
|
|
complete: rest => {
|
2022-02-18 08:26:42 +00:00
|
|
|
|
uni.hideToast();
|
2022-02-12 11:33:47 +00:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 上传文件 封装请求
|
|
|
|
|
const uploadFile = (url, options) => {
|
|
|
|
|
let tempData = options || {}
|
|
|
|
|
uni.showLoading({title: "上传中..."})
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
uni.uploadFile({
|
2022-02-13 13:38:30 +00:00
|
|
|
|
url: `${uni.getStorageSync('hostapi')}${url}`,
|
2022-02-12 11:33:47 +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) {
|
2022-02-18 08:26:42 +00:00
|
|
|
|
reject(e.data)
|
2022-02-12 11:33:47 +00:00
|
|
|
|
},
|
|
|
|
|
complete: () => {
|
2022-02-18 08:26:42 +00:00
|
|
|
|
uni.hideToast();
|
2022-02-12 11:33:47 +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) => {
|
|
|
|
|
return uploadFile(url, options)
|
|
|
|
|
}
|
|
|
|
|
}
|