生产管理系统前端 - fetchapi 基础提交

This commit is contained in:
2025-10-27 11:08:23 +08:00
parent 1f1d94ed84
commit 2b39c1dd1a
12 changed files with 2191 additions and 7 deletions

View File

@@ -0,0 +1,204 @@
import createClient from 'openapi-fetch';
import type { paths } from './v1.d.ts';
// 创建 API 客户端
const client = createClient<paths>({
baseUrl: 'https://gitea-admin-test-app-app.dev.maimaiag.com/docs',
// 可以添加默认 headers
headers: {
'Content-Type': 'application/json',
},
});
// 添加认证的客户端
export const authClient = {
...client,
// 包装添加 token 的方法
withAuth: (token: string) => ({
...client,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
},
}),
};
// 测试连接
export const testConnection = async () => {
try {
// 尝试获取一个简单的接口来测试连接
const { data, error, response } = await client.GET('/users', {
params: {
query: { page: 1, limit: 1 }
}
});
if (error) {
console.warn('API 连接测试失败:', error);
return { success: false, error };
}
console.log('API 连接测试成功:', { status: response?.status, data });
return { success: true, data };
} catch (err) {
console.error('API 连接测试出错:', err);
return { success: false, error: err instanceof Error ? err.message : '未知错误' };
}
};
// API 方法封装
export const api = {
// 用户管理 API
users: {
// 获取用户列表
getList: async (params?: {
page?: number;
limit?: number;
search?: string;
}) => {
const { data, error, response } = await client.GET('/users', {
params: {
query: params,
},
});
if (error) {
console.error('获取用户列表失败:', error);
throw new Error(`API Error: ${error}`);
}
return data;
},
// 获取用户详情
getDetail: async (id: number) => {
const { data, error } = await client.GET('/users/{id}', {
params: {
path: { id },
},
});
if (error) {
console.error('获取用户详情失败:', error);
throw new Error(`API Error: ${error}`);
}
return data;
},
},
// 农机管理 API
machinery: {
// 获取农机列表
getList: async (params?: {
page?: number;
limit?: number;
status?: 'running' | 'idle' | 'maintenance' | 'error' | 'offline';
}) => {
const { data, error } = await client.GET('/machinery', {
params: {
query: params,
},
});
if (error) {
console.error('获取农机列表失败:', error);
throw new Error(`API Error: ${error}`);
}
return data;
},
// 获取农机详情
getDetail: async (id: number) => {
const { data, error } = await client.GET('/machinery/{id}', {
params: {
path: { id },
},
});
if (error) {
console.error('获取农机详情失败:', error);
throw new Error(`API Error: ${error}`);
}
return data;
},
// 创建农机
create: async (machineryData: {
name: string;
type: 'tractor' | 'harvester' | 'planter' | 'sprayer' | 'irrigation';
model: string;
serial_number?: string;
operator_id?: number;
purchase_date?: string;
}) => {
const { data, error } = await client.POST('/machinery', {
body: machineryData,
});
if (error) {
console.error('创建农机失败:', error);
throw new Error(`API Error: ${error}`);
}
return data;
},
// 更新农机
update: async (
id: number,
updateData: {
name?: string;
status?: 'running' | 'idle' | 'maintenance' | 'error' | 'offline';
operator_id?: number;
last_maintenance?: string;
next_maintenance?: string;
}
) => {
const { data, error } = await client.PUT('/machinery/{id}', {
params: {
path: { id },
},
body: updateData,
});
if (error) {
console.error('更新农机失败:', error);
throw new Error(`API Error: ${error}`);
}
return data;
},
// 删除农机
delete: async (id: number) => {
const { error } = await client.DELETE('/machinery/{id}', {
params: {
path: { id },
},
});
if (error) {
console.error('删除农机失败:', error);
throw new Error(`API Error: ${error}`);
}
return true; // 删除成功
},
},
};
// 类型导出(供组件使用)
export type {
User,
Machinery,
Location,
Coordinates,
CreateMachineryRequest,
UpdateMachineryRequest,
Error as ApiError
} from './v1.d.ts';
export default client;

526
crop-x/src/lib/api/v1.d.ts vendored Normal file
View File

@@ -0,0 +1,526 @@
/**
* This file was auto-generated by openapi-typescript.
* Do not make direct changes to the file.
*/
export interface paths {
"/users": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
/**
* 获取用户列表
* @description 获取所有用户的分页列表
*/
get: {
parameters: {
query?: {
/** @description 页码 */
page?: number;
/** @description 每页数量 */
limit?: number;
/** @description 搜索关键词 */
search?: string;
};
header?: never;
path?: never;
cookie?: never;
};
requestBody?: never;
responses: {
/** @description 成功获取用户列表 */
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": {
/** @example 200 */
code?: number;
/** @example success */
message?: string;
data?: {
users?: components["schemas"]["User"][];
/** @example 100 */
total?: number;
/** @example 1 */
page?: number;
/** @example 20 */
limit?: number;
};
};
};
};
};
};
put?: never;
post?: never;
delete?: never;
options?: never;
head?: never;
patch?: never;
trace?: never;
};
"/users/{id}": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
/**
* 获取用户详情
* @description 根据用户ID获取用户详细信息
*/
get: {
parameters: {
query?: never;
header?: never;
path: {
/** @description 用户ID */
id: number;
};
cookie?: never;
};
requestBody?: never;
responses: {
/** @description 成功获取用户详情 */
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": {
/** @example 200 */
code?: number;
/** @example success */
message?: string;
data?: components["schemas"]["User"];
};
};
};
/** @description 用户不存在 */
404: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["Error"];
};
};
};
};
put?: never;
post?: never;
delete?: never;
options?: never;
head?: never;
patch?: never;
trace?: never;
};
"/machinery": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
/**
* 获取农机列表
* @description 获取所有农机的分页列表
*/
get: {
parameters: {
query?: {
page?: number;
limit?: number;
/** @description 农机状态筛选 */
status?: "running" | "idle" | "maintenance" | "error" | "offline";
};
header?: never;
path?: never;
cookie?: never;
};
requestBody?: never;
responses: {
/** @description 成功获取农机列表 */
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": {
/** @example 200 */
code?: number;
/** @example success */
message?: string;
data?: {
machinery?: components["schemas"]["Machinery"][];
/** @example 50 */
total?: number;
};
};
};
};
};
};
put?: never;
/**
* 创建农机
* @description 创建新的农机记录
*/
post: {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
requestBody: {
content: {
"application/json": components["schemas"]["CreateMachineryRequest"];
};
};
responses: {
/** @description 农机创建成功 */
201: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": {
/** @example 201 */
code?: number;
/** @example 农机创建成功 */
message?: string;
data?: components["schemas"]["Machinery"];
};
};
};
};
};
delete?: never;
options?: never;
head?: never;
patch?: never;
trace?: never;
};
"/machinery/{id}": {
parameters: {
query?: never;
header?: never;
path?: never;
cookie?: never;
};
/** 获取农机详情 */
get: {
parameters: {
query?: never;
header?: never;
path: {
id: number;
};
cookie?: never;
};
requestBody?: never;
responses: {
/** @description 成功获取农机详情 */
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": {
/** @example 200 */
code?: number;
/** @example success */
message?: string;
data?: components["schemas"]["Machinery"];
};
};
};
};
};
/** 更新农机信息 */
put: {
parameters: {
query?: never;
header?: never;
path: {
id: number;
};
cookie?: never;
};
requestBody: {
content: {
"application/json": components["schemas"]["UpdateMachineryRequest"];
};
};
responses: {
/** @description 农机更新成功 */
200: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": {
/** @example 200 */
code?: number;
/** @example 农机更新成功 */
message?: string;
data?: components["schemas"]["Machinery"];
};
};
};
};
};
post?: never;
/** 删除农机 */
delete: {
parameters: {
query?: never;
header?: never;
path: {
id: number;
};
cookie?: never;
};
requestBody?: never;
responses: {
/** @description 农机删除成功 */
204: {
headers: {
[name: string]: unknown;
};
content?: never;
};
};
};
options?: never;
head?: never;
patch?: never;
trace?: never;
};
}
export type webhooks = Record<string, never>;
export interface components {
schemas: {
User: {
/**
* @description 用户ID
* @example 1
*/
id?: number;
/**
* @description 用户名
* @example john_doe
*/
username?: string;
/**
* Format: email
* @description 邮箱地址
* @example john@example.com
*/
email?: string;
/**
* @description 全名
* @example John Doe
*/
full_name?: string;
/**
* @description 手机号码
* @example 13800138000
*/
phone?: string;
/**
* @description 用户角色
* @example operator
* @enum {string}
*/
role?: "admin" | "manager" | "operator" | "viewer";
/**
* @description 用户状态
* @example active
* @enum {string}
*/
status?: "active" | "inactive" | "suspended";
/**
* Format: date-time
* @description 创建时间
* @example 2024-01-15T10:30:00Z
*/
created_at?: string;
/**
* Format: date-time
* @description 更新时间
* @example 2024-01-15T10:30:00Z
*/
updated_at?: string;
};
Machinery: {
/**
* @description 农机ID
* @example 1
*/
id?: number;
/**
* @description 农机名称
* @example 拖拉机-001
*/
name?: string;
/**
* @description 农机类型
* @example tractor
* @enum {string}
*/
type?: "tractor" | "harvester" | "planter" | "sprayer" | "irrigation";
/**
* @description 型号
* @example John Deere 6M Series
*/
model?: string;
/**
* @description 序列号
* @example JD6M123456
*/
serial_number?: string;
/**
* @description 农机状态
* @example idle
* @enum {string}
*/
status?: "running" | "idle" | "maintenance" | "error" | "offline";
location?: components["schemas"]["Location"];
operator?: components["schemas"]["User"];
/**
* Format: date
* @description 购买日期
* @example 2024-01-01
*/
purchase_date?: string;
/**
* Format: date
* @description 上次维护日期
* @example 2024-06-15
*/
last_maintenance?: string;
/**
* Format: date
* @description 下次维护日期
* @example 2024-09-15
*/
next_maintenance?: string;
/**
* Format: date-time
* @description 创建时间
*/
created_at?: string;
/**
* Format: date-time
* @description 更新时间
*/
updated_at?: string;
};
Location: {
/**
* @description 地块ID
* @example 1
*/
field_id?: number;
/**
* @description 地块名称
* @example 北区A地块
*/
field_name?: string;
coordinates?: components["schemas"]["Coordinates"];
};
Coordinates: {
/**
* Format: double
* @description 纬度
* @example 39.9042
*/
latitude?: number;
/**
* Format: double
* @description 经度
* @example 116.4074
*/
longitude?: number;
};
CreateMachineryRequest: {
/** @description 农机名称 */
name: string;
/**
* @description 农机类型
* @enum {string}
*/
type: "tractor" | "harvester" | "planter" | "sprayer" | "irrigation";
/** @description 型号 */
model: string;
/** @description 序列号 */
serial_number?: string;
/**
* @description 操作员ID
* @example 1
*/
operator_id?: number;
/**
* Format: date
* @description 购买日期
*/
purchase_date?: string;
};
UpdateMachineryRequest: {
/** @description 农机名称 */
name?: string;
/**
* @description 农机状态
* @enum {string}
*/
status?: "running" | "idle" | "maintenance" | "error" | "offline";
/** @description 操作员ID */
operator_id?: number;
/**
* Format: date
* @description 上次维护日期
*/
last_maintenance?: string;
/**
* Format: date
* @description 下次维护日期
*/
next_maintenance?: string;
};
Error: {
/**
* @description 错误代码
* @example 404
*/
code?: number;
/**
* @description 错误信息
* @example 资源不存在
*/
message?: string;
/**
* @description 错误详情
* @example {
* "field": "id",
* "reason": "用户ID不存在"
* }
*/
details?: Record<string, never>;
};
};
responses: never;
parameters: never;
requestBodies: never;
headers: never;
pathItems: never;
}
export type $defs = Record<string, never>;
export type operations = Record<string, never>;