[AI Generated]: feat(*): 新增 lefu 蓝牙服务层、mock 业务数据层及 request 请求封装,接入各页面
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
import type { ApiResponse, HttpMethod, RequestOptions } from './types'
|
||||
|
||||
export type { ApiResponse, RequestOptions }
|
||||
|
||||
const BASE_URL = 'https://device.shuziweidao.com/gateway/'
|
||||
|
||||
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['X-Access-Token'] = token
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.request({
|
||||
url: BASE_URL + url,
|
||||
data,
|
||||
method,
|
||||
header,
|
||||
success: (res) => {
|
||||
if (loading) _hideLoading()
|
||||
const body = res.data as ApiResponse<T>
|
||||
if (body.code === 200) {
|
||||
resolve(body)
|
||||
} else if (body.code === 401) {
|
||||
wx.clearStorageSync()
|
||||
wx.reLaunch({ url: '/pages/login/login' })
|
||||
reject(body)
|
||||
} else {
|
||||
wx.showToast({ title: 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)
|
||||
|
||||
export const del = <T = any>(
|
||||
url: string,
|
||||
data?: Record<string, any>,
|
||||
options?: RequestOptions,
|
||||
): Promise<ApiResponse<T>> => request<T>(url, data, 'DELETE', {
|
||||
contentType: 'application/x-www-form-urlencoded',
|
||||
...options,
|
||||
})
|
||||
@@ -0,0 +1,16 @@
|
||||
export interface ApiResponse<T = any> {
|
||||
code: number
|
||||
result: T
|
||||
message?: string
|
||||
}
|
||||
|
||||
export type HttpMethod = 'GET' | 'POST' | 'PUT' | 'DELETE'
|
||||
|
||||
export interface RequestOptions {
|
||||
/** 是否显示 loading,默认 true */
|
||||
loading?: boolean
|
||||
/** loading 文案,默认"加载中..." */
|
||||
loadingTitle?: string
|
||||
/** Content-Type,默认 application/json */
|
||||
contentType?: 'application/json' | 'application/x-www-form-urlencoded'
|
||||
}
|
||||
Reference in New Issue
Block a user