80 lines
2.4 KiB
TypeScript
80 lines
2.4 KiB
TypeScript
import type { ApiResponse, PaginatedData } from '@mp-pilates/shared'
|
|
|
|
const BASE_URL = (() => {
|
|
try {
|
|
const { miniProgram } = uni.getAccountInfoSync()
|
|
if (miniProgram.envVersion !== 'develop') {
|
|
return 'https://focus.richarjiang.com/api'
|
|
}
|
|
} catch {
|
|
// 非小程序环境,使用开发地址
|
|
}
|
|
return 'http://localhost:3000/api'
|
|
})()
|
|
|
|
interface RequestOptions {
|
|
readonly url: string
|
|
readonly method?: 'GET' | 'POST' | 'PUT' | 'DELETE'
|
|
readonly data?: Record<string, unknown>
|
|
readonly header?: Record<string, string>
|
|
}
|
|
|
|
export function request<T>(options: RequestOptions): Promise<T> {
|
|
return new Promise((resolve, reject) => {
|
|
const token = uni.getStorageSync('token') as string
|
|
|
|
uni.request({
|
|
url: `${BASE_URL}${options.url}`,
|
|
method: options.method || 'GET',
|
|
data: options.data,
|
|
header: {
|
|
'Content-Type': 'application/json',
|
|
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
|
...options.header,
|
|
},
|
|
success: (res) => {
|
|
if (res.statusCode === 401) {
|
|
uni.removeStorageSync('token')
|
|
uni.showToast({ title: '请重新登录', icon: 'none' })
|
|
reject(new Error('Unauthorized'))
|
|
return
|
|
}
|
|
if (res.statusCode >= 400) {
|
|
const body = res.data as ApiResponse<unknown>
|
|
reject(new Error(body?.message || `请求失败 (${res.statusCode})`))
|
|
return
|
|
}
|
|
const body = res.data as ApiResponse<T>
|
|
if (body.success) {
|
|
resolve(body.data as T)
|
|
} else {
|
|
reject(new Error(body.message || '请求失败'))
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
reject(new Error(err.errMsg || '网络请求失败'))
|
|
},
|
|
})
|
|
})
|
|
}
|
|
|
|
export function get<T>(url: string, data?: Record<string, unknown>): Promise<T> {
|
|
return request<T>({ url, method: 'GET', data })
|
|
}
|
|
|
|
export function post<T>(url: string, data?: Record<string, unknown>): Promise<T> {
|
|
return request<T>({ url, method: 'POST', data })
|
|
}
|
|
|
|
export function put<T>(url: string, data?: Record<string, unknown>): Promise<T> {
|
|
return request<T>({ url, method: 'PUT', data })
|
|
}
|
|
|
|
export function del<T>(url: string, data?: Record<string, unknown>): Promise<T> {
|
|
return request<T>({ url, method: 'DELETE', data })
|
|
}
|
|
|
|
export function getPaginated<T>(url: string, params?: Record<string, unknown>): Promise<PaginatedData<T>> {
|
|
return request<PaginatedData<T>>({ url, method: 'GET', data: params })
|
|
}
|