微信修改
This commit is contained in:
49
urbanLifelineWeb/packages/workcase_wechat/api/base.ts
Normal file
49
urbanLifelineWeb/packages/workcase_wechat/api/base.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* workcase_wechat API 封装
|
||||
* 使用 uni.request 替代 axios
|
||||
*/
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
declare const uni: {
|
||||
getStorageSync: (key: string) => any
|
||||
request: (options: any) => void
|
||||
}
|
||||
|
||||
import type { ResultDomain } from '../types'
|
||||
|
||||
// API 基础配置
|
||||
const BASE_URL = 'http://localhost:8180'
|
||||
|
||||
// 通用请求方法
|
||||
export function request<T>(options: {
|
||||
url: string
|
||||
method?: 'GET' | 'POST' | 'PUT' | 'DELETE'
|
||||
data?: any
|
||||
header?: Record<string, string>
|
||||
}): Promise<ResultDomain<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: any) => {
|
||||
if (res.statusCode === 200) {
|
||||
resolve(res.data as ResultDomain<T>)
|
||||
} else {
|
||||
reject(new Error(`请求失败: ${res.statusCode}`))
|
||||
}
|
||||
},
|
||||
fail: (err: any) => {
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user