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.
 
 
 

66 lines
1.8 KiB

const appConfig = {
// BASE_URL: 'http://127.0.0.1:48080/app-api',
BASE_URL: 'http://119.23.220.80:16801/app-api',
TENANT_ID: 151
}
let clientId: any
const fetch = async (url: string, options: any) => {
if (!url) {
console.warn('url is must be string!')
return
}
const $config = useRuntimeConfig()
const reqUrl = url.indexOf('http') > -1 ? url : appConfig.BASE_URL + url
if (!options.headers) {
options.credentials = 'include' // 跨域携带cookie
options.headers = {
'tenant-id': appConfig.TENANT_ID
}
}
return new Promise((resolve, reject) => {
console.log(reqUrl)
useFetch(reqUrl, { ...options }).then(({ data, error }) => {
if (error && error.value) {
reject(error.value)
return
}
const res: any = data.value
if (res && res.code === 0) {
// return res 正确应该返回
resolve(res.data)
} else if (res && (res.msg || res.errMsg || res.message)) {
console.error(res.msg)
if (res.code === 401) { // 会话过期跳登录
setTimeout(()=>{
navigateTo('/login')
}, 1000)
return
}
reject(res.message)
}
}).catch(err => {
reject(err)
})
})
}
export default new class Http {
get(url: string, params: any) {
return fetch(url, { method: 'GET', params })
}
post(url: string, body: any) {
return fetch(url, { method: 'POST', body })
}
reqData(url: string, body: any) {
return fetch(url, { method: 'POST', body, headers: {} })
}
}