a2bei4-utils 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/a2bei4.utils.cjs.js +415 -110
- package/dist/a2bei4.utils.cjs.js.map +1 -1
- package/dist/a2bei4.utils.cjs.min.js +1 -1
- package/dist/a2bei4.utils.cjs.min.js.map +1 -1
- package/dist/a2bei4.utils.esm.js +408 -107
- package/dist/a2bei4.utils.esm.js.map +1 -1
- package/dist/a2bei4.utils.esm.min.js +1 -1
- package/dist/a2bei4.utils.esm.min.js.map +1 -1
- package/dist/a2bei4.utils.umd.js +415 -110
- package/dist/a2bei4.utils.umd.js.map +1 -1
- package/dist/a2bei4.utils.umd.min.js +1 -1
- package/dist/a2bei4.utils.umd.min.js.map +1 -1
- package/dist/date.cjs +191 -100
- package/dist/date.cjs.map +1 -1
- package/dist/date.js +186 -97
- package/dist/date.js.map +1 -1
- package/dist/menu.cjs +202 -0
- package/dist/menu.cjs.map +1 -0
- package/dist/menu.js +200 -0
- package/dist/menu.js.map +1 -0
- package/dist/tree.cjs +21 -6
- package/dist/tree.cjs.map +1 -1
- package/dist/tree.js +21 -7
- package/dist/tree.js.map +1 -1
- package/package.json +12 -7
- package/types/date.d.ts +186 -47
- package/types/index.d.ts +259 -48
- package/types/menu.d.ts +62 -0
- package/types/tree.d.ts +14 -2
package/dist/a2bei4.utils.cjs.js
CHANGED
|
@@ -799,133 +799,222 @@ function randomDateInRange(date1, date2) {
|
|
|
799
799
|
return new Date(v1 + Math.floor(Math.random() * (v2 - v1 + 1)));
|
|
800
800
|
}
|
|
801
801
|
|
|
802
|
+
//#region 持续时间相关
|
|
803
|
+
|
|
804
|
+
/**
|
|
805
|
+
* 时间持续对象(完整版本,包含年月日时分秒毫秒)
|
|
806
|
+
* @typedef {Object} DurationObject
|
|
807
|
+
* @property {number} years - 年数
|
|
808
|
+
* @property {number} months - 月数(0-11)
|
|
809
|
+
* @property {number} days - 天数(0-29,取决于 monthDays)
|
|
810
|
+
* @property {number} hours - 小时数(0-23)
|
|
811
|
+
* @property {number} minutes - 分钟数(0-59)
|
|
812
|
+
* @property {number} seconds - 秒数(0-59)
|
|
813
|
+
* @property {number} milliseconds - 毫秒数(0-999)
|
|
814
|
+
*/
|
|
815
|
+
|
|
802
816
|
/**
|
|
803
|
-
*
|
|
817
|
+
* 时间持续对象(最大单位为天)
|
|
818
|
+
* @typedef {Object} DurationMaxDayObject
|
|
819
|
+
* @property {number} days - 天数
|
|
820
|
+
* @property {number} hours - 小时数(0-23)
|
|
821
|
+
* @property {number} minutes - 分钟数(0-59)
|
|
822
|
+
* @property {number} seconds - 秒数(0-59)
|
|
823
|
+
* @property {number} milliseconds - 毫秒数(0-999)
|
|
824
|
+
*/
|
|
825
|
+
|
|
826
|
+
/**
|
|
827
|
+
* 时间持续对象(最大单位为小时)
|
|
828
|
+
* @typedef {Object} DurationMaxHourObject
|
|
829
|
+
* @property {number} hours - 小时数
|
|
830
|
+
* @property {number} minutes - 分钟数(0-59)
|
|
831
|
+
* @property {number} seconds - 秒数(0-59)
|
|
832
|
+
* @property {number} milliseconds - 毫秒数(0-999)
|
|
833
|
+
*/
|
|
834
|
+
|
|
835
|
+
/**
|
|
836
|
+
* 将毫秒转换为时间持续对象。
|
|
804
837
|
*
|
|
805
|
-
* @param {
|
|
806
|
-
* @param {
|
|
807
|
-
* @
|
|
838
|
+
* @param {number} milliseconds - 毫秒数(非负整数)
|
|
839
|
+
* @param {Object} [options] - 选项对象。
|
|
840
|
+
* @param {number} [options.yearDays=365] - 一年的天数。
|
|
841
|
+
* @param {number} [options.monthDays=30] - 一月的天数。
|
|
842
|
+
* @returns {DurationObject} 时间持续对象
|
|
843
|
+
* @throws {TypeError} 当 milliseconds 不是有效数字
|
|
844
|
+
* @throws {RangeError} 当 milliseconds 为负数
|
|
845
|
+
* @throws {RangeError} 当 yearDays 或 monthDays 不是正整数
|
|
846
|
+
*
|
|
847
|
+
* @example
|
|
848
|
+
* // 基本用法
|
|
849
|
+
* millisecond2Duration(42070000500);
|
|
850
|
+
* // 返回: { years: 1, months: 4, days: 1, hours: 22, minutes: 6, seconds: 40, milliseconds: 500 }
|
|
808
851
|
*/
|
|
809
|
-
function
|
|
810
|
-
//
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
852
|
+
function millisecond2Duration(milliseconds, options = { yearDays: 365, monthDays: 30 }) {
|
|
853
|
+
// 参数验证
|
|
854
|
+
if (typeof milliseconds !== "number" || isNaN(milliseconds)) {
|
|
855
|
+
throw new TypeError("milliseconds must be a valid number");
|
|
856
|
+
}
|
|
857
|
+
if (milliseconds < 0) {
|
|
858
|
+
throw new RangeError("milliseconds must be a non-negative number");
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
// 默认选项
|
|
862
|
+
const { yearDays = 365, monthDays = 30 } = options;
|
|
863
|
+
|
|
864
|
+
// 选项验证
|
|
865
|
+
if (!Number.isInteger(yearDays) || yearDays <= 0) {
|
|
866
|
+
throw new RangeError("yearDays must be a positive integer");
|
|
867
|
+
}
|
|
868
|
+
if (!Number.isInteger(monthDays) || monthDays <= 0) {
|
|
869
|
+
throw new RangeError("monthDays must be a positive integer");
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
const totalMilliseconds = Math.floor(milliseconds);
|
|
873
|
+
const ms = totalMilliseconds % 1000;
|
|
874
|
+
const diffSeconds = Math.floor(totalMilliseconds / 1000);
|
|
875
|
+
|
|
818
876
|
const seconds = diffSeconds % 60;
|
|
877
|
+
const minutes = Math.floor(diffSeconds / 60) % 60;
|
|
878
|
+
const hours = Math.floor(diffSeconds / 3600) % 24;
|
|
819
879
|
|
|
820
|
-
|
|
880
|
+
// 计算年、月、日
|
|
881
|
+
const totalDays = Math.floor(diffSeconds / 3600 / 24);
|
|
882
|
+
const years = Math.floor(totalDays / yearDays);
|
|
883
|
+
const remainingDays = totalDays % yearDays;
|
|
884
|
+
const months = Math.floor(remainingDays / monthDays);
|
|
885
|
+
const days = remainingDays % monthDays;
|
|
821
886
|
|
|
822
|
-
return {
|
|
823
|
-
days,
|
|
824
|
-
hours: padZero(hours),
|
|
825
|
-
minutes: padZero(minutes),
|
|
826
|
-
seconds: padZero(seconds)
|
|
827
|
-
};
|
|
887
|
+
return { years, months, days, hours, minutes, seconds, milliseconds: ms };
|
|
828
888
|
}
|
|
829
889
|
|
|
830
890
|
/**
|
|
831
|
-
*
|
|
832
|
-
*
|
|
833
|
-
*
|
|
834
|
-
* @
|
|
835
|
-
* @
|
|
836
|
-
* @
|
|
837
|
-
*
|
|
838
|
-
*
|
|
839
|
-
* @param {boolean} [options.showZero] - 是否强制显示 0 秒
|
|
840
|
-
* @returns {string} 拼接后的时长文本,如“1天 02小时 30分钟”
|
|
841
|
-
* @throws {TypeError} 当 totalSeconds 为非数字或负数时抛出
|
|
891
|
+
* 将毫秒转换为时间持续对象(最大单位为天)。
|
|
892
|
+
* @param {number} milliseconds - 毫秒数(非负整数)
|
|
893
|
+
* @returns {DurationMaxDayObject} 包含天、小时、分钟、秒、毫秒的时间持续对象
|
|
894
|
+
* @throws {TypeError} 当 milliseconds 不是有效数字时抛出
|
|
895
|
+
* @throws {RangeError} 当 milliseconds 为负数时抛出
|
|
896
|
+
* @example
|
|
897
|
+
* // 返回 { days: 486, hours: 22, minutes: 6, seconds: 40, milliseconds: 500 }
|
|
898
|
+
* millisecond2DurationMaxDay(42070000500);
|
|
842
899
|
*/
|
|
843
|
-
function
|
|
844
|
-
if (typeof
|
|
845
|
-
throw new TypeError("
|
|
900
|
+
function millisecond2DurationMaxDay(milliseconds) {
|
|
901
|
+
if (typeof milliseconds !== "number" || isNaN(milliseconds)) {
|
|
902
|
+
throw new TypeError("milliseconds must be a valid number");
|
|
903
|
+
}
|
|
904
|
+
if (milliseconds < 0) {
|
|
905
|
+
throw new RangeError("milliseconds must be a non-negative number");
|
|
846
906
|
}
|
|
847
907
|
|
|
848
|
-
|
|
849
|
-
const
|
|
850
|
-
|
|
851
|
-
month: "月",
|
|
852
|
-
day: "天",
|
|
853
|
-
hour: "小时",
|
|
854
|
-
minute: "分钟",
|
|
855
|
-
second: "秒"
|
|
856
|
-
};
|
|
908
|
+
const totalMilliseconds = Math.floor(milliseconds);
|
|
909
|
+
const ms = totalMilliseconds % 1000;
|
|
910
|
+
const diffSeconds = Math.floor(totalMilliseconds / 1000);
|
|
857
911
|
|
|
858
|
-
|
|
859
|
-
const
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
{ key: "second", seconds: 1 }
|
|
866
|
-
];
|
|
867
|
-
|
|
868
|
-
// 3. 合并用户自定义文本
|
|
869
|
-
const labels = Object.assign({}, DEFAULT_LABELS, options.labels);
|
|
870
|
-
|
|
871
|
-
// 4. 根据 maxUnit / minUnit 截取
|
|
872
|
-
let start = 0,
|
|
873
|
-
end = UNIT_TABLE.length;
|
|
874
|
-
if (options.maxUnit) {
|
|
875
|
-
const idx = UNIT_TABLE.findIndex((u) => u.key === options.maxUnit);
|
|
876
|
-
if (idx !== -1) start = idx;
|
|
877
|
-
}
|
|
878
|
-
if (options.minUnit) {
|
|
879
|
-
const idx = UNIT_TABLE.findIndex((u) => u.key === options.minUnit);
|
|
880
|
-
if (idx !== -1) end = idx + 1;
|
|
881
|
-
}
|
|
882
|
-
const units = UNIT_TABLE.slice(start, end);
|
|
883
|
-
if (!units.length) units.push(UNIT_TABLE[UNIT_TABLE.length - 1]); // 保底秒
|
|
884
|
-
|
|
885
|
-
// 5. 逐级计算
|
|
886
|
-
let rest = Math.floor(totalSeconds);
|
|
887
|
-
const parts = [];
|
|
888
|
-
|
|
889
|
-
for (const { key, seconds } of units) {
|
|
890
|
-
const val = Math.floor(rest / seconds);
|
|
891
|
-
rest %= seconds;
|
|
892
|
-
|
|
893
|
-
const shouldShow = val > 0 || (options.showZero && key === "second");
|
|
894
|
-
if (shouldShow || (parts.length === 0 && rest === 0)) {
|
|
895
|
-
parts.push(`${val}${labels[key]}`);
|
|
896
|
-
}
|
|
897
|
-
}
|
|
912
|
+
const seconds = diffSeconds % 60;
|
|
913
|
+
const minutes = Math.floor(diffSeconds / 60) % 60;
|
|
914
|
+
const hours = Math.floor(diffSeconds / 3600) % 24;
|
|
915
|
+
const days = Math.floor(diffSeconds / 3600 / 24);
|
|
916
|
+
|
|
917
|
+
return { days, hours, minutes, seconds, milliseconds: ms };
|
|
918
|
+
}
|
|
898
919
|
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
920
|
+
/**
|
|
921
|
+
* 将毫秒转换为时间持续对象(最大单位为小时)。
|
|
922
|
+
* @param {number} milliseconds - 毫秒数(非负整数)
|
|
923
|
+
* @returns {DurationMaxHourObject} 包含小时、分钟、秒、毫秒的时间持续对象
|
|
924
|
+
* @throws {TypeError} 当 milliseconds 不是有效数字时抛出
|
|
925
|
+
* @throws {RangeError} 当 milliseconds 为负数时抛出
|
|
926
|
+
* @example
|
|
927
|
+
* // 返回 { hours: 11686, minutes: 6, seconds: 40, milliseconds: 500 }
|
|
928
|
+
* millisecond2DurationMaxHour(42070000500);
|
|
929
|
+
*/
|
|
930
|
+
function millisecond2DurationMaxHour(milliseconds) {
|
|
931
|
+
if (typeof milliseconds !== "number" || isNaN(milliseconds)) {
|
|
932
|
+
throw new TypeError("milliseconds must be a valid number");
|
|
902
933
|
}
|
|
934
|
+
if (milliseconds < 0) {
|
|
935
|
+
throw new RangeError("milliseconds must be a non-negative number");
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
const totalMilliseconds = Math.floor(milliseconds);
|
|
939
|
+
const ms = totalMilliseconds % 1000;
|
|
940
|
+
const diffSeconds = Math.floor(totalMilliseconds / 1000);
|
|
903
941
|
|
|
904
|
-
|
|
942
|
+
const seconds = diffSeconds % 60;
|
|
943
|
+
const minutes = Math.floor(diffSeconds / 60) % 60;
|
|
944
|
+
const hours = Math.floor(diffSeconds / 3600);
|
|
945
|
+
|
|
946
|
+
return { hours, minutes, seconds, milliseconds: ms };
|
|
905
947
|
}
|
|
906
948
|
|
|
907
949
|
/**
|
|
908
|
-
*
|
|
950
|
+
* 将秒转换为时间持续对象。
|
|
909
951
|
*
|
|
910
|
-
* @param {number}
|
|
911
|
-
* @param {
|
|
912
|
-
* @
|
|
952
|
+
* @param {number} seconds - 秒数(非负整数)
|
|
953
|
+
* @param {Object} [options] - 选项对象。
|
|
954
|
+
* @param {number} [options.yearDays=365] - 一年的天数。
|
|
955
|
+
* @param {number} [options.monthDays=30] - 一月的天数。
|
|
956
|
+
* @returns {DurationObject} 时间持续对象
|
|
957
|
+
* @throws {TypeError} 当 seconds 不是有效数字
|
|
958
|
+
* @throws {RangeError} 当 seconds 为负数
|
|
959
|
+
* @throws {RangeError} 当 yearDays 或 monthDays 不是正整数
|
|
960
|
+
*
|
|
961
|
+
* @example
|
|
962
|
+
* // 基本用法
|
|
963
|
+
* second2Duration(42070000.5);
|
|
964
|
+
* // 返回: { years: 1, months: 4, days: 1, hours: 22, minutes: 6, seconds: 40, milliseconds: 500 }
|
|
913
965
|
*/
|
|
914
|
-
function
|
|
915
|
-
|
|
966
|
+
function second2Duration(seconds, options = { yearDays: 365, monthDays: 30 }) {
|
|
967
|
+
if (typeof seconds !== "number" || isNaN(seconds)) {
|
|
968
|
+
throw new TypeError("seconds must be a valid number");
|
|
969
|
+
}
|
|
970
|
+
if (seconds < 0) {
|
|
971
|
+
throw new RangeError("seconds must be a non-negative number");
|
|
972
|
+
}
|
|
973
|
+
return millisecond2Duration(seconds * 1000, options);
|
|
916
974
|
}
|
|
917
975
|
|
|
918
976
|
/**
|
|
919
|
-
*
|
|
920
|
-
*
|
|
921
|
-
* @
|
|
922
|
-
* @
|
|
923
|
-
* @
|
|
977
|
+
* 将秒转换为时间持续对象(最大单位为天)。
|
|
978
|
+
* @param {number} seconds - 秒数(非负整数)
|
|
979
|
+
* @returns {DurationMaxDayObject} 包含天、小时、分钟、秒、毫秒的时间持续对象
|
|
980
|
+
* @throws {TypeError} 当 seconds 不是有效数字时抛出
|
|
981
|
+
* @throws {RangeError} 当 seconds 为负数时抛出
|
|
982
|
+
* @example
|
|
983
|
+
* // 返回 { days: 486, hours: 22, minutes: 6, seconds: 40, milliseconds: 500 }
|
|
984
|
+
* second2DurationMaxDay(42070000.5);
|
|
985
|
+
*/
|
|
986
|
+
function second2DurationMaxDay(seconds) {
|
|
987
|
+
if (typeof seconds !== "number" || isNaN(seconds)) {
|
|
988
|
+
throw new TypeError("seconds must be a valid number");
|
|
989
|
+
}
|
|
990
|
+
if (seconds < 0) {
|
|
991
|
+
throw new RangeError("seconds must be a non-negative number");
|
|
992
|
+
}
|
|
993
|
+
return millisecond2DurationMaxDay(seconds * 1000);
|
|
994
|
+
}
|
|
995
|
+
|
|
996
|
+
/**
|
|
997
|
+
* 将秒转换为时间持续对象(最大单位为小时)。
|
|
998
|
+
* @param {number} seconds - 秒数(非负整数)
|
|
999
|
+
* @returns {DurationMaxHourObject} 包含小时、分钟、秒、毫秒的时间持续对象
|
|
1000
|
+
* @throws {TypeError} 当 seconds 不是有效数字时抛出
|
|
1001
|
+
* @throws {RangeError} 当 seconds 为负数时抛出
|
|
1002
|
+
* @example
|
|
1003
|
+
* // 返回 { hours: 11686, minutes: 6, seconds: 40, milliseconds: 500 }
|
|
1004
|
+
* second2DurationMaxHour(42070000.5);
|
|
924
1005
|
*/
|
|
925
|
-
function
|
|
926
|
-
|
|
1006
|
+
function second2DurationMaxHour(seconds) {
|
|
1007
|
+
if (typeof seconds !== "number" || isNaN(seconds)) {
|
|
1008
|
+
throw new TypeError("seconds must be a valid number");
|
|
1009
|
+
}
|
|
1010
|
+
if (seconds < 0) {
|
|
1011
|
+
throw new RangeError("seconds must be a non-negative number");
|
|
1012
|
+
}
|
|
1013
|
+
return millisecond2DurationMaxHour(seconds * 1000);
|
|
927
1014
|
}
|
|
928
1015
|
|
|
1016
|
+
//#endregion
|
|
1017
|
+
|
|
929
1018
|
/**
|
|
930
1019
|
* 根据小时数返回对应的时间段名称。
|
|
931
1020
|
*
|
|
@@ -1380,6 +1469,204 @@ class MyId {
|
|
|
1380
1469
|
}
|
|
1381
1470
|
}
|
|
1382
1471
|
|
|
1472
|
+
/**
|
|
1473
|
+
* 处理数据库菜单项,生成 UI 菜单树和路由配置。
|
|
1474
|
+
*
|
|
1475
|
+
* @param {Array<Object>} menuItems - 原始菜单项数组,树形结构
|
|
1476
|
+
* @param {Object} [options] - 配置选项
|
|
1477
|
+
* @param {string} [options.idKey='id'] - 数据源 ID 键名
|
|
1478
|
+
* @param {string} [options.codeKey='code'] - 数据源编码键名, 用于拼接路由名称和路由路径
|
|
1479
|
+
* @param {string} [options.labelKey='text'] - 数据源标签键名
|
|
1480
|
+
* @param {string} [options.childrenKey='children'] - 数据源子节点键名
|
|
1481
|
+
* @param {string} [options.extendKey='extend'] - 数据源扩展对象键名
|
|
1482
|
+
* @param {string} [options.menuIdKey='key'] - 输出菜单 ID 键名
|
|
1483
|
+
* @param {string} [options.menuLabelKey='label'] - 输出菜单标签键名
|
|
1484
|
+
* @param {string} [options.menuChildrenKey='children'] - 输出菜单子节点键名
|
|
1485
|
+
* @param {string} [options.menuExtendKey='extend'] - 输出菜单扩展对象键名
|
|
1486
|
+
* @param {string} [options.routeNameKey='name'] - 路由名称键名
|
|
1487
|
+
* @param {string} [options.routePathKey='path'] - 路由路径键名
|
|
1488
|
+
* @param {string} [options.routeMetaKey='meta'] - 路由元数据键名
|
|
1489
|
+
* @param {Function} [options.handleNodeItem] - 节点处理钩子,参数:(nodeSimple) => void
|
|
1490
|
+
* @param {Function} [options.handleMenuItem] - 菜单项处理钩子,参数:(uiMenuItem, nodeSimple) => void
|
|
1491
|
+
* @param {Function} [options.handleRouteItem] - 路由项处理钩子,参数:(routeItem, nodeSimple) => void
|
|
1492
|
+
*
|
|
1493
|
+
* @returns {Object} 处理结果
|
|
1494
|
+
* @returns {Array<Object>} returns.uiMenuItems - UI 菜单树形结构数组
|
|
1495
|
+
* @returns {Array<Object>} returns.routeItems - 扁平化路由配置数组
|
|
1496
|
+
* @returns {Map<string, Object>} returns.nodeMap - 节点 ID 到节点数据的映射表
|
|
1497
|
+
*
|
|
1498
|
+
* @example
|
|
1499
|
+
* const menuItems = [
|
|
1500
|
+
* { id: '1', code: 'system', text: '系统管理', children: [
|
|
1501
|
+
* { id: '1-1', code: 'user', text: '用户管理' }
|
|
1502
|
+
* ]}
|
|
1503
|
+
* ];
|
|
1504
|
+
*
|
|
1505
|
+
* const result = handleDbMenuItems(menuItems, {
|
|
1506
|
+
* handleRouteItem: (route, node) => {
|
|
1507
|
+
* route.component = () => import(`./views/${node.code}.vue`);
|
|
1508
|
+
* }
|
|
1509
|
+
* });
|
|
1510
|
+
*
|
|
1511
|
+
* // result.uiMenuItems: [{ key: '1', label: '系统管理', children: [...] }]
|
|
1512
|
+
* // result.routeItems: [{ name: 'system.user', path: '/system/user', meta: { id: '1-1' } }]
|
|
1513
|
+
* // result.nodeMap: Map { '1' => {...}, '1-1' => {...} }
|
|
1514
|
+
*/
|
|
1515
|
+
function handleDbMenuItems(menuItems, options = {}) {
|
|
1516
|
+
// 1. 统一配置项,简化调用
|
|
1517
|
+
const {
|
|
1518
|
+
// 数据源键名
|
|
1519
|
+
idKey = "id",
|
|
1520
|
+
codeKey = "code",
|
|
1521
|
+
labelKey = "text",
|
|
1522
|
+
childrenKey = "children",
|
|
1523
|
+
extendKey = "extend",
|
|
1524
|
+
|
|
1525
|
+
// 输出目标键名
|
|
1526
|
+
menuIdKey = "key",
|
|
1527
|
+
menuLabelKey = "label",
|
|
1528
|
+
menuChildrenKey = "children",
|
|
1529
|
+
menuExtendKey = "extend",
|
|
1530
|
+
|
|
1531
|
+
routeNameKey = "name",
|
|
1532
|
+
routePathKey = "path",
|
|
1533
|
+
routeMetaKey = "meta",
|
|
1534
|
+
|
|
1535
|
+
// 钩子函数
|
|
1536
|
+
handleNodeItem,
|
|
1537
|
+
handleMenuItem,
|
|
1538
|
+
handleRouteItem
|
|
1539
|
+
} = options;
|
|
1540
|
+
|
|
1541
|
+
const uiMenuItems = [];
|
|
1542
|
+
const routeItems = [];
|
|
1543
|
+
const nodeMap = new Map(); // id -> node (纯净的节点数据)
|
|
1544
|
+
const nodePathMap = new Map(); // id -> [{ code, id }] (仅存储路径计算所需的轻量数据)
|
|
1545
|
+
|
|
1546
|
+
/* ---------- 1. 建立索引:扁平化节点 & 构建路径索引 ---------- */
|
|
1547
|
+
// 使用栈进行深度优先遍历,记录路径
|
|
1548
|
+
const stack = [];
|
|
1549
|
+
if (Array.isArray(menuItems)) {
|
|
1550
|
+
menuItems.forEach((n) => stack.push({ node: n, parentPath: [] }));
|
|
1551
|
+
}
|
|
1552
|
+
|
|
1553
|
+
while (stack.length) {
|
|
1554
|
+
const { node, parentPath } = stack.pop();
|
|
1555
|
+
const idValue = node[idKey];
|
|
1556
|
+
|
|
1557
|
+
// 构建当前节点的路径片段
|
|
1558
|
+
const currentPathSegment = { [idKey]: idValue };
|
|
1559
|
+
const fullPath = [...parentPath, currentPathSegment];
|
|
1560
|
+
|
|
1561
|
+
// 存储路径信息用于后续路由生成
|
|
1562
|
+
nodePathMap.set(idValue, fullPath);
|
|
1563
|
+
|
|
1564
|
+
// 创建轻量级的节点对象存入 nodeMap
|
|
1565
|
+
// 注意:这里先创建基础结构,路由信息在第二步补充
|
|
1566
|
+
const nodeSimple = {
|
|
1567
|
+
...node,
|
|
1568
|
+
[extendKey]: {}, // 初始化扩展对象
|
|
1569
|
+
[childrenKey]: null // 去除原始 children,避免引用污染
|
|
1570
|
+
};
|
|
1571
|
+
nodeMap.set(idValue, nodeSimple);
|
|
1572
|
+
|
|
1573
|
+
// 处理子节点
|
|
1574
|
+
const childrenNodes = node[childrenKey];
|
|
1575
|
+
if (Array.isArray(childrenNodes)) {
|
|
1576
|
+
// 逆序压栈,保持原序
|
|
1577
|
+
for (let i = childrenNodes.length - 1; i >= 0; i--) {
|
|
1578
|
+
stack.push({ node: childrenNodes[i], parentPath: fullPath });
|
|
1579
|
+
}
|
|
1580
|
+
}
|
|
1581
|
+
}
|
|
1582
|
+
|
|
1583
|
+
/* ---------- 2. 构建树形结构 & 生成路由 ---------- */
|
|
1584
|
+
const buildStack = [];
|
|
1585
|
+
if (Array.isArray(menuItems)) {
|
|
1586
|
+
for (let i = menuItems.length - 1; i >= 0; i--) {
|
|
1587
|
+
buildStack.push({ node: menuItems[i], parentUi: undefined });
|
|
1588
|
+
}
|
|
1589
|
+
}
|
|
1590
|
+
|
|
1591
|
+
while (buildStack.length) {
|
|
1592
|
+
const { node, parentUi } = buildStack.pop();
|
|
1593
|
+
const idValue = node[idKey];
|
|
1594
|
+
|
|
1595
|
+
// 从 Map 中取出之前创建好的节点对象
|
|
1596
|
+
const nodeSimple = nodeMap.get(idValue);
|
|
1597
|
+
|
|
1598
|
+
// 2.1 构建 UI 菜单项
|
|
1599
|
+
const uiMenuItem = {
|
|
1600
|
+
[menuIdKey]: idValue,
|
|
1601
|
+
[menuLabelKey]: node[labelKey],
|
|
1602
|
+
[menuExtendKey]: {} // UI 菜单专用扩展
|
|
1603
|
+
};
|
|
1604
|
+
|
|
1605
|
+
// 处理子节点
|
|
1606
|
+
const childrenNodes = node[childrenKey];
|
|
1607
|
+
const hasChildren = Array.isArray(childrenNodes) && childrenNodes.length > 0;
|
|
1608
|
+
|
|
1609
|
+
if (hasChildren) {
|
|
1610
|
+
// 有子节点 -> 继续压栈,传递当前 uiMenuItem 作为父级
|
|
1611
|
+
for (let i = childrenNodes.length - 1; i >= 0; i--) {
|
|
1612
|
+
buildStack.push({ node: childrenNodes[i], parentUi: uiMenuItem });
|
|
1613
|
+
}
|
|
1614
|
+
} else {
|
|
1615
|
+
// 2.2 无子节点 -> 生成路由配置
|
|
1616
|
+
|
|
1617
|
+
// 提取 code 和 id 数组
|
|
1618
|
+
const codePaths = [],
|
|
1619
|
+
keyPaths = [];
|
|
1620
|
+
nodePathMap.get(idValue).forEach((item) => {
|
|
1621
|
+
const idValue = item[idKey];
|
|
1622
|
+
const nodeSimple = nodeMap.get(idValue);
|
|
1623
|
+
keyPaths.push(idValue);
|
|
1624
|
+
codePaths.push(nodeSimple[codeKey]);
|
|
1625
|
+
});
|
|
1626
|
+
|
|
1627
|
+
const routeName = codePaths.join(".");
|
|
1628
|
+
const routePath = "/" + codePaths.join("/");
|
|
1629
|
+
|
|
1630
|
+
const routeItem = {
|
|
1631
|
+
[routeNameKey]: routeName,
|
|
1632
|
+
[routePathKey]: routePath,
|
|
1633
|
+
[routeMetaKey]: { [idKey]: idValue }
|
|
1634
|
+
};
|
|
1635
|
+
|
|
1636
|
+
// 执行路由钩子
|
|
1637
|
+
if (typeof handleRouteItem === "function") {
|
|
1638
|
+
handleRouteItem(routeItem, nodeSimple);
|
|
1639
|
+
}
|
|
1640
|
+
routeItems.push(routeItem);
|
|
1641
|
+
|
|
1642
|
+
// 补充扩展信息
|
|
1643
|
+
uiMenuItem[menuExtendKey].routeName = routeName;
|
|
1644
|
+
uiMenuItem[menuExtendKey].keyPaths = keyPaths;
|
|
1645
|
+
|
|
1646
|
+
nodeSimple[extendKey].routeName = routeName;
|
|
1647
|
+
nodeSimple[extendKey].keyPaths = keyPaths;
|
|
1648
|
+
}
|
|
1649
|
+
|
|
1650
|
+
// 2.3 挂载到父级 UI 或根数组
|
|
1651
|
+
if (parentUi) {
|
|
1652
|
+
// 逻辑赋值,确保 children 数组存在
|
|
1653
|
+
(parentUi[menuChildrenKey] || (parentUi[menuChildrenKey] = [])).push(uiMenuItem);
|
|
1654
|
+
} else {
|
|
1655
|
+
if (typeof handleMenuItem === "function") {
|
|
1656
|
+
handleMenuItem(uiMenuItem, nodeSimple);
|
|
1657
|
+
}
|
|
1658
|
+
uiMenuItems.push(uiMenuItem);
|
|
1659
|
+
}
|
|
1660
|
+
|
|
1661
|
+
// 执行节点钩子
|
|
1662
|
+
if (typeof handleNodeItem === "function") {
|
|
1663
|
+
handleNodeItem(nodeSimple);
|
|
1664
|
+
}
|
|
1665
|
+
}
|
|
1666
|
+
|
|
1667
|
+
return { uiMenuItems, routeItems, nodeMap };
|
|
1668
|
+
}
|
|
1669
|
+
|
|
1383
1670
|
/**
|
|
1384
1671
|
* 基于 `setTimeout` 的“间隔循环”定时器。
|
|
1385
1672
|
* 每次任务执行完成后才计算下一次间隔,避免任务堆积。
|
|
@@ -1500,24 +1787,23 @@ function flatCompleteTree2NestedTree(nodes, parentId = 0, { idKey = "id", parent
|
|
|
1500
1787
|
}
|
|
1501
1788
|
|
|
1502
1789
|
/**
|
|
1503
|
-
* 在嵌套树中按 `id`
|
|
1790
|
+
* 在嵌套树中按 `id` 递归查找节点
|
|
1504
1791
|
*
|
|
1505
1792
|
* @template T extends Record<PropertyKey, any>
|
|
1506
1793
|
* @param {string | number} id - 要查找的 id
|
|
1507
1794
|
* @param {T[]} arr - 嵌套树森林
|
|
1508
|
-
* @param {string} [resultKey='name'] - 需要返回的字段
|
|
1509
1795
|
* @param {string} [idKey='id'] - 主键字段
|
|
1510
1796
|
* @param {string} [childrenKey='children'] - 子节点字段
|
|
1511
|
-
* @returns {any}
|
|
1797
|
+
* @returns {any} 找到的节点;未找到返回 `undefined`
|
|
1512
1798
|
*/
|
|
1513
|
-
const
|
|
1799
|
+
const findTreeNodeById = function findTreeNodeByIdFn(id, arr, idKey = "id", childrenKey = "children") {
|
|
1514
1800
|
if (Array.isArray(arr) && arr.length > 0) {
|
|
1515
1801
|
for (let i = 0; i < arr.length; i++) {
|
|
1516
1802
|
const item = arr[i];
|
|
1517
1803
|
if (item[idKey]?.toString() === id?.toString()) {
|
|
1518
|
-
return item
|
|
1804
|
+
return item;
|
|
1519
1805
|
} else if (Array.isArray(item[childrenKey]) && item[childrenKey].length > 0) {
|
|
1520
|
-
const result =
|
|
1806
|
+
const result = findTreeNodeByIdFn(id, item[childrenKey], idKey, childrenKey);
|
|
1521
1807
|
if (result) {
|
|
1522
1808
|
return result;
|
|
1523
1809
|
}
|
|
@@ -1526,6 +1812,21 @@ const findObjAttrValueById = function findObjAttrValueByIdFn(id, arr, resultKey
|
|
|
1526
1812
|
}
|
|
1527
1813
|
};
|
|
1528
1814
|
|
|
1815
|
+
/**
|
|
1816
|
+
* 在嵌套树中按 `id` 递归查找节点,并返回其指定属性值。
|
|
1817
|
+
*
|
|
1818
|
+
* @template T extends Record<PropertyKey, any>
|
|
1819
|
+
* @param {string | number} id - 要查找的 id
|
|
1820
|
+
* @param {T[]} arr - 嵌套树森林
|
|
1821
|
+
* @param {string} [resultKey='name'] - 需要返回的字段
|
|
1822
|
+
* @param {string} [idKey='id'] - 主键字段
|
|
1823
|
+
* @param {string} [childrenKey='children'] - 子节点字段
|
|
1824
|
+
* @returns {any} 找到的值;未找到返回 `undefined`
|
|
1825
|
+
*/
|
|
1826
|
+
function findObjAttrValueById(id, arr, resultKey = "name", idKey = "id", childrenKey = "children") {
|
|
1827
|
+
return findTreeNodeById(id, arr, idKey, childrenKey)?.[resultKey];
|
|
1828
|
+
}
|
|
1829
|
+
|
|
1529
1830
|
/**
|
|
1530
1831
|
* 从服务端返回的已选 id 数组里,提取出
|
|
1531
1832
|
* 1. 叶子节点
|
|
@@ -2012,7 +2313,6 @@ exports.MyEvent_CrossPagePlugin = MyEvent_CrossPagePlugin;
|
|
|
2012
2313
|
exports.MyId = MyId;
|
|
2013
2314
|
exports.WebSocketManager = WebSocketManager;
|
|
2014
2315
|
exports.assignExisting = assignExisting;
|
|
2015
|
-
exports.calcTimeDifference = calcTimeDifference;
|
|
2016
2316
|
exports.debounce = debounce;
|
|
2017
2317
|
exports.deepCloneByJSON = deepCloneByJSON;
|
|
2018
2318
|
exports.downloadByBlob = downloadByBlob;
|
|
@@ -2023,10 +2323,8 @@ exports.downloadJSON = downloadJSON;
|
|
|
2023
2323
|
exports.extractFullyCheckedKeys = extractFullyCheckedKeys;
|
|
2024
2324
|
exports.fetchOrDownloadByUrl = fetchOrDownloadByUrl;
|
|
2025
2325
|
exports.findObjAttrValueById = findObjAttrValueById;
|
|
2326
|
+
exports.findTreeNodeById = findTreeNodeById;
|
|
2026
2327
|
exports.flatCompleteTree2NestedTree = flatCompleteTree2NestedTree;
|
|
2027
|
-
exports.formatDuration = formatDuration;
|
|
2028
|
-
exports.formatDurationMaxDay = formatDurationMaxDay;
|
|
2029
|
-
exports.formatDurationMaxHour = formatDurationMaxHour;
|
|
2030
2328
|
exports.formatTimeForLocale = formatTimeForLocale;
|
|
2031
2329
|
exports.getAllSearchParams = getAllSearchParams;
|
|
2032
2330
|
exports.getDataType = getDataType;
|
|
@@ -2035,12 +2333,16 @@ exports.getGUID = getGUID;
|
|
|
2035
2333
|
exports.getSearchParam = getSearchParam;
|
|
2036
2334
|
exports.getTimePeriodName = getTimePeriodName;
|
|
2037
2335
|
exports.getViewportSize = getViewportSize;
|
|
2336
|
+
exports.handleDbMenuItems = handleDbMenuItems;
|
|
2038
2337
|
exports.isBlob = isBlob;
|
|
2039
2338
|
exports.isDate = isDate;
|
|
2040
2339
|
exports.isFunction = isFunction;
|
|
2041
2340
|
exports.isNonEmptyString = isNonEmptyString;
|
|
2042
2341
|
exports.isPlainObject = isPlainObject;
|
|
2043
2342
|
exports.isPromise = isPromise;
|
|
2343
|
+
exports.millisecond2Duration = millisecond2Duration;
|
|
2344
|
+
exports.millisecond2DurationMaxDay = millisecond2DurationMaxDay;
|
|
2345
|
+
exports.millisecond2DurationMaxHour = millisecond2DurationMaxHour;
|
|
2044
2346
|
exports.moveItem = moveItem;
|
|
2045
2347
|
exports.nestedTree2IdMap = nestedTree2IdMap;
|
|
2046
2348
|
exports.pcmToWavBlob = pcmToWavBlob;
|
|
@@ -2050,6 +2352,9 @@ exports.randomHan = randomHan;
|
|
|
2050
2352
|
exports.randomHanOrEn = randomHanOrEn;
|
|
2051
2353
|
exports.randomIntInRange = randomIntInRange;
|
|
2052
2354
|
exports.readBlobAsText = readBlobAsText;
|
|
2355
|
+
exports.second2Duration = second2Duration;
|
|
2356
|
+
exports.second2DurationMaxDay = second2DurationMaxDay;
|
|
2357
|
+
exports.second2DurationMaxHour = second2DurationMaxHour;
|
|
2053
2358
|
exports.shuffle = shuffle;
|
|
2054
2359
|
exports.throttle = throttle;
|
|
2055
2360
|
exports.toDate = toDate;
|