Merge branch 'light_v3'
This commit is contained in:
@@ -39,6 +39,9 @@ class HttpService {
|
||||
private baseURL: string
|
||||
private timeout: number
|
||||
private enableLog: boolean
|
||||
private loadingCount: number = 0 // 全局loading计数器
|
||||
private currentLoadingText: string = '' // 当前loading文本
|
||||
private hideLoadingTimer: NodeJS.Timeout | null = null // 隐藏loading的延时器
|
||||
|
||||
constructor() {
|
||||
// 使用环境配置
|
||||
@@ -104,6 +107,47 @@ class HttpService {
|
||||
}
|
||||
}
|
||||
|
||||
// 显示loading(支持多个并发请求)
|
||||
private showLoading(loadingText: string): void {
|
||||
// 如果正在延时隐藏loading,取消延时
|
||||
if (this.hideLoadingTimer) {
|
||||
clearTimeout(this.hideLoadingTimer)
|
||||
this.hideLoadingTimer = null
|
||||
}
|
||||
|
||||
this.loadingCount++
|
||||
this.currentLoadingText = loadingText
|
||||
|
||||
// 只有第一个请求时才显示loading
|
||||
if (this.loadingCount === 1) {
|
||||
Taro.showLoading({
|
||||
title: loadingText,
|
||||
mask: true
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 隐藏loading(支持多个并发请求)
|
||||
private hideLoading(): void {
|
||||
this.loadingCount = Math.max(0, this.loadingCount - 1)
|
||||
|
||||
// 只有所有请求都完成时才隐藏loading
|
||||
if (this.loadingCount === 0) {
|
||||
// 清除之前的延时器
|
||||
if (this.hideLoadingTimer) {
|
||||
clearTimeout(this.hideLoadingTimer)
|
||||
this.hideLoadingTimer = null
|
||||
}
|
||||
|
||||
// 延时300ms后隐藏loading,避免频繁切换
|
||||
this.hideLoadingTimer = setTimeout(() => {
|
||||
Taro.hideLoading()
|
||||
this.currentLoadingText = ''
|
||||
this.hideLoadingTimer = null
|
||||
}, 800)
|
||||
}
|
||||
}
|
||||
|
||||
// 处理响应
|
||||
private handleResponse<T>(response: any, showToast: boolean): Promise<ApiResponse<T>> {
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -234,10 +278,7 @@ class HttpService {
|
||||
|
||||
// 显示加载提示
|
||||
if (showLoading) {
|
||||
Taro.showLoading({
|
||||
title: loadingText,
|
||||
mask: true
|
||||
})
|
||||
this.showLoading(loadingText)
|
||||
}
|
||||
|
||||
try {
|
||||
@@ -257,11 +298,6 @@ class HttpService {
|
||||
} catch (error) {
|
||||
this.log('error', '请求失败', error)
|
||||
|
||||
// 在模拟模式下返回模拟数据
|
||||
if (envConfig.enableMock && isDevelopment()) {
|
||||
this.log('info', '使用模拟数据')
|
||||
return this.getMockResponse<T>(url, method)
|
||||
}
|
||||
|
||||
Taro.showToast({
|
||||
title: '网络连接失败',
|
||||
@@ -272,28 +308,11 @@ class HttpService {
|
||||
} finally {
|
||||
// 隐藏加载提示
|
||||
if (showLoading) {
|
||||
Taro.hideLoading()
|
||||
this.hideLoading()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 获取模拟数据
|
||||
private getMockResponse<T>(url: string, method: string): ApiResponse<T> {
|
||||
this.log('info', `返回模拟数据: ${method} ${url}`)
|
||||
|
||||
return {
|
||||
code: 200,
|
||||
success: true,
|
||||
message: '模拟请求成功',
|
||||
data: {
|
||||
mockData: true,
|
||||
url,
|
||||
method,
|
||||
timestamp: new Date().toISOString()
|
||||
} as T
|
||||
}
|
||||
}
|
||||
|
||||
// GET请求
|
||||
get<T = any>(url: string, params?: Record<string, any>, config?: Partial<RequestConfig>): Promise<ApiResponse<T>> {
|
||||
return this.request<T>({
|
||||
@@ -315,41 +334,6 @@ class HttpService {
|
||||
}
|
||||
|
||||
|
||||
uploadFile() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
// PUT请求
|
||||
put<T = any>(url: string, data?: any, config?: Partial<RequestConfig>): Promise<ApiResponse<T>> {
|
||||
return this.request<T>({
|
||||
url,
|
||||
method: 'PUT',
|
||||
data,
|
||||
...config
|
||||
})
|
||||
}
|
||||
|
||||
// DELETE请求
|
||||
delete<T = any>(url: string, params?: Record<string, any>, config?: Partial<RequestConfig>): Promise<ApiResponse<T>> {
|
||||
return this.request<T>({
|
||||
url,
|
||||
method: 'DELETE',
|
||||
params,
|
||||
...config
|
||||
})
|
||||
}
|
||||
|
||||
// PATCH请求
|
||||
patch<T = any>(url: string, data?: any, config?: Partial<RequestConfig>): Promise<ApiResponse<T>> {
|
||||
return this.request<T>({
|
||||
url,
|
||||
method: 'PATCH',
|
||||
data,
|
||||
...config
|
||||
})
|
||||
}
|
||||
|
||||
// 获取当前环境信息
|
||||
getEnvInfo() {
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user