@hhfenpm/medical-agent-utils 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.
- package/README.md +157 -0
- package/dist/index.esm.js +243 -0
- package/dist/index.js +254 -0
- package/package.json +42 -0
package/README.md
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
# @hhfenpm/medical-agent-utils
|
|
2
|
+
|
|
3
|
+
通用工具函数库,包含 UUID 生成、对象属性获取、设备检测等常用工具函数。
|
|
4
|
+
|
|
5
|
+
## 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @hhfenpm/medical-agent-utils
|
|
9
|
+
# 或
|
|
10
|
+
yarn add @hhfenpm/medical-agent-utils
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## 使用方式
|
|
14
|
+
|
|
15
|
+
### 按需引入
|
|
16
|
+
|
|
17
|
+
```javascript
|
|
18
|
+
// 引入单个函数
|
|
19
|
+
import { uuid, get, isTablet } from '@hhfenpm/medical-agent-utils'
|
|
20
|
+
|
|
21
|
+
// 使用
|
|
22
|
+
const id = uuid()
|
|
23
|
+
const value = get(obj, 'path.to.value', defaultValue)
|
|
24
|
+
if (isTablet()) {
|
|
25
|
+
console.log('当前是平板设备')
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### 全量引入
|
|
30
|
+
|
|
31
|
+
```javascript
|
|
32
|
+
// 引入所有工具
|
|
33
|
+
import utils from '@hhfenpm/medical-agent-utils'
|
|
34
|
+
|
|
35
|
+
// 使用
|
|
36
|
+
const id = utils.uuid.uuid()
|
|
37
|
+
const value = utils.get.get(obj, 'path.to.value')
|
|
38
|
+
const deviceInfo = utils.device.getDeviceInfo()
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## API 文档
|
|
42
|
+
|
|
43
|
+
### UUID 生成 (uuid)
|
|
44
|
+
|
|
45
|
+
#### `uuid()`
|
|
46
|
+
|
|
47
|
+
生成短 UUID。
|
|
48
|
+
|
|
49
|
+
```javascript
|
|
50
|
+
import { uuid } from '@hhfenpm/medical-agent-utils'
|
|
51
|
+
|
|
52
|
+
const id = uuid() // 'abc123'
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
#### `uuidLong()`
|
|
56
|
+
|
|
57
|
+
生成长 UUID。
|
|
58
|
+
|
|
59
|
+
```javascript
|
|
60
|
+
import { uuidLong } from '@hhfenpm/medical-agent-utils'
|
|
61
|
+
|
|
62
|
+
const longId = uuidLong() // 'abc123def456ghi789'
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
### 对象属性获取 (get)
|
|
68
|
+
|
|
69
|
+
#### `get(object, path, defaultValue)`
|
|
70
|
+
|
|
71
|
+
安全获取对象属性,类似 lodash.get。
|
|
72
|
+
|
|
73
|
+
```javascript
|
|
74
|
+
import { get } from '@hhfenpm/medical-agent-utils'
|
|
75
|
+
|
|
76
|
+
const obj = { a: { b: { c: 123 } } }
|
|
77
|
+
const value = get(obj, 'a.b.c', 0) // 123
|
|
78
|
+
const defaultValue = get(obj, 'a.b.d', 0) // 0
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
**参数:**
|
|
82
|
+
|
|
83
|
+
- `object` - 目标对象
|
|
84
|
+
- `path` - 属性路径(字符串或数组)
|
|
85
|
+
- `defaultValue` - 默认值(可选)
|
|
86
|
+
|
|
87
|
+
---
|
|
88
|
+
|
|
89
|
+
### 设备检测 (device)
|
|
90
|
+
|
|
91
|
+
#### `isTablet()`
|
|
92
|
+
|
|
93
|
+
检测当前设备是否为平板。
|
|
94
|
+
|
|
95
|
+
```javascript
|
|
96
|
+
import { isTablet } from '@hhfenpm/medical-agent-utils'
|
|
97
|
+
|
|
98
|
+
if (isTablet()) {
|
|
99
|
+
console.log('当前是平板设备')
|
|
100
|
+
}
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
#### `getDeviceType()`
|
|
104
|
+
|
|
105
|
+
获取设备类型。
|
|
106
|
+
|
|
107
|
+
```javascript
|
|
108
|
+
import { getDeviceType } from '@hhfenpm/medical-agent-utils'
|
|
109
|
+
|
|
110
|
+
getDeviceType() // 'desktop' | 'tablet' | 'mobile'
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
#### `isTouchDevice()`
|
|
114
|
+
|
|
115
|
+
检测是否为触摸设备。
|
|
116
|
+
|
|
117
|
+
```javascript
|
|
118
|
+
import { isTouchDevice } from '@hhfenpm/medical-agent-utils'
|
|
119
|
+
|
|
120
|
+
if (isTouchDevice()) {
|
|
121
|
+
console.log('支持触摸操作')
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
#### `getDeviceInfo()`
|
|
126
|
+
|
|
127
|
+
获取完整设备信息。
|
|
128
|
+
|
|
129
|
+
```javascript
|
|
130
|
+
import { getDeviceInfo } from '@hhfenpm/medical-agent-utils'
|
|
131
|
+
|
|
132
|
+
const info = getDeviceInfo()
|
|
133
|
+
// {
|
|
134
|
+
// isTablet: false,
|
|
135
|
+
// deviceType: 'desktop',
|
|
136
|
+
// isTouchDevice: false,
|
|
137
|
+
// userAgent: '...',
|
|
138
|
+
// screenWidth: 1920,
|
|
139
|
+
// screenHeight: 1080,
|
|
140
|
+
// devicePixelRatio: 1,
|
|
141
|
+
// orientation: 'landscape-primary',
|
|
142
|
+
// }
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## 浏览器兼容性
|
|
146
|
+
|
|
147
|
+
- 现代浏览器(Chrome, Firefox, Safari, Edge)
|
|
148
|
+
- IE 11+(部分功能需要 polyfill)
|
|
149
|
+
|
|
150
|
+
## 注意事项
|
|
151
|
+
|
|
152
|
+
1. 部分函数依赖浏览器环境(如 `window`, `navigator`),在 SSR 场景下已添加环境判断
|
|
153
|
+
2. 使用 Rollup 构建,支持 Tree-shaking,按需引入时只会打包使用的函数
|
|
154
|
+
|
|
155
|
+
## 许可证
|
|
156
|
+
|
|
157
|
+
MIT
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* UUID 生成工具
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 生成短 UUID
|
|
7
|
+
* @returns {string} UUID 字符串
|
|
8
|
+
*/
|
|
9
|
+
function uuid() {
|
|
10
|
+
let random;
|
|
11
|
+
|
|
12
|
+
try {
|
|
13
|
+
const arr = new Uint32Array(1);
|
|
14
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/RandomSource/getRandomValues
|
|
15
|
+
if (typeof window !== 'undefined' && window.crypto) {
|
|
16
|
+
window.crypto.getRandomValues(arr);
|
|
17
|
+
random = arr[0] & 2147483647;
|
|
18
|
+
} else {
|
|
19
|
+
random = Math.floor(Math.random() * 2147483648);
|
|
20
|
+
}
|
|
21
|
+
} catch (b) {
|
|
22
|
+
random = Math.floor(Math.random() * 2147483648);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return random.toString(36)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* 生成长 UUID
|
|
30
|
+
* @returns {string} 长 UUID 字符串
|
|
31
|
+
*/
|
|
32
|
+
function uuidLong() {
|
|
33
|
+
return `${uuid()}${uuid()}${uuid()}`
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
var uuid$1 = /*#__PURE__*/Object.freeze({
|
|
37
|
+
__proto__: null,
|
|
38
|
+
default: uuid,
|
|
39
|
+
uuid: uuid,
|
|
40
|
+
uuidLong: uuidLong
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 安全获取对象属性工具
|
|
45
|
+
* 类似 lodash.get
|
|
46
|
+
*/
|
|
47
|
+
|
|
48
|
+
function baseGet(object, path) {
|
|
49
|
+
path = castPath(path);
|
|
50
|
+
|
|
51
|
+
var index = 0,
|
|
52
|
+
length = path.length;
|
|
53
|
+
|
|
54
|
+
while (object != null && index < length) {
|
|
55
|
+
object = object[path[index++]];
|
|
56
|
+
}
|
|
57
|
+
return index && index == length ? object : undefined
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function castPath(value, object) {
|
|
61
|
+
if (Array.isArray(value)) {
|
|
62
|
+
return value
|
|
63
|
+
}
|
|
64
|
+
return stringToPath(String(value))
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function stringToPath(string) {
|
|
68
|
+
var result = [];
|
|
69
|
+
if (string.charCodeAt(0) === 46 /* . */) {
|
|
70
|
+
result.push('');
|
|
71
|
+
}
|
|
72
|
+
string.replace(
|
|
73
|
+
/[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g,
|
|
74
|
+
function (match, number, quote, subString) {
|
|
75
|
+
result.push(quote ? subString.replace(/\\(\\)?/g, '$1') : number || match);
|
|
76
|
+
}
|
|
77
|
+
);
|
|
78
|
+
return result
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 获取对象属性
|
|
83
|
+
* @param {Object} object - 目标对象
|
|
84
|
+
* @param {string|Array} path - 属性路径
|
|
85
|
+
* @param {*} defaultValue - 默认值
|
|
86
|
+
* @returns {*} 属性值
|
|
87
|
+
*/
|
|
88
|
+
function get(object, path, defaultValue) {
|
|
89
|
+
var result = object == null ? undefined : baseGet(object, path);
|
|
90
|
+
return result === undefined ? defaultValue : result
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
var get$1 = /*#__PURE__*/Object.freeze({
|
|
94
|
+
__proto__: null,
|
|
95
|
+
get: get
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* 设备检测工具函数
|
|
100
|
+
*/
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 检测当前设备是否为平板
|
|
104
|
+
* @returns {boolean} 是否为平板设备
|
|
105
|
+
*/
|
|
106
|
+
function isTablet() {
|
|
107
|
+
if (typeof window === 'undefined' || typeof navigator === 'undefined') {
|
|
108
|
+
return false
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// 获取用户代理字符串
|
|
112
|
+
const userAgent = navigator.userAgent.toLowerCase();
|
|
113
|
+
|
|
114
|
+
// 检测常见的平板设备标识
|
|
115
|
+
const tabletPatterns = [
|
|
116
|
+
/ipad/, // iPad
|
|
117
|
+
/android.*tablet/, // Android 平板
|
|
118
|
+
/kindle/, // Kindle
|
|
119
|
+
/silk/, // Amazon Silk
|
|
120
|
+
/playbook/, // BlackBerry PlayBook
|
|
121
|
+
/bb10/, // BlackBerry 10
|
|
122
|
+
/rim tablet/, // BlackBerry Tablet OS
|
|
123
|
+
/windows.*touch/, // Windows 平板
|
|
124
|
+
/tablet/, // 通用平板标识
|
|
125
|
+
];
|
|
126
|
+
|
|
127
|
+
// 检查用户代理是否匹配平板模式
|
|
128
|
+
const isTabletByUserAgent = tabletPatterns.some((pattern) =>
|
|
129
|
+
pattern.test(userAgent)
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
// 检测屏幕尺寸和触摸支持
|
|
133
|
+
const hasTouch =
|
|
134
|
+
'ontouchstart' in window || navigator.maxTouchPoints > 0;
|
|
135
|
+
const screenWidth = window.screen.width;
|
|
136
|
+
const screenHeight = window.screen.height;
|
|
137
|
+
const minDimension = Math.min(screenWidth, screenHeight);
|
|
138
|
+
const maxDimension = Math.max(screenWidth, screenHeight);
|
|
139
|
+
|
|
140
|
+
// 平板设备的典型屏幕尺寸范围
|
|
141
|
+
const isTabletByScreenSize =
|
|
142
|
+
minDimension >= 600 &&
|
|
143
|
+
minDimension <= 1200 &&
|
|
144
|
+
maxDimension >= 800 &&
|
|
145
|
+
maxDimension <= 1600;
|
|
146
|
+
|
|
147
|
+
// 检测是否为移动设备但屏幕较大(可能是平板)
|
|
148
|
+
const isMobile = /android|webos|iphone|ipod|blackberry|iemobile|opera mini/i.test(
|
|
149
|
+
userAgent
|
|
150
|
+
);
|
|
151
|
+
const isLargeMobile = isMobile && minDimension >= 600;
|
|
152
|
+
|
|
153
|
+
// 综合判断:用户代理匹配 或 (屏幕尺寸符合平板特征 且 支持触摸) 或 (大屏移动设备)
|
|
154
|
+
return (
|
|
155
|
+
isTabletByUserAgent ||
|
|
156
|
+
(isTabletByScreenSize && hasTouch) ||
|
|
157
|
+
isLargeMobile
|
|
158
|
+
)
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* 检测当前设备类型
|
|
163
|
+
* @returns {string} 'desktop' | 'tablet' | 'mobile'
|
|
164
|
+
*/
|
|
165
|
+
function getDeviceType() {
|
|
166
|
+
if (typeof window === 'undefined' || typeof navigator === 'undefined') {
|
|
167
|
+
return 'desktop'
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (isTablet()) {
|
|
171
|
+
return 'tablet'
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
const userAgent = navigator.userAgent.toLowerCase();
|
|
175
|
+
const isMobile = /android|webos|iphone|ipod|blackberry|iemobile|opera mini/i.test(
|
|
176
|
+
userAgent
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
return isMobile ? 'mobile' : 'desktop'
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* 检测是否为触摸设备
|
|
184
|
+
* @returns {boolean} 是否为触摸设备
|
|
185
|
+
*/
|
|
186
|
+
function isTouchDevice() {
|
|
187
|
+
if (typeof window === 'undefined' || typeof navigator === 'undefined') {
|
|
188
|
+
return false
|
|
189
|
+
}
|
|
190
|
+
return 'ontouchstart' in window || navigator.maxTouchPoints > 0
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* 获取设备信息
|
|
195
|
+
* @returns {object} 设备信息对象
|
|
196
|
+
*/
|
|
197
|
+
function getDeviceInfo() {
|
|
198
|
+
if (typeof window === 'undefined' || typeof navigator === 'undefined') {
|
|
199
|
+
return {
|
|
200
|
+
isTablet: false,
|
|
201
|
+
deviceType: 'desktop',
|
|
202
|
+
isTouchDevice: false,
|
|
203
|
+
userAgent: '',
|
|
204
|
+
screenWidth: 0,
|
|
205
|
+
screenHeight: 0,
|
|
206
|
+
devicePixelRatio: 1,
|
|
207
|
+
orientation: 'unknown',
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
return {
|
|
212
|
+
isTablet: isTablet(),
|
|
213
|
+
deviceType: getDeviceType(),
|
|
214
|
+
isTouchDevice: isTouchDevice(),
|
|
215
|
+
userAgent: navigator.userAgent,
|
|
216
|
+
screenWidth: window.screen.width,
|
|
217
|
+
screenHeight: window.screen.height,
|
|
218
|
+
devicePixelRatio: window.devicePixelRatio || 1,
|
|
219
|
+
orientation: screen.orientation ? screen.orientation.type : 'unknown',
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
var device = /*#__PURE__*/Object.freeze({
|
|
224
|
+
__proto__: null,
|
|
225
|
+
getDeviceInfo: getDeviceInfo,
|
|
226
|
+
getDeviceType: getDeviceType,
|
|
227
|
+
isTablet: isTablet,
|
|
228
|
+
isTouchDevice: isTouchDevice
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* @hhfenpm/medical-agent-utils
|
|
233
|
+
* 通用工具函数库
|
|
234
|
+
*/
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
var index = {
|
|
238
|
+
uuid: uuid$1,
|
|
239
|
+
get: get$1,
|
|
240
|
+
device,
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
export { index as default, get, getDeviceInfo, getDeviceType, isTablet, isTouchDevice, uuid, uuidLong };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* UUID 生成工具
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* 生成短 UUID
|
|
11
|
+
* @returns {string} UUID 字符串
|
|
12
|
+
*/
|
|
13
|
+
function uuid() {
|
|
14
|
+
let random;
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
const arr = new Uint32Array(1);
|
|
18
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/RandomSource/getRandomValues
|
|
19
|
+
if (typeof window !== 'undefined' && window.crypto) {
|
|
20
|
+
window.crypto.getRandomValues(arr);
|
|
21
|
+
random = arr[0] & 2147483647;
|
|
22
|
+
} else {
|
|
23
|
+
random = Math.floor(Math.random() * 2147483648);
|
|
24
|
+
}
|
|
25
|
+
} catch (b) {
|
|
26
|
+
random = Math.floor(Math.random() * 2147483648);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return random.toString(36)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* 生成长 UUID
|
|
34
|
+
* @returns {string} 长 UUID 字符串
|
|
35
|
+
*/
|
|
36
|
+
function uuidLong() {
|
|
37
|
+
return `${uuid()}${uuid()}${uuid()}`
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
var uuid$1 = /*#__PURE__*/Object.freeze({
|
|
41
|
+
__proto__: null,
|
|
42
|
+
default: uuid,
|
|
43
|
+
uuid: uuid,
|
|
44
|
+
uuidLong: uuidLong
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 安全获取对象属性工具
|
|
49
|
+
* 类似 lodash.get
|
|
50
|
+
*/
|
|
51
|
+
|
|
52
|
+
function baseGet(object, path) {
|
|
53
|
+
path = castPath(path);
|
|
54
|
+
|
|
55
|
+
var index = 0,
|
|
56
|
+
length = path.length;
|
|
57
|
+
|
|
58
|
+
while (object != null && index < length) {
|
|
59
|
+
object = object[path[index++]];
|
|
60
|
+
}
|
|
61
|
+
return index && index == length ? object : undefined
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function castPath(value, object) {
|
|
65
|
+
if (Array.isArray(value)) {
|
|
66
|
+
return value
|
|
67
|
+
}
|
|
68
|
+
return stringToPath(String(value))
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function stringToPath(string) {
|
|
72
|
+
var result = [];
|
|
73
|
+
if (string.charCodeAt(0) === 46 /* . */) {
|
|
74
|
+
result.push('');
|
|
75
|
+
}
|
|
76
|
+
string.replace(
|
|
77
|
+
/[^.[\]]+|\[(?:(-?\d+(?:\.\d+)?)|(["'])((?:(?!\2)[^\\]|\\.)*?)\2)\]|(?=(?:\.|\[\])(?:\.|\[\]|$))/g,
|
|
78
|
+
function (match, number, quote, subString) {
|
|
79
|
+
result.push(quote ? subString.replace(/\\(\\)?/g, '$1') : number || match);
|
|
80
|
+
}
|
|
81
|
+
);
|
|
82
|
+
return result
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* 获取对象属性
|
|
87
|
+
* @param {Object} object - 目标对象
|
|
88
|
+
* @param {string|Array} path - 属性路径
|
|
89
|
+
* @param {*} defaultValue - 默认值
|
|
90
|
+
* @returns {*} 属性值
|
|
91
|
+
*/
|
|
92
|
+
function get(object, path, defaultValue) {
|
|
93
|
+
var result = object == null ? undefined : baseGet(object, path);
|
|
94
|
+
return result === undefined ? defaultValue : result
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
var get$1 = /*#__PURE__*/Object.freeze({
|
|
98
|
+
__proto__: null,
|
|
99
|
+
get: get
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 设备检测工具函数
|
|
104
|
+
*/
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* 检测当前设备是否为平板
|
|
108
|
+
* @returns {boolean} 是否为平板设备
|
|
109
|
+
*/
|
|
110
|
+
function isTablet() {
|
|
111
|
+
if (typeof window === 'undefined' || typeof navigator === 'undefined') {
|
|
112
|
+
return false
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// 获取用户代理字符串
|
|
116
|
+
const userAgent = navigator.userAgent.toLowerCase();
|
|
117
|
+
|
|
118
|
+
// 检测常见的平板设备标识
|
|
119
|
+
const tabletPatterns = [
|
|
120
|
+
/ipad/, // iPad
|
|
121
|
+
/android.*tablet/, // Android 平板
|
|
122
|
+
/kindle/, // Kindle
|
|
123
|
+
/silk/, // Amazon Silk
|
|
124
|
+
/playbook/, // BlackBerry PlayBook
|
|
125
|
+
/bb10/, // BlackBerry 10
|
|
126
|
+
/rim tablet/, // BlackBerry Tablet OS
|
|
127
|
+
/windows.*touch/, // Windows 平板
|
|
128
|
+
/tablet/, // 通用平板标识
|
|
129
|
+
];
|
|
130
|
+
|
|
131
|
+
// 检查用户代理是否匹配平板模式
|
|
132
|
+
const isTabletByUserAgent = tabletPatterns.some((pattern) =>
|
|
133
|
+
pattern.test(userAgent)
|
|
134
|
+
);
|
|
135
|
+
|
|
136
|
+
// 检测屏幕尺寸和触摸支持
|
|
137
|
+
const hasTouch =
|
|
138
|
+
'ontouchstart' in window || navigator.maxTouchPoints > 0;
|
|
139
|
+
const screenWidth = window.screen.width;
|
|
140
|
+
const screenHeight = window.screen.height;
|
|
141
|
+
const minDimension = Math.min(screenWidth, screenHeight);
|
|
142
|
+
const maxDimension = Math.max(screenWidth, screenHeight);
|
|
143
|
+
|
|
144
|
+
// 平板设备的典型屏幕尺寸范围
|
|
145
|
+
const isTabletByScreenSize =
|
|
146
|
+
minDimension >= 600 &&
|
|
147
|
+
minDimension <= 1200 &&
|
|
148
|
+
maxDimension >= 800 &&
|
|
149
|
+
maxDimension <= 1600;
|
|
150
|
+
|
|
151
|
+
// 检测是否为移动设备但屏幕较大(可能是平板)
|
|
152
|
+
const isMobile = /android|webos|iphone|ipod|blackberry|iemobile|opera mini/i.test(
|
|
153
|
+
userAgent
|
|
154
|
+
);
|
|
155
|
+
const isLargeMobile = isMobile && minDimension >= 600;
|
|
156
|
+
|
|
157
|
+
// 综合判断:用户代理匹配 或 (屏幕尺寸符合平板特征 且 支持触摸) 或 (大屏移动设备)
|
|
158
|
+
return (
|
|
159
|
+
isTabletByUserAgent ||
|
|
160
|
+
(isTabletByScreenSize && hasTouch) ||
|
|
161
|
+
isLargeMobile
|
|
162
|
+
)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* 检测当前设备类型
|
|
167
|
+
* @returns {string} 'desktop' | 'tablet' | 'mobile'
|
|
168
|
+
*/
|
|
169
|
+
function getDeviceType() {
|
|
170
|
+
if (typeof window === 'undefined' || typeof navigator === 'undefined') {
|
|
171
|
+
return 'desktop'
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
if (isTablet()) {
|
|
175
|
+
return 'tablet'
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const userAgent = navigator.userAgent.toLowerCase();
|
|
179
|
+
const isMobile = /android|webos|iphone|ipod|blackberry|iemobile|opera mini/i.test(
|
|
180
|
+
userAgent
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
return isMobile ? 'mobile' : 'desktop'
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* 检测是否为触摸设备
|
|
188
|
+
* @returns {boolean} 是否为触摸设备
|
|
189
|
+
*/
|
|
190
|
+
function isTouchDevice() {
|
|
191
|
+
if (typeof window === 'undefined' || typeof navigator === 'undefined') {
|
|
192
|
+
return false
|
|
193
|
+
}
|
|
194
|
+
return 'ontouchstart' in window || navigator.maxTouchPoints > 0
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* 获取设备信息
|
|
199
|
+
* @returns {object} 设备信息对象
|
|
200
|
+
*/
|
|
201
|
+
function getDeviceInfo() {
|
|
202
|
+
if (typeof window === 'undefined' || typeof navigator === 'undefined') {
|
|
203
|
+
return {
|
|
204
|
+
isTablet: false,
|
|
205
|
+
deviceType: 'desktop',
|
|
206
|
+
isTouchDevice: false,
|
|
207
|
+
userAgent: '',
|
|
208
|
+
screenWidth: 0,
|
|
209
|
+
screenHeight: 0,
|
|
210
|
+
devicePixelRatio: 1,
|
|
211
|
+
orientation: 'unknown',
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return {
|
|
216
|
+
isTablet: isTablet(),
|
|
217
|
+
deviceType: getDeviceType(),
|
|
218
|
+
isTouchDevice: isTouchDevice(),
|
|
219
|
+
userAgent: navigator.userAgent,
|
|
220
|
+
screenWidth: window.screen.width,
|
|
221
|
+
screenHeight: window.screen.height,
|
|
222
|
+
devicePixelRatio: window.devicePixelRatio || 1,
|
|
223
|
+
orientation: screen.orientation ? screen.orientation.type : 'unknown',
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
var device = /*#__PURE__*/Object.freeze({
|
|
228
|
+
__proto__: null,
|
|
229
|
+
getDeviceInfo: getDeviceInfo,
|
|
230
|
+
getDeviceType: getDeviceType,
|
|
231
|
+
isTablet: isTablet,
|
|
232
|
+
isTouchDevice: isTouchDevice
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* @hhfenpm/medical-agent-utils
|
|
237
|
+
* 通用工具函数库
|
|
238
|
+
*/
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
var index = {
|
|
242
|
+
uuid: uuid$1,
|
|
243
|
+
get: get$1,
|
|
244
|
+
device,
|
|
245
|
+
};
|
|
246
|
+
|
|
247
|
+
exports.default = index;
|
|
248
|
+
exports.get = get;
|
|
249
|
+
exports.getDeviceInfo = getDeviceInfo;
|
|
250
|
+
exports.getDeviceType = getDeviceType;
|
|
251
|
+
exports.isTablet = isTablet;
|
|
252
|
+
exports.isTouchDevice = isTouchDevice;
|
|
253
|
+
exports.uuid = uuid;
|
|
254
|
+
exports.uuidLong = uuidLong;
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hhfenpm/medical-agent-utils",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "通用工具函数库,包含UUID生成、对象属性获取、设备检测等常用工具",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"module": "dist/index.esm.js",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"README.md"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "rollup -c",
|
|
13
|
+
"dev": "rollup -c -w",
|
|
14
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
15
|
+
"prepublishOnly": "npm run build"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"utils",
|
|
19
|
+
"tools",
|
|
20
|
+
"uuid",
|
|
21
|
+
"get",
|
|
22
|
+
"device"
|
|
23
|
+
],
|
|
24
|
+
"author": "",
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@rollup/plugin-commonjs": "^25.0.0",
|
|
28
|
+
"@rollup/plugin-node-resolve": "^15.0.0",
|
|
29
|
+
"rollup": "^3.29.5"
|
|
30
|
+
},
|
|
31
|
+
"publishConfig": {
|
|
32
|
+
"access": "public"
|
|
33
|
+
},
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": ""
|
|
37
|
+
},
|
|
38
|
+
"bugs": {
|
|
39
|
+
"url": ""
|
|
40
|
+
},
|
|
41
|
+
"homepage": ""
|
|
42
|
+
}
|