2024-11-21 13:20:45 +08:00
|
|
|
|
import { i18n } from '@/i18n';
|
2026-05-19 21:25:28 +08:00
|
|
|
|
import { Msg } from '@/hooks/useI18n';
|
2024-11-21 13:20:45 +08:00
|
|
|
|
|
2021-01-08 15:37:32 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 不符合业务断言错误
|
|
|
|
|
|
*/
|
|
|
|
|
|
class AssertError extends Error {
|
|
|
|
|
|
constructor(message: string) {
|
2026-05-19 21:25:28 +08:00
|
|
|
|
Msg.error(message);
|
2021-07-28 18:03:19 +08:00
|
|
|
|
super(message);
|
2021-01-08 15:37:32 +08:00
|
|
|
|
// 错误类名
|
2023-07-06 20:59:22 +08:00
|
|
|
|
this.name = 'AssertError';
|
2021-01-08 15:37:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-03 11:29:33 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 断言表达式为true
|
2023-07-06 20:59:22 +08:00
|
|
|
|
*
|
2022-03-03 11:29:33 +08:00
|
|
|
|
* @param condition 条件表达式
|
2025-05-20 21:04:47 +08:00
|
|
|
|
* @param msgOrI18nKey 错误消息 或者 i18n key
|
2022-03-03 11:29:33 +08:00
|
|
|
|
*/
|
2025-05-20 21:04:47 +08:00
|
|
|
|
export function isTrue(condition: boolean, msgOrI18nKey: string) {
|
2022-03-03 11:29:33 +08:00
|
|
|
|
if (!condition) {
|
2025-05-20 21:04:47 +08:00
|
|
|
|
throw new AssertError(i18n.global.t(msgOrI18nKey));
|
2022-03-03 11:29:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 断言不能为空值,即null,0,''等
|
2023-07-06 20:59:22 +08:00
|
|
|
|
*
|
2022-03-03 11:29:33 +08:00
|
|
|
|
* @param obj 对象1
|
|
|
|
|
|
* @param msg 错误消息
|
|
|
|
|
|
*/
|
2023-07-06 20:59:22 +08:00
|
|
|
|
export function notBlank(obj: any, msg: string) {
|
2026-04-27 05:13:40 +00:00
|
|
|
|
if (obj == null || obj == undefined || !obj) {
|
2024-11-26 17:32:44 +08:00
|
|
|
|
throw new AssertError(msg);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (Array.isArray(obj) && obj.length == 0) {
|
|
|
|
|
|
throw new AssertError(msg);
|
|
|
|
|
|
}
|
2022-03-03 11:29:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-21 13:20:45 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 断言不能为空值,即null,0,''等
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param obj 对象
|
|
|
|
|
|
* @param field 字段(支持i18n msgKey)
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function notBlankI18n(obj: any, field: string) {
|
|
|
|
|
|
notBlank(obj, i18n.global.t('common.fieldNotEmpty', { field: i18n.global.t(field) }));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-03 11:29:33 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 断言两对象相等
|
2023-07-06 20:59:22 +08:00
|
|
|
|
*
|
2022-03-03 11:29:33 +08:00
|
|
|
|
* @param obj1 对象1
|
|
|
|
|
|
* @param obj2 对象2
|
|
|
|
|
|
* @param msg 错误消息
|
|
|
|
|
|
*/
|
2023-07-06 20:59:22 +08:00
|
|
|
|
export function isEquals(obj1: any, obj2: any, msg: string) {
|
2022-03-03 11:29:33 +08:00
|
|
|
|
isTrue(obj1 === obj2, msg);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-08 15:37:32 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 断言对象不为null或undefiend
|
2023-07-06 20:59:22 +08:00
|
|
|
|
*
|
2021-01-08 15:37:32 +08:00
|
|
|
|
* @param obj 对象
|
|
|
|
|
|
* @param msg 错误提示
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function notNull(obj: any, msg: string) {
|
|
|
|
|
|
if (obj == null || obj == undefined) {
|
2023-07-06 20:59:22 +08:00
|
|
|
|
throw new AssertError(msg);
|
2021-01-08 15:37:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-07-06 20:59:22 +08:00
|
|
|
|
* 断言字符串不能为空
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param str 字符串
|
|
|
|
|
|
* @param msg 错误提示
|
|
|
|
|
|
*/
|
2021-01-08 15:37:32 +08:00
|
|
|
|
export function notEmpty(str: string, msg: string) {
|
|
|
|
|
|
if (str == null || str == undefined || str == '') {
|
|
|
|
|
|
throw new AssertError(msg);
|
|
|
|
|
|
}
|
2023-07-06 20:59:22 +08:00
|
|
|
|
}
|
2024-11-21 13:20:45 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 断言字符串不能为空
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param str 字符串
|
|
|
|
|
|
* @param field 字段(支持i18n msgKey)
|
|
|
|
|
|
*/
|
|
|
|
|
|
export function notEmptyI18n(str: string, field: string) {
|
|
|
|
|
|
notEmpty(str, i18n.global.t('common.fieldNotEmpty', { field: i18n.global.t(field) }));
|
|
|
|
|
|
}
|