- API 路由统一: 移除 /api/h5/,统一 /api/pms/ 入口 - 二进制改名: api-server -> pms-api(与服务名一致) - nginx: 合并 /api/ + /pms/api/ 为 /api/pms/ - 新增 docs/API.md 接口文档 - 后端: CabinetDetail 增加 rssi/pow_fail/address/soc 等字段 - Redis 管道批量读取仓体实时数据(SOC/电压/电流/功率) - DB: 所有 TIMESTAMP 列改为 DATETIME(修复类型不匹配) - 用户管理: 新增创建时间/修改时间列,状态改用 Switch - 角色管理/用户管理: 操作按钮改为图标 - PC 设备详情: 基本信息补全 RSSI/ICCID/电源/地址 - PC 卡片: 通道统计改用 Lucide 图标,颜色统一 - PC 二维码: canvas 渲染,含编号文字,支持右键下载 - H5: 按 uni-app 规范修正导航(navigateTo 替代 reLaunch) - H5: 详情页加返回导航条 - H5: 扫码支持摄像头(jsQR)+ ?id= 参数 - H5: 底部 tabBar 在所有页面常显 - H5: 首页统计改为 6 项(设备/通道/空闲/充电/充满/故障) - H5: 下拉刷新 / 上拉加载 - 产品首页: 添加业务联系 - 文档: 清理过时文件,补充缺失规范章节
3.8 KiB
3.8 KiB
name, description
| name | description |
|---|---|
| uni-network | Promise-based HTTP client for uni-app - axios-inspired API with uni.request as default transport |
uni-network
Promise-based HTTP client for uni-app, inspired by axios@0.27.2. Uses uni.request/uni.uploadFile/uni.downloadFile as the underlying transport.
Installation
npm i @uni-helper/uni-network
Features
- Promise-based API
- Request/response interceptors
- Request cancellation
- TypeScript support
- Composition API integration
- Upload/download support
Basic Usage
import { un } from '@uni-helper/uni-network'
// GET request
un('/user/123')
// POST request
un({
method: 'POST',
url: '/user/123',
data: {
firstName: 'Fred',
lastName: 'Flintstone',
},
})
Request Aliases
import { un } from '@uni-helper/uni-network'
// HTTP verbs
un.get('/users')
un.post('/users', { name: 'John' })
un.put('/users/1', { name: 'Jane' })
un.patch('/users/1', { age: 30 })
un.delete('/users/1')
// Upload/download
un.upload({
url: '/upload',
filePath: tempFilePath,
name: 'file',
})
un.download({
url: '/files/report.pdf',
})
Creating an Instance
import { createUniNetwork } from '@uni-helper/uni-network'
const http = createUniNetwork({
baseURL: 'https://api.example.com',
timeout: 10000,
headers: {
'Content-Type': 'application/json',
},
})
// Use the instance
http.get('/users')
Interceptors
import { un } from '@uni-helper/uni-network'
// Request interceptor
un.interceptors.request.use(
(config) => {
// Add auth token
const token = uni.getStorageSync('token')
if (token) {
config.headers = config.headers || {}
config.headers.Authorization = `Bearer ${token}`
}
return config
},
(error) => {
return Promise.reject(error)
}
)
// Response interceptor
un.interceptors.response.use(
(response) => {
// Transform response data
return response.data
},
(error) => {
// Handle errors globally
if (error.statusCode === 401) {
uni.redirectTo({ url: '/pages/login/index' })
}
return Promise.reject(error)
}
)
Configuration
interface UniNetworkConfig {
// URL
url?: string
// Base URL
baseURL?: string
// Method
method?: 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | ...
// Headers
headers?: Record<string, string>
// Request data
data?: any
// Query params
params?: Record<string, any>
// Timeout in ms
timeout?: number
// Response type
responseType?: 'text' | 'arraybuffer'
// Data type
dataType?: 'json' | 'string'
// Enable http2
enableHttp2?: boolean
// Enable quic
enableQuic?: boolean
// Enable cache
enableCache?: boolean
// SSL verify
sslVerify?: boolean
}
Request Cancellation
import { un } from '@uni-helper/uni-network'
const controller = new AbortController()
un.get('/long-request', {
signal: controller.signal,
})
// Cancel after 5 seconds
setTimeout(() => {
controller.abort()
}, 5000)
Error Handling
import { un, UniNetworkError } from '@uni-helper/uni-network'
try {
const response = await un.get('/users')
} catch (error) {
if (error instanceof UniNetworkError) {
console.log('Request failed:', error.message)
console.log('Status:', error.statusCode)
}
}
Composition API
import { useUniNetwork } from '@uni-helper/uni-network'
const { data, error, loading, execute } = useUniNetwork({
url: '/users',
method: 'GET',
})
// Execute manually
await execute()
TypeScript Support
import { un } from '@uni-helper/uni-network'
interface User {
id: number
name: string
email: string
}
// Typed response
const { data } = await un.get<User>('/users/1')
console.log(data.name) // Type-safe access