js工具方法
... 2020-9-25 About 1 min
# js工具方法
# 识别设备类型
// 判断设备类型的方法,设备像素比
const ua = window.navigator.userAgent;
const isAndroid = /Android/i.test(ua);
const isIOS = /iP[hone|ad|od] OS/i.test(ua);
const isIphone = /iPhone/i.test(ua);
// iphoneX iphoneXS 刘海高度 30px
const isIphoneX = !!(
isIphone &&
window.devicePixelRatio &&
window.devicePixelRatio === 3 &&
window.screen.width === 375 &&
window.screen.height === 812
);
// 刘海高度: 44px
const isIphoneXSMAX = !!(
isIphone &&
window.devicePixelRatio &&
window.devicePixelRatio === 3 &&
window.screen.width === 414 &&
window.screen.height === 896
);
// 刘海高度 33px
const isIphoneXR = !!(
isIphone &&
window.devicePixelRatio &&
window.devicePixelRatio === 2 &&
window.screen.width === 414 &&
window.screen.height === 896
);
// iPhoneX版本以上的刘海屏
const isIphoneXup = isIphoneX && isIphoneXSMAX && isIphoneXR;
export default {
isAndroid,
isIOS,
isIphone,
isIphoneX,
isIphoneXSMAX,
isIphoneXR,
isIphoneXup,
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 使用ES6简单封装localStoage、sessionStorage
class store {
constructor (store) {
if (!store) {
return new Error('当前环境暂不支持localStorage')
}
this._store = store
}
setItem (_k, _v) {
if (!this._store) return
let kType = this.getType(_k)
if (kType === 'string') {
this._store.setItem(_k, this.filterValue(_v))
}else{
return new Error('key只能为字符串')
}
}
getItem (_k) {
if (!this._store) return
let kType = this.getType(_k)
if (kType !== 'string') {
return new Error('key只能为字符串')
}
return this._store.getItem(_k)
}
removeItem (_k) {
if (!this._store) return
let kType = this.getType(_k)
if (kType !== 'string') {
return new Error('key只能为字符串')
}
return this._store.removeItem(_k)
}
clear () {
if (!this._store) return
this._store.clear()
}
getType (key) {
return Object.prototype.toString.call(key).match(/\[object (.*?)\]/)[1].toLowerCase()
}
filterValue (value) {
let vType = this.getType(value)
let nullValue = ['undefined', 'null']
let stringValue = ['boolean', 'number', 'string']
if (~nullValue.indexOf(vType) || isNaN(value)) {
return ''
}
if (~stringValue.indexOf(vType)) {
return value
}
return JSON.stringify(value)
}
}
class LocalStorage extends store {
constructor (store) {
super(store)
}
WX_USER_ID = 'WX_USER_ID'
}
class SessionStorage extends store {
constructor (store) {
super(store)
}
WX_SSO_TITLE = 'WX_SSO_TITLE'
}
const LS = new LocalStorage(window.localStorage || localStorage)
const SS = new SessionStorage(window.sessionStorage || sessionStorage)
export {LS,SS}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
使用
import {LS,SS} from './storage.js'
LS.setItem()
SS.getItem()
1
2
3
2
3