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