11import { PointType , type PointLimit } from '@/types/point' ;
22import axios from 'axios' ;
3+ import { ElMessage } from 'element-plus' ;
4+
35// const apiUrl = "http://127.0.0.1:8888";
46// 使用相对路径,Nginx会代理到正确的后端地址
57const API_BASE_URL = import . meta. env . VUE_APP_API_BASE || '/' ; // Nginx会将/api代理到实际后端
@@ -12,27 +14,40 @@ export const instance = axios.create({
1214 }
1315} ) ;
1416
15- // 封装请求方法
16- export const requestApi = async ( url : string , method : string , data : any ) : Promise < any > => {
17- try {
18- const response = await instance . request ( {
19- url,
20- method,
21- data
22- } ) ;
17+ // 添加响应拦截器
18+ instance . interceptors . response . use (
19+ ( response ) => {
2320 // 检查业务响应码
24- if ( response . data . code !== 200 ) {
25- throw new Error ( response . data . message || '请求失败' ) ;
21+ if ( response . data && response . data . code !== 200 ) {
22+ const errorMsg = response . data . message || '请求失败' ;
23+ ElMessage . error ( errorMsg ) ;
24+ return Promise . reject ( new Error ( errorMsg ) ) ;
2625 }
27- return response . data . data ;
28- } catch ( error ) {
26+ return response ;
27+ } ,
28+ ( error ) => {
2929 // 统一错误处理
30+ let message = '网络请求失败' ;
3031 if ( axios . isAxiosError ( error ) ) {
31- throw new Error ( error . response ?. data ?. message || error . message ) ;
32+ message = error . response ?. data ?. message || error . message ;
33+ } else if ( error instanceof Error ) {
34+ message = error . message ;
3235 }
33- throw error ;
36+ ElMessage . error ( message ) ;
37+ return Promise . reject ( error ) ;
3438 }
39+ ) ;
40+
41+ // 封装请求方法
42+ export const requestApi = async ( url : string , method : string , data : any ) : Promise < any > => {
43+ const response = await instance . request ( {
44+ url,
45+ method,
46+ data
47+ } ) ;
48+ return response . data . data ;
3549}
50+
3651export async function getDeviceList ( ) : Promise < Array < string > > {
3752 try {
3853 const data = await requestApi ( `/device/get_device_list` , 'post' , null ) ;
@@ -143,17 +158,12 @@ export async function getDeviceTable(deviceName: string, slaveId: number, pointN
143158}
144159
145160export async function editPointData ( deviceName : string , pointCode : string , pointValue : number ) : Promise < boolean > {
146- try {
147- const data = await requestApi ( '/device/edit_point_data/' , 'post' , {
148- device_name : deviceName ,
149- point_code : pointCode ,
150- point_value : pointValue ,
151- } ) ;
152- return data ;
153- } catch ( error ) {
154- console . error ( 'Error stop simulation:' , error ) ;
155- return false ;
156- }
161+ const data = await requestApi ( '/device/edit_point_data/' , 'post' , {
162+ device_name : deviceName ,
163+ point_code : pointCode ,
164+ point_value : pointValue ,
165+ } ) ;
166+ return data ;
157167}
158168
159169
@@ -167,8 +177,8 @@ export async function editPointLimit(deviceName: string, pointCode: string, minV
167177 } ) ;
168178 return data ;
169179 } catch ( error ) {
170- console . error ( 'Error stop simulation :' , error ) ;
171- return false ;
180+ console . error ( 'Error editing point limit :' , error ) ;
181+ throw error ;
172182 }
173183}
174184
@@ -187,7 +197,7 @@ export async function getPointLimit(deviceName: string, pointCode: string): Prom
187197 pointLimit . maxValueLimit = data . max_value_limit ;
188198 return pointLimit ;
189199 } catch ( error ) {
190- console . error ( 'Error stop simulation :' , error ) ;
200+ console . error ( 'Error getting point limit :' , error ) ;
191201 return pointLimit ;
192202 }
193203}
@@ -199,8 +209,8 @@ export async function resetPointData(deviceName: string): Promise<boolean> {
199209 } ) ;
200210 return data ;
201211 } catch ( error ) {
202- console . error ( 'Error stop simulation :' , error ) ;
203- return false ;
212+ console . error ( 'Error resetting point data :' , error ) ;
213+ throw error ;
204214 }
205215}
206216
@@ -227,7 +237,7 @@ export async function setSinglePointSimulateMethod(deviceName: string, pointCode
227237 return data ;
228238 } catch ( error ) {
229239 console . error ( 'Error setting single point simulate method:' , error ) ;
230- return false ;
240+ throw error ;
231241 }
232242}
233243
@@ -241,7 +251,7 @@ export async function setSinglePointStep(deviceName: string, pointCode: string,
241251 return data ;
242252 } catch ( error ) {
243253 console . error ( 'Error setting single point step:' , error ) ;
244- return false ;
254+ throw error ;
245255 }
246256}
247257
@@ -256,7 +266,7 @@ export async function setPointSimulationRange(deviceName: string, pointCode: str
256266 return data ;
257267 } catch ( error ) {
258268 console . error ( 'Error setting point simulation range:' , error ) ;
259- return false ;
269+ throw error ;
260270 }
261271}
262272
@@ -270,7 +280,7 @@ export async function editPointMetadata(deviceName: string, pointCode: string, m
270280 return data ;
271281 } catch ( error ) {
272282 console . error ( 'Error editing point metadata:' , error ) ;
273- return false ;
283+ throw error ;
274284 }
275285}
276286
@@ -296,7 +306,7 @@ export async function startAutoRead(deviceName: string): Promise<boolean> {
296306 return data ;
297307 } catch ( error ) {
298308 console . error ( 'Error starting auto read:' , error ) ;
299- return false ;
309+ throw error ;
300310 }
301311}
302312
@@ -308,7 +318,7 @@ export async function stopAutoRead(deviceName: string): Promise<boolean> {
308318 return data ;
309319 } catch ( error ) {
310320 console . error ( 'Error stopping auto read:' , error ) ;
311- return false ;
321+ throw error ;
312322 }
313323}
314324
@@ -321,7 +331,7 @@ export async function manualRead(deviceName: string, interval: number = 0): Prom
321331 return data ;
322332 } catch ( error ) {
323333 console . error ( 'Error performing manual read:' , error ) ;
324- return false ;
334+ throw error ;
325335 }
326336}
327337
@@ -371,7 +381,7 @@ export async function clearMessages(deviceName: string): Promise<boolean> {
371381 return data ;
372382 } catch ( error ) {
373383 console . error ( 'Error clearing messages:' , error ) ;
374- return false ;
384+ throw error ;
375385 }
376386}
377387
@@ -418,7 +428,7 @@ export async function addPoint(deviceName: string, pointData: PointCreateData):
418428 return data ;
419429 } catch ( error ) {
420430 console . error ( 'Error adding point:' , error ) ;
421- return false ;
431+ throw error ;
422432 }
423433}
424434
@@ -432,7 +442,7 @@ export async function addPointsBatch(deviceName: string, frameType: number, poin
432442 return data ;
433443 } catch ( error ) {
434444 console . error ( 'Error adding points batch:' , error ) ;
435- return false ;
445+ throw error ;
436446 }
437447}
438448
@@ -445,7 +455,7 @@ export async function deletePoint(deviceName: string, pointCode: string): Promis
445455 return data ;
446456 } catch ( error ) {
447457 console . error ( 'Error deleting point:' , error ) ;
448- return false ;
458+ throw error ;
449459 }
450460}
451461
@@ -458,7 +468,7 @@ export async function addSlave(deviceName: string, slaveId: number): Promise<boo
458468 return data ;
459469 } catch ( error ) {
460470 console . error ( 'Error adding slave:' , error ) ;
461- return false ;
471+ throw error ;
462472 }
463473}
464474
@@ -471,7 +481,7 @@ export async function deleteSlave(deviceName: string, slaveId: number): Promise<
471481 return data ;
472482 } catch ( error ) {
473483 console . error ( 'Error deleting slave:' , error ) ;
474- return false ;
484+ throw error ;
475485 }
476486}
477487
@@ -485,7 +495,7 @@ export async function editSlave(deviceName: string, oldSlaveId: number, newSlave
485495 return data ;
486496 } catch ( error ) {
487497 console . error ( 'Error editing slave:' , error ) ;
488- return false ;
498+ throw error ;
489499 }
490500}
491501
@@ -498,6 +508,70 @@ export async function clearPoints(deviceName: string, slaveId: number): Promise<
498508 return data ;
499509 } catch ( error ) {
500510 console . error ( 'Error clearing points:' , error ) ;
501- return 0 ;
511+ throw error ;
512+ }
513+ }
514+
515+ // ===== 变更追溯 =====
516+
517+ export interface ChangeRecord {
518+ source : string ;
519+ source_label : string ;
520+ old_value : any ;
521+ new_value : any ;
522+ old_real_value : any ;
523+ new_real_value : any ;
524+ timestamp : number ;
525+ time : string ;
526+ detail : string ;
527+ client_info ?: string ;
528+ }
529+
530+ export interface PointChangeHistoryResponse {
531+ point_code : string ;
532+ tracking_enabled : boolean ;
533+ maxlen : number ;
534+ history : ChangeRecord [ ] ;
535+ count : number ;
536+ }
537+
538+ export async function getPointChangeHistory ( deviceName : string , pointCode : string ) : Promise < PointChangeHistoryResponse | null > {
539+ try {
540+ const data = await requestApi ( '/device/get_point_change_history' , 'post' , {
541+ device_name : deviceName ,
542+ point_code : pointCode ,
543+ } ) ;
544+ return data ;
545+ } catch ( error ) {
546+ console . error ( 'Error getting point change history:' , error ) ;
547+ return null ;
548+ }
549+ }
550+
551+ export async function setChangeTrackingConfig ( deviceName : string , pointCode : string , enabled : boolean , maxlen ?: number ) : Promise < boolean > {
552+ try {
553+ const data = await requestApi ( '/device/set_change_tracking' , 'post' , {
554+ device_name : deviceName ,
555+ point_code : pointCode ,
556+ enabled : enabled ,
557+ maxlen : maxlen
558+ } ) ;
559+ return data ;
560+ } catch ( error ) {
561+ console . error ( 'Error setting change tracking config:' , error ) ;
562+ throw error ;
563+ }
564+ }
565+
566+ export async function clearPointChangeHistory ( deviceName : string , pointCode : string ) : Promise < boolean > {
567+ try {
568+ const data = await requestApi ( '/device/clear_point_change_history' , 'post' , {
569+ device_name : deviceName ,
570+ point_code : pointCode ,
571+ } ) ;
572+ return data ;
573+ } catch ( error ) {
574+ console . error ( 'Error clearing point change history:' , error ) ;
575+ return false ;
502576 }
503577}
0 commit comments