130 lines
3.2 KiB
JavaScript
130 lines
3.2 KiB
JavaScript
const KEY_TOKEN = 'COMPREHENSIVE_VENUE_TOKEN'
|
|
|
|
const API_BASE_URL = import.meta.env.VITE_COMPREHENSIVE_API_BASE_URL || 'http://127.0.0.1:8001'
|
|
|
|
const normalizeError = (res, fallbackMessage = '请求服务失败') => {
|
|
const body = res?.data || res || {}
|
|
return {
|
|
code: body.code || res?.statusCode || 50000,
|
|
message: body.message || fallbackMessage,
|
|
errors: body.errors || null,
|
|
statusCode: res?.statusCode
|
|
}
|
|
}
|
|
|
|
export const authStorage = {
|
|
setToken(token) {
|
|
uni.setStorageSync(KEY_TOKEN, token)
|
|
},
|
|
getToken() {
|
|
return uni.getStorageSync(KEY_TOKEN)
|
|
},
|
|
clear() {
|
|
uni.removeStorageSync(KEY_TOKEN)
|
|
},
|
|
isLoggedIn() {
|
|
return !!uni.getStorageSync(KEY_TOKEN)
|
|
}
|
|
}
|
|
|
|
export function request(options = {}) {
|
|
const token = authStorage.getToken()
|
|
const header = {
|
|
'Content-Type': 'application/json',
|
|
...(options.header || {})
|
|
}
|
|
|
|
if (token) {
|
|
header.Authorization = `Bearer ${token}`
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
uni.request({
|
|
url: options.url.startsWith('http') ? options.url : `${API_BASE_URL}${options.url}`,
|
|
method: options.method || 'GET',
|
|
data: options.data,
|
|
header,
|
|
success: (res) => {
|
|
const body = res.data
|
|
|
|
if (res.statusCode === 401 || body?.code === 40001) {
|
|
authStorage.clear()
|
|
}
|
|
|
|
if (res.statusCode >= 200 && res.statusCode < 300 && body?.code === 0) {
|
|
resolve(body.data)
|
|
return
|
|
}
|
|
|
|
const error = normalizeError(res, `请求错误 ${res.statusCode}`)
|
|
uni.showToast({
|
|
title: error.message,
|
|
icon: 'none'
|
|
})
|
|
reject(error)
|
|
},
|
|
fail: (err) => {
|
|
const error = normalizeError(err, '网络连接失败,请检查服务')
|
|
uni.showToast({
|
|
title: error.message,
|
|
icon: 'none'
|
|
})
|
|
reject(error)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
export function uploadFile(options = {}) {
|
|
const token = authStorage.getToken()
|
|
const header = {
|
|
...(options.header || {})
|
|
}
|
|
|
|
if (token) {
|
|
header.Authorization = `Bearer ${token}`
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
uni.uploadFile({
|
|
url: options.url.startsWith('http') ? options.url : `${API_BASE_URL}${options.url}`,
|
|
filePath: options.filePath,
|
|
name: options.name || 'file',
|
|
formData: options.formData || {},
|
|
header,
|
|
success: (res) => {
|
|
let body = {}
|
|
try {
|
|
body = JSON.parse(res.data || '{}')
|
|
} catch (error) {
|
|
body = {}
|
|
}
|
|
|
|
if (res.statusCode === 401 || body?.code === 40001) {
|
|
authStorage.clear()
|
|
}
|
|
|
|
if (res.statusCode >= 200 && res.statusCode < 300 && body?.code === 0) {
|
|
resolve(body.data)
|
|
return
|
|
}
|
|
|
|
const uploadError = normalizeError({ ...res, data: body }, `上传错误 ${res.statusCode}`)
|
|
uni.showToast({
|
|
title: uploadError.message,
|
|
icon: 'none'
|
|
})
|
|
reject(uploadError)
|
|
},
|
|
fail: (err) => {
|
|
const uploadError = normalizeError(err, '文件上传失败,请检查服务')
|
|
uni.showToast({
|
|
title: uploadError.message,
|
|
icon: 'none'
|
|
})
|
|
reject(uploadError)
|
|
}
|
|
})
|
|
})
|
|
}
|