@tmsfe/tms-core 0.0.75 → 0.0.76

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/navbarUtils.js +186 -19
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmsfe/tms-core",
3
- "version": "0.0.75",
3
+ "version": "0.0.76",
4
4
  "description": "tms运行时框架",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,28 +1,195 @@
1
+ let systemInfo = null;
2
+
3
+ /**
4
+ * 获取系统信息,有缓存
5
+ * 返回数据同wx.getSystemInfoSync
6
+ * @returns {Object} 系统信息
7
+ */
8
+ const getSystemInfoSync = () => {
9
+ if (systemInfo) return systemInfo;
10
+ let res;
11
+ try {
12
+ systemInfo = wx.getSystemInfoSync();
13
+ res = systemInfo;
14
+ } catch (_) {
15
+ res = {};
16
+ }
17
+ return res;
18
+ };
19
+
20
+ /**
21
+ * 获取系统信息
22
+ * @returns {Object} 系统信息
23
+ */
24
+ const getSysInfo = () => {
25
+ let sysInfo = {};
26
+ try {
27
+ sysInfo = getSystemInfoSync();
28
+ sysInfo.getSystemInfoSuccess = true;
29
+ } catch (e) {
30
+ // 获取系统信息失败时,使用默认数据
31
+ sysInfo = { getSystemInfoSuccess: false, statusBarHeight: 0, windowWidth: 0 };
32
+ }
33
+ return sysInfo;
34
+ };
35
+
36
+ /**
37
+ * 获取胶囊位置信息
38
+ * @returns {Object} 胶囊位置信息
39
+ */
40
+ const getMenuButtonRectInfo = () => {
41
+ let menuButtonRectInfo = {};
42
+
43
+ try {
44
+ menuButtonRectInfo = wx.getMenuButtonBoundingClientRect();
45
+ menuButtonRectInfo.getMenuRectInfoSuccess = true;
46
+ } catch (e) {
47
+ // 获取胶囊位置信息失败,使用默认值
48
+ menuButtonRectInfo = { getMenuRectInfoSuccess: false };
49
+ }
50
+
51
+ return menuButtonRectInfo;
52
+ };
53
+
54
+ /**
55
+ * 版本比较函数
56
+ * @param {String} sourceVersion 作为基准版本号
57
+ * @param {String} targetVersion 目标比较版本号
58
+ * @returns {Number} 比较结果
59
+ * 返回值说明:
60
+ * 1 : 大于基准版本号
61
+ * 0 : 等于基准版本号
62
+ * -1: 小于基准版本号
63
+ */
64
+ const compareVersion = (sourceVersion, targetVersion) => {
65
+ if (typeof sourceVersion !== 'string' || typeof targetVersion !== 'string') {
66
+ throw new Error('版本比较参数类型有误');
67
+ }
68
+
69
+ const toInt = n => parseInt(n, 10); // eslint-disable-line require-jsdoc
70
+ const sourceArray = sourceVersion.split('.').map(toInt);
71
+ const targetArray = targetVersion.split('.').map(toInt);
72
+
73
+ for (let i = 0; i < sourceArray.length; i += 1) {
74
+ if (sourceArray[i] > targetArray[i]) {
75
+ return 1;
76
+ } if (sourceArray[i] < targetArray[i]) {
77
+ return -1;
78
+ }
79
+ }
80
+
81
+ return 0;
82
+ };
83
+
84
+ /**
85
+ * 胶囊高度适配,以兼容获取到的胶囊高度值非法的情况
86
+ * @param {Number} height 胶囊高度
87
+ * @param {Boolean} isIOS 是否是ios系统
88
+ * @returns {Number} 胶囊高度
89
+ */
90
+ const formatMenuHeight = (height, isIOS) => {
91
+ if (height > 0) {
92
+ return height;
93
+ }
94
+
95
+ return isIOS ? 32 : 34;
96
+ };
97
+
98
+ /**
99
+ * 计算自定义导航栏布局信息
100
+ * @param {Boolean} isIOS 是否是ios平台
101
+ * @param {Number} statusBarHeight 状态栏高度
102
+ * @param {String} apiCategory API类别
103
+ * @returns {Object} data 导航栏布局信息
104
+ * @returns {Number} data.statusBarHeight 状态栏高度,单位rpx
105
+ * @returns {Number} data.navBarHeight 导航栏高度(不含状态栏),单位rpx
106
+ * @returns {Number} data.menuTop 菜单按钮上边距,单位rpx
107
+ * @returns {Number} data.menuRight 菜单按钮右边缘距离屏幕左边缘的距离,单位rpx
108
+ * @returns {Number} data.menuWidth 菜单按钮宽度,单位rpx
109
+ * @returns {Number} data.menuHeight 菜单按钮高度,单位rpx
110
+ */
111
+ const calculateNavBarLayout = (isIOS, statusBarHeight, apiCategory) => {
112
+ const data = {};
113
+ const menuTopOnIos = statusBarHeight + 4;
114
+ const menuTopOnAndroid = statusBarHeight + 8; // statusBarHeight高度,不同机型差距较大,不适合写死
115
+ const menuRectInfo = getMenuButtonRectInfo(); // 插件下基础库2.15.0起支持
116
+ let { right, width, height = 0, top } = menuRectInfo;
117
+ // 数据有异常时使用默认值
118
+ if (top <= 0) {
119
+ top = isIOS ? menuTopOnIos : menuTopOnAndroid;
120
+ }
121
+ if (right <= 0) {
122
+ right = isIOS ? 368 : 383;
123
+ }
124
+ if (width < 0) {
125
+ width = isIOS ? 87 : 94;
126
+ }
127
+ height = formatMenuHeight(height, isIOS);
128
+
129
+ data.statusBarHeight = calStatusBarHeight(statusBarHeight, apiCategory); // 顶部状态栏高度
130
+ data.navBarHeight = calNavBarHeight(top, height, statusBarHeight, apiCategory); // 小程序导航栏高度
131
+ data.menuTop = top; // 菜单按钮上边距
132
+ data.menuRight = right; // 菜单按钮右边距离屏幕左边缘的距离
133
+ data.menuWidth = width; // 菜单按钮宽度
134
+ data.menuHeight = height; // 菜单按钮高度
135
+ return data;
136
+ };
137
+
138
+ const calStatusBarHeight = (statusBarHeight, apiCategory) => {
139
+ if (apiCategory !== 'embedded') return statusBarHeight;
140
+ return 0;
141
+ };
142
+
143
+ const calNavBarHeight = (menuTop, menuHeight, statusBarHeight, apiCategory) => {
144
+ if (apiCategory !== 'embedded') {
145
+ return menuHeight + ((menuTop - statusBarHeight) * 2);
146
+ }
147
+ return menuHeight + (7 * 2);
148
+ };
1
149
 
2
150
  /**
3
151
  * 获取导航栏相关信息
4
152
  * @returns {Object} data 导航栏布局信息
5
153
  * @returns {Number} data.enable 是否支持自定义导航栏
6
- * @returns {Number} data.statusBarHeight 状态栏高度,单位px
7
- * @returns {Number} data.navBarHeight 导航栏高度(不含状态栏),单位px
8
- * @returns {Number} data.menuTop 菜单按钮上边距,单位px
9
- * @returns {Number} data.menuRight 菜单按钮右边缘距离屏幕左边缘的距离,单位px
10
- * @returns {Number} data.menuWidth 菜单按钮宽度,单位px
11
- * @returns {Number} data.menuHeight 菜单按钮高度,单位px
154
+ * @returns {Number} data.statusBarHeight 状态栏高度,单位rpx
155
+ * @returns {Number} data.navBarHeight 导航栏高度(不含状态栏),单位rpx
156
+ * @returns {Number} data.menuTop 菜单按钮上边距,单位rpx
157
+ * @returns {Number} data.menuRight 菜单按钮右边缘距离屏幕左边缘的距离,单位rpx
158
+ * @returns {Number} data.menuWidth 菜单按钮宽度,单位rpx
159
+ * @returns {Number} data.menuHeight 菜单按钮高度,单位rpx
160
+ * @returns {Object} data.enterOptions 启动参数
161
+ * @returns {Object} data.enterOptions.apiCategory API 类别(不同apiCategory场景下的API有不同限制,UI也有不同)
12
162
  */
13
- const getNavBarConfigData = () => {
14
- const { statusBarHeight } = wx.getSystemInfoSync();
15
- const { right, width, height = 0, top } = wx.getMenuButtonBoundingClientRect(); // 插件下基础库2.15.0起支持
16
- const embedded = wx.getEnterOptionsSync().apiCategory === 'embedded'; // 是否半屏模式拉起小程序
17
- return {
18
- enable: true,
19
- statusBarHeight: embedded ? 0 : statusBarHeight, // 顶部状态栏高度
20
- navBarHeight: embedded ? (height + (7 * 2)) : (height + ((top - statusBarHeight) * 2)), // 小程序导航栏高度
21
- menuTop: top, // 菜单按钮上边距
22
- menuRight: right, // 菜单按钮右边距离屏幕左边缘的距离
23
- menuWidth: width, // 菜单按钮宽度
24
- menuHeight: height, // 菜单按钮高度
25
- };
163
+ const getNavBarConfigData = () => { // eslint-disable-line require-jsdoc
164
+ const systemInfo = getSysInfo();
165
+ const { brand, getSystemInfoSuccess, platform } = systemInfo;
166
+ const isIOS = platform === 'ios';
167
+ const StatusBarHeightOnIOS = 44;
168
+ const StatusBarHeightOnAndroid = 24;
169
+ let { statusBarHeight, version } = systemInfo;
170
+ // 获取系统信息失败,给设置一个默认版本
171
+ if (!getSystemInfoSuccess || !version) {
172
+ version = '7.0.1';
173
+ }
174
+ // 获取状态栏高度失败 | 接口调用成功,但数据为0时,使用默认值
175
+ if (!getSystemInfoSuccess || (getSystemInfoSuccess && statusBarHeight <= 0)) {
176
+ statusBarHeight = isIOS ? StatusBarHeightOnIOS : StatusBarHeightOnAndroid;
177
+ }
178
+
179
+ const enable = compareVersion(version, '7.0.0') >= 0 || brand === 'devtools'; // 微信版本是否支持自定义顶栏,不支持时自动隐藏
180
+ const enterOptions = getEnterOptions();
181
+ if (enable) {
182
+ return { enable, ...calculateNavBarLayout(isIOS, statusBarHeight, enterOptions.apiCategory) };
183
+ }
184
+
185
+ return { enable };
186
+ };
187
+
188
+ // 获取需要对外暴露的启动参数
189
+ const getEnterOptions = () => {
190
+ const options = wx.getEnterOptionsSync();
191
+ const { apiCategory = 'default' } = options || {};
192
+ return { apiCategory };
26
193
  };
27
194
 
28
195
  export {