gis-common 2.1.2 → 2.2.3
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/package.json +5 -3
- package/src/assets/images/location.png +0 -0
- package/src/constant/ErrorTypeConstant.js +20 -0
- package/src/constant/EventTypeConstant.js +23 -0
- package/src/constant/GraphicConstant.js +16 -0
- package/src/constant/LayerType.js +9 -0
- package/src/constant/index.js +4 -0
- package/src/core/AudioPlayer.js +30 -0
- package/src/core/CanvasDrawer.js +51 -0
- package/src/core/DevicePixelRatio.js +86 -0
- package/src/core/ElQuery.js +88 -0
- package/src/core/EventDispatcher.js +93 -0
- package/src/core/HashMap.js +36 -0
- package/src/core/MqttClient.js +108 -0
- package/src/core/WebSocketClient.js +104 -0
- package/src/core/WebStorage.js +73 -0
- package/src/index.js +10 -0
- package/src/utils/AnimateUtils.js +78 -0
- package/src/utils/ArrayUtils.js +82 -0
- package/src/utils/BrowserUtils.js +107 -0
- package/src/utils/CommUtils.js +230 -0
- package/src/utils/Cookie.js +23 -0
- package/src/utils/CoordsUtils.js +139 -0
- package/src/utils/DateUtils.js +130 -0
- package/src/utils/DomUtils.js +94 -0
- package/src/utils/FileUtils.js +105 -0
- package/src/utils/GeoUtils.js +242 -0
- package/src/utils/MathUtils.js +49 -0
- package/src/utils/OptimizeUtils.js +94 -0
- package/src/utils/StringUtils.js +135 -0
- package/src/utils/index.js +13 -0
- package/dist/resource.min.js +0 -2686
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import EventType from '../constant/EventTypeConstant'
|
|
2
|
+
import EventDispatcher from './EventDispatcher'
|
|
3
|
+
export default class WebSocketClient extends EventDispatcher {
|
|
4
|
+
constructor(url = 'ws://127.0.0.1:10088') {
|
|
5
|
+
super()
|
|
6
|
+
this.maxCheckTimes = 10
|
|
7
|
+
this.url = url
|
|
8
|
+
this.checkTimes = 0
|
|
9
|
+
this.connectStatus = false
|
|
10
|
+
this.connect()
|
|
11
|
+
this.connCheckStatus(this.maxCheckTimes)
|
|
12
|
+
// this.heartbeat()
|
|
13
|
+
WebSocketClient.instance = this
|
|
14
|
+
}
|
|
15
|
+
connect() {
|
|
16
|
+
this.disconnect()
|
|
17
|
+
if (this.url) {
|
|
18
|
+
try {
|
|
19
|
+
console.info('创建ws连接>>>' + this.url)
|
|
20
|
+
this.client = new WebSocket(this.url)
|
|
21
|
+
if (this.client) {
|
|
22
|
+
const self = this
|
|
23
|
+
this.client.onopen = function (message) {
|
|
24
|
+
self.dispatchEvent({
|
|
25
|
+
type: EventType.WEB_SOCKET_CONNECT,
|
|
26
|
+
message,
|
|
27
|
+
})
|
|
28
|
+
}
|
|
29
|
+
this.client.onmessage = function (message) {
|
|
30
|
+
self.connectStatus = true
|
|
31
|
+
self.dispatchEvent({
|
|
32
|
+
type: EventType.WEB_SOCKET_MESSAGE,
|
|
33
|
+
message,
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
this.client.onclose = function (message) {
|
|
37
|
+
self.dispatchEvent({
|
|
38
|
+
type: EventType.WEB_SOCKET_CLOSE,
|
|
39
|
+
message,
|
|
40
|
+
})
|
|
41
|
+
}
|
|
42
|
+
if (this.checkTimes === this.maxCheckTimes) {
|
|
43
|
+
this.client.onerror = function (message) {
|
|
44
|
+
self.dispatchEvent({
|
|
45
|
+
type: EventType.WEB_SOCKET_ERROR,
|
|
46
|
+
message,
|
|
47
|
+
})
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
} catch (ex) {
|
|
52
|
+
console.error('创建ws连接失败' + this.url + ':' + ex)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
disconnect() {
|
|
57
|
+
if (this.client) {
|
|
58
|
+
try {
|
|
59
|
+
console.log('ws断开连接' + this.url)
|
|
60
|
+
this.client.close()
|
|
61
|
+
this.client = null
|
|
62
|
+
} catch (ex) {
|
|
63
|
+
this.client = null
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
connCheckStatus(times) {
|
|
68
|
+
if (this.checkTimes > times) return
|
|
69
|
+
setTimeout(() => {
|
|
70
|
+
this.checkTimes++
|
|
71
|
+
if (this.client && this.client.readyState !== 0 && this.client.readyState !== 1) {
|
|
72
|
+
this.connect()
|
|
73
|
+
}
|
|
74
|
+
this.connCheckStatus(times)
|
|
75
|
+
}, 2000)
|
|
76
|
+
}
|
|
77
|
+
send(message) {
|
|
78
|
+
if (this.client && this.client.readyState === 1) {
|
|
79
|
+
this.client.send(message)
|
|
80
|
+
return true
|
|
81
|
+
}
|
|
82
|
+
console.error(this.url + '消息发送失败:' + message)
|
|
83
|
+
return false
|
|
84
|
+
}
|
|
85
|
+
heartbeat() {
|
|
86
|
+
setTimeout(() => {
|
|
87
|
+
if (this.client && this.client.readyState === 1) {
|
|
88
|
+
this.send('HeartBeat')
|
|
89
|
+
}
|
|
90
|
+
console.log('HeartBeat,' + this.url)
|
|
91
|
+
setTimeout(this.heartbeat, 30000)
|
|
92
|
+
}, 1000)
|
|
93
|
+
}
|
|
94
|
+
static getInstance() {
|
|
95
|
+
if (WebSocketClient.instance) {
|
|
96
|
+
return WebSocketClient.instance
|
|
97
|
+
} else {
|
|
98
|
+
return Object.freeze(new WebSocketClient())
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
static get _instance() {
|
|
102
|
+
return this.getInstance()
|
|
103
|
+
}
|
|
104
|
+
}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import ErrorType from "../constant/ErrorTypeConstant";
|
|
2
|
+
export default class WebStorage {
|
|
3
|
+
constructor(type = 1) {
|
|
4
|
+
this.cacheType = type;
|
|
5
|
+
if (type === 1) {
|
|
6
|
+
this.storage = window.localStorage;
|
|
7
|
+
} else if (type === 2) {
|
|
8
|
+
this.storage = window.sessionStorage;
|
|
9
|
+
} else {
|
|
10
|
+
throw new Error(ErrorType.PARAMETER_ERROR);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 设置带过期时间的LocalStorage
|
|
16
|
+
* @param key 缓存关键字
|
|
17
|
+
* @param value 缓存对象,可以是任意类型
|
|
18
|
+
* @param expired 以秒为单位,默认为1小时
|
|
19
|
+
* @returns {Object}
|
|
20
|
+
*/
|
|
21
|
+
setItem(key, value, expired) {
|
|
22
|
+
const tempObj = { key, value };
|
|
23
|
+
if (this.cacheType === 1) {
|
|
24
|
+
const currTime = Date.now();
|
|
25
|
+
if (expired) {
|
|
26
|
+
tempObj.expired = currTime + expired * 1000;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
this.storage[key] = JSON.stringify(tempObj);
|
|
30
|
+
return tempObj;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/***
|
|
34
|
+
* 获取带过期时间的缓存
|
|
35
|
+
* @param key
|
|
36
|
+
* @returns {null|*}
|
|
37
|
+
*/
|
|
38
|
+
getItem(key) {
|
|
39
|
+
const storageData = this.storage.getItem(key) || null;
|
|
40
|
+
const tempObj = JSON.parse(storageData) || {};
|
|
41
|
+
if (this.cacheType === 1) {
|
|
42
|
+
const expired = tempObj.expired || Date.now();
|
|
43
|
+
if (Date.now() > expired) {
|
|
44
|
+
this.remove(key);
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return tempObj.value;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 移除指定缓存
|
|
53
|
+
* @param keys
|
|
54
|
+
*/
|
|
55
|
+
remove(keys) {
|
|
56
|
+
if (keys) {
|
|
57
|
+
if (keys.indexOf(",") > -1) {
|
|
58
|
+
keys.split(",").forEach(key => {
|
|
59
|
+
this.storage.removeItem(key);
|
|
60
|
+
});
|
|
61
|
+
} else {
|
|
62
|
+
this.storage.removeItem(keys);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 移出全部缓存
|
|
69
|
+
*/
|
|
70
|
+
clear() {
|
|
71
|
+
this.storage.clear();
|
|
72
|
+
}
|
|
73
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './utils'
|
|
2
|
+
export * from './constant'
|
|
3
|
+
export { default as AudioPlayer } from './core/AudioPlayer'
|
|
4
|
+
export { default as CanvasDrawer } from './core/CanvasDrawer'
|
|
5
|
+
export { default as ElQuery } from './core/ElQuery'
|
|
6
|
+
export { default as EventDispatcher } from './core/EventDispatcher'
|
|
7
|
+
export { default as HashMap } from './core/HashMap'
|
|
8
|
+
export { default as WebSocketClient } from './core/WebSocketClient'
|
|
9
|
+
export { default as WebStorage } from './core/WebStorage'
|
|
10
|
+
export { default as DevicePixelRatio } from './core/DevicePixelRatio'
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
/**
|
|
3
|
+
* 多属性的动画函数
|
|
4
|
+
* @param {*} ele 需要运动的节点
|
|
5
|
+
* @param {*} attr_options 传入的是一个需要运动的属性包括运动的目标值,操作的节点属性
|
|
6
|
+
* @param {*} timefn 运动的形式,默认是缓冲运动
|
|
7
|
+
* @param {*} speed 运动的速度
|
|
8
|
+
*/
|
|
9
|
+
animate(ele, attr_options, callback, timefn = "swing", speed = 5) {
|
|
10
|
+
for (var attr in attr_options) {
|
|
11
|
+
attr_options[attr] = {
|
|
12
|
+
target:
|
|
13
|
+
attr === "opacity"
|
|
14
|
+
? parseInt(attr_options[attr] * 100)
|
|
15
|
+
: attr_options[attr],
|
|
16
|
+
// 需要计算得到
|
|
17
|
+
iNow:
|
|
18
|
+
attr === "opacity"
|
|
19
|
+
? parseInt(getComputedStyle(ele)[attr] * 100)
|
|
20
|
+
: parseInt(getComputedStyle(ele)[attr])
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// 判断是不是匀速运动
|
|
25
|
+
if (timefn === "liner") {
|
|
26
|
+
speed =
|
|
27
|
+
attr_options[attr].iNow < attr_options[attr].target
|
|
28
|
+
? Math.abs(speed)
|
|
29
|
+
: -Math.abs(speed);
|
|
30
|
+
}
|
|
31
|
+
// 为了防止每个节点的定时器冲突,每次进入的时候我们需要先清理下节点的定时器
|
|
32
|
+
clearInterval(ele.timer);
|
|
33
|
+
// 然后再去设置定时器
|
|
34
|
+
ele.timer = setInterval(function() {
|
|
35
|
+
// 因为是多属性运动,我们首先循环遍历每一个属性
|
|
36
|
+
for (var attr in attr_options) {
|
|
37
|
+
var target = attr_options[attr].target;
|
|
38
|
+
|
|
39
|
+
var iNow = attr_options[attr].iNow;
|
|
40
|
+
|
|
41
|
+
// 计算speed 判断是不是缓冲运动
|
|
42
|
+
if (timefn === "swing") {
|
|
43
|
+
speed = (target - iNow) / 20;
|
|
44
|
+
// 为了防止速度无法达到一个整数
|
|
45
|
+
speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 运动之中条件的判断
|
|
49
|
+
if (Math.abs(target - iNow) <= Math.abs(speed)) {
|
|
50
|
+
if (attr === "opacity") {
|
|
51
|
+
ele.style[attr] = target / 100;
|
|
52
|
+
} else {
|
|
53
|
+
ele.style[attr] = target + "px";
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// 每次一个属性运动完成之后我们就直接将它删除
|
|
57
|
+
delete attr_options[attr];
|
|
58
|
+
|
|
59
|
+
// 每次都去循环看一下还有没有属性需要操作
|
|
60
|
+
for (var num in attr_options) {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
// 清理定时器 因为我们是一个定时器绑定多个属性操作所以我们需要保证所有的属性都完成之后我们才可以将定时器清除
|
|
64
|
+
clearInterval(ele.timer);
|
|
65
|
+
// 等结束之后我们需要判断这个回到函数是函数我们就回调
|
|
66
|
+
typeof callback === "function" ? callback() : "";
|
|
67
|
+
} else {
|
|
68
|
+
attr_options[attr].iNow += speed;
|
|
69
|
+
if (attr === "opacity") {
|
|
70
|
+
ele.style[attr] = attr_options[attr].iNow / 100;
|
|
71
|
+
} else {
|
|
72
|
+
ele.style[attr] = attr_options[attr].iNow + "px";
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}, 30);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import CommUtils from './CommUtils'
|
|
2
|
+
/**
|
|
3
|
+
* 分组函数,暂时无法进行多层分组。如有需要可以使用groupBy+map+flat方法打平
|
|
4
|
+
* @param {*} f
|
|
5
|
+
*/
|
|
6
|
+
Array.prototype.groupBy = function (f) {
|
|
7
|
+
var groups = {}
|
|
8
|
+
this.forEach(function (o) {
|
|
9
|
+
var group = JSON.stringify(f(o))
|
|
10
|
+
groups[group] = groups[group] || []
|
|
11
|
+
groups[group].push(o)
|
|
12
|
+
})
|
|
13
|
+
return Object.keys(groups).map((group) => groups[group])
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* 用于数组对象去重
|
|
17
|
+
* 单纯数组可用[...new Set(arr)]
|
|
18
|
+
* @param {*} f
|
|
19
|
+
*/
|
|
20
|
+
Array.prototype.distinct = function (f) {
|
|
21
|
+
const arr = [],
|
|
22
|
+
obj = {}
|
|
23
|
+
this.forEach((item) => {
|
|
24
|
+
const val = f(item)
|
|
25
|
+
!obj[val] && (obj[val] = arr.push(item))
|
|
26
|
+
})
|
|
27
|
+
return arr
|
|
28
|
+
}
|
|
29
|
+
Array.prototype.max = function () {
|
|
30
|
+
return Math.max.apply({}, this)
|
|
31
|
+
}
|
|
32
|
+
Array.prototype.min = function () {
|
|
33
|
+
return Math.min.apply({}, this)
|
|
34
|
+
}
|
|
35
|
+
Array.prototype.sum = function () {
|
|
36
|
+
return this.length > 0 ? this.reduce((prev = 0, curr = 0) => prev + curr) : 0
|
|
37
|
+
}
|
|
38
|
+
Array.prototype.avg = function () {
|
|
39
|
+
return this.length ? this.sum() / this.length : 0
|
|
40
|
+
}
|
|
41
|
+
Array.prototype.desc = function (f) {
|
|
42
|
+
return this.sort((n1, n2) => (f ? f(n2) : n2) - (f ? f(n1) : n1))
|
|
43
|
+
}
|
|
44
|
+
Array.prototype.asc = function (f) {
|
|
45
|
+
return this.sort((n1, n2) => (f ? f(n1) : n1) - (f ? f(n2) : n2))
|
|
46
|
+
}
|
|
47
|
+
Array.prototype.clear = function () {
|
|
48
|
+
this.length = 0
|
|
49
|
+
return this
|
|
50
|
+
}
|
|
51
|
+
export default {
|
|
52
|
+
asArray(obj) {
|
|
53
|
+
return CommUtils.isEmpty(obj) ? [] : Array.isArray(obj) ? obj : [obj]
|
|
54
|
+
},
|
|
55
|
+
generateArray(length) {
|
|
56
|
+
return [...new Array(length).keys()]
|
|
57
|
+
},
|
|
58
|
+
union(...args) {
|
|
59
|
+
let res = []
|
|
60
|
+
args.forEach((arg) => {
|
|
61
|
+
if (Array.isArray(arg)) {
|
|
62
|
+
res = res.concat(arg.filter((v) => !res.includes(v)))
|
|
63
|
+
}
|
|
64
|
+
})
|
|
65
|
+
return res
|
|
66
|
+
},
|
|
67
|
+
intersection(...args) {
|
|
68
|
+
let res = args[0] || []
|
|
69
|
+
args.forEach((arg) => {
|
|
70
|
+
if (Array.isArray(arg)) {
|
|
71
|
+
res = res.filter((v) => arg.includes(v))
|
|
72
|
+
}
|
|
73
|
+
})
|
|
74
|
+
return res
|
|
75
|
+
},
|
|
76
|
+
unionAll(...args) {
|
|
77
|
+
return [...args].flat().filter((d) => !!d)
|
|
78
|
+
},
|
|
79
|
+
difference(...args) {
|
|
80
|
+
return this.union(...args).filter((d) => !this.intersection(...args).includes(d))
|
|
81
|
+
},
|
|
82
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
export default {
|
|
2
|
+
getUrlParams(href = window.location.href, needDecode = true) {
|
|
3
|
+
const reg = /([^&=]+)=([\w\W]*?)(&|$|#)/g
|
|
4
|
+
const { search, hash } = new URL(href)
|
|
5
|
+
const args = [search, hash]
|
|
6
|
+
let obj = {}
|
|
7
|
+
for (let i = 0; i < args.length; i++) {
|
|
8
|
+
const str = args[i]
|
|
9
|
+
if (str) {
|
|
10
|
+
const s = str.replace(/#|\//g, '')
|
|
11
|
+
const arr = s.split('?')
|
|
12
|
+
if (arr.length > 1) {
|
|
13
|
+
for (let i = 1; i < arr.length; i++) {
|
|
14
|
+
let res
|
|
15
|
+
while ((res = reg.exec(arr[i]))) {
|
|
16
|
+
obj[res[1]] = needDecode ? decodeURIComponent(res[2]) : res[2]
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return obj
|
|
23
|
+
},
|
|
24
|
+
getExplorer() {
|
|
25
|
+
var explorer = window.navigator.userAgent
|
|
26
|
+
if (explorer.indexOf('MSIE') >= 0) {
|
|
27
|
+
return 'IE'
|
|
28
|
+
}
|
|
29
|
+
if (!!window.ActiveXObject || 'ActiveXObject' in window) {
|
|
30
|
+
// IE
|
|
31
|
+
return 'IE'
|
|
32
|
+
} else if (explorer.indexOf('Firefox') >= 0) {
|
|
33
|
+
// Firefox
|
|
34
|
+
return 'Firefox'
|
|
35
|
+
} else if (explorer.indexOf('Chrome') >= 0) {
|
|
36
|
+
// Chrome
|
|
37
|
+
return 'Chrome'
|
|
38
|
+
} else if (explorer.indexOf('Opera') >= 0) {
|
|
39
|
+
// Opera
|
|
40
|
+
return 'Opera'
|
|
41
|
+
} else if (explorer.indexOf('Safari') >= 0) {
|
|
42
|
+
// Safari
|
|
43
|
+
return 'Safari'
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
detectOS() {
|
|
47
|
+
let os_type = ''
|
|
48
|
+
const windows = navigator.userAgent.indexOf('Windows', 0) != -1 ? 1 : 0
|
|
49
|
+
const mac = navigator.userAgent.indexOf('mac', 0) != -1 ? 1 : 0
|
|
50
|
+
const linux = navigator.userAgent.indexOf('Linux', 0) != -1 ? 1 : 0
|
|
51
|
+
const unix = navigator.userAgent.indexOf('X11', 0) != -1 ? 1 : 0
|
|
52
|
+
if (windows) os_type = 'MS Windows'
|
|
53
|
+
else if (mac) os_type = 'Apple mac'
|
|
54
|
+
else if (linux) os_type = 'Linux'
|
|
55
|
+
else if (unix) os_type = 'Unix'
|
|
56
|
+
return os_type
|
|
57
|
+
},
|
|
58
|
+
switchFullScreen(status) {
|
|
59
|
+
if (status) {
|
|
60
|
+
const element = document.documentElement
|
|
61
|
+
if (element.requestFullscreen) {
|
|
62
|
+
element.requestFullscreen()
|
|
63
|
+
} else if (element.msRequestFullscreen) {
|
|
64
|
+
element.msRequestFullscreen()
|
|
65
|
+
} else if (element.mozRequestFullScreen) {
|
|
66
|
+
element.mozRequestFullScreen()
|
|
67
|
+
} else if (element.webkitRequestFullscreen) {
|
|
68
|
+
element.webkitRequestFullscreen()
|
|
69
|
+
}
|
|
70
|
+
} else {
|
|
71
|
+
if (document.exitFullscreen) {
|
|
72
|
+
document.exitFullscreen()
|
|
73
|
+
} else if (document.msExitFullscreen) {
|
|
74
|
+
document.msExitFullscreen()
|
|
75
|
+
} else if (document.mozCancelFullScreen) {
|
|
76
|
+
document.mozCancelFullScreen()
|
|
77
|
+
} else if (document.webkitExitFullscreen) {
|
|
78
|
+
document.webkitExitFullscreen()
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
/**
|
|
83
|
+
* scale屏幕适配方案
|
|
84
|
+
*/
|
|
85
|
+
refreshScale() {
|
|
86
|
+
const baseWidth = document.documentElement.clientWidth
|
|
87
|
+
const baseHeight = document.documentElement.clientHeight
|
|
88
|
+
const appStyle = document.getElementById('app').style
|
|
89
|
+
const realRatio = baseWidth / baseHeight
|
|
90
|
+
const designRatio = 16 / 9
|
|
91
|
+
let scaleRate = baseWidth / 1920
|
|
92
|
+
if (realRatio > designRatio) {
|
|
93
|
+
scaleRate = baseHeight / 1080
|
|
94
|
+
}
|
|
95
|
+
appStyle.transformOrigin = 'left top'
|
|
96
|
+
appStyle.transform = `scale(${scaleRate}) translateX(-49.99%)`
|
|
97
|
+
appStyle.width = `${baseWidth / scaleRate}px`
|
|
98
|
+
},
|
|
99
|
+
/**
|
|
100
|
+
* rem屏幕适配方案
|
|
101
|
+
*/
|
|
102
|
+
getHtmlFontSize() {
|
|
103
|
+
const htmlwidth = document.documentElement.clientWidth || document.body.clientWidth
|
|
104
|
+
const htmlDom = document.querySelector('html')
|
|
105
|
+
htmlDom.style.fontSize = htmlwidth / 192 + 'px'
|
|
106
|
+
},
|
|
107
|
+
}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { ErrorType } from '../constant'
|
|
2
|
+
|
|
3
|
+
/* eslint-disable no-extend-native, space-in-parens */
|
|
4
|
+
export default {
|
|
5
|
+
/**
|
|
6
|
+
* 判断数据类型
|
|
7
|
+
*
|
|
8
|
+
* @param {*} data
|
|
9
|
+
* @returns {*}
|
|
10
|
+
*/
|
|
11
|
+
getDataType(data) {
|
|
12
|
+
return Object.prototype.toString.call(data).slice(8, -1)
|
|
13
|
+
},
|
|
14
|
+
isEmpty(value) {
|
|
15
|
+
if (value == null) {
|
|
16
|
+
// 等同于 value === undefined || value === null
|
|
17
|
+
return true
|
|
18
|
+
}
|
|
19
|
+
const type = this.getDataType(value)
|
|
20
|
+
switch (type) {
|
|
21
|
+
case 'String':
|
|
22
|
+
return value.trim() === ''
|
|
23
|
+
case 'Array':
|
|
24
|
+
return !value.length
|
|
25
|
+
case 'Object':
|
|
26
|
+
return !Object.keys(value).length
|
|
27
|
+
case 'Boolean':
|
|
28
|
+
return !value
|
|
29
|
+
default:
|
|
30
|
+
return false // 其他对象均视作非空
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
isNotEmpty(val) {
|
|
34
|
+
return !this.isEmpty(val)
|
|
35
|
+
},
|
|
36
|
+
json2form(json) {
|
|
37
|
+
const formData = new FormData()
|
|
38
|
+
Object.keys(json).forEach((key) => {
|
|
39
|
+
formData.append(key, json[key] instanceof Object ? JSON.stringify(json[key]) : json[key])
|
|
40
|
+
})
|
|
41
|
+
return formData
|
|
42
|
+
},
|
|
43
|
+
/**
|
|
44
|
+
* json转换成get参数形式
|
|
45
|
+
*
|
|
46
|
+
* @param {*} json
|
|
47
|
+
* @returns {*}
|
|
48
|
+
*/
|
|
49
|
+
json2Query(json) {
|
|
50
|
+
var tempArr = []
|
|
51
|
+
for (var i in json) {
|
|
52
|
+
var key = i
|
|
53
|
+
var value = json[i]
|
|
54
|
+
tempArr.push(key + '=' + value)
|
|
55
|
+
}
|
|
56
|
+
var urlParamsStr = tempArr.join('&')
|
|
57
|
+
return urlParamsStr
|
|
58
|
+
},
|
|
59
|
+
generateGuid() {
|
|
60
|
+
const S4 = function () {
|
|
61
|
+
return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
|
|
62
|
+
}
|
|
63
|
+
return S4() + S4() + S4() + S4() + S4() + S4() + S4() + S4()
|
|
64
|
+
},
|
|
65
|
+
decodeDict(...args) {
|
|
66
|
+
let res = ''
|
|
67
|
+
if (args.length > 1) {
|
|
68
|
+
const items = args.slice(1, args.length % 2 === 0 ? args.length - 1 : args.length)
|
|
69
|
+
for (let i = 0; i < items.length; i = i + 2) {
|
|
70
|
+
const item = items[i]
|
|
71
|
+
if (args[0] === item) {
|
|
72
|
+
res = items[i + 1]
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
if (!res && args.length % 2 === 0) {
|
|
76
|
+
res = args[args.length - 1]
|
|
77
|
+
}
|
|
78
|
+
} else {
|
|
79
|
+
res = args[0]
|
|
80
|
+
}
|
|
81
|
+
return res
|
|
82
|
+
},
|
|
83
|
+
extend(dest, ...args) {
|
|
84
|
+
let i, j, len, src
|
|
85
|
+
for (j = 0, len = args.length; j < len; j++) {
|
|
86
|
+
src = args[j]
|
|
87
|
+
for (i in src) {
|
|
88
|
+
dest[i] = src[i]
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return dest
|
|
92
|
+
},
|
|
93
|
+
rgb2hex(rgb) {
|
|
94
|
+
var hex = '#' + ((1 << 24) + (rgb[0] << 16) + (rgb[1] << 8) + rgb[2]).toString(16).slice(1)
|
|
95
|
+
return hex
|
|
96
|
+
},
|
|
97
|
+
/**
|
|
98
|
+
* 将平级对象列表转换为树形结构对象列表
|
|
99
|
+
*
|
|
100
|
+
* @param {Array} data
|
|
101
|
+
* @param {string} [idPropertyName="id"]
|
|
102
|
+
* @param {string} [parentIdPropertyName="parentId"]
|
|
103
|
+
* @param {string} [childrenPropertyName="children"]
|
|
104
|
+
* @returns {*}
|
|
105
|
+
*/
|
|
106
|
+
convertToTree2(data, idPropertyName = 'id', parentIdPropertyName = 'parentId', childrenPropertyName = 'children') {
|
|
107
|
+
const result = []
|
|
108
|
+
function buildChildren(item) {
|
|
109
|
+
const id = item[idPropertyName]
|
|
110
|
+
const children = data.filter((item2) => item2.hasOwnProperty(parentIdPropertyName) && item2[parentIdPropertyName] === id)
|
|
111
|
+
if (children.length > 0) {
|
|
112
|
+
children.forEach(buildChildren)
|
|
113
|
+
item[childrenPropertyName] = children
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
for (const item of data) {
|
|
117
|
+
if (data.findIndex((t) => t[idPropertyName] === item[parentIdPropertyName]) === -1) {
|
|
118
|
+
buildChildren(item)
|
|
119
|
+
result.push(item)
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return result
|
|
123
|
+
},
|
|
124
|
+
/**
|
|
125
|
+
* 异步加载script
|
|
126
|
+
*
|
|
127
|
+
* @param {*} url
|
|
128
|
+
*/
|
|
129
|
+
asyncLoadScript(url) {
|
|
130
|
+
return new Promise((resolve, reject) => {
|
|
131
|
+
try {
|
|
132
|
+
var oscript = document.createElement('script')
|
|
133
|
+
if (oscript.readyState) {
|
|
134
|
+
// ie8及以下版本
|
|
135
|
+
oscript.onreadystatechange = function () {
|
|
136
|
+
if (oscript.readyState === 'complete' || oscript.readyState === 'loaded') {
|
|
137
|
+
resolve(oscript)
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
} else {
|
|
141
|
+
oscript.onload = function () {
|
|
142
|
+
resolve(oscript)
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
oscript.type = 'text/javascript'
|
|
146
|
+
oscript.src = url
|
|
147
|
+
document.body.appendChild(oscript)
|
|
148
|
+
} catch (error) {
|
|
149
|
+
reject(error)
|
|
150
|
+
}
|
|
151
|
+
})
|
|
152
|
+
},
|
|
153
|
+
loadStyle(urls) {
|
|
154
|
+
urls.forEach((url) => {
|
|
155
|
+
const css = document.createElement('link')
|
|
156
|
+
css.href = url
|
|
157
|
+
css.rel = 'stylesheet'
|
|
158
|
+
css.type = 'text/css'
|
|
159
|
+
document.head.appendChild(css)
|
|
160
|
+
})
|
|
161
|
+
},
|
|
162
|
+
/**
|
|
163
|
+
* 提取json中的value组成一个新的字符串
|
|
164
|
+
* eg: template('aaa{key}', json)
|
|
165
|
+
*
|
|
166
|
+
* @param {*} str
|
|
167
|
+
* @param {*} data
|
|
168
|
+
* @returns {*}
|
|
169
|
+
*/
|
|
170
|
+
template(str, data) {
|
|
171
|
+
const templateRe = /\{ *([\w_-]+) *\}/g
|
|
172
|
+
return str.replace(templateRe, function (str, key) {
|
|
173
|
+
var value = data[key]
|
|
174
|
+
if (value === undefined) {
|
|
175
|
+
throw new Error(ErrorType.JSON_VALUE_ERROR + str)
|
|
176
|
+
} else if (typeof value === 'function') {
|
|
177
|
+
value = value(data)
|
|
178
|
+
}
|
|
179
|
+
return value
|
|
180
|
+
})
|
|
181
|
+
},
|
|
182
|
+
deleteEmptyProperty(data) {
|
|
183
|
+
return Object.fromEntries(
|
|
184
|
+
Object.keys(data)
|
|
185
|
+
.filter((d) => !this.isEmpty(data[d]))
|
|
186
|
+
.map((i) => [i, data[i]])
|
|
187
|
+
)
|
|
188
|
+
},
|
|
189
|
+
deepAssign() {
|
|
190
|
+
let len = arguments.length,
|
|
191
|
+
target = arguments[0]
|
|
192
|
+
if (this.getDataType(target) !== 'Object') {
|
|
193
|
+
target = {}
|
|
194
|
+
}
|
|
195
|
+
for (let i = 1; i < len; i++) {
|
|
196
|
+
let source = arguments[i]
|
|
197
|
+
if (this.getDataType(source) === 'Object') {
|
|
198
|
+
for (let s in source) {
|
|
199
|
+
if (s === '__proto__' || target === source[s]) {
|
|
200
|
+
continue
|
|
201
|
+
}
|
|
202
|
+
if (this.getDataType(source[s]) === 'Object') {
|
|
203
|
+
target[s] = this.deepAssign(target[s], source[s])
|
|
204
|
+
} else {
|
|
205
|
+
target[s] = source[s]
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return target
|
|
211
|
+
},
|
|
212
|
+
handleCopyValue(text) {
|
|
213
|
+
if (!navigator.clipboard && window.isSecureContext) {
|
|
214
|
+
return navigator.clipboard.writeText(text)
|
|
215
|
+
} else {
|
|
216
|
+
const textArea = document.createElement('textarea')
|
|
217
|
+
textArea.style.position = 'fixed'
|
|
218
|
+
textArea.style.top = textArea.style.left = '-100vh'
|
|
219
|
+
textArea.style.opacity = '0'
|
|
220
|
+
textArea.value = text
|
|
221
|
+
document.body.appendChild(textArea)
|
|
222
|
+
textArea.focus()
|
|
223
|
+
textArea.select()
|
|
224
|
+
return new Promise((resolve, reject) => {
|
|
225
|
+
document.execCommand('copy') ? resolve() : reject(new Error('copy failed'))
|
|
226
|
+
textArea.remove()
|
|
227
|
+
})
|
|
228
|
+
}
|
|
229
|
+
},
|
|
230
|
+
}
|