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 {
|
interface RequestOptions {
|
||||||
url: string
|
url: string
|
||||||
method?: 'GET' | 'POST' | 'PUT' | 'DELETE'
|
method?: 'GET' | 'POST' | 'PUT' | 'DELETE'
|
||||||
data?: any
|
data?: any
|
||||||
header?: any
|
header?: Record<string, string>
|
||||||
needToken?: boolean
|
needToken?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 后端统一响应格式 */
|
||||||
interface ResponseData<T = any> {
|
interface ResponseData<T = any> {
|
||||||
code: number
|
code: number
|
||||||
message: string
|
msg: string
|
||||||
data: T
|
data: T
|
||||||
|
errcode: number
|
||||||
|
errmsg: string
|
||||||
}
|
}
|
||||||
|
|
||||||
const BASE_URL = ''
|
/** 从本地存储获取 token */
|
||||||
const TOKEN_KEY = 'app_token'
|
|
||||||
|
|
||||||
function getToken(): string {
|
function getToken(): string {
|
||||||
return uni.getStorageSync(TOKEN_KEY) || ''
|
return uni.getStorageSync(TOKEN_KEY) || ''
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 保存 token 到本地存储 */
|
||||||
function setToken(token: string): void {
|
function setToken(token: string): void {
|
||||||
uni.setStorageSync(TOKEN_KEY, token)
|
uni.setStorageSync(TOKEN_KEY, token)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 移除本地存储中的 token */
|
||||||
function removeToken(): void {
|
function removeToken(): void {
|
||||||
uni.removeStorageSync(TOKEN_KEY)
|
uni.removeStorageSync(TOKEN_KEY)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 通用请求函数 */
|
||||||
function request<T = any>(options: RequestOptions): Promise<T> {
|
function request<T = any>(options: RequestOptions): Promise<T> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const { url, method = 'GET', data, needToken = true } = options
|
const { url, method = 'GET', data, needToken = true } = options
|
||||||
|
|
||||||
const header: any = {
|
const header: Record<string, string> = {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
...options.header
|
...options.header
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 自动注入 Bearer Token
|
||||||
if (needToken) {
|
if (needToken) {
|
||||||
const token = getToken()
|
const token = getToken()
|
||||||
if (token) {
|
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({
|
uni.request({
|
||||||
url: BASE_URL + url,
|
url: fullUrl,
|
||||||
method,
|
method,
|
||||||
data,
|
data,
|
||||||
header,
|
header,
|
||||||
success: (res: any) => {
|
success: (res: any) => {
|
||||||
const result = res.data as ResponseData<T>
|
const result = res.data as ResponseData<T>
|
||||||
if (res.statusCode === 200) {
|
|
||||||
if (result.code === 0 || result.code === 200) {
|
// HTTP 状态码检查
|
||||||
|
if (res.statusCode === 401 || result?.code === 401) {
|
||||||
|
removeToken()
|
||||||
|
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)
|
resolve(result.data as T)
|
||||||
} else if (result.code === 401) {
|
|
||||||
removeToken()
|
|
||||||
uni.showToast({ title: '请先登录', icon: 'none' })
|
|
||||||
reject(new Error(result.message || '未授权'))
|
|
||||||
} else {
|
} else {
|
||||||
uni.showToast({ title: result.message || '请求失败', icon: 'none' })
|
const errMsg = result.msg || result.errmsg || '请求失败'
|
||||||
reject(new Error(result.message || '请求失败'))
|
uni.showToast({ title: errMsg, icon: 'none' })
|
||||||
|
reject(new Error(errMsg))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
uni.showToast({ title: '网络错误', icon: 'none' })
|
uni.showToast({ title: '网络错误', icon: 'none' })
|
||||||
reject(new Error('网络错误'))
|
reject(new Error(`HTTP ${res.statusCode}`))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
fail: (err: any) => {
|
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 })
|
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 })
|
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 })
|
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 })
|
return request<T>({ url, method: 'DELETE', data, needToken })
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,6 +119,5 @@ export {
|
|||||||
del,
|
del,
|
||||||
getToken,
|
getToken,
|
||||||
setToken,
|
setToken,
|
||||||
removeToken,
|
removeToken
|
||||||
BASE_URL
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user