refactor: 重构项目结构

- 优化 `车站-设备-告警`  轮询机制
- 改进设备卡片的布局
- 支持修改设备
- 告警轮询中获取完整告警数据
- 车站告警详情支持导出完整的 `今日告警列表`
- 支持将状态持久化到 `IndexedDB`
- 新增轮询控制 (调试模式)
- 新增离线开发模式 (调试模式)
- 新增 `IndexedDB` 数据控制 (调试模式)
This commit is contained in:
yangsy
2025-12-11 13:42:22 +08:00
commit 37781216b2
278 changed files with 17988 additions and 0 deletions

142
src/stores/user.ts Normal file
View File

@@ -0,0 +1,142 @@
import { useStationStore } from './station';
import { usePollingStore } from './polling';
import { userClient, type LoginParams, type LoginResult, type Station } from '@/apis';
import type { Result } from '@/types';
import { AesEncryption, getAppEnvConfig } from '@/utils';
import axios, { AxiosError } from 'axios';
import dayjs from 'dayjs';
import { defineStore } from 'pinia';
import { computed, ref } from 'vue';
import { NDM_USER_STORE_ID } from '@/constants';
const getHeaders = () => {
const { lampAuthorization, lampClientId, lampClientSecret } = getAppEnvConfig();
const newAuthorization = window.btoa(`${lampClientId}:${lampClientSecret}`);
const authorization = lampAuthorization.trim() !== '' ? lampAuthorization : newAuthorization;
return {
'content-type': 'application/json',
'accept-language': 'zh-CN,zh;q=0.9',
accept: 'application/json, text/plain, */*',
ApplicationId: '1',
TenantId: '1',
Authorization: authorization,
};
};
const aesEncryption = new AesEncryption();
export const useUserStore = defineStore(
NDM_USER_STORE_ID,
() => {
const userLoginResult = ref<LoginResult | null>(null);
const userInfo = ref<any>(null);
const lampLoginResultRecord = ref<Record<string, LoginResult> | null>(null);
const isLamp = computed(() => userInfo.value?.['id'] === '2');
const resetStore = () => {
userLoginResult.value = null;
userInfo.value = null;
lampLoginResultRecord.value = null;
};
const userLogin = async (loginParams: LoginParams) => {
const { username, password, code, key, grantType } = loginParams;
const body = {
username: aesEncryption.encryptByAES(username),
password: aesEncryption.encryptByAES(password),
code,
key,
grantType,
};
const { data: respData } = await axios.post<Result<LoginResult>>(`/api/oauth/anyTenant/login`, body, { headers: getHeaders() });
if (!respData.isSuccess) {
console.error(respData);
window.$dialog.destroyAll();
window.$dialog.error({
closable: false,
maskClosable: false,
title: '错误提示',
content: respData.msg,
positiveText: '确认',
onPositiveClick: () => {
window.$message.destroyAll();
},
});
throw new AxiosError(respData.msg, `${respData.code}`);
} else {
userLoginResult.value = respData.data;
}
};
const userLogout = async () => {
const [err] = await userClient.post(`/api/oauth/anyUser/logout`, { token: userLoginResult.value?.token });
if (err) throw err;
resetStore();
};
const userGetInfo = async (options?: { signal?: AbortSignal }) => {
const { signal } = options ?? {};
const [err, info] = await userClient.get<any>(`/api/oauth/anyone/getUserInfoById`, { signal });
if (err || !info) {
throw err;
}
userInfo.value = info;
};
const lampLogin = async (stationCode: Station['code']) => {
const { data: accountRecord } = await axios.get<Record<string, { username: string; password: string }>>(`/minio/ndm/ndm-accounts.json?_t=${dayjs().unix()}`);
const body = {
username: aesEncryption.encryptByAES(accountRecord[stationCode]?.username ?? ''),
password: aesEncryption.encryptByAES(accountRecord[stationCode]?.password ?? ''),
grantType: 'PASSWORD',
};
const { data: respData } = await axios.post<Result<LoginResult>>(`/${stationCode}/api/oauth/anyTenant/login`, body, { headers: getHeaders() });
// 如果登录返回失败,需要提示用户检查用户名和密码配置,并全局停止轮询
if (!respData.isSuccess) {
console.error(respData);
const stationStore = useStationStore();
const stationName = stationStore.stations.find((station) => station.code === stationCode)?.name ?? '';
window.$dialog.destroyAll();
window.$dialog.error({
closable: false,
maskClosable: false,
draggable: true,
title: `${stationName}登录失败`,
content: `请检查该车站的用户名和密码配置,并在确认无误后刷新页面!`,
positiveText: '刷新',
onPositiveClick: () => {
window.$dialog.destroyAll();
window.location.reload();
},
});
// 登录失败时,需要全局停止轮询
const pollingStore = usePollingStore();
pollingStore.stopPolling();
throw new AxiosError(respData.msg, `${respData.code}`);
} else {
if (lampLoginResultRecord.value === null) {
lampLoginResultRecord.value = {};
}
lampLoginResultRecord.value[stationCode] = respData.data;
}
};
return {
userLoginResult,
userInfo,
lampLoginResultRecord,
isLamp,
resetStore,
userLogin,
userLogout,
userGetInfo,
lampLogin,
};
},
{
persist: true,
},
);