From 207913a25302d5f983b97c67ef8a94a10d9c0ecf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=9A=E7=9A=93?= <1736436516@qq.com> Date: Tue, 26 Nov 2024 16:09:50 +0800 Subject: [PATCH] delEvnents --- src/stores/globalTimerStore.ts | 59 ++++++++++++++++++++++++++++++++++ src/utils/boxApi.ts | 43 ++++++++++++++++++------- 2 files changed, 90 insertions(+), 12 deletions(-) create mode 100644 src/stores/globalTimerStore.ts diff --git a/src/stores/globalTimerStore.ts b/src/stores/globalTimerStore.ts new file mode 100644 index 0000000..c0510d1 --- /dev/null +++ b/src/stores/globalTimerStore.ts @@ -0,0 +1,59 @@ +import {ref} from 'vue' +import { defineStore } from 'pinia'; + +// 定义回调类型 +type TimerCallback = () => void; + +export const useGlobalTimerStore = defineStore('globalTimer', () => { + const intervalId = ref(null); // 定时器 ID + const refreshIntervalMs = ref(300000); // 默认刷新间隔(5 分钟) + const registeredCallbacks = ref([]); // 注册的回调函数列表 + + // 注册回调 + const registerCallback = (callback: TimerCallback) => { + if (typeof callback === 'function' && !registeredCallbacks.value.includes(callback)) { + registeredCallbacks.value.push(callback); + } + }; + + // 注销回调 + const unregisterCallback = (callback: TimerCallback) => { + const index = registeredCallbacks.value.indexOf(callback); + if (index !== -1) { + registeredCallbacks.value.splice(index, 1); + } + }; + + // 启动定时器 + const startTimer = () => { + if (!intervalId.value) { + intervalId.value = window.setInterval(() => { + registeredCallbacks.value.forEach((callback) => callback()); + }, refreshIntervalMs.value); + } + }; + + // 停止定时器 + const stopTimer = () => { + if (intervalId.value) { + clearInterval(intervalId.value); + intervalId.value = null; + } + }; + + // 设置定时器间隔 + const setRefreshInterval = (interval: number) => { + refreshIntervalMs.value = interval; + stopTimer(); + startTimer(); + }; + + return { + refreshIntervalMs, + registerCallback, + unregisterCallback, + startTimer, + stopTimer, + setRefreshInterval, + }; +}); diff --git a/src/utils/boxApi.ts b/src/utils/boxApi.ts index abca082..e1885a4 100644 --- a/src/utils/boxApi.ts +++ b/src/utils/boxApi.ts @@ -19,10 +19,7 @@ class BoxApi { private readonly apiAllCameras: string = "/camera/cameras/get_all"; private readonly superCamera: string = "/camera/cameras"; private readonly superRule: string = "/rules"; - private readonly getEventByIdUrl: string = "/event/events/retrieves"; - - - + private readonly superEvents: string = "/event/events"; private readonly loginConfig: object = { headers: { @@ -85,7 +82,7 @@ class BoxApi { try { const res = await this.axios.post(this.apiLogin, loginData, this.loginConfig) - console.log(res) + // console.log(res) if (res.data.err.ec === 0) { this.token = res.data.ret.token; await this.updateCodemap(this.token); @@ -253,7 +250,7 @@ class BoxApi { public async updateCamera(token: string | null = null, cameraId: number, jsonData: CameraData): Promise { const url = `${this.superCamera}/${cameraId}`; // const { rules, ...cameraData } = jsonData; - console.log("接口接收的摄像设置>>>>>>>>>>>>>>", jsonData); + // console.log("接口接收的摄像设置>>>>>>>>>>>>>>", jsonData); try { const newCamera = { @@ -277,7 +274,7 @@ class BoxApi { // jsonData: CameraData // ): Promise { // const url = `${this.superCamera}/${cameraId}`; - + // // 确保 mode 为小写 // const cameraData = { // name: jsonData.name, @@ -285,9 +282,9 @@ class BoxApi { // ? jsonData.mode.toLowerCase() // : undefined, // 非法值转为 undefined // }; - + // console.log("接口接收的摄像头更新数据:", cameraData); - + // try { // const res = await this.axios.patch(url, cameraData, this._authHeader(token)); // if (res.data.err.ec === 0) { @@ -300,7 +297,27 @@ class BoxApi { // throw error; // } // } - + public async delEvents(token: string | null = null, eventIds: number[]): Promise<{ id: number, success: boolean, message?: string }[]> { + const results: { id: number, success: boolean, message?: string }[] = []; + + for (const eventId of eventIds) { + const url = `${this.superEvents}/${eventId}`; + try { + await this.axios.delete(url, this._authHeader(token)); + results.push({ id: eventId, success: true }); + } catch (error: any) { + results.push({ + id: eventId, + success: false, + message: error.response?.data?.err?.dm || error.message + }); + } + } + + return results; + } + + public async updateRule(token: string | null = null, rules: RuleData[]): Promise { const results: any[] = []; @@ -312,7 +329,7 @@ class BoxApi { ...rule, schedule: rule.schedule || {}, }; - console.log("接口接收的规则设置>>>>>>>>>>>>>>", cleanedRule); + // console.log("接口接收的规则设置>>>>>>>>>>>>>>", cleanedRule); const res = await this.axios.patch(url, cleanedRule, this._authHeader(token)); if (res.data.err.ec === 0) { results.push({ id: rule.id, success: true, data: res.data.ret }); @@ -476,7 +493,7 @@ class BoxApi { public async getEventById(id: number, token: string | null = null): Promise { try { - const url = `${this.getEventByIdUrl}`; + const url = `${this.superEvents}/retrieves`; const params = { id }; const res = await this.axios.get(url, { ...this._authHeader(token), @@ -493,6 +510,8 @@ class BoxApi { throw error; } } + + // public async getOneEvent(token: string | null = null): Promise { // try { // return await this.getEvents(1, 0, token);