@cpzxrobot/sdk 1.3.114 → 1.3.116

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,449 @@
1
+ # AI报表模板接口使用说明
2
+
3
+ ## 概述
4
+
5
+ AI报表模板接口用于管理工厂的AI报表模板,包括查询模板列表、新增和修改模板等功能。
6
+
7
+ ## 接口基础信息
8
+
9
+ **Base URL**: `/api/v2/aiform/template`
10
+
11
+ ## 如何使用
12
+
13
+ 通过 `cpzxrobot().aiform.template` 访问AI报表模板相关功能。
14
+
15
+ 通过 `cpzxrobot().aiform.record` 访问文件上传和记录相关功能。
16
+
17
+ ## 可用方法
18
+
19
+ ### 1. 查询模板列表
20
+
21
+ **方法**: `list(factoryId: number): Promise<AiformTemplateListResponse>`
22
+
23
+ **接口路径**: `GET /api/v2/aiform/template/list/{factoryId}`
24
+
25
+ **功能**: 根据工厂ID查询该工厂下的所有AI报表模板
26
+
27
+ **参数**:
28
+ - `factoryId` (number): 工厂ID,必填
29
+
30
+ **返回值**:
31
+ ```typescript
32
+ interface AiformTemplateListResponse {
33
+ code: number; // 状态码
34
+ message: string; // 提示信息
35
+ data: AiformTemplate[]; // 模板列表
36
+ }
37
+
38
+ interface AiformTemplate {
39
+ id: number; // 模板ID
40
+ factoryId: number; // 工厂ID
41
+ name: string; // 模板名称
42
+ content: string; // 模板内容
43
+ attention?: string; // 注意事项
44
+ }
45
+ ```
46
+
47
+ **使用示例**:
48
+
49
+ ```javascript
50
+ // 查询工厂1001的模板列表
51
+ const response = await cpzxrobot().aiform.template.list(1001);
52
+
53
+ if (response.code === 200) {
54
+ const templates = response.data;
55
+ console.log(`找到 ${templates.length} 个模板:`);
56
+
57
+ templates.forEach(template => {
58
+ console.log(`ID: ${template.id}, 名称: ${template.name}`);
59
+ console.log(`内容: ${template.content.substring(0, 100)}...`);
60
+ if (template.attention) {
61
+ console.log(`注意事项: ${template.attention}`);
62
+ }
63
+ });
64
+ } else {
65
+ console.error('获取模板列表失败:', response.message);
66
+ }
67
+ ```
68
+
69
+ ### 2. 新增或修改模板
70
+
71
+ **方法**: `save(request: AiformTemplateSaveRequest): Promise<AiformTemplateSaveResponse>`
72
+
73
+ **接口路径**: `POST /api/v2/aiform/template/save`
74
+
75
+ **功能**: 新增或修改AI报表模板。当请求体中包含 `id` 时执行修改操作,不包含 `id` 时执行新增操作。
76
+
77
+ **请求参数**:
78
+ ```typescript
79
+ interface AiformTemplateSaveRequest {
80
+ id?: number; // 模板ID(传则修改,不传则新增)
81
+ factoryId: number; // 工厂ID,必填
82
+ name: string; // 模板名称,必填
83
+ content: string; // 模板内容,必填
84
+ attention?: string; // 注意事项,可选
85
+ }
86
+ ```
87
+
88
+ **返回值**:
89
+ ```typescript
90
+ interface AiformTemplateSaveResponse {
91
+ code: number; // 状态码
92
+ message: string; // 提示信息
93
+ }
94
+ ```
95
+
96
+ **使用示例**:
97
+
98
+ #### 新增模板
99
+
100
+ ```javascript
101
+ // 新增模板
102
+ const response = await cpzxrobot().aiform.template.save({
103
+ factoryId: 1001,
104
+ name: "生产日报模板",
105
+ content: "## 生产日报\n\n日期: {{date}}\n\n产量: {{production}}\n\n质量: {{quality}}",
106
+ attention: "请确保填写真实数据"
107
+ });
108
+
109
+ if (response.code === 200) {
110
+ console.log('模板创建成功');
111
+ } else {
112
+ console.error('模板创建失败:', response.message);
113
+ }
114
+ ```
115
+
116
+ #### 修改模板
117
+
118
+ ```javascript
119
+ // 修改模板
120
+ const response = await cpzxrobot().aiform.template.save({
121
+ id: 1, // 模板ID
122
+ factoryId: 1001,
123
+ name: "生产日报模板(更新版)",
124
+ content: "## 生产日报\n\n日期: {{date}}\n\n产量: {{production}}\n\n质量: {{quality}}\n\n备注: {{remark}}",
125
+ attention: "请确保填写真实数据,新增加了备注字段"
126
+ });
127
+
128
+ if (response.code === 200) {
129
+ console.log('模板更新成功');
130
+ } else {
131
+ console.error('模板更新失败:', response.message);
132
+ }
133
+ ```
134
+
135
+ ### 3. 上传表格图片
136
+
137
+ **方法**: `upload(file: File, templateId: number): Promise<AiformFileUploadResponse>`
138
+
139
+ **接口路径**: `POST /api/v2/aiform/file/upload`
140
+
141
+ **功能**: 上传表格图片到 MinIO,并关联到指定的模板。上传成功后自动异步触发大模型解析。
142
+
143
+ **参数**:
144
+ - `file` (File): 表格图片文件,必填
145
+ - `templateId` (number): 关联的模板ID,必填
146
+
147
+ **返回值**:
148
+ ```typescript
149
+ interface AiformFileUploadResponse {
150
+ code: number; // 状态码
151
+ message: string; // 提示信息
152
+ data: File; // 文件记录
153
+ }
154
+
155
+ interface File {
156
+ id: number; // 文件记录ID
157
+ code?: string; // 业务编码
158
+ tableKey: number; // 关联业务ID(模板ID)
159
+ tableName: string; // 关联业务表名(固定值 aiform_template)
160
+ fileName: string; // 原始文件名
161
+ storageName: string; // 存储文件名
162
+ fileUrl: string; // MinIO存储路径
163
+ accessUrl: string; // 预签名访问链接
164
+ fileExt: string; // 文件扩展名
165
+ server: string; // 存储服务(minio)
166
+ type: string; // 文件类型(image)
167
+ status: number; // 状态:0-已上传 1-解析中 2-已解析
168
+ createTime: string; // 创建时间
169
+ updateTime: string; // 更新时间
170
+ }
171
+ ```
172
+
173
+ **使用示例**:
174
+
175
+ ```javascript
176
+ // HTML文件输入示例
177
+ <input type="file" id="fileInput" accept="image/*">
178
+
179
+ // 上传文件
180
+ async function uploadFile() {
181
+ const fileInput = document.getElementById('fileInput');
182
+ const file = fileInput.files[0];
183
+ const templateId = 1; // 模板ID
184
+
185
+ if (!file) {
186
+ alert('请选择文件');
187
+ return;
188
+ }
189
+
190
+ try {
191
+ const response = await cpzxrobot().aiform.record.upload(file, templateId);
192
+ console.log('上传成功:', response.data);
193
+ console.log('文件状态:', getFileStatusText(response.data.status));
194
+ console.log('访问链接:', response.data.accessUrl);
195
+
196
+ // 可以开始轮询状态
197
+ trackFileStatus(templateId, response.data.id);
198
+ } catch (error) {
199
+ console.error('上传失败:', error.message);
200
+ }
201
+ }
202
+
203
+ function getFileStatusText(status) {
204
+ const statusMap = {
205
+ 0: '已上传',
206
+ 1: '解析中',
207
+ 2: '已解析'
208
+ };
209
+ return statusMap[status] || '未知';
210
+ }
211
+
212
+ // 绑定事件
213
+ document.getElementById('fileInput').addEventListener('change', uploadFile);
214
+ ```
215
+
216
+ ### 4. 根据模板ID查询上传记录
217
+
218
+ **方法**: `list(templateId: number): Promise<AiformFileListResponse>`
219
+
220
+ **接口路径**: `GET /api/v2/aiform/file/list/{templateId}`
221
+
222
+ **功能**: 根据模板ID查询该模板下所有上传的文件记录,按创建时间倒序排列。
223
+
224
+ **参数**:
225
+ - `templateId` (number): 模板ID,必填
226
+
227
+ **返回值**:
228
+ ```typescript
229
+ interface AiformFileListResponse {
230
+ code: number; // 状态码
231
+ message: string; // 提示信息
232
+ data: File[]; // 文件记录列表
233
+ }
234
+ ```
235
+
236
+ **使用示例**:
237
+
238
+ ```javascript
239
+ // 获取模板的上传记录
240
+ async function getTemplateRecords(templateId) {
241
+ try {
242
+ const response = await cpzxrobot().aiform.record.list(templateId);
243
+
244
+ if (response.code === 200) {
245
+ const records = response.data;
246
+ console.log(`找到 ${records.length} 条上传记录:`);
247
+
248
+ records.forEach((record, index) => {
249
+ console.log(`\n记录 ${index + 1}:`);
250
+ console.log(`文件名: ${record.fileName}`);
251
+ console.log(`状态: ${getFileStatusText(record.status)}`);
252
+ console.log(`创建时间: ${record.createTime}`);
253
+ console.log(`访问链接: ${record.accessUrl}`);
254
+ });
255
+ }
256
+ } catch (error) {
257
+ console.error('获取记录失败:', error.message);
258
+ }
259
+ }
260
+
261
+ // 调用示例
262
+ getTemplateRecords(1); // 查询模板ID为1的上传记录
263
+ ```
264
+
265
+ ### 5. 轮询文件解析状态
266
+
267
+ **功能**: 上传文件后,需要轮询文件状态以确认解析进度。
268
+
269
+ **使用示例**:
270
+
271
+ ```javascript
272
+ async function trackFileStatus(templateId, fileId) {
273
+ let status = 0; // 初始状态
274
+ let attempts = 0;
275
+ const maxAttempts = 30; // 最多轮询30次
276
+
277
+ console.log('开始跟踪文件解析状态...');
278
+
279
+ while (status !== 2 && attempts < maxAttempts) {
280
+ try {
281
+ const response = await cpzxrobot().aiform.record.list(templateId);
282
+ const fileRecord = response.data.find(record => record.id === fileId);
283
+
284
+ if (fileRecord) {
285
+ status = fileRecord.status;
286
+ console.log(`当前状态: ${getFileStatusText(status)}`);
287
+
288
+ if (status === 2) {
289
+ console.log('文件已成功解析!');
290
+ return;
291
+ }
292
+ }
293
+
294
+ await new Promise(resolve => setTimeout(resolve, 5000)); // 每5秒轮询一次
295
+ attempts++;
296
+ } catch (error) {
297
+ console.error('轮询失败:', error.message);
298
+ await new Promise(resolve => setTimeout(resolve, 5000));
299
+ attempts++;
300
+ }
301
+ }
302
+
303
+ if (attempts >= maxAttempts) {
304
+ console.log('解析超时,请稍后手动检查状态');
305
+ }
306
+ }
307
+ ```
308
+
309
+ ## 错误处理
310
+
311
+ ### 常见错误
312
+
313
+ 1. **参数验证错误**:
314
+ - `工厂ID不能为空` - 未提供factoryId参数
315
+ - `模板名称不能为空` - 未提供name参数
316
+ - `模板内容不能为空` - 未提供content参数
317
+ - `文件不能为空` - 未提供file参数
318
+ - `模板ID不能为空` - 未提供templateId参数
319
+
320
+ 2. **API错误**:
321
+ - 当API返回非200状态码时,会抛出包含错误信息的异常
322
+
323
+ ### 错误处理示例
324
+
325
+ ```javascript
326
+ try {
327
+ // 尝试获取模板列表
328
+ const response = await cpzxrobot().aiform.template.list(1001);
329
+ console.log('模板列表:', response.data);
330
+ } catch (error) {
331
+ console.error('操作失败:', error.message);
332
+ }
333
+
334
+ try {
335
+ // 尝试保存模板
336
+ const response = await cpzxrobot().aiform.template.save({
337
+ factoryId: 1001,
338
+ name: "测试模板",
339
+ content: "模板内容"
340
+ });
341
+ console.log('保存成功:', response.message);
342
+ } catch (error) {
343
+ console.error('保存失败:', error.message);
344
+ }
345
+
346
+ try {
347
+ // 尝试上传文件
348
+ const file = document.getElementById('fileInput').files[0];
349
+ const response = await cpzxrobot().aiform.record.upload(file, 1);
350
+ console.log('上传成功:', response.data);
351
+ } catch (error) {
352
+ console.error('上传失败:', error.message);
353
+ }
354
+
355
+ try {
356
+ // 尝试获取上传记录
357
+ const response = await cpzxrobot().aiform.record.list(1);
358
+ console.log('上传记录:', response.data);
359
+ } catch (error) {
360
+ console.error('获取记录失败:', error.message);
361
+ }
362
+ ```
363
+
364
+ ## 完整使用示例
365
+
366
+ ```javascript
367
+ async function manageAITemplates() {
368
+ try {
369
+ const aiform = cpzxrobot().aiform.template;
370
+ const factoryId = 1001;
371
+
372
+ console.log('=== 管理AI报表模板 ===');
373
+
374
+ // 1. 获取模板列表
375
+ console.log('\n1. 获取模板列表:');
376
+ const listResponse = await aiform.list(factoryId);
377
+ if (listResponse.code === 200) {
378
+ console.log(`找到 ${listResponse.data.length} 个模板:`);
379
+ listResponse.data.forEach((template, index) => {
380
+ console.log(` ${index + 1}. ${template.name} (ID: ${template.id})`);
381
+ });
382
+ }
383
+
384
+ // 2. 新增模板
385
+ console.log('\n2. 新增模板:');
386
+ const createResponse = await aiform.save({
387
+ factoryId: factoryId,
388
+ name: "新增测试模板",
389
+ content: "## 测试模板\n\n这是一个测试模板",
390
+ attention: "测试用模板"
391
+ });
392
+ console.log(`新增结果: ${createResponse.message}`);
393
+
394
+ // 3. 再次获取模板列表
395
+ console.log('\n3. 再次获取模板列表:');
396
+ const updatedListResponse = await aiform.list(factoryId);
397
+ if (updatedListResponse.code === 200) {
398
+ console.log(`现在有 ${updatedListResponse.data.length} 个模板:`);
399
+ updatedListResponse.data.forEach((template, index) => {
400
+ console.log(` ${index + 1}. ${template.name} (ID: ${template.id})`);
401
+ });
402
+ }
403
+
404
+ // 4. 修改最后一个模板
405
+ if (updatedListResponse.data.length > 0) {
406
+ const lastTemplate = updatedListResponse.data[updatedListResponse.data.length - 1];
407
+ console.log(`\n4. 修改模板: ${lastTemplate.name}`);
408
+
409
+ const updateResponse = await aiform.save({
410
+ id: lastTemplate.id,
411
+ factoryId: factoryId,
412
+ name: `${lastTemplate.name} (已更新)`,
413
+ content: lastTemplate.content + '\n\n更新时间: ' + new Date().toLocaleString(),
414
+ attention: lastTemplate.attention + ' - 已更新'
415
+ });
416
+ console.log(`修改结果: ${updateResponse.message}`);
417
+ }
418
+
419
+ // 5. 最终模板列表
420
+ console.log('\n5. 最终模板列表:');
421
+ const finalListResponse = await aiform.list(factoryId);
422
+ if (finalListResponse.code === 200) {
423
+ console.log(`最终有 ${finalListResponse.data.length} 个模板:`);
424
+ finalListResponse.data.forEach((template, index) => {
425
+ console.log(` ${index + 1}. ${template.name} (ID: ${template.id})`);
426
+ });
427
+ }
428
+
429
+ } catch (error) {
430
+ console.error('操作失败:', error.message);
431
+ }
432
+ }
433
+
434
+ // 执行操作
435
+ manageAITemplates();
436
+ ```
437
+
438
+ ## 注意事项
439
+
440
+ 1. **权限要求**: 使用这些接口需要具有相应的工厂管理权限
441
+ 2. **模板内容**: 模板内容支持使用模板变量(如 `{{date}}`),具体变量格式请参考AI报表系统的相关文档
442
+ 3. **参数验证**: 所有必填参数必须提供,否则会抛出错误
443
+ 4. **错误处理**: 建议使用 try-catch 捕获可能的错误
444
+ 5. **网络请求**: 这些接口是异步网络请求,需要使用 `async/await` 或 Promise 处理
445
+
446
+ ## 版本信息
447
+
448
+ - **SDK版本**: 1.3.114+
449
+ - **接口版本**: v2
package/aiform_gateway.ts CHANGED
@@ -3,6 +3,8 @@ import type {
3
3
  AiformTemplateListResponse,
4
4
  AiformTemplateSaveRequest,
5
5
  AiformTemplateSaveResponse,
6
+ AiformFileUploadResponse,
7
+ AiformFileListResponse,
6
8
  } from ".";
7
9
 
8
10
  export class AiformGateway extends Object {
@@ -66,4 +68,55 @@ export class AiformGateway extends Object {
66
68
  },
67
69
  };
68
70
  }
71
+
72
+ get record() {
73
+ return {
74
+ /**
75
+ * 上传表格图片
76
+ * @param file 表格图片文件
77
+ * @param templateId 关联的模板ID
78
+ * @returns Promise 包含文件记录信息
79
+ */
80
+ upload: async (templateId: number): Promise<AiformFileUploadResponse> => {
81
+ const axios = await this.context.ready;
82
+
83
+ if (!templateId) {
84
+ throw new Error('模板ID不能为空');
85
+ }
86
+
87
+ return axios.upload(`/api/v2/aiform/file/upload`,{
88
+ title: "请选择要解析的表格图片",
89
+ data: {
90
+ templateId,
91
+ }
92
+ }).then((res) => {
93
+ if (res.data.code !== 200) {
94
+ throw new Error(res.data.message || '上传文件失败');
95
+ }
96
+ return res.data;
97
+ });
98
+ },
99
+
100
+ /**
101
+ * 根据模板ID查询上传记录
102
+ * @param templateId 模板ID
103
+ * @returns Promise 包含文件记录列表
104
+ */
105
+ list: async (templateId: number): Promise<AiformFileListResponse> => {
106
+ const axios = await this.context.ready;
107
+
108
+ // 参数验证
109
+ if (!templateId) {
110
+ throw new Error('模板ID不能为空');
111
+ }
112
+
113
+ return axios.get(`/api/v2/aiform/file/list/${templateId}`).then((res) => {
114
+ if (res.data.code !== 200) {
115
+ throw new Error(res.data.message || '获取文件列表失败');
116
+ }
117
+ return res.data;
118
+ });
119
+ },
120
+ };
121
+ }
69
122
  }
@@ -63,5 +63,50 @@ class AiformGateway extends Object {
63
63
  }),
64
64
  };
65
65
  }
66
+ get record() {
67
+ return {
68
+ /**
69
+ * 上传表格图片
70
+ * @param file 表格图片文件
71
+ * @param templateId 关联的模板ID
72
+ * @returns Promise 包含文件记录信息
73
+ */
74
+ upload: (templateId) => __awaiter(this, void 0, void 0, function* () {
75
+ const axios = yield this.context.ready;
76
+ if (!templateId) {
77
+ throw new Error('模板ID不能为空');
78
+ }
79
+ return axios.upload(`/api/v2/aiform/file/upload`, {
80
+ title: "请选择要解析的表格图片",
81
+ data: {
82
+ templateId,
83
+ }
84
+ }).then((res) => {
85
+ if (res.data.code !== 200) {
86
+ throw new Error(res.data.message || '上传文件失败');
87
+ }
88
+ return res.data;
89
+ });
90
+ }),
91
+ /**
92
+ * 根据模板ID查询上传记录
93
+ * @param templateId 模板ID
94
+ * @returns Promise 包含文件记录列表
95
+ */
96
+ list: (templateId) => __awaiter(this, void 0, void 0, function* () {
97
+ const axios = yield this.context.ready;
98
+ // 参数验证
99
+ if (!templateId) {
100
+ throw new Error('模板ID不能为空');
101
+ }
102
+ return axios.get(`/api/v2/aiform/file/list/${templateId}`).then((res) => {
103
+ if (res.data.code !== 200) {
104
+ throw new Error(res.data.message || '获取文件列表失败');
105
+ }
106
+ return res.data;
107
+ });
108
+ }),
109
+ };
110
+ }
66
111
  }
67
112
  exports.AiformGateway = AiformGateway;
@@ -52,6 +52,8 @@ class ProductionGateway extends Object {
52
52
  params: {
53
53
  factory_id: factoryId
54
54
  }
55
+ }).then((res) => {
56
+ return res.data;
55
57
  });
56
58
  }),
57
59
  create: (request) => __awaiter(this, void 0, void 0, function* () {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cpzxrobot/sdk",
3
- "version": "1.3.114",
3
+ "version": "1.3.116",
4
4
  "description": "提供给上海正芯数智APP第三方H5应用使用的SDK",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -48,6 +48,8 @@ export class ProductionGateway extends Object {
48
48
  params: {
49
49
  factory_id: factoryId
50
50
  }
51
+ }).then((res) => {
52
+ return res.data;
51
53
  });
52
54
  },
53
55
  create: async (request: {
package/types.d.ts CHANGED
@@ -859,6 +859,64 @@ interface AiformTemplateSaveResponse {
859
859
  message: string;
860
860
  }
861
861
 
862
+ /**
863
+ * 文件记录接口(通用文件表 app_v2_file)
864
+ */
865
+ interface File {
866
+ /** 文件记录ID */
867
+ id: number;
868
+ /** 业务编码 */
869
+ code?: string;
870
+ /** 关联业务ID(此处为模板ID) */
871
+ tableKey: number;
872
+ /** 关联业务表名(固定值 aiform_template) */
873
+ tableName: string;
874
+ /** 原始文件名 */
875
+ fileName: string;
876
+ /** 存储文件名(系统统一生成) */
877
+ storageName: string;
878
+ /** MinIO存储路径 */
879
+ fileUrl: string;
880
+ /** 预签名访问链接(动态生成,有效期7天,不存数据库) */
881
+ accessUrl: string;
882
+ /** 文件扩展名 */
883
+ fileExt: string;
884
+ /** 存储服务(minio) */
885
+ server: string;
886
+ /** 文件类型(image) */
887
+ type: string;
888
+ /** 状态:0-已上传 1-解析中 2-已解析 */
889
+ status: number;
890
+ /** 创建时间 */
891
+ createTime: string;
892
+ /** 更新时间 */
893
+ updateTime: string;
894
+ }
895
+
896
+ /**
897
+ * 文件上传响应接口
898
+ */
899
+ interface AiformFileUploadResponse {
900
+ /** 响应码 */
901
+ code: number;
902
+ /** 提示信息 */
903
+ message: string;
904
+ /** 文件记录 */
905
+ data: File;
906
+ }
907
+
908
+ /**
909
+ * 文件列表响应接口
910
+ */
911
+ interface AiformFileListResponse {
912
+ /** 响应码 */
913
+ code: number;
914
+ /** 提示信息 */
915
+ message: string;
916
+ /** 文件记录列表 */
917
+ data: File[];
918
+ }
919
+
862
920
  declare class Cpzxrobot {
863
921
  transport: TransportGateway;
864
922
  ready: Promise<MyAxiosInstance>;
@@ -966,6 +1024,9 @@ declare module "@cpzxrobot/sdk" {
966
1024
  AiformTemplateListResponse,
967
1025
  AiformTemplateSaveRequest,
968
1026
  AiformTemplateSaveResponse,
1027
+ File,
1028
+ AiformFileUploadResponse,
1029
+ AiformFileListResponse,
969
1030
  AlarmRuleDetail,
970
1031
  AlarmRuleListResponse,
971
1032
  AlarmRuleDetailResponse,