yrjy_mini_sdk 1.0.0

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.
@@ -0,0 +1,100 @@
1
+ /**
2
+ * 公共工具方法
3
+ */
4
+
5
+ /**
6
+ * 获取当前页面栈
7
+ * @returns {Array} 页面栈数组
8
+ */
9
+ export function getCurrentPages() {
10
+ let pages = [];
11
+ try {
12
+ if (process.env.UNI_PLATFORM === 'harmonyos') {
13
+ const pageManager = uni.getAppPageManager();
14
+ if (pageManager && typeof pageManager.getPages === 'function') {
15
+ pages = pageManager.getPages() || [];
16
+ }
17
+ } else {
18
+ // 检查 uni 对象是否存在且 getCurrentPages 是一个函数
19
+ if (typeof uni === 'object' && uni !== null && typeof uni.getCurrentPages === 'function') {
20
+ pages = uni.getCurrentPages() || [];
21
+ }
22
+ }
23
+ } catch (error) {
24
+ console.error('Error getting pages:', error);
25
+ }
26
+ return pages;
27
+ }
28
+
29
+ /**
30
+ * 从页面对象中获取页面路径
31
+ * @param {Object} page 页面对象
32
+ * @param {Object} scope Vue 实例作用域(可选)
33
+ * @returns {string} 页面路径
34
+ */
35
+ export function getPagePath(page, scope) {
36
+ let pagePath = '';
37
+ if (page) {
38
+ if (page.route) {
39
+ pagePath = page.route;
40
+ } else if (page.__route__) {
41
+ pagePath = page.__route__;
42
+ } else if (page.path) {
43
+ pagePath = page.path;
44
+ } else if (scope && typeof scope === 'object' && scope !== null) {
45
+ // 尝试从 Vue 实例中获取页面路径
46
+ if (scope.route) {
47
+ pagePath = scope.route;
48
+ } else if (scope.__route__) {
49
+ pagePath = scope.__route__;
50
+ } else if (scope.path) {
51
+ pagePath = scope.path;
52
+ }
53
+ }
54
+ }
55
+ return pagePath;
56
+ }
57
+
58
+ /**
59
+ * 安全解析 JSON 字符串
60
+ * @param {string} jsonString JSON 字符串
61
+ * @param {*} defaultValue 默认值
62
+ * @returns {*} 解析后的对象或默认值
63
+ */
64
+ export function safeJsonParse(jsonString, defaultValue = {}) {
65
+ try {
66
+ if (jsonString && typeof jsonString === 'string') {
67
+ return JSON.parse(jsonString);
68
+ }
69
+ return defaultValue;
70
+ } catch (error) {
71
+ console.error('❌ 解析 JSON 失败:', error);
72
+ return defaultValue;
73
+ }
74
+ }
75
+
76
+ /**
77
+ * 安全获取嵌套对象属性
78
+ * @param {Object} obj 对象
79
+ * @param {Array} path 属性路径数组
80
+ * @param {*} defaultValue 默认值
81
+ * @returns {*} 属性值或默认值
82
+ */
83
+ export function safeGet(obj, path, defaultValue = undefined) {
84
+ try {
85
+ if (!obj || !Array.isArray(path)) {
86
+ return defaultValue;
87
+ }
88
+ let result = obj;
89
+ for (const key of path) {
90
+ if (result === null || result === undefined) {
91
+ return defaultValue;
92
+ }
93
+ result = result[key];
94
+ }
95
+ return result === undefined ? defaultValue : result;
96
+ } catch (error) {
97
+ console.error('❌ 安全获取属性失败:', error);
98
+ return defaultValue;
99
+ }
100
+ }