204 lines
4.9 KiB
JavaScript
204 lines
4.9 KiB
JavaScript
/**
|
||
* HTTP的封装, 基于uni.request
|
||
* 包括: 通用响应结果的处理 和 业务的增删改查函数
|
||
*
|
||
* @author terrfly
|
||
* @site https://www.jeequan.com
|
||
* @date 2021/12/16 18:35
|
||
*/
|
||
|
||
// 导入全局属性
|
||
import appConfig from '@/config/appConfig.js'
|
||
import storageManage from '@/commons/utils/storageManage.js'
|
||
|
||
// let baseUrl = '' //测试
|
||
// let baseUrl = 'https://cashier-client.sxczgkj.cn/cashier-client' //测试环境
|
||
// let baseUrl = 'https://pre-cashierclient.sxczgkj.cn/cashier-client' //预发布环境
|
||
// let baseUrl = 'https://cashierclient.sxczgkj.cn/cashier-client' //线上环境
|
||
let baseUrl = appConfig.ENV_ENUM.VITE_API_URL
|
||
|
||
// 多少 ms 以内, 不提示loading
|
||
const loadingShowTime = 200
|
||
|
||
|
||
function getHeader(){
|
||
const headerObject={}
|
||
headerObject["Authorization"] = storageManage.token()
|
||
headerObject["Content-Type"] = 'application/json'
|
||
headerObject["loginname"] = 'admin'
|
||
headerObject["token"] = 'eyJhbGciOiJIUzI1NiJ9.eyJ1c2VyVHlwZSI6Ik1HIiwiZXhwIjoxNjkwMTgwNzE2LCJ1c2VySWQiOiIyNDQiLCJpYXQiOjE2ODg3MDk0ODcsImxvZ2luTmFtZSI6ImFkbWluIn0.lqxxvv2-FcecQngMBorz4MpkB3mIJQDG-IUULQyV-KQ'
|
||
headerObject["userId"] = '244'
|
||
return headerObject
|
||
}
|
||
|
||
// 通用处理逻辑
|
||
function commonsProcess(showLoading, httpReqCallback) {
|
||
|
||
// 判断是否请求完成(用作 是否loading )
|
||
// 包括: 'ing', 'ingLoading', 'finish'
|
||
let reqState = 'ing'
|
||
|
||
// 是否已经提示的错误信息
|
||
let isShowErrorToast = false
|
||
|
||
|
||
// 请求完成, 需要处理的动作
|
||
let reqFinishFunc = () => {
|
||
|
||
if (reqState == 'ingLoading') { // 关闭loading弹层
|
||
uni.hideLoading()
|
||
}
|
||
reqState = 'finish' // 请求完毕
|
||
}
|
||
|
||
// 明确显示loading
|
||
if (showLoading) {
|
||
// xx ms内响应完成,不提示loading
|
||
setTimeout(() => {
|
||
if (reqState == 'ing') {
|
||
reqState = 'ingLoading'
|
||
uni.showLoading({ title: '请稍后', mask: true })
|
||
}
|
||
}, loadingShowTime)
|
||
}
|
||
|
||
return httpReqCallback().then((httpData) => {
|
||
reqFinishFunc(); // 请求完毕的动作
|
||
|
||
// 从http响应数据中解构响应数据 [ 响应码、 bodyData ]
|
||
let {
|
||
statusCode,
|
||
data
|
||
} = httpData
|
||
// 避免混淆重新命名
|
||
let bodyData = data
|
||
if (statusCode == 500) {
|
||
isShowErrorToast = true
|
||
return Promise.reject(bodyData) // 跳转到catch函数
|
||
}
|
||
if (statusCode == 401) {
|
||
// storageManage.token(null, true)
|
||
// 提示信息
|
||
isShowErrorToast = true
|
||
// infoBox.showErrorToast('请登录').then(() => {
|
||
// go.to("PAGES_LOGIN", {}, go.GO_TYPE_RELAUNCH)
|
||
// })
|
||
return Promise.reject(bodyData) // 跳转到catch函数
|
||
}
|
||
// http响应码不正确
|
||
if (statusCode != 200 && statusCode != 204 && statusCode != 201) {
|
||
isShowErrorToast = true
|
||
uni.showToast({
|
||
title: data.message || '服务器异常',
|
||
icon: 'none'
|
||
})
|
||
return Promise.reject(bodyData) // 跳转到catch函数
|
||
}
|
||
|
||
|
||
// 构造请求成功的响应数据
|
||
return Promise.resolve(bodyData)
|
||
|
||
}).catch(res => {
|
||
if(res.status==401){
|
||
uni.showToast({
|
||
title: '登录失效',
|
||
icon: 'none'
|
||
})
|
||
}
|
||
if(res.status==500){
|
||
uni.showToast({
|
||
title: '网络异常',
|
||
icon: 'none'
|
||
})
|
||
}
|
||
reqFinishFunc(); // 请求完毕的动作
|
||
|
||
// 如果没有提示错误, 那么此处提示 异常。
|
||
if (!isShowErrorToast) {
|
||
uni.showToast({
|
||
title: '请求网络异常',
|
||
icon: 'none'
|
||
})
|
||
}
|
||
|
||
return Promise.reject(res)
|
||
|
||
}).finally(() => { // finally 是 then结束后再执行, 此处不适用。 需要在请求完成后立马调用: reqFinishFunc()
|
||
|
||
});
|
||
|
||
}
|
||
|
||
|
||
// 默认 显示loading(控制 xxs 内 不提示loading )
|
||
function req(uri, data, method = "GET", showLoading = true, extParams = {}) {
|
||
// headerObject[appConfig.tokenKey] = storageManage.token()
|
||
return commonsProcess(showLoading, () => {
|
||
return uni.request(
|
||
Object.assign({
|
||
url: baseUrl + uri,
|
||
data: data,
|
||
method: method,
|
||
header: getHeader()
|
||
}, extParams)
|
||
)
|
||
})
|
||
}
|
||
|
||
|
||
// 默认 显示loading(控制 xxs 内 不提示loading )
|
||
function request(args) {
|
||
const {
|
||
url,
|
||
data,
|
||
params,
|
||
method = "GET",
|
||
showLoading = true,
|
||
extParams = {}
|
||
} = args
|
||
let headerObject = {}
|
||
// headerObject[appConfig.tokenKey] = storageManage.token()
|
||
|
||
return commonsProcess(showLoading, () => {
|
||
return uni.request(
|
||
Object.assign({
|
||
url: baseUrl + url,
|
||
data: params||data,
|
||
method: method,
|
||
header: getHeader()
|
||
}, extParams)
|
||
)
|
||
})
|
||
}
|
||
|
||
|
||
|
||
// 上传
|
||
function upload(uri, data, file, showLoading = true, extParams = {}) {
|
||
// 放置token
|
||
let headerObject = {}
|
||
// headerObject[appConfig.tokenKey] = storageManage.token()
|
||
|
||
return commonsProcess(showLoading, () => {
|
||
return uni.uploadFile(
|
||
Object.assign({
|
||
url: baseUrl + uri,
|
||
formData: data,
|
||
name: "file",
|
||
filePath: file.path,
|
||
header: getHeader()
|
||
}, extParams)
|
||
).then((httpData) => {
|
||
// uni.upload 返回bodyData 的是 string类型。 需要解析。
|
||
httpData.data = JSON.parse(httpData.data)
|
||
return Promise.resolve(httpData)
|
||
})
|
||
})
|
||
}
|
||
|
||
export default {
|
||
req: req,
|
||
request,
|
||
upload: upload
|
||
} |