更新代码

This commit is contained in:
duan
2025-01-06 17:24:15 +08:00
parent f016c3094d
commit 4ec188968d
10 changed files with 493 additions and 54 deletions

View File

@@ -1,17 +1,20 @@
// 导入全局属性
import {
sm4DecryptByResData
} from '@/utils/encryptUtil.js'
import infoBox from "@/utils/infoBox.js"
import { reject } from 'lodash';
import {
reject
} from 'lodash';
import config from '@/commons/config.js'
// 测试服
let baseUrl = config.baseApiUrl
const loadingShowTime = 200
function getHeader(){
const headerObject={}
headerObject["token"] = uni.getStorageSync('token')
function getHeader() {
const headerObject = {}
headerObject["token"] = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJzdWIiOiIyNjkyNSIsImlhdCI6MTczNTg4OTk5NCwiZXhwIjoxNzM2NDk0Nzk0fQ.j-KFEE1FHckmFCO8UA884RBWvpMv8MfEGj7GPGf3kVo6sHeORl043Yle_w7HdTZKPpQqSr2LQLCq_rUxG4qqwA'
// headerObject["token"] = uni.getStorageSync('token')
return headerObject
}
@@ -61,7 +64,7 @@ function commonsProcess(showLoading, httpReqCallback) {
// http响应码不正确
if (statusCode != 200 && statusCode != 204 && statusCode != 201) {
isShowErrorToast = true
data.message=data.message=='Bad credentials'?'用户名或密码错误':data.message
data.message = data.message == 'Bad credentials' ? '用户名或密码错误' : data.message
infoBox.showToast(data.message || '服务器异常')
return Promise.reject(bodyData) // 跳转到catch函数
}
@@ -79,15 +82,16 @@ function commonsProcess(showLoading, httpReqCallback) {
return Promise.resolve(bodyData)
}).catch(res => {
if(res.status==401){
infoBox.showErrorToast(res.message||'请登录').then(() => {
uni.redirectTo({url: '/pages/login/index'})
if (res.status == 401) {
infoBox.showErrorToast(res.message || '请登录').then(() => {
uni.redirectTo({
url: '/pages/login/index'
})
reject()
})
}
if(res.status==500){
infoBox.showErrorToast(res.message||'服务器异常').then(() => {
})
if (res.status == 500) {
infoBox.showErrorToast(res.message || '服务器异常').then(() => {})
}
reqFinishFunc(); // 请求完毕的动作
@@ -134,17 +138,30 @@ function request(args) {
return commonsProcess(showLoading, () => {
return uni.request(
Object.assign({
url: baseUrl + url,
data: params||data,
url: slash(baseUrl, url),
data: params || data,
method: method,
header: getHeader()
header: getHeader()
}, extParams)
)
})
}
// 处理/
function slash(baseUrl, url) {
let u = ''
if (baseUrl[baseUrl.length - 1] == '/') {
u += baseUrl
} else {
u = u + baseUrl + '/'
}
if (url[0] == '/') {
u = u + url.slice(1)
} else {
u = u + url
}
return u
}
// 上传
function upload(uri, data, file, showLoading = true, extParams = {}) {
@@ -156,14 +173,14 @@ function upload(uri, data, file, showLoading = true, extParams = {}) {
url: baseUrl + uri,
formData: data,
name: "file",
filePath: file.path||file.url,
header: getHeader()
filePath: file.path || file.url,
header: getHeader()
}, extParams)
).then((httpData) => {
// uni.upload 返回bodyData 的是 string类型。 需要解析。
httpData.data = JSON.parse(httpData.data)
return Promise.resolve(httpData)
}).catch(err=>{
}).catch(err => {
uni.hideLoading()
infoBox.showErrorToast(`上传失败`)
})