惠企通
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

68 lines
2.2 KiB

interface ResponseOptions {
url: string
headers?: { [key: string]: string }
method?: 'OPTIONS' | 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'TRACE' | 'CONNECT'
data?: { [key: string]: any }
isSinglePost?: boolean
}
export const request = {
isLock: false,
http({ url = '', headers = {}, data = {}, method = 'POST', isSinglePost = false }: ResponseOptions) {
const _this = this
return new Promise(function (resolve, reject) {
if (isSinglePost && _this.isLock) {
reject({ message: '加载中' })
}
_this.isLock = true
// #ifdef APP-PLUS
url = import.meta.env.VITE_APP_BASE_URL + url
// #endif
// #ifdef H5
url = import.meta.env.VITE_APP_BASE_PRE + url
// #endif
// #ifdef MP-WEIXIN
url = import.meta.env.VITE_APP_BASE_URL + url
// #endif
const header = Object.assign(
{ 'content-type': 'application/json', Authorization: '', token: uni.getStorageSync('access_token') || '' },
headers
)
uni.request({
url,
header,
method,
data,
success(res) {
const data = res.data as { code: number; data: object; msg: string }
switch (data.code) {
case 1:
resolve(res.data)
break
case 401:
uni.showToast({ title: data.msg, icon: 'none', mask: true })
uni.removeStorageSync('access_token')
break
default:
uni.showToast({ title: data.msg, icon: 'none', mask: true })
reject(res.data)
break
}
},
fail(err) {
uni.showToast({ title: '网络错误', icon: 'none' })
reject(err)
},
complete() {
_this.isLock = false
}
})
})
}
}