请求封装处理
This commit is contained in:
parent
24036aad58
commit
81a7a71d24
212
http/http.js
212
http/http.js
|
|
@ -1,128 +1,144 @@
|
||||||
// 导入全局属性
|
// 导入全局属性
|
||||||
import {
|
import { sm4DecryptByResData } from "@/utils/encryptUtil.js";
|
||||||
sm4DecryptByResData
|
import infoBox from "@/utils/infoBox.js";
|
||||||
} from '@/utils/encryptUtil.js'
|
import { reject } from "lodash";
|
||||||
import infoBox from "@/utils/infoBox.js"
|
import config from "@/commons/config.js";
|
||||||
import {
|
|
||||||
reject
|
|
||||||
} from 'lodash';
|
|
||||||
import config from '@/commons/config.js'
|
|
||||||
// 测试服
|
// 测试服
|
||||||
let baseUrl = config.baseApiUrl + 'app'
|
let baseUrl = config.baseApiUrl + "app";
|
||||||
const loadingShowTime = 200
|
const loadingShowTime = 200;
|
||||||
|
|
||||||
function getHeader() {
|
function getHeader() {
|
||||||
const headerObject = {}
|
const headerObject = {};
|
||||||
headerObject["token"] = uni.getStorageSync('token')
|
headerObject["token"] = uni.getStorageSync("token");
|
||||||
return headerObject
|
return headerObject;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 通用处理逻辑
|
// 通用处理逻辑
|
||||||
function commonsProcess(showLoading, httpReqCallback) {
|
function commonsProcess(showLoading, httpReqCallback) {
|
||||||
// 判断是否请求完成(用作 是否loading )
|
// 判断是否请求完成(用作 是否loading )
|
||||||
// 包括: 'ing', 'ingLoading', 'finish'
|
// 包括: 'ing', 'ingLoading', 'finish'
|
||||||
let reqState = 'ing'
|
let reqState = "ing";
|
||||||
// 是否已经提示的错误信息
|
// 是否已经提示的错误信息
|
||||||
let isShowErrorToast = false
|
let isShowErrorToast = false;
|
||||||
// 请求完成, 需要处理的动作
|
// 请求完成, 需要处理的动作
|
||||||
let reqFinishFunc = () => {
|
let reqFinishFunc = () => {
|
||||||
if (reqState == 'ingLoading') { // 关闭loading弹层
|
if (reqState == "ingLoading") {
|
||||||
infoBox.hideLoading()
|
// 关闭loading弹层
|
||||||
}
|
infoBox.hideLoading();
|
||||||
reqState = 'finish' // 请求完毕
|
|
||||||
}
|
}
|
||||||
|
reqState = "finish"; // 请求完毕
|
||||||
|
};
|
||||||
// 明确显示loading
|
// 明确显示loading
|
||||||
if (showLoading) {
|
if (showLoading) {
|
||||||
// xx ms内响应完成,不提示loading
|
// xx ms内响应完成,不提示loading
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
if (reqState == 'ing') {
|
if (reqState == "ing") {
|
||||||
reqState = 'ingLoading'
|
reqState = "ingLoading";
|
||||||
infoBox.showLoading()
|
infoBox.showLoading();
|
||||||
}
|
}
|
||||||
}, loadingShowTime)
|
}, loadingShowTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
return httpReqCallback().then((httpData) => {
|
return httpReqCallback()
|
||||||
|
.then((httpData) => {
|
||||||
reqFinishFunc(); // 请求完毕的动作
|
reqFinishFunc(); // 请求完毕的动作
|
||||||
// 从http响应数据中解构响应数据 [ 响应码、 bodyData ]
|
// 从http响应数据中解构响应数据 [ 响应码、 bodyData ]
|
||||||
let {
|
let { statusCode, data } = httpData;
|
||||||
statusCode,
|
|
||||||
data
|
|
||||||
} = httpData
|
|
||||||
// 避免混淆重新命名
|
// 避免混淆重新命名
|
||||||
let bodyData = data
|
let bodyData = data;
|
||||||
if (statusCode == 500) {
|
if (statusCode == 500) {
|
||||||
isShowErrorToast = true
|
isShowErrorToast = true;
|
||||||
return Promise.reject(bodyData) // 跳转到catch函数
|
return Promise.reject(bodyData); // 跳转到catch函数
|
||||||
}
|
}
|
||||||
|
|
||||||
if (statusCode == 401) {
|
if (statusCode == 401) {
|
||||||
// 提示信息
|
// 提示信息
|
||||||
isShowErrorToast = true
|
isShowErrorToast = true;
|
||||||
return Promise.reject(bodyData) // 跳转到catch函数
|
return Promise.reject(bodyData); // 跳转到catch函数
|
||||||
}
|
}
|
||||||
// http响应码不正确
|
// http响应码不正确
|
||||||
if (statusCode != 200 && statusCode != 204 && statusCode != 201) {
|
if (statusCode != 200 && statusCode != 204 && statusCode != 201) {
|
||||||
isShowErrorToast = true
|
isShowErrorToast = true;
|
||||||
data.message = data.message == 'Bad credentials' ? '用户名或密码错误' : data.message
|
data.message =
|
||||||
infoBox.showToast(data.message || '服务器异常')
|
data.message == "Bad credentials" ? "用户名或密码错误" : data.message;
|
||||||
return Promise.reject(bodyData) // 跳转到catch函数
|
infoBox.showToast(data.message || "服务器异常");
|
||||||
|
return Promise.reject(bodyData); // 跳转到catch函数
|
||||||
|
}
|
||||||
|
if (bodyData.code == 401) {
|
||||||
|
// 提示信息
|
||||||
|
isShowErrorToast = true;
|
||||||
|
return Promise.reject(bodyData); // 跳转到catch函数
|
||||||
|
}
|
||||||
|
if (bodyData.code != 0) {
|
||||||
|
infoBox.showToast(data.message);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 加密数据
|
// 加密数据
|
||||||
if (!bodyData.data && bodyData.encryptData) {
|
if (!bodyData.data && bodyData.encryptData) {
|
||||||
|
|
||||||
return Promise.resolve({
|
return Promise.resolve({
|
||||||
bizData: sm4DecryptByResData(bodyData.encryptData),
|
bizData: sm4DecryptByResData(bodyData.encryptData),
|
||||||
code: bodyData.code
|
code: bodyData.code,
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// 构造请求成功的响应数据
|
// 构造请求成功的响应数据
|
||||||
return Promise.resolve(bodyData)
|
return Promise.resolve(bodyData.data || bodyData.page);
|
||||||
|
|
||||||
}).catch(res => {
|
|
||||||
if (res.status == 401) {
|
|
||||||
infoBox.showErrorToast(res.message || '请登录').then(() => {
|
|
||||||
uni.redirectTo({
|
|
||||||
url: '/pages/login/index'
|
|
||||||
})
|
|
||||||
reject()
|
|
||||||
})
|
})
|
||||||
|
.catch((res) => {
|
||||||
|
console.log(res);
|
||||||
|
if (res.status == 404) {
|
||||||
|
infoBox.showErrorToast("接口404").then(() => {});
|
||||||
|
reject();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if (res.status == 500) {
|
|
||||||
infoBox.showErrorToast(res.message || '服务器异常').then(() => {})
|
if (res.code == 401) {
|
||||||
|
infoBox.showErrorToast(res.message || "请登录").then(() => {
|
||||||
|
uni.redirectTo({
|
||||||
|
url: "/pages/login/index",
|
||||||
|
});
|
||||||
|
reject();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (res.code != 0) {
|
||||||
|
infoBox.showErrorToast(res.msg).then(() => {});
|
||||||
|
reject();
|
||||||
|
}
|
||||||
|
if (res.code == 500) {
|
||||||
|
infoBox.showErrorToast(res.message || "服务器异常").then(() => {});
|
||||||
|
reject();
|
||||||
}
|
}
|
||||||
reqFinishFunc(); // 请求完毕的动作
|
reqFinishFunc(); // 请求完毕的动作
|
||||||
|
|
||||||
// 如果没有提示错误, 那么此处提示 异常。
|
// 如果没有提示错误, 那么此处提示 异常。
|
||||||
if (!isShowErrorToast) {
|
if (!isShowErrorToast) {
|
||||||
infoBox.showErrorToast(`请求网络异常`)
|
infoBox.showErrorToast(`请求网络异常`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Promise.reject(res)
|
return Promise.reject(res);
|
||||||
|
})
|
||||||
}).finally(() => { // finally 是 then结束后再执行, 此处不适用。 需要在请求完成后立马调用: reqFinishFunc()
|
.finally(() => {
|
||||||
|
// finally 是 then结束后再执行, 此处不适用。 需要在请求完成后立马调用: reqFinishFunc()
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 默认 显示loading(控制 xxs 内 不提示loading )
|
// 默认 显示loading(控制 xxs 内 不提示loading )
|
||||||
function req(uri, data, method = "GET", showLoading = true, extParams = {}) {
|
function req(uri, data, method = "GET", showLoading = true, extParams = {}) {
|
||||||
return commonsProcess(showLoading, () => {
|
return commonsProcess(showLoading, () => {
|
||||||
return uni.request(
|
return uni.request(
|
||||||
Object.assign({
|
Object.assign(
|
||||||
|
{
|
||||||
url: baseUrl + uri,
|
url: baseUrl + uri,
|
||||||
data: data,
|
data: data,
|
||||||
method: method,
|
method: method,
|
||||||
header: getHeader()
|
header: getHeader(),
|
||||||
}, extParams)
|
},
|
||||||
|
extParams
|
||||||
)
|
)
|
||||||
})
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 默认 显示loading(控制 xxs 内 不提示loading )
|
// 默认 显示loading(控制 xxs 内 不提示loading )
|
||||||
function request(args) {
|
function request(args) {
|
||||||
const {
|
const {
|
||||||
|
|
@ -131,63 +147,71 @@ function request(args) {
|
||||||
params,
|
params,
|
||||||
method = "GET",
|
method = "GET",
|
||||||
showLoading = true,
|
showLoading = true,
|
||||||
extParams = {}
|
extParams = {},
|
||||||
} = args
|
} = args;
|
||||||
let headerObject = {}
|
let headerObject = {};
|
||||||
return commonsProcess(showLoading, () => {
|
return commonsProcess(showLoading, () => {
|
||||||
return uni.request(
|
return uni.request(
|
||||||
Object.assign({
|
Object.assign(
|
||||||
|
{
|
||||||
url: slash(baseUrl, url),
|
url: slash(baseUrl, url),
|
||||||
data: params || data,
|
data: params || data,
|
||||||
method: method,
|
method: method,
|
||||||
header: getHeader()
|
header: getHeader(),
|
||||||
}, extParams)
|
},
|
||||||
|
extParams
|
||||||
)
|
)
|
||||||
})
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
// 处理/
|
// 处理/
|
||||||
function slash(baseUrl, url) {
|
function slash(baseUrl, url) {
|
||||||
let u = ''
|
let u = "";
|
||||||
if (baseUrl[baseUrl.length - 1] == '/') {
|
if (baseUrl[baseUrl.length - 1] == "/") {
|
||||||
u += baseUrl
|
u += baseUrl;
|
||||||
} else {
|
} else {
|
||||||
u = u + baseUrl + '/'
|
u = u + baseUrl + "/";
|
||||||
}
|
}
|
||||||
if (url[0] == '/') {
|
if (url[0] == "/") {
|
||||||
u = u + url.slice(1)
|
u = u + url.slice(1);
|
||||||
} else {
|
} else {
|
||||||
u = u + url
|
u = u + url;
|
||||||
}
|
}
|
||||||
return u
|
return u;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 上传
|
// 上传
|
||||||
function upload(uri, data, file, showLoading = true, extParams = {}) {
|
function upload(uri, data, file, showLoading = true, extParams = {}) {
|
||||||
// 放置token
|
// 放置token
|
||||||
let headerObject = {}
|
let headerObject = {};
|
||||||
return commonsProcess(showLoading, () => {
|
return commonsProcess(showLoading, () => {
|
||||||
return uni.uploadFile(
|
return uni
|
||||||
Object.assign({
|
.uploadFile(
|
||||||
|
Object.assign(
|
||||||
|
{
|
||||||
url: baseUrl + uri,
|
url: baseUrl + uri,
|
||||||
formData: data,
|
formData: data,
|
||||||
name: "file",
|
name: "file",
|
||||||
filePath: file.path || file.url,
|
filePath: file.path || file.url,
|
||||||
header: getHeader()
|
header: getHeader(),
|
||||||
}, extParams)
|
},
|
||||||
).then((httpData) => {
|
extParams
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.then((httpData) => {
|
||||||
// uni.upload 返回bodyData 的是 string类型。 需要解析。
|
// uni.upload 返回bodyData 的是 string类型。 需要解析。
|
||||||
httpData.data = JSON.parse(httpData.data)
|
httpData.data = JSON.parse(httpData.data);
|
||||||
return Promise.resolve(httpData)
|
return Promise.resolve(httpData);
|
||||||
}).catch(err => {
|
|
||||||
uni.hideLoading()
|
|
||||||
infoBox.showErrorToast(`上传失败`)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
uni.hideLoading();
|
||||||
|
infoBox.showErrorToast(`上传失败`);
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
req: req,
|
req: req,
|
||||||
request,
|
request,
|
||||||
upload: upload
|
upload: upload,
|
||||||
}
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue