refactor: 重写 request.ts,baseURL 从环境配置读取
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
bed3359564
commit
c3896f9227
+42
-22
@@ -1,41 +1,49 @@
|
||||
import { API_BASE, TOKEN_KEY } from '../config'
|
||||
|
||||
/** 请求选项 */
|
||||
interface RequestOptions {
|
||||
url: string
|
||||
method?: 'GET' | 'POST' | 'PUT' | 'DELETE'
|
||||
data?: any
|
||||
header?: any
|
||||
header?: Record<string, string>
|
||||
needToken?: boolean
|
||||
}
|
||||
|
||||
/** 后端统一响应格式 */
|
||||
interface ResponseData<T = any> {
|
||||
code: number
|
||||
message: string
|
||||
msg: string
|
||||
data: T
|
||||
errcode: number
|
||||
errmsg: string
|
||||
}
|
||||
|
||||
const BASE_URL = ''
|
||||
const TOKEN_KEY = 'app_token'
|
||||
|
||||
/** 从本地存储获取 token */
|
||||
function getToken(): string {
|
||||
return uni.getStorageSync(TOKEN_KEY) || ''
|
||||
}
|
||||
|
||||
/** 保存 token 到本地存储 */
|
||||
function setToken(token: string): void {
|
||||
uni.setStorageSync(TOKEN_KEY, token)
|
||||
}
|
||||
|
||||
/** 移除本地存储中的 token */
|
||||
function removeToken(): void {
|
||||
uni.removeStorageSync(TOKEN_KEY)
|
||||
}
|
||||
|
||||
/** 通用请求函数 */
|
||||
function request<T = any>(options: RequestOptions): Promise<T> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const { url, method = 'GET', data, needToken = true } = options
|
||||
|
||||
const header: any = {
|
||||
const header: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
...options.header
|
||||
}
|
||||
|
||||
// 自动注入 Bearer Token
|
||||
if (needToken) {
|
||||
const token = getToken()
|
||||
if (token) {
|
||||
@@ -43,27 +51,40 @@ function request<T = any>(options: RequestOptions): Promise<T> {
|
||||
}
|
||||
}
|
||||
|
||||
// 拼接完整 URL
|
||||
const fullUrl = url.startsWith('http') ? url : `${API_BASE}${url}`
|
||||
|
||||
uni.request({
|
||||
url: BASE_URL + url,
|
||||
url: fullUrl,
|
||||
method,
|
||||
data,
|
||||
header,
|
||||
success: (res: any) => {
|
||||
const result = res.data as ResponseData<T>
|
||||
if (res.statusCode === 200) {
|
||||
if (result.code === 0 || result.code === 200) {
|
||||
resolve(result.data as T)
|
||||
} else if (result.code === 401) {
|
||||
|
||||
// HTTP 状态码检查
|
||||
if (res.statusCode === 401 || result?.code === 401) {
|
||||
removeToken()
|
||||
uni.showToast({ title: '请先登录', icon: 'none' })
|
||||
reject(new Error(result.message || '未授权'))
|
||||
uni.showToast({ title: '登录已过期,请重新登录', icon: 'none' })
|
||||
setTimeout(() => {
|
||||
uni.navigateTo({ url: '/pages/login/login' })
|
||||
}, 1500)
|
||||
reject(new Error('未授权'))
|
||||
return
|
||||
}
|
||||
|
||||
if (res.statusCode >= 200 && res.statusCode < 300) {
|
||||
// 业务状态码检查:code === 200 或 errcode === 0 均视为成功
|
||||
if (result.code === 200 || result.errcode === 0) {
|
||||
resolve(result.data as T)
|
||||
} else {
|
||||
uni.showToast({ title: result.message || '请求失败', icon: 'none' })
|
||||
reject(new Error(result.message || '请求失败'))
|
||||
const errMsg = result.msg || result.errmsg || '请求失败'
|
||||
uni.showToast({ title: errMsg, icon: 'none' })
|
||||
reject(new Error(errMsg))
|
||||
}
|
||||
} else {
|
||||
uni.showToast({ title: '网络错误', icon: 'none' })
|
||||
reject(new Error('网络错误'))
|
||||
reject(new Error(`HTTP ${res.statusCode}`))
|
||||
}
|
||||
},
|
||||
fail: (err: any) => {
|
||||
@@ -74,19 +95,19 @@ function request<T = any>(options: RequestOptions): Promise<T> {
|
||||
})
|
||||
}
|
||||
|
||||
function get<T = any>(url: string, data?: any, needToken: boolean = true): Promise<T> {
|
||||
function get<T = any>(url: string, data?: any, needToken = true): Promise<T> {
|
||||
return request<T>({ url, method: 'GET', data, needToken })
|
||||
}
|
||||
|
||||
function post<T = any>(url: string, data?: any, needToken: boolean = true): Promise<T> {
|
||||
function post<T = any>(url: string, data?: any, needToken = true): Promise<T> {
|
||||
return request<T>({ url, method: 'POST', data, needToken })
|
||||
}
|
||||
|
||||
function put<T = any>(url: string, data?: any, needToken: boolean = true): Promise<T> {
|
||||
function put<T = any>(url: string, data?: any, needToken = true): Promise<T> {
|
||||
return request<T>({ url, method: 'PUT', data, needToken })
|
||||
}
|
||||
|
||||
function del<T = any>(url: string, data?: any, needToken: boolean = true): Promise<T> {
|
||||
function del<T = any>(url: string, data?: any, needToken = true): Promise<T> {
|
||||
return request<T>({ url, method: 'DELETE', data, needToken })
|
||||
}
|
||||
|
||||
@@ -98,6 +119,5 @@ export {
|
||||
del,
|
||||
getToken,
|
||||
setToken,
|
||||
removeToken,
|
||||
BASE_URL
|
||||
removeToken
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user