Files
bodyWeight/miniprogram/utils/request/index.ts
T
17792275749andClaude Opus 4.7 d6e59df4e9 feat: 详细报告接入 v3 测量历史接口,设备列表接入设备接口
- 详细报告按 scaleType 区分 4/8 电极渲染,节段卡片 8 电极专属
- 设备列表改为接口驱动并新增空态
- 登录后统一跳首页,请求层兼容 code 0 返回风格

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-08-20 17:20:07 +08:00

77 lines
2.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { ApiResponse, HttpMethod, RequestOptions } from './types'
import { BASE_URL, TIMEOUT } from './config'
export type { ApiResponse, RequestOptions }
let _loadingCount = 0
const _showLoading = (title: string): void => {
if (_loadingCount === 0) wx.showLoading({ title, mask: true })
_loadingCount++
}
const _hideLoading = (): void => {
if (_loadingCount > 0) _loadingCount--
if (_loadingCount === 0) wx.hideLoading()
}
const request = <T = any>(
url: string,
data?: Record<string, any>,
method: HttpMethod = 'GET',
options: RequestOptions = {},
): Promise<ApiResponse<T>> => {
const { loading = true, loadingTitle = '加载中...', contentType = 'application/json' } = options
if (loading) _showLoading(loadingTitle)
const header: Record<string, string> = { 'content-type': contentType }
const token = wx.getStorageSync('token') as string
if (token) header['abtoken'] = token
return new Promise((resolve, reject) => {
wx.request({
url: BASE_URL + url,
data,
method,
header,
timeout: TIMEOUT,
success: (res) => {
if (loading) _hideLoading()
const body = res.data as ApiResponse<T>
const code = body.code
// 兼容两种后端返回风格:code 200(自定义)或 code 0jeecg
if (code === 200 || code === 0 || code === '0') {
resolve(body)
} else {
wx.showToast({ title: body.msg || body.message || '请求失败', icon: 'none' })
reject(body)
}
},
fail: (err) => {
if (loading) _hideLoading()
wx.showToast({ title: '网络异常,请重试', icon: 'none' })
reject(err)
},
})
})
}
export const get = <T = any>(
url: string,
data?: Record<string, any>,
options?: RequestOptions,
): Promise<ApiResponse<T>> => request<T>(url, data, 'GET', options)
export const post = <T = any>(
url: string,
data?: Record<string, any>,
options?: RequestOptions,
): Promise<ApiResponse<T>> => request<T>(url, data, 'POST', options)
export const put = <T = any>(
url: string,
data?: Record<string, any>,
options?: RequestOptions,
): Promise<ApiResponse<T>> => request<T>(url, data, 'PUT', options)