delEvnents

This commit is contained in:
龚皓 2024-11-26 16:09:50 +08:00
parent f0b6d900ac
commit 207913a253
2 changed files with 90 additions and 12 deletions

View File

@ -0,0 +1,59 @@
import {ref} from 'vue'
import { defineStore } from 'pinia';
// 定义回调类型
type TimerCallback = () => void;
export const useGlobalTimerStore = defineStore('globalTimer', () => {
const intervalId = ref<number | null>(null); // 定时器 ID
const refreshIntervalMs = ref(300000); // 默认刷新间隔5 分钟)
const registeredCallbacks = ref<TimerCallback[]>([]); // 注册的回调函数列表
// 注册回调
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,
};
});

View File

@ -19,10 +19,7 @@ class BoxApi {
private readonly apiAllCameras: string = "/camera/cameras/get_all"; private readonly apiAllCameras: string = "/camera/cameras/get_all";
private readonly superCamera: string = "/camera/cameras"; private readonly superCamera: string = "/camera/cameras";
private readonly superRule: string = "/rules"; private readonly superRule: string = "/rules";
private readonly getEventByIdUrl: string = "/event/events/retrieves"; private readonly superEvents: string = "/event/events";
private readonly loginConfig: object = { private readonly loginConfig: object = {
headers: { headers: {
@ -85,7 +82,7 @@ class BoxApi {
try { try {
const res = await this.axios.post(this.apiLogin, loginData, this.loginConfig) const res = await this.axios.post(this.apiLogin, loginData, this.loginConfig)
console.log(res) // console.log(res)
if (res.data.err.ec === 0) { if (res.data.err.ec === 0) {
this.token = res.data.ret.token; this.token = res.data.ret.token;
await this.updateCodemap(this.token); await this.updateCodemap(this.token);
@ -253,7 +250,7 @@ class BoxApi {
public async updateCamera(token: string | null = null, cameraId: number, jsonData: CameraData): Promise<any> { public async updateCamera(token: string | null = null, cameraId: number, jsonData: CameraData): Promise<any> {
const url = `${this.superCamera}/${cameraId}`; const url = `${this.superCamera}/${cameraId}`;
// const { rules, ...cameraData } = jsonData; // const { rules, ...cameraData } = jsonData;
console.log("接口接收的摄像设置>>>>>>>>>>>>>>", jsonData); // console.log("接口接收的摄像设置>>>>>>>>>>>>>>", jsonData);
try { try {
const newCamera = { const newCamera = {
@ -300,6 +297,26 @@ class BoxApi {
// throw error; // 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<any[]> { public async updateRule(token: string | null = null, rules: RuleData[]): Promise<any[]> {
@ -312,7 +329,7 @@ class BoxApi {
...rule, ...rule,
schedule: rule.schedule || {}, schedule: rule.schedule || {},
}; };
console.log("接口接收的规则设置>>>>>>>>>>>>>>", cleanedRule); // console.log("接口接收的规则设置>>>>>>>>>>>>>>", cleanedRule);
const res = await this.axios.patch(url, cleanedRule, this._authHeader(token)); const res = await this.axios.patch(url, cleanedRule, this._authHeader(token));
if (res.data.err.ec === 0) { if (res.data.err.ec === 0) {
results.push({ id: rule.id, success: true, data: res.data.ret }); 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<any> { public async getEventById(id: number, token: string | null = null): Promise<any> {
try { try {
const url = `${this.getEventByIdUrl}`; const url = `${this.superEvents}/retrieves`;
const params = { id }; const params = { id };
const res = await this.axios.get(url, { const res = await this.axios.get(url, {
...this._authHeader(token), ...this._authHeader(token),
@ -493,6 +510,8 @@ class BoxApi {
throw error; throw error;
} }
} }
// public async getOneEvent(token: string | null = null): Promise<any> { // public async getOneEvent(token: string | null = null): Promise<any> {
// try { // try {
// return await this.getEvents(1, 0, token); // return await this.getEvents(1, 0, token);