fix: 优化错误提示
This commit is contained in:
1 parent
d4f629106f
commit
3f61e92549
9 files changed
+122
-23
No files matched your search
@@ -84,7 +84,17 @@ export function useLoginAuth(deps: LoginDeps, options: LoginAuthOptions) {
|
|||||||
event?.stopPropagation();
|
event?.stopPropagation();
|
||||||
|
|
||||||
if (!loginUsername.value.trim()) {
|
if (!loginUsername.value.trim()) {
|
||||||
msg.warning(t('login.usernameRequired'));
|
msg.warning({
|
||||||
|
title: t('login.usernameRequired'),
|
||||||
|
message: t('login.usernameRequiredHint'),
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!loginPassword.value) {
|
||||||
|
msg.warning({
|
||||||
|
title: t('login.passwordRequired'),
|
||||||
|
message: t('login.passwordRequiredHint'),
|
||||||
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,7 +125,10 @@ export function useLoginAuth(deps: LoginDeps, options: LoginAuthOptions) {
|
|||||||
if (rawError === 'Database connection failed') errorText = t('login.dbConnectionFailed');
|
if (rawError === 'Database connection failed') errorText = t('login.dbConnectionFailed');
|
||||||
if (rawError === 'Internal server error') errorText = t('login.internalError');
|
if (rawError === 'Internal server error') errorText = t('login.internalError');
|
||||||
console.error(e);
|
console.error(e);
|
||||||
msg.error(errorText || t('login.requestFailed'));
|
msg.error({
|
||||||
|
title: t('login.failed'),
|
||||||
|
message: errorText || t('login.requestFailed'),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,12 @@ const messages = {
|
|||||||
"defaultMessage": "When clicked the OK button, this dialog will be closed after 1 second",
|
"defaultMessage": "When clicked the OK button, this dialog will be closed after 1 second",
|
||||||
"cancel": "Cancel",
|
"cancel": "Cancel",
|
||||||
"confirm": "OK"
|
"confirm": "OK"
|
||||||
|
},
|
||||||
|
"messageDialog": {
|
||||||
|
"success": "Success",
|
||||||
|
"info": "Notice",
|
||||||
|
"warning": "Warning",
|
||||||
|
"error": "Error"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"scheduleRuns": {
|
"scheduleRuns": {
|
||||||
@@ -135,6 +141,12 @@ const messages = {
|
|||||||
"defaultMessage": "点击“确定”按钮后,该对话框将在 1 秒后关闭",
|
"defaultMessage": "点击“确定”按钮后,该对话框将在 1 秒后关闭",
|
||||||
"cancel": "取消",
|
"cancel": "取消",
|
||||||
"confirm": "确定"
|
"confirm": "确定"
|
||||||
|
},
|
||||||
|
"messageDialog": {
|
||||||
|
"success": "操作成功",
|
||||||
|
"info": "提示",
|
||||||
|
"warning": "警告",
|
||||||
|
"error": "错误"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"scheduleRuns": {
|
"scheduleRuns": {
|
||||||
@@ -253,6 +265,12 @@ const messages = {
|
|||||||
"defaultMessage": "點擊「確定」按鈕後,此對話框將在 1 秒後關閉",
|
"defaultMessage": "點擊「確定」按鈕後,此對話框將在 1 秒後關閉",
|
||||||
"cancel": "取消",
|
"cancel": "取消",
|
||||||
"confirm": "確定"
|
"confirm": "確定"
|
||||||
|
},
|
||||||
|
"messageDialog": {
|
||||||
|
"success": "操作成功",
|
||||||
|
"info": "提示",
|
||||||
|
"warning": "警告",
|
||||||
|
"error": "錯誤"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"scheduleRuns": {
|
"scheduleRuns": {
|
||||||
@@ -371,6 +389,12 @@ const messages = {
|
|||||||
"defaultMessage": "เมื่อกดปุ่มตกลง กล่องโต้ตอบนี้จะปิดหลังจาก 1 วินาที",
|
"defaultMessage": "เมื่อกดปุ่มตกลง กล่องโต้ตอบนี้จะปิดหลังจาก 1 วินาที",
|
||||||
"cancel": "ยกเลิก",
|
"cancel": "ยกเลิก",
|
||||||
"confirm": "ตกลง"
|
"confirm": "ตกลง"
|
||||||
|
},
|
||||||
|
"messageDialog": {
|
||||||
|
"success": "สำเร็จ",
|
||||||
|
"info": "แจ้งเตือน",
|
||||||
|
"warning": "คำเตือน",
|
||||||
|
"error": "ข้อผิดพลาด"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"scheduleRuns": {
|
"scheduleRuns": {
|
||||||
|
|||||||
@@ -13,7 +13,10 @@
|
|||||||
},
|
},
|
||||||
"failed": "Login failed",
|
"failed": "Login failed",
|
||||||
"inputError": "Please enter username and password",
|
"inputError": "Please enter username and password",
|
||||||
"usernameRequired": "Please enter username",
|
"usernameRequired": "Username cannot be empty",
|
||||||
|
"usernameRequiredHint": "Please enter your username",
|
||||||
|
"passwordRequired": "Password cannot be empty",
|
||||||
|
"passwordRequiredHint": "Please enter your password",
|
||||||
"loginRequired": "Please login first",
|
"loginRequired": "Please login first",
|
||||||
"requestFailed": "Login request failed",
|
"requestFailed": "Login request failed",
|
||||||
"forgotPassword": "Forgot Password?",
|
"forgotPassword": "Forgot Password?",
|
||||||
|
|||||||
@@ -13,7 +13,10 @@
|
|||||||
},
|
},
|
||||||
"failed": "การเข้าสู่ระบบล้มเหลว",
|
"failed": "การเข้าสู่ระบบล้มเหลว",
|
||||||
"inputError": "กรุณากรอกชื่อผู้ใช้และรหัสผ่าน",
|
"inputError": "กรุณากรอกชื่อผู้ใช้และรหัสผ่าน",
|
||||||
"usernameRequired": "กรุณากรอกชื่อผู้ใช้",
|
"usernameRequired": "ชื่อผู้ใช้ต้องไม่ว่าง",
|
||||||
|
"usernameRequiredHint": "กรุณากรอกชื่อผู้ใช้",
|
||||||
|
"passwordRequired": "รหัสผ่านต้องไม่ว่าง",
|
||||||
|
"passwordRequiredHint": "กรุณากรอกรหัสผ่าน",
|
||||||
"loginRequired": "กรุณาเข้าสู่ระบบก่อนเริ่มการสนทนา",
|
"loginRequired": "กรุณาเข้าสู่ระบบก่อนเริ่มการสนทนา",
|
||||||
"requestFailed": "คำขอเข้าสู่ระบบล้มเหลว",
|
"requestFailed": "คำขอเข้าสู่ระบบล้มเหลว",
|
||||||
"forgotPassword": "ลืมรหัสผ่าน?",
|
"forgotPassword": "ลืมรหัสผ่าน?",
|
||||||
|
|||||||
@@ -13,7 +13,10 @@
|
|||||||
},
|
},
|
||||||
"failed": "登录失败",
|
"failed": "登录失败",
|
||||||
"inputError": "请输入用户名和密码",
|
"inputError": "请输入用户名和密码",
|
||||||
"usernameRequired": "请输入用户名",
|
"usernameRequired": "用户名不能为空",
|
||||||
|
"usernameRequiredHint": "请输入用户名",
|
||||||
|
"passwordRequired": "密码不能为空",
|
||||||
|
"passwordRequiredHint": "请输入密码",
|
||||||
"loginRequired": "请先登录",
|
"loginRequired": "请先登录",
|
||||||
"requestFailed": "登录请求失败",
|
"requestFailed": "登录请求失败",
|
||||||
"forgotPassword": "忘记密码?",
|
"forgotPassword": "忘记密码?",
|
||||||
|
|||||||
@@ -13,7 +13,10 @@
|
|||||||
},
|
},
|
||||||
"failed": "登錄失敗",
|
"failed": "登錄失敗",
|
||||||
"inputError": "請輸入用戶名和密碼",
|
"inputError": "請輸入用戶名和密碼",
|
||||||
"usernameRequired": "請輸入用戶名",
|
"usernameRequired": "用戶名不能為空",
|
||||||
|
"usernameRequiredHint": "請輸入用戶名",
|
||||||
|
"passwordRequired": "密碼不能為空",
|
||||||
|
"passwordRequiredHint": "請輸入密碼",
|
||||||
"loginRequired": "請先登錄後再進入對話",
|
"loginRequired": "請先登錄後再進入對話",
|
||||||
"requestFailed": "登錄請求失敗",
|
"requestFailed": "登錄請求失敗",
|
||||||
"forgotPassword": "忘記密碼?",
|
"forgotPassword": "忘記密碼?",
|
||||||
|
|||||||
@@ -2,9 +2,12 @@
|
|||||||
/**
|
/**
|
||||||
* shared/components/LevelMessageDialog.vue
|
* shared/components/LevelMessageDialog.vue
|
||||||
* 全局分级消息弹窗(info/success/warning/error)。
|
* 全局分级消息弹窗(info/success/warning/error)。
|
||||||
|
* 依赖:vue-i18n、lucide-vue-next。
|
||||||
|
* 逻辑:App.vue 挂载本组件;useGlobalMessageDialog 驱动 title(顶部摘要)与 message(正文)展示。
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { computed, onBeforeUnmount, watch } from 'vue';
|
import { computed, onBeforeUnmount, watch } from 'vue';
|
||||||
|
import { useI18n } from 'vue-i18n';
|
||||||
import { Check, Info, AlertTriangle, XCircle, X } from 'lucide-vue-next';
|
import { Check, Info, AlertTriangle, XCircle, X } from 'lucide-vue-next';
|
||||||
|
|
||||||
type MessageLevel = 'success' | 'info' | 'warning' | 'error';
|
type MessageLevel = 'success' | 'info' | 'warning' | 'error';
|
||||||
@@ -47,16 +50,18 @@ const ICON_MAP: Record<MessageLevel, typeof Check> = {
|
|||||||
error: XCircle,
|
error: XCircle,
|
||||||
};
|
};
|
||||||
|
|
||||||
const DEFAULT_TITLE_MAP: Record<MessageLevel, string> = {
|
const { t } = useI18n();
|
||||||
success: 'Success',
|
|
||||||
info: 'Info',
|
const DEFAULT_TITLE_KEY_MAP: Record<MessageLevel, string> = {
|
||||||
warning: 'Warning',
|
success: 'common.messageDialog.success',
|
||||||
error: 'Error',
|
info: 'common.messageDialog.info',
|
||||||
|
warning: 'common.messageDialog.warning',
|
||||||
|
error: 'common.messageDialog.error',
|
||||||
};
|
};
|
||||||
|
|
||||||
const panelClass = computed(() => ALERT_CLASS[props.level]);
|
const panelClass = computed(() => ALERT_CLASS[props.level]);
|
||||||
const resolvedIcon = computed(() => ICON_MAP[props.level]);
|
const resolvedIcon = computed(() => ICON_MAP[props.level]);
|
||||||
const resolvedTitle = computed(() => props.title || DEFAULT_TITLE_MAP[props.level]);
|
const resolvedTitle = computed(() => props.title || t(DEFAULT_TITLE_KEY_MAP[props.level]));
|
||||||
const resolvedMessage = computed(() => props.message || '');
|
const resolvedMessage = computed(() => props.message || '');
|
||||||
let autoCloseTimer: ReturnType<typeof setTimeout> | null = null;
|
let autoCloseTimer: ReturnType<typeof setTimeout> | null = null;
|
||||||
|
|
||||||
@@ -139,7 +144,10 @@ onBeforeUnmount(() => {
|
|||||||
<h3 class="text-base font-semibold leading-6">
|
<h3 class="text-base font-semibold leading-6">
|
||||||
{{ resolvedTitle }}
|
{{ resolvedTitle }}
|
||||||
</h3>
|
</h3>
|
||||||
<p class="level-dialog-message mt-3 text-sm leading-7 whitespace-pre-line break-words opacity-90">
|
<p
|
||||||
|
v-if="resolvedMessage"
|
||||||
|
class="level-dialog-message mt-3 text-sm leading-7 whitespace-pre-line break-words opacity-90"
|
||||||
|
>
|
||||||
{{ resolvedMessage }}
|
{{ resolvedMessage }}
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
/**
|
/**
|
||||||
* shared/composables/useGlobalMessageDialog.ts
|
* shared/composables/useGlobalMessageDialog.ts
|
||||||
* 全局消息弹窗单例状态与展示控制。
|
* 全局消息弹窗单例状态与展示控制。
|
||||||
|
* 依赖:vue ref/readonly。
|
||||||
|
* 逻辑:App.vue 绑定本 composable 状态至 LevelMessageDialog;业务层经 useMessage 调用。
|
||||||
*/
|
*/
|
||||||
import { readonly, ref } from 'vue';
|
import { readonly, ref } from 'vue';
|
||||||
|
|
||||||
@@ -34,6 +36,13 @@ const formatMessageForDialog = (level: MessageLevel, raw: string) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const useGlobalMessageDialog = () => {
|
export const useGlobalMessageDialog = () => {
|
||||||
|
/**
|
||||||
|
* 打开全局分级消息弹窗。
|
||||||
|
* 核心逻辑:写入 level/title/message 单例状态;title 为顶部摘要,message 为正文。
|
||||||
|
* @param nextLevel - 消息级别
|
||||||
|
* @param nextMessage - 正文内容
|
||||||
|
* @param nextTitle - 顶部标题;为空时由 LevelMessageDialog 按级别取 i18n 默认文案
|
||||||
|
*/
|
||||||
const showMessageDialog = (
|
const showMessageDialog = (
|
||||||
nextLevel: MessageLevel,
|
nextLevel: MessageLevel,
|
||||||
nextMessage: string,
|
nextMessage: string,
|
||||||
|
|||||||
@@ -1,33 +1,66 @@
|
|||||||
/**
|
/**
|
||||||
* shared/composables/useMessage.ts
|
* shared/composables/useMessage.ts
|
||||||
* 业务层消息助手,封装全局 LevelMessageDialog 调用。
|
* 业务层消息助手,封装全局 LevelMessageDialog 调用。
|
||||||
|
* 依赖:useGlobalMessageDialog。
|
||||||
|
* 逻辑:各业务 composable 通过本模块弹出 info/success/warning/error 提示。
|
||||||
*/
|
*/
|
||||||
import { useGlobalMessageDialog } from './useGlobalMessageDialog';
|
import { useGlobalMessageDialog } from './useGlobalMessageDialog';
|
||||||
|
|
||||||
type MessageTitle = string | undefined;
|
export type MessageTitle = string | undefined;
|
||||||
|
|
||||||
|
/** 顶部标题 + 正文内容的结构化入参 */
|
||||||
|
export type MessagePayload = {
|
||||||
|
title: string;
|
||||||
|
message: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type MessageInput = string | MessagePayload;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将字符串或结构化入参统一为 title / message。
|
||||||
|
* 核心逻辑:对象形式时 title 为顶部摘要、message 为正文;字符串形式时 message 为正文、title 取第二参数。
|
||||||
|
* @param input - 正文或 { title, message }
|
||||||
|
* @param title - 字符串入参时的可选顶部标题
|
||||||
|
* @returns 归一化后的 title 与 message
|
||||||
|
*/
|
||||||
|
const resolveMessageInput = (input: MessageInput, title?: MessageTitle) => {
|
||||||
|
if (typeof input === 'object') {
|
||||||
|
return { title: input.title, message: input.message };
|
||||||
|
}
|
||||||
|
return { title: title ?? '', message: input };
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 业务层消息助手:
|
* 业务层消息助手:
|
||||||
* - 统一复用全局 LevelMessageDialog
|
* - 统一复用全局 LevelMessageDialog
|
||||||
* - 提供更短的方法名,减少重复传 level 字符串
|
* - msg.warning({ title: '用户名不能为空', message: '请输入用户名' }):双行
|
||||||
|
* - msg.error('用户不存在', '登录失败') 或 msg.error({ title: '登录失败', message: '用户不存在' })
|
||||||
*/
|
*/
|
||||||
export const useMessage = () => {
|
export const useMessage = () => {
|
||||||
const { showMessageDialog } = useGlobalMessageDialog();
|
const { showMessageDialog } = useGlobalMessageDialog();
|
||||||
|
|
||||||
const info = (message: string, title?: MessageTitle) =>
|
const info = (input: MessageInput, title?: MessageTitle) => {
|
||||||
showMessageDialog('info', message, title);
|
const resolved = resolveMessageInput(input, title);
|
||||||
|
showMessageDialog('info', resolved.message, resolved.title);
|
||||||
|
};
|
||||||
|
|
||||||
const success = (message: string, title?: MessageTitle) =>
|
const success = (input: MessageInput, title?: MessageTitle) => {
|
||||||
showMessageDialog('success', message, title);
|
const resolved = resolveMessageInput(input, title);
|
||||||
|
showMessageDialog('success', resolved.message, resolved.title);
|
||||||
|
};
|
||||||
|
|
||||||
const warning = (message: string, title?: MessageTitle) =>
|
const warning = (input: MessageInput, title?: MessageTitle) => {
|
||||||
showMessageDialog('warning', message, title);
|
const resolved = resolveMessageInput(input, title);
|
||||||
|
showMessageDialog('warning', resolved.message, resolved.title);
|
||||||
|
};
|
||||||
|
|
||||||
// 兼容常见缩写调用:msg.warn(...)
|
// 兼容常见缩写调用:msg.warn(...)
|
||||||
const warn = warning;
|
const warn = warning;
|
||||||
|
|
||||||
const error = (message: string, title?: MessageTitle) =>
|
const error = (input: MessageInput, title?: MessageTitle) => {
|
||||||
showMessageDialog('error', message, title);
|
const resolved = resolveMessageInput(input, title);
|
||||||
|
showMessageDialog('error', resolved.message, resolved.title);
|
||||||
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
info,
|
info,
|
||||||
|
|||||||
Reference in new issue
Block a user