feat(app): initialize uni-app with routing, stores, and infrastructure
- Vue 3 + TypeScript + Pinia + SCSS - 3-tab navigation (home, booking, profile) + 11 sub-pages - HTTP client with JWT auth, request interceptors - Pinia stores: user (auth, profile, memberships), studio, booking - Utility functions: price formatting, date helpers - WeChat login helper - All pages as stubs ready for implementation
This commit is contained in:
69
packages/app/src/utils/request.ts
Normal file
69
packages/app/src/utils/request.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import type { ApiResponse, PaginatedData } from '@mp-pilates/shared'
|
||||
|
||||
const BASE_URL = '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 })
|
||||
}
|
||||
Reference in New Issue
Block a user