refactor(frontend): 重构前端第二版
This commit is contained in:
1 parent
2631f78b00
commit
4b562cfa82
205 files changed
+15399
-12015
No files matched your search
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* shared/composables/useGlobalMessageDialog.ts
|
||||
* 全局消息弹窗单例状态与展示控制。
|
||||
*/
|
||||
import { readonly, ref } from 'vue';
|
||||
|
||||
export type MessageLevel = 'success' | 'info' | 'warning' | 'error';
|
||||
|
||||
// 全局单例状态:整个应用共享一套消息弹框。
|
||||
const visible = ref(false);
|
||||
const level = ref<MessageLevel>('info');
|
||||
const title = ref('');
|
||||
const message = ref('');
|
||||
|
||||
const MAX_DIALOG_MESSAGE_LENGTH = 240;
|
||||
|
||||
const normalizeMessage = (raw: string) => {
|
||||
if (!raw) return '';
|
||||
return raw
|
||||
.replace(/\s+/g, ' ')
|
||||
.replace(/<[^>]*>/g, '')
|
||||
.trim();
|
||||
};
|
||||
|
||||
const formatMessageForDialog = (level: MessageLevel, raw: string) => {
|
||||
const normalized = normalizeMessage(raw);
|
||||
if (!normalized) return '';
|
||||
|
||||
// Error messages often include long backend stack/driver details.
|
||||
if (level === 'error' && normalized.length > MAX_DIALOG_MESSAGE_LENGTH) {
|
||||
return `${normalized.slice(0, MAX_DIALOG_MESSAGE_LENGTH)}...`;
|
||||
}
|
||||
return normalized;
|
||||
};
|
||||
|
||||
export const useGlobalMessageDialog = () => {
|
||||
const showMessageDialog = (
|
||||
nextLevel: MessageLevel,
|
||||
nextMessage: string,
|
||||
nextTitle = ''
|
||||
) => {
|
||||
level.value = nextLevel;
|
||||
title.value = nextTitle;
|
||||
message.value = formatMessageForDialog(nextLevel, nextMessage);
|
||||
visible.value = true;
|
||||
};
|
||||
|
||||
const closeMessageDialog = () => {
|
||||
visible.value = false;
|
||||
title.value = '';
|
||||
message.value = '';
|
||||
};
|
||||
|
||||
return {
|
||||
messageDialogVisible: visible,
|
||||
messageDialogLevel: readonly(level),
|
||||
messageDialogTitle: readonly(title),
|
||||
messageDialogContent: readonly(message),
|
||||
showMessageDialog,
|
||||
closeMessageDialog,
|
||||
};
|
||||
};
|
||||
Reference in new issue
Block a user