feat:首页和案例库功能
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { API_BASE_URL, API_TIMEOUT } from '@/constants/config'
|
||||
import { getAccessToken } from '@/utils/auth-storage'
|
||||
import { handleUnauthorized } from '@/utils/auth-guard'
|
||||
import { getAccessToken, getAuthClientId } from '@/utils/auth-storage'
|
||||
|
||||
export interface RequestOptions {
|
||||
url: string
|
||||
@@ -41,12 +42,14 @@ export function request<T = unknown>(options: RequestOptions): Promise<T> {
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const token = getAccessToken()
|
||||
const clientId = getAuthClientId()
|
||||
uni.request({
|
||||
url: resolveUrl(url),
|
||||
method,
|
||||
data,
|
||||
header: {
|
||||
'Content-Type': 'application/json',
|
||||
...(clientId ? { clientid: clientId } : {}),
|
||||
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
||||
...header,
|
||||
},
|
||||
@@ -64,6 +67,12 @@ export function request<T = unknown>(options: RequestOptions): Promise<T> {
|
||||
}
|
||||
|
||||
const message = getErrorMessage(apiBody, '请求失败')
|
||||
if (apiBody.code === 401) {
|
||||
handleUnauthorized(message)
|
||||
reject(new Error(message))
|
||||
return
|
||||
}
|
||||
|
||||
if (showErrorToast)
|
||||
uni.showToast({ title: message, icon: 'none' })
|
||||
reject(new Error(message))
|
||||
@@ -74,6 +83,12 @@ export function request<T = unknown>(options: RequestOptions): Promise<T> {
|
||||
return
|
||||
}
|
||||
|
||||
if (statusCode === 401) {
|
||||
handleUnauthorized('登录状态异常,请重新登录')
|
||||
reject(new Error('登录状态异常,请重新登录'))
|
||||
return
|
||||
}
|
||||
|
||||
const message = `请求失败(${statusCode})`
|
||||
if (showErrorToast)
|
||||
uni.showToast({ title: message, icon: 'none' })
|
||||
|
||||
Reference in New Issue
Block a user