first commit
This commit is contained in:
77
src/utils/common.ts
Normal file
77
src/utils/common.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
/**
|
||||
* 公共工具函数
|
||||
*/
|
||||
import type { Key } from 'ant-design-vue/es/table/interface'
|
||||
import type { TablePaginationConfig } from 'ant-design-vue'
|
||||
|
||||
/**
|
||||
* 格式化日期
|
||||
*/
|
||||
export function formatDate(str: string): string {
|
||||
return new Date(str).toLocaleDateString('zh-CN')
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化日期时间
|
||||
*/
|
||||
export function formatDateTime(str: string): string {
|
||||
return new Date(str).toLocaleString('zh-CN')
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化数字(超过1000显示为x.xk)
|
||||
*/
|
||||
export function formatCount(num: number): string {
|
||||
if (num >= 1000) {
|
||||
return (num / 1000).toFixed(1) + 'k'
|
||||
}
|
||||
return num.toString()
|
||||
}
|
||||
|
||||
/**
|
||||
* 从数组中随机取一个元素(带类型安全)
|
||||
*/
|
||||
export function randomItem<T>(arr: T[]): T {
|
||||
return arr[Math.floor(Math.random() * arr.length)]!
|
||||
}
|
||||
|
||||
/**
|
||||
* 模拟延迟
|
||||
*/
|
||||
export function delay(ms: number): Promise<void> {
|
||||
return new Promise(resolve => setTimeout(resolve, ms))
|
||||
}
|
||||
|
||||
/**
|
||||
* 表格选择变更处理器类型
|
||||
*/
|
||||
export type TableSelectChange = (keys: Key[]) => void
|
||||
|
||||
/**
|
||||
* 表格分页变更处理器类型
|
||||
*/
|
||||
export type TablePaginationChange = (pagination: TablePaginationConfig) => void
|
||||
|
||||
/**
|
||||
* 创建表格分页变更处理函数
|
||||
*/
|
||||
export function createTableChangeHandler(
|
||||
paginationRef: { current: number; pageSize: number },
|
||||
loadFn: () => void
|
||||
) {
|
||||
return (pagination: TablePaginationConfig) => {
|
||||
paginationRef.current = pagination.current || 1
|
||||
paginationRef.pageSize = pagination.pageSize || 10
|
||||
loadFn()
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建表格选择变更处理函数
|
||||
*/
|
||||
export function createSelectChangeHandler(selectedKeysRef: { value: Key[] }) {
|
||||
return (keys: Key[]) => {
|
||||
selectedKeysRef.value = keys
|
||||
}
|
||||
}
|
||||
|
||||
112
src/utils/request.ts
Normal file
112
src/utils/request.ts
Normal file
@@ -0,0 +1,112 @@
|
||||
/**
|
||||
* Axios 基础配置
|
||||
*/
|
||||
import axios, { type AxiosInstance, type AxiosRequestConfig, type AxiosResponse, type InternalAxiosRequestConfig } from 'axios'
|
||||
import { message } from 'ant-design-vue'
|
||||
import type { ApiResponse } from '@/types'
|
||||
|
||||
// 创建axios实例
|
||||
const service: AxiosInstance = axios.create({
|
||||
baseURL: import.meta.env.VITE_API_BASE_URL || '/api',
|
||||
timeout: 30000,
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
})
|
||||
|
||||
// 请求拦截器
|
||||
service.interceptors.request.use(
|
||||
(config: InternalAxiosRequestConfig) => {
|
||||
// 从localStorage获取token
|
||||
const token = localStorage.getItem('token')
|
||||
if (token && config.headers) {
|
||||
config.headers.Authorization = `Bearer ${token}`
|
||||
}
|
||||
return config
|
||||
},
|
||||
(error) => {
|
||||
console.error('请求错误:', error)
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
// 响应拦截器
|
||||
service.interceptors.response.use(
|
||||
(response: AxiosResponse<ApiResponse>) => {
|
||||
const res = response.data
|
||||
|
||||
// 根据业务状态码判断请求是否成功
|
||||
if (res.code === 200 || res.success) {
|
||||
return response
|
||||
}
|
||||
|
||||
// 处理业务错误
|
||||
message.error(res.message || '请求失败')
|
||||
|
||||
// 处理特定错误码
|
||||
if (res.code === 401) {
|
||||
// token过期或未授权,跳转登录
|
||||
localStorage.removeItem('token')
|
||||
window.location.href = '/login'
|
||||
}
|
||||
|
||||
return Promise.reject(new Error(res.message || '请求失败'))
|
||||
},
|
||||
(error) => {
|
||||
console.error('响应错误:', error)
|
||||
|
||||
// 处理HTTP错误
|
||||
let errorMessage = '网络错误,请稍后重试'
|
||||
if (error.response) {
|
||||
const { status } = error.response
|
||||
switch (status) {
|
||||
case 400:
|
||||
errorMessage = '请求参数错误'
|
||||
break
|
||||
case 401:
|
||||
errorMessage = '未授权,请重新登录'
|
||||
localStorage.removeItem('token')
|
||||
window.location.href = '/login'
|
||||
break
|
||||
case 403:
|
||||
errorMessage = '拒绝访问'
|
||||
break
|
||||
case 404:
|
||||
errorMessage = '请求资源不存在'
|
||||
break
|
||||
case 500:
|
||||
errorMessage = '服务器内部错误'
|
||||
break
|
||||
default:
|
||||
errorMessage = `请求失败(${status})`
|
||||
}
|
||||
} else if (error.code === 'ECONNABORTED') {
|
||||
errorMessage = '请求超时,请稍后重试'
|
||||
}
|
||||
|
||||
message.error(errorMessage)
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
// 封装请求方法
|
||||
export const request = {
|
||||
get<T = unknown>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<ApiResponse<T>>> {
|
||||
return service.get(url, config)
|
||||
},
|
||||
|
||||
post<T = unknown>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<AxiosResponse<ApiResponse<T>>> {
|
||||
return service.post(url, data, config)
|
||||
},
|
||||
|
||||
put<T = unknown>(url: string, data?: unknown, config?: AxiosRequestConfig): Promise<AxiosResponse<ApiResponse<T>>> {
|
||||
return service.put(url, data, config)
|
||||
},
|
||||
|
||||
delete<T = unknown>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<ApiResponse<T>>> {
|
||||
return service.delete(url, config)
|
||||
}
|
||||
}
|
||||
|
||||
export default service
|
||||
|
||||
Reference in New Issue
Block a user