@aochuang/common 1.0.1 → 1.0.126
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/common/index.js +13 -0
- package/common/resizable/index.js +3 -0
- package/common/resizable/resizable.vue +260 -0
- package/common/scroll-load-more/index.js +3 -0
- package/common/scroll-load-more/scroll-load-more.vue +397 -0
- package/common/static-map/index.js +3 -0
- package/common/static-map/static-map.vue +87 -0
- package/common/tooltip/tooltip.vue +27 -7
- package/common/viewer/index.js +4 -0
- package/common/viewer/util.js +40 -0
- package/common/viewer/viewer.vue +607 -0
- package/common/viewer-pedestal/index.js +3 -0
- package/common/viewer-pedestal/viewer-pedestal.vue +70 -0
- package/common/voice/index.js +21 -0
- package/common/voice/util.js +18 -0
- package/common/voice/voice-player-aac.js +584 -0
- package/common/voice/voice-player-audio.js +159 -0
- package/common/voice/voice-player.js +309 -0
- package/common/voice/voice.vue +274 -0
- package/common/voice-player/index.js +3 -0
- package/common/voice-player/voice-player.vue +785 -0
- package/common/waterfall/index.js +3 -0
- package/common/waterfall/waterfall.vue +169 -0
- package/common/wechat-emoji/emoji/666.png +0 -0
- package/common/wechat-emoji/emoji/Emm.png +0 -0
- package/common/wechat-emoji/emoji/OK.png +0 -0
- package/common/wechat-emoji/emoji//344/272/262/344/272/262.png +0 -0
- package/common/wechat-emoji/emoji//344/276/277/344/276/277.png +0 -0
- package/common/wechat-emoji/emoji//345/201/267/347/254/221.png +0 -0
- package/common/wechat-emoji/emoji//345/202/262/346/205/242.png +0 -0
- package/common/wechat-emoji/emoji//345/206/215/350/247/201.png +0 -0
- package/common/wechat-emoji/emoji//345/207/213/350/260/242.png +0 -0
- package/common/wechat-emoji/emoji//345/210/240/351/231/244.png +0 -0
- package/common/wechat-emoji/emoji//345/212/240/346/262/271.png +0 -0
- package/common/wechat-emoji/emoji//345/213/276/345/274/225.png +0 -0
- package/common/wechat-emoji/emoji//345/217/221/345/221/206.png +0 -0
- package/common/wechat-emoji/emoji//345/217/221/346/200/222.png +0 -0
- package/common/wechat-emoji/emoji//345/217/221/346/212/226.png +0 -0
- package/common/wechat-emoji/emoji//345/217/257/346/200/234.png +0 -0
- package/common/wechat-emoji/emoji//345/217/263/345/223/274/345/223/274.png +0 -0
- package/common/wechat-emoji/emoji//345/217/271/346/260/224.png +0 -0
- package/common/wechat-emoji/emoji//345/220/203/347/223/234.png +0 -0
- package/common/wechat-emoji/emoji//345/220/210/345/215/201.png +0 -0
- package/common/wechat-emoji/emoji//345/220/220.png +0 -0
- package/common/wechat-emoji/emoji//345/220/220/350/210/214/345/244/264.png +0 -0
- package/common/wechat-emoji/emoji//345/221/262/347/211/231.png +0 -0
- package/common/wechat-emoji/emoji//345/222/222/351/252/202.png +0 -0
- package/common/wechat-emoji/emoji//345/222/226/345/225/241.png +0 -0
- package/common/wechat-emoji/emoji//345/223/207.png +0 -0
- package/common/wechat-emoji/emoji//345/223/210/346/254/240.png +0 -0
- package/common/wechat-emoji/emoji//345/225/244/351/205/222.png +0 -0
- package/common/wechat-emoji/emoji//345/230/230.png +0 -0
- package/common/wechat-emoji/emoji//345/230/264/345/224/207.png +0 -0
- package/common/wechat-emoji/emoji//345/230/277/345/223/210.png +0 -0
- package/common/wechat-emoji/emoji//345/233/247.png +0 -0
- package/common/wechat-emoji/emoji//345/233/260.png +0 -0
- package/common/wechat-emoji/emoji//345/235/217/347/254/221.png +0 -0
- package/common/wechat-emoji/emoji//345/244/247/345/223/255.png +0 -0
- package/common/wechat-emoji/emoji//345/244/251/345/225/212.png +0 -0
- package/common/wechat-emoji/emoji//345/244/252/351/230/263.png +0 -0
- package/common/wechat-emoji/emoji//345/244/261/346/234/233.png +0 -0
- package/common/wechat-emoji/emoji//345/245/213/346/226/227.png +0 -0
- package/common/wechat-emoji/emoji//345/245/270/347/254/221.png +0 -0
- package/common/wechat-emoji/emoji//345/245/275/347/232/204.png +0 -0
- package/common/wechat-emoji/emoji//345/247/224/345/261/210.png +0 -0
- package/common/wechat-emoji/emoji//345/256/263/347/276/236.png +0 -0
- package/common/wechat-emoji/emoji//345/260/264/345/260/254.png +0 -0
- package/common/wechat-emoji/emoji//345/267/246/345/223/274/345/223/274.png +0 -0
- package/common/wechat-emoji/emoji//345/272/206/347/245/235.png +0 -0
- package/common/wechat-emoji/emoji//345/274/261.png +0 -0
- package/common/wechat-emoji/emoji//345/274/272.png +0 -0
- package/common/wechat-emoji/emoji//345/276/227/346/204/217.png +0 -0
- package/common/wechat-emoji/emoji//345/276/256/347/254/221.png +0 -0
- package/common/wechat-emoji/emoji//345/277/203/347/242/216.png +0 -0
- package/common/wechat-emoji/emoji//345/277/253/345/223/255/344/272/206.png +0 -0
- package/common/wechat-emoji/emoji//346/200/204/347/201/253.png +0 -0
- package/common/wechat-emoji/emoji//346/201/220/346/203/247.png +0 -0
- package/common/wechat-emoji/emoji//346/202/240/351/227/262.png +0 -0
- package/common/wechat-emoji/emoji//346/203/212/346/201/220.png +0 -0
- package/common/wechat-emoji/emoji//346/203/212/350/256/266.png +0 -0
- package/common/wechat-emoji/emoji//346/204/211/345/277/253.png +0 -0
- package/common/wechat-emoji/emoji//346/206/250/347/254/221.png +0 -0
- package/common/wechat-emoji/emoji//346/211/223/350/204/270.png +0 -0
- package/common/wechat-emoji/emoji//346/212/223/347/213/202.png +0 -0
- package/common/wechat-emoji/emoji//346/212/240/351/274/273.png +0 -0
- package/common/wechat-emoji/emoji//346/212/261/346/213/263.png +0 -0
- package/common/wechat-emoji/emoji//346/213/245/346/212/261.png +0 -0
- package/common/wechat-emoji/emoji//346/213/263/345/244/264.png +0 -0
- package/common/wechat-emoji/emoji//346/215/202/350/204/270.png +0 -0
- package/common/wechat-emoji/emoji//346/217/241/346/211/213.png +0 -0
- package/common/wechat-emoji/emoji//346/222/207/345/230/264.png +0 -0
- package/common/wechat-emoji/emoji//346/223/246/346/261/227.png +0 -0
- package/common/wechat-emoji/emoji//346/225/262/346/211/223.png +0 -0
- package/common/wechat-emoji/emoji//346/227/240/350/257/255.png +0 -0
- package/common/wechat-emoji/emoji//346/227/272/346/237/264.png +0 -0
- package/common/wechat-emoji/emoji//346/231/225.png +0 -0
- package/common/wechat-emoji/emoji//346/234/210/344/272/256.png +0 -0
- package/common/wechat-emoji/emoji//346/234/272/346/231/272.png +0 -0
- package/common/wechat-emoji/emoji//346/261/227.png +0 -0
- package/common/wechat-emoji/emoji//346/265/201/346/261/227.png +0 -0
- package/common/wechat-emoji/emoji//346/265/201/346/263/252.png +0 -0
- package/common/wechat-emoji/emoji//347/202/270/345/274/271.png +0 -0
- package/common/wechat-emoji/emoji//347/203/237/350/212/261.png +0 -0
- package/common/wechat-emoji/emoji//347/210/206/347/253/271.png +0 -0
- package/common/wechat-emoji/emoji//347/210/261/345/277/203.png +0 -0
- package/common/wechat-emoji/emoji//347/214/252/345/244/264.png +0 -0
- package/common/wechat-emoji/emoji//347/216/253/347/221/260.png +0 -0
- package/common/wechat-emoji/emoji//347/224/237/347/227/205.png +0 -0
- package/common/wechat-emoji/emoji//347/226/221/351/227/256.png +0 -0
- package/common/wechat-emoji/emoji//347/231/274.png +0 -0
- package/common/wechat-emoji/emoji//347/231/275/347/234/274.png +0 -0
- package/common/wechat-emoji/emoji//347/232/261/347/234/211.png +0 -0
- package/common/wechat-emoji/emoji//347/235/241.png +0 -0
- package/common/wechat-emoji/emoji//347/240/264/346/266/225/344/270/272/347/254/221.png +0 -0
- package/common/wechat-emoji/emoji//347/244/274/347/211/251.png +0 -0
- package/common/wechat-emoji/emoji//347/244/276/344/274/232/347/244/276/344/274/232.png +0 -0
- package/common/wechat-emoji/emoji//347/246/217.png +0 -0
- package/common/wechat-emoji/emoji//347/254/221/350/204/270.png +0 -0
- package/common/wechat-emoji/emoji//347/272/242/345/214/205.png +0 -0
- package/common/wechat-emoji/emoji//347/277/273/347/231/275/347/234/274.png +0 -0
- package/common/wechat-emoji/emoji//350/200/266.png +0 -0
- package/common/wechat-emoji/emoji//350/203/234/345/210/251.png +0 -0
- package/common/wechat-emoji/emoji//350/204/270/347/272/242.png +0 -0
- package/common/wechat-emoji/emoji//350/211/262.png +0 -0
- package/common/wechat-emoji/emoji//350/213/246/346/266/251.png +0 -0
- package/common/wechat-emoji/emoji//350/217/234/345/210/200.png +0 -0
- package/common/wechat-emoji/emoji//350/233/213/347/263/225.png +0 -0
- package/common/wechat-emoji/emoji//350/241/260.png +0 -0
- package/common/wechat-emoji/emoji//350/243/202/345/274/200.png +0 -0
- package/common/wechat-emoji/emoji//350/245/277/347/223/234.png +0 -0
- package/common/wechat-emoji/emoji//350/256/251/346/210/221/347/234/213/347/234/213.png +0 -0
- package/common/wechat-emoji/emoji//350/260/203/347/232/256.png +0 -0
- package/common/wechat-emoji/emoji//350/267/263/350/267/263.png +0 -0
- package/common/wechat-emoji/emoji//350/275/254/345/234/210.png +0 -0
- package/common/wechat-emoji/emoji//351/204/231/350/247/206.png +0 -0
- package/common/wechat-emoji/emoji//351/227/255/345/230/264.png +0 -0
- package/common/wechat-emoji/emoji//351/230/264/351/231/251.png +0 -0
- package/common/wechat-emoji/emoji//351/232/276/350/277/207.png +0 -0
- package/common/wechat-emoji/emoji//351/252/267/351/253/205.png +0 -0
- package/common/wechat-emoji/emoji//351/254/274/350/204/270.png +0 -0
- package/common/wechat-emoji/emoji//351/270/241.png +0 -0
- package/common/wechat-emoji/emoji//351/274/223/346/216/214.png +0 -0
- package/common/wechat-emoji/index.js +14 -0
- package/common/wechat-emoji/order.json +111 -0
- package/common/wechat-emoji/utils.js +52 -0
- package/common/wechat-emoji/wechat-emoji.vue +74 -0
- package/common/wechat-emoji-select/index.js +3 -0
- package/common/wechat-emoji-select/wechat-emoji-select.vue +67 -0
- package/components/advanced-filter/advanced-filter-condition.vue +1 -1
- package/components/advanced-filter/advanced-filter.vue +6 -1
- package/components/advanced-filter/index.js +11 -12
- package/components/avatar/avatar.vue +9 -5
- package/components/button/button.vue +15 -2
- package/components/date-picker/date-picker.vue +9 -2
- package/components/dialog/dialog.vue +6 -1
- package/components/dropdown/dropdown-item.vue +10 -2
- package/components/dropdown/dropdown.vue +45 -5
- package/components/dropdown/index.js +8 -8
- package/components/empty/empty.vue +29 -2
- package/components/filter/filter.vue +1 -1
- package/components/filter/index.js +10 -10
- package/components/form/form.vue +33 -2
- package/components/form/index.js +11 -0
- package/components/grid/grid-column.vue +1 -0
- package/components/grid/grid-table.vue +6 -4
- package/components/grid/grid.vue +15 -3
- package/components/grid/index.js +31 -32
- package/components/icon/demo_index.html +2752 -1073
- package/components/icon/iconfont.css +299 -7
- package/components/icon/iconfont.js +1 -1
- package/components/icon/iconfont.json +518 -7
- package/components/icon/iconfont.ttf +0 -0
- package/components/icon/iconfont.woff +0 -0
- package/components/icon/iconfont.woff2 +0 -0
- package/components/image/image.vue +91 -13
- package/components/image/index.js +3 -1
- package/components/image/util.js +82 -1
- package/components/index.js +6 -2
- package/components/menu/index.js +13 -7
- package/components/menu/menu-group.vue +6 -1
- package/components/menu/menu-item.vue +57 -3
- package/components/menu/menu.vue +1 -1
- package/components/message/message.js +12 -9
- package/components/message-box/index.js +4 -0
- package/components/pagination/pagination.vue +11 -7
- package/components/popover/popover.vue +24 -1
- package/components/searchbox/searchbox.vue +15 -3
- package/components/select/index.js +2 -0
- package/components/select/select.vue +7 -1
- package/components/skeleton/index.js +2 -0
- package/components/sortable/index.js +7 -5
- package/components/sortable/sortable.vue +5 -0
- package/components/tabbar/index.js +9 -0
- package/components/tabbar/tabbar-item.vue +162 -0
- package/components/tabbar/tabbar.vue +49 -0
- package/components/tabs/index.js +2 -0
- package/components/tabs/tabs.vue +5 -0
- package/components/tag/index.js +4 -4
- package/components/tag/tag.less +12 -3
- package/components/textarea/textarea.vue +19 -3
- package/components/time-picker/time-picker.vue +7 -0
- package/components/toolbar/index.js +2 -0
- package/components/transfer/index.js +4 -4
- package/components/transfer/transfer-element.vue +109 -0
- package/components/transfer/transfer.vue +22 -7
- package/components/tree/tree.vue +175 -16
- package/components/video/video.vue +50 -2
- package/package.json +12 -1
- package/uni-common/more-button/more-button.vue +23 -0
- package/uni-common/popup-select/popup-select.vue +217 -0
- package/uni-common/portray/portray.vue +54 -0
- package/uni-common/refresh-view/refresh-view.vue +94 -0
- package/uni-common/scroll-load-more/scroll-load-more.vue +201 -0
- package/uni-common/tree-popup-select/index.js +3 -0
- package/uni-common/tree-popup-select/tree-popup-select-panel.vue +92 -0
- package/uni-common/tree-popup-select/tree-popup-select.vue +124 -0
- package/uni-common/tree-popup-select/util.js +19 -0
- package/uni-common/wechat-emoji/index.js +13 -0
- package/uni-common/wechat-emoji/order.json +438 -0
- package/uni-common/wechat-emoji/utils.js +52 -0
- package/uni-common/wechat-emoji/wechat-emoji.vue +74 -0
- package/uni-components/avatar/avatar.vue +81 -0
- package/uni-components/badge/badge.vue +57 -0
- package/uni-components/empty/empty.vue +27 -0
- package/uni-components/exception/exception.vue +47 -0
- package/uni-components/icon/demo.css +539 -0
- package/uni-components/icon/demo_index.html +1591 -0
- package/uni-components/icon/icon.vue +48 -0
- package/uni-components/icon/iconfont.css +259 -0
- package/uni-components/icon/iconfont.js +1 -0
- package/uni-components/icon/iconfont.json +436 -0
- package/uni-components/icon/iconfont.ttf +0 -0
- package/uni-components/icon/iconfont.woff +0 -0
- package/uni-components/icon/iconfont.woff2 +0 -0
- package/uni-components/icon/index.js +3 -0
- package/uni-components/image/image.vue +38 -0
- package/uni-components/image/index.js +3 -0
- package/uni-components/loading/index.js +3 -0
- package/uni-components/loading/loading.vue +30 -0
- package/uni-components/message/message-item.vue +29 -0
- package/uni-components/message/message.js +80 -0
- package/uni-components/message/message.vue +30 -0
- package/uni-components/message-box/message-box-item.vue +54 -0
- package/uni-components/message-box/message-box.js +91 -0
- package/uni-components/message-box/message-box.vue +52 -0
- package/uni-components/picker/picker.vue +270 -0
- package/uni-components/popup/index.js +3 -0
- package/uni-components/popup/popup.vue +143 -0
- package/uni-components/search/index.js +3 -0
- package/uni-components/search/search.vue +86 -0
- package/uni-components/skeleton/skeleton-chat.vue +85 -0
- package/uni-components/skeleton/skeleton-contact.vue +84 -0
- package/uni-components/skeleton/skeleton-detail.vue +84 -0
- package/uni-components/skeleton/skeleton.vue +32 -0
- package/uni-components/tabbar/tabbar-item.vue +33 -0
- package/uni-components/tabbar/tabbar.vue +87 -0
- package/uni-components/tree/index.js +3 -0
- package/uni-components/tree/tree-select-loading.vue +48 -0
- package/uni-components/tree/tree-select-node.vue +202 -0
- package/uni-components/tree/tree-select-store.js +520 -0
- package/uni-components/tree/tree-select.vue +210 -0
- package/uni-components/tree/utils.js +33 -0
- package/uni-components/video/index.js +3 -0
- package/uni-components/video/video.vue +29 -0
- package/uni-components/voice/index.js +3 -0
- package/uni-components/voice/voice.vue +24 -0
- package/uni-components/waterfall/index.js +3 -0
- package/uni-components/waterfall/util.js +72 -0
- package/uni-components/waterfall/waterfall.vue +147 -0
- package/utils/data.js +128 -0
- package/utils/date.js +484 -44
- package/utils/dom.js +37 -0
- package/utils/event.js +92 -0
- package/utils/file.js +76 -0
- package/utils/json.js +22 -1
- package/utils/network-detector.js +258 -0
- package/utils/oss.js +11 -0
- package/utils/share-message.js +81 -0
- package/utils/share-websocket.js +127 -0
- package/utils/string.js +59 -0
- package/utils/time-task.js +60 -0
- package/utils/url.js +46 -0
- package/utils/util.js +416 -1
package/utils/file.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 加载远程script文件
|
|
3
|
+
*/
|
|
4
|
+
export function loadScript (url, moduleName) {
|
|
5
|
+
return new Promise((resolve, reject) => {
|
|
6
|
+
if (window[moduleName]) {
|
|
7
|
+
resolve(window[moduleName])
|
|
8
|
+
} else {
|
|
9
|
+
import('load-script').then(module => {
|
|
10
|
+
const loadScript = module.default ? module.default : module
|
|
11
|
+
loadScript(url, () => {
|
|
12
|
+
if (moduleName) {
|
|
13
|
+
if (typeof window[moduleName] === 'undefined') {
|
|
14
|
+
reject(new Error('未成功加载' + moduleName))
|
|
15
|
+
} else {
|
|
16
|
+
resolve(window[moduleName])
|
|
17
|
+
}
|
|
18
|
+
} else {
|
|
19
|
+
resolve()
|
|
20
|
+
}
|
|
21
|
+
})
|
|
22
|
+
}, reject)
|
|
23
|
+
}
|
|
24
|
+
})
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 根据src地址获取文件类型
|
|
29
|
+
*/
|
|
30
|
+
export function getExtByFileSrc (fileSrc) {
|
|
31
|
+
if (!fileSrc) {
|
|
32
|
+
return
|
|
33
|
+
}
|
|
34
|
+
return fileSrc.substr(fileSrc.lastIndexOf('.') + 1).toLowerCase()
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 下载文件
|
|
39
|
+
*/
|
|
40
|
+
export function downloadFile (content, fileName) {
|
|
41
|
+
const blob = new Blob([content])
|
|
42
|
+
if ('download' in document.createElement('a')) { // 非IE下载
|
|
43
|
+
const elink = document.createElement('a')
|
|
44
|
+
elink.download = fileName
|
|
45
|
+
elink.style.display = 'none'
|
|
46
|
+
elink.href = URL.createObjectURL(blob)
|
|
47
|
+
document.body.appendChild(elink)
|
|
48
|
+
elink.click()
|
|
49
|
+
URL.revokeObjectURL(elink.href) // 释放URL 对象
|
|
50
|
+
document.body.removeChild(elink)
|
|
51
|
+
} else { // IE10+下载
|
|
52
|
+
navigator.msSaveBlob(blob, fileName)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* 从url中获取文件名称
|
|
58
|
+
*/
|
|
59
|
+
export function getNameByFileUrl (url, keepExt) {
|
|
60
|
+
let fileName = url ? url.substr(url.lastIndexOf('/') + 1) : ''
|
|
61
|
+
// https://wpj-dev3-kefures.oss-cn-shenzhen.aliyuncs.com/friend/1877240539286642688/1877546834264719360/3/NFFNxEi3JH6oHxKcfexK1/3-1pp3162605_%E5%89%AF%E6%9C%AC_(1).jpg&_msgId=b4f4fc5e6e3a4e39a5dec64adb2f22b9
|
|
62
|
+
if (fileName.lastIndexOf('&') >= 0) {
|
|
63
|
+
fileName = fileName.substr(0, fileName.lastIndexOf('&'))
|
|
64
|
+
}
|
|
65
|
+
if (fileName) {
|
|
66
|
+
try {
|
|
67
|
+
fileName = window.decodeURIComponent(fileName)
|
|
68
|
+
} catch (e) {
|
|
69
|
+
console.log(e)
|
|
70
|
+
}
|
|
71
|
+
if (keepExt !== true) {
|
|
72
|
+
fileName = fileName.substr(0, fileName.lastIndexOf('.'))
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return fileName
|
|
76
|
+
}
|
package/utils/json.js
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
import X2JS from 'x2js'
|
|
2
|
+
|
|
3
|
+
const x2js = new X2JS({
|
|
4
|
+
attributePrefix: ''
|
|
5
|
+
})
|
|
6
|
+
|
|
1
7
|
export function stringify (data, ag1, ag2) {
|
|
2
8
|
if (data && typeof data === 'object') {
|
|
3
9
|
data = JSON.stringify(data, ag1, ag2)
|
|
@@ -16,4 +22,19 @@ export function parse (data) {
|
|
|
16
22
|
|
|
17
23
|
export function clone (data) {
|
|
18
24
|
return JSON.parse(JSON.stringify(data))
|
|
19
|
-
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function parseXml (xml) {
|
|
28
|
+
let result = null
|
|
29
|
+
try {
|
|
30
|
+
result = x2js.xml2js(xml)
|
|
31
|
+
if (typeof result === 'string') {
|
|
32
|
+
result = JSON.parse(result)
|
|
33
|
+
} else if (result && result.html && result.html.body && result.html.body.parsererror) {
|
|
34
|
+
result = null
|
|
35
|
+
}
|
|
36
|
+
} catch (e) {
|
|
37
|
+
console.log(e)
|
|
38
|
+
}
|
|
39
|
+
return result
|
|
40
|
+
}
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { formatDate } from './date'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 纯网页端网络状态检测工具(适配网络场景)
|
|
5
|
+
* 核心能力:基础在线状态 + 网络连通性校验 + 弱网判断 + 30s自动检查 + 本地缓存(最多3000条)
|
|
6
|
+
* 适配场景:仅网络、无外网的环境,能精准判断“网络连通/网络断开”
|
|
7
|
+
*/
|
|
8
|
+
export default class NetworkDetector {
|
|
9
|
+
constructor(options = {}) {
|
|
10
|
+
// 默认配置(调整为网络适配)
|
|
11
|
+
this.config = Object.assign({
|
|
12
|
+
// 替换为你的网络轻量校验接口(如IM服务端的ping接口)
|
|
13
|
+
checkUrl: null, // 网络接口示例
|
|
14
|
+
// 校验超时时间(网络建议缩短,比如2秒)
|
|
15
|
+
timeout: 1000 * 10,
|
|
16
|
+
// 弱网判断阈值(网络可根据实际情况调整)
|
|
17
|
+
weakNetworkThreshold: {
|
|
18
|
+
downlink: 1,
|
|
19
|
+
rtt: 300,
|
|
20
|
+
effectiveType: ['slow-2g', '2g', '3g']
|
|
21
|
+
},
|
|
22
|
+
// 是否仅网络环境(标记场景)
|
|
23
|
+
isIntranetOnly: true,
|
|
24
|
+
// 网络状态变化回调(新增网络状态标识)
|
|
25
|
+
onStatusChange: (state) => {},
|
|
26
|
+
autoCheckInterval: 30000,
|
|
27
|
+
maxLogCount: 3000,
|
|
28
|
+
storageKey: 'im_network_status_logs'
|
|
29
|
+
}, options);
|
|
30
|
+
|
|
31
|
+
// 缓存当前网络状态(新增isIntranetConnected标识网络连通性)
|
|
32
|
+
this.currentState = {
|
|
33
|
+
isOnline: navigator.onLine, // 浏览器基础在线状态(网卡是否激活)
|
|
34
|
+
isIntranetConnected: false, // 核心:网络是否连通(替代原isRealConnected)
|
|
35
|
+
isWeakNetwork: false, // 是否网络弱网
|
|
36
|
+
networkType: 'unknown',
|
|
37
|
+
effectiveType: 'unknown',
|
|
38
|
+
downlink: 0,
|
|
39
|
+
rtt: 0,
|
|
40
|
+
isIntranetOnly: this.config.isIntranetOnly // 标记是否为纯网络环境
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
this.checkTimer = null;
|
|
44
|
+
this.networkLogs = this.loadLogsFromLocalStorage();
|
|
45
|
+
|
|
46
|
+
this.bindEvents();
|
|
47
|
+
this.startAutoCheck();
|
|
48
|
+
this.checkNetworkStatus();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 从localStorage加载历史日志
|
|
53
|
+
*/
|
|
54
|
+
loadLogsFromLocalStorage() {
|
|
55
|
+
try {
|
|
56
|
+
const rawLogs = localStorage.getItem(this.config.storageKey);
|
|
57
|
+
return rawLogs ? JSON.parse(rawLogs) : [];
|
|
58
|
+
} catch (e) {
|
|
59
|
+
console.error('加载本地日志失败:', e);
|
|
60
|
+
return [];
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* 绑定浏览器网络监听事件
|
|
66
|
+
*/
|
|
67
|
+
bindEvents() {
|
|
68
|
+
window.addEventListener('online', () => this.checkNetworkStatus());
|
|
69
|
+
window.addEventListener('offline', () => this.checkNetworkStatus());
|
|
70
|
+
|
|
71
|
+
const networkInfo = navigator.connection || navigator.networkInformation;
|
|
72
|
+
if (networkInfo) {
|
|
73
|
+
networkInfo.addEventListener('change', () => this.checkNetworkStatus());
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 启动30秒自动检查定时器
|
|
79
|
+
*/
|
|
80
|
+
startAutoCheck() {
|
|
81
|
+
if (this.checkTimer) clearInterval(this.checkTimer);
|
|
82
|
+
this.checkTimer = setInterval(() => {
|
|
83
|
+
this.checkNetworkStatus();
|
|
84
|
+
}, this.config.autoCheckInterval);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* 核心方法:检测网络网络状态
|
|
89
|
+
*/
|
|
90
|
+
checkNetworkStatus() {
|
|
91
|
+
const isOnline = navigator.onLine;
|
|
92
|
+
const networkDetail = this.getNetworkDetail();
|
|
93
|
+
|
|
94
|
+
// 校验网络连通性(替换原外网连通性校验)
|
|
95
|
+
this.checkIntranetConnectivity()
|
|
96
|
+
.then((isIntranetConnected) => {
|
|
97
|
+
const isWeakNetwork = this.judgeWeakNetwork(networkDetail);
|
|
98
|
+
const newState = {
|
|
99
|
+
isOnline,
|
|
100
|
+
isIntranetConnected, // 核心:网络连通状态
|
|
101
|
+
isWeakNetwork,
|
|
102
|
+
...networkDetail,
|
|
103
|
+
isIntranetOnly: this.config.isIntranetOnly
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
if (JSON.stringify(this.currentState) !== JSON.stringify(newState)) {
|
|
107
|
+
this.currentState = newState;
|
|
108
|
+
this.config.onStatusChange(newState);
|
|
109
|
+
console.log('[网络网络检测] 状态更新:', newState);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
this.recordNetworkLog({
|
|
113
|
+
timestamp: formatDate(new Date()),
|
|
114
|
+
...newState
|
|
115
|
+
});
|
|
116
|
+
})
|
|
117
|
+
.catch((err) => {
|
|
118
|
+
console.error('[网络网络检测] 网络连通性校验失败:', err);
|
|
119
|
+
const newState = {
|
|
120
|
+
isOnline,
|
|
121
|
+
isIntranetConnected: false,
|
|
122
|
+
isWeakNetwork: true,
|
|
123
|
+
...networkDetail,
|
|
124
|
+
isIntranetOnly: this.config.isIntranetOnly,
|
|
125
|
+
error: err.message || '网络连通性校验超时/失败'
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
if (JSON.stringify(this.currentState) !== JSON.stringify(newState)) {
|
|
129
|
+
this.currentState = newState;
|
|
130
|
+
this.config.onStatusChange(newState);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
this.recordNetworkLog({
|
|
134
|
+
timestamp: formatDate(new Date()),
|
|
135
|
+
error: err.message || '网络连通性校验超时/失败',
|
|
136
|
+
...newState
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* 记录网络日志(逻辑不变)
|
|
143
|
+
*/
|
|
144
|
+
recordNetworkLog(log) {
|
|
145
|
+
this.networkLogs.push(log);
|
|
146
|
+
if (this.networkLogs.length > this.config.maxLogCount) {
|
|
147
|
+
this.networkLogs.shift();
|
|
148
|
+
}
|
|
149
|
+
try {
|
|
150
|
+
localStorage.setItem(this.config.storageKey, JSON.stringify(this.networkLogs));
|
|
151
|
+
} catch (e) {
|
|
152
|
+
console.error('保存日志到localStorage失败:', e);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
clearNetworkLog () {
|
|
157
|
+
this.networkLogs = []
|
|
158
|
+
localStorage.removeItem(this.config.storageKey);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* 辅助方法:获取网络类型/带宽/时延等详情
|
|
163
|
+
*/
|
|
164
|
+
getNetworkDetail() {
|
|
165
|
+
const networkInfo = navigator.connection || navigator.networkInformation;
|
|
166
|
+
if (!networkInfo) {
|
|
167
|
+
return {
|
|
168
|
+
networkType: 'unknown',
|
|
169
|
+
effectiveType: 'unknown',
|
|
170
|
+
downlink: 0,
|
|
171
|
+
rtt: 0
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return {
|
|
176
|
+
networkType: networkInfo.type || 'unknown',
|
|
177
|
+
effectiveType: networkInfo.effectiveType || 'unknown',
|
|
178
|
+
downlink: networkInfo.downlink || 0,
|
|
179
|
+
rtt: networkInfo.rtt || 0
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* 校验网络连通性
|
|
185
|
+
*/
|
|
186
|
+
checkIntranetConnectivity() {
|
|
187
|
+
return new Promise((resolve, reject) => {
|
|
188
|
+
const timeoutTimer = setTimeout(() => {
|
|
189
|
+
reject(new Error('网络请求超时'));
|
|
190
|
+
}, this.config.timeout);
|
|
191
|
+
|
|
192
|
+
fetch(this.config.checkUrl, {
|
|
193
|
+
method: 'GET',
|
|
194
|
+
cache: 'no-cache'
|
|
195
|
+
})
|
|
196
|
+
.then((response) => {
|
|
197
|
+
clearTimeout(timeoutTimer);
|
|
198
|
+
// 校验网络接口状态码(200-299为连通)
|
|
199
|
+
if (response.ok) {
|
|
200
|
+
resolve(true);
|
|
201
|
+
} else {
|
|
202
|
+
reject(new Error(`网络接口返回异常状态码:${response.status}`));
|
|
203
|
+
}
|
|
204
|
+
})
|
|
205
|
+
.catch((err) => {
|
|
206
|
+
clearTimeout(timeoutTimer);
|
|
207
|
+
reject(err);
|
|
208
|
+
});
|
|
209
|
+
})
|
|
210
|
+
// 统一捕获异常,返回false(保持逻辑兼容)
|
|
211
|
+
.catch(() => false);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* 辅助方法:判断网络是否弱网
|
|
216
|
+
*/
|
|
217
|
+
judgeWeakNetwork(networkDetail) {
|
|
218
|
+
const { downlink, rtt, effectiveType } = networkDetail;
|
|
219
|
+
const { weakNetworkThreshold } = this.config;
|
|
220
|
+
|
|
221
|
+
return (
|
|
222
|
+
downlink < weakNetworkThreshold.downlink ||
|
|
223
|
+
rtt > weakNetworkThreshold.rtt ||
|
|
224
|
+
weakNetworkThreshold.effectiveType.includes(effectiveType)
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* 主动获取当前网络网络状态
|
|
230
|
+
*/
|
|
231
|
+
getCurrentState() {
|
|
232
|
+
return JSON.parse(JSON.stringify(this.currentState));
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* 获取本地缓存的所有日志
|
|
237
|
+
*/
|
|
238
|
+
getLocalLogs() {
|
|
239
|
+
return Promise.resolve(JSON.parse(JSON.stringify(this.networkLogs)));
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* 销毁监听+定时器
|
|
244
|
+
*/
|
|
245
|
+
destroy() {
|
|
246
|
+
window.removeEventListener('online', () => this.checkNetworkStatus());
|
|
247
|
+
window.removeEventListener('offline', () => this.checkNetworkStatus());
|
|
248
|
+
const networkInfo = navigator.connection || navigator.networkInformation;
|
|
249
|
+
if (networkInfo) {
|
|
250
|
+
networkInfo.removeEventListener('change', () => this.checkNetworkStatus());
|
|
251
|
+
}
|
|
252
|
+
if (this.checkTimer) {
|
|
253
|
+
clearInterval(this.checkTimer);
|
|
254
|
+
this.checkTimer = null;
|
|
255
|
+
}
|
|
256
|
+
console.log('[网络网络检测] 监听+定时器已销毁');
|
|
257
|
+
}
|
|
258
|
+
}
|
package/utils/oss.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { unique } from './string'
|
|
2
|
+
import Event from './event'
|
|
3
|
+
|
|
4
|
+
const canCreateShareMessage = () => {
|
|
5
|
+
return !!window.SharedWorker
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export const CreateShareMessage = (options) => {
|
|
9
|
+
options = Object.assign({
|
|
10
|
+
workerUrl: null
|
|
11
|
+
}, options)
|
|
12
|
+
if (canCreateShareMessage()) {
|
|
13
|
+
const event = new Event()
|
|
14
|
+
|
|
15
|
+
const worker = new SharedWorker(options.workerUrl)
|
|
16
|
+
|
|
17
|
+
const callbackMap = {}
|
|
18
|
+
|
|
19
|
+
const callPort = (name, data) => {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
let timer = null
|
|
22
|
+
const callbackId = unique()
|
|
23
|
+
callbackMap[callbackId] = (data) => {
|
|
24
|
+
clearTimeout(timer)
|
|
25
|
+
resolve(data)
|
|
26
|
+
}
|
|
27
|
+
worker.port.postMessage({
|
|
28
|
+
type: 'message',
|
|
29
|
+
name,
|
|
30
|
+
data,
|
|
31
|
+
callbackId
|
|
32
|
+
})
|
|
33
|
+
timer = setTimeout(() => {
|
|
34
|
+
reject('timeout')
|
|
35
|
+
}, 1000 * 60)
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// 监听 SharedWorker 的回复消息
|
|
40
|
+
worker.port.onmessage = function(evt) {
|
|
41
|
+
if (!evt.data) {
|
|
42
|
+
return
|
|
43
|
+
}
|
|
44
|
+
if (evt.data.name === '$heartbeat') {
|
|
45
|
+
worker.port.postMessage({
|
|
46
|
+
type: '$heartbeat'
|
|
47
|
+
})
|
|
48
|
+
} else {
|
|
49
|
+
if (callbackMap[evt.data.name]) {
|
|
50
|
+
callbackMap[evt.data.name](evt.data.data)
|
|
51
|
+
delete callbackMap[evt.data.name]
|
|
52
|
+
} else {
|
|
53
|
+
event.emit(evt.data.name, {
|
|
54
|
+
data: evt.data.data
|
|
55
|
+
})
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
worker.port.start()
|
|
61
|
+
return {
|
|
62
|
+
onMessage (name, fn) {
|
|
63
|
+
event.on(name, fn)
|
|
64
|
+
},
|
|
65
|
+
postMessage (name, data) {
|
|
66
|
+
callPort('postMessage', {
|
|
67
|
+
name,
|
|
68
|
+
data
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
} else {
|
|
73
|
+
// 部分手机不支持shareWorker,暂时不兼容,大部分场景都是通知的,移动端可以不需要通知
|
|
74
|
+
return {
|
|
75
|
+
onMessage (name, fn) {
|
|
76
|
+
},
|
|
77
|
+
postMessage (name, data) {
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { unique } from './string'
|
|
2
|
+
import Event from './event'
|
|
3
|
+
|
|
4
|
+
const CONNECT_STATUS = {
|
|
5
|
+
Ready: -1,
|
|
6
|
+
Close: 0,
|
|
7
|
+
Connecting: 1,
|
|
8
|
+
Succcess: 2,
|
|
9
|
+
Error: 3
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const canCreateShareWebSocket = () => {
|
|
13
|
+
return !!window.SharedWorker
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const CreateShareWebSocket = (options) => {
|
|
17
|
+
options = Object.assign({
|
|
18
|
+
workerUrl: null
|
|
19
|
+
}, options)
|
|
20
|
+
|
|
21
|
+
const event = new Event()
|
|
22
|
+
|
|
23
|
+
const worker = new SharedWorker(options.workerUrl)
|
|
24
|
+
|
|
25
|
+
const callbackMap = {}
|
|
26
|
+
|
|
27
|
+
const callPort = (name, data) => {
|
|
28
|
+
return new Promise((resolve, reject) => {
|
|
29
|
+
let timer = null
|
|
30
|
+
const callbackId = unique()
|
|
31
|
+
callbackMap[callbackId] = (data) => {
|
|
32
|
+
clearTimeout(timer)
|
|
33
|
+
resolve(data)
|
|
34
|
+
}
|
|
35
|
+
worker.port.postMessage({
|
|
36
|
+
type: 'socket',
|
|
37
|
+
name,
|
|
38
|
+
data,
|
|
39
|
+
callbackId
|
|
40
|
+
})
|
|
41
|
+
timer = setTimeout(() => {
|
|
42
|
+
reject('timeout')
|
|
43
|
+
}, 1000 * 60)
|
|
44
|
+
})
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// 监听 SharedWorker 的回复消息
|
|
48
|
+
worker.port.onmessage = function(evt) {
|
|
49
|
+
if (!evt.data) {
|
|
50
|
+
return
|
|
51
|
+
}
|
|
52
|
+
if (evt.data.name === '$heartbeat') {
|
|
53
|
+
worker.port.postMessage({
|
|
54
|
+
type: '$heartbeat'
|
|
55
|
+
})
|
|
56
|
+
} else {
|
|
57
|
+
if (callbackMap[evt.data.name]) {
|
|
58
|
+
callbackMap[evt.data.name](evt.data.data)
|
|
59
|
+
delete callbackMap[evt.data.name]
|
|
60
|
+
} else {
|
|
61
|
+
event.emit(evt.data.name, evt.data.data)
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
worker.port.start()
|
|
67
|
+
|
|
68
|
+
return class ShareWebSocket {
|
|
69
|
+
|
|
70
|
+
onopen = null
|
|
71
|
+
|
|
72
|
+
onmessage = null
|
|
73
|
+
|
|
74
|
+
onclose = null
|
|
75
|
+
|
|
76
|
+
onerror = null
|
|
77
|
+
|
|
78
|
+
constructor (url) {
|
|
79
|
+
callPort('setUrl', url).then(() => {
|
|
80
|
+
callPort('getConnectStatus').then(evt => {
|
|
81
|
+
// 如果已经连接成功了,那么需要手动响应一下onopen事件
|
|
82
|
+
if ([CONNECT_STATUS.Succcess].includes(evt.data)) {
|
|
83
|
+
this.onopen && this.onopen()
|
|
84
|
+
}
|
|
85
|
+
})
|
|
86
|
+
})
|
|
87
|
+
this.handleOpen = () => {
|
|
88
|
+
this.onopen && this.onopen()
|
|
89
|
+
}
|
|
90
|
+
this.handleMessage = (evt) => {
|
|
91
|
+
this.onmessage && this.onmessage(evt)
|
|
92
|
+
}
|
|
93
|
+
this.handleClose = (evt) => {
|
|
94
|
+
this.onclose && this.onclose(evt)
|
|
95
|
+
}
|
|
96
|
+
this.handleError = (evt) => {
|
|
97
|
+
this.onerror && this.onerror(evt)
|
|
98
|
+
}
|
|
99
|
+
event.on('onopen', this.handleOpen)
|
|
100
|
+
event.on('onmessage', this.handleMessage)
|
|
101
|
+
event.on('onclose', this.handleClose)
|
|
102
|
+
event.on('onerror', this.handleError)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
setUrl (url) {
|
|
106
|
+
callPort('setUrl', url)
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
send (data) {
|
|
110
|
+
callPort('send', data)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
close (code, reason) {
|
|
114
|
+
callPort('close', {
|
|
115
|
+
code,
|
|
116
|
+
reason
|
|
117
|
+
})
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
destroy () {
|
|
121
|
+
event.off('onopen', this.handleOpen)
|
|
122
|
+
event.off('onmessage', this.handleMessage)
|
|
123
|
+
event.off('onclose', this.handleClose)
|
|
124
|
+
event.off('onerror', this.handleError)
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
package/utils/string.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
export function html2Escape (sHtml) {
|
|
2
|
+
return (sHtml || '').replace(/[<>]/g, (c) => {
|
|
3
|
+
return {
|
|
4
|
+
'<': '<',
|
|
5
|
+
'>': '>'
|
|
6
|
+
}[c]
|
|
7
|
+
})
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function md5 (str) {
|
|
11
|
+
return new Promise((resolve, reject) => {
|
|
12
|
+
import('md5').then(module => {
|
|
13
|
+
const md5 = module.default
|
|
14
|
+
resolve(md5(str))
|
|
15
|
+
}, reject)
|
|
16
|
+
})
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
let num = Date.now()
|
|
20
|
+
export function unique () {
|
|
21
|
+
num++
|
|
22
|
+
return 'u_' + num
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// =====================
|
|
26
|
+
// 公共实体映射表(仅一份,两个函数共用)
|
|
27
|
+
// =====================
|
|
28
|
+
const HTML_ENTITY = {
|
|
29
|
+
// 实体 -> 原文
|
|
30
|
+
decode: {" ":" ","<":"<",">":">","&":"&",""":"\"","'":"'","¢":"¢","£":"£","¥":"¥","€":"€","©":"©","®":"®","™":"™","×":"×","÷":"÷","–":"–","—":"—","‘":"‘","’":"’","“":"“","”":"”","…":"…","¡":"¡","¤":"¤","¦":"¦","§":"§","¨":"¨","ª":"ª","«":"«","¬":"¬","­":"","¯":"¯","°":"°","±":"±","²":"²","³":"³","´":"´","µ":"µ","¶":"¶","·":"·","¸":"¸","¹":"¹","º":"º","»":"»","¼":"¼","½":"½","¾":"¾","¿":"¿","À":"À","Á":"Á","Â":"Â","Ã":"Ã","Ä":"Ä","Å":"Å","Æ":"Æ","Ç":"Ç","È":"È","É":"É","Ê":"Ê","Ë":"Ë","Ì":"Ì","Í":"Í","Î":"Î","Ï":"Ï","Ð":"Ð","Ñ":"Ñ","Ò":"Ò","Ó":"Ó","Ô":"Ô","Õ":"Õ","Ö":"Ö","Ø":"Ø","Ù":"Ù","Ú":"Ú","Û":"Û","Ü":"Ü","Ý":"Ý","Þ":"Þ","ß":"ß","à":"à","á":"á","â":"â","ã":"ã","ä":"ä","å":"å","æ":"æ","ç":"ç","è":"è","é":"é","ê":"ê","ë":"ë","ì":"ì","í":"í","î":"î","ï":"ï","ð":"ð","ñ":"ñ","ò":"ò","ó":"ó","ô":"ô","õ":"õ","ö":"ö","ø":"ø","ù":"ù","ú":"ú","û":"û","ü":"ü","ý":"ý","þ":"þ","ÿ":"ÿ"},
|
|
31
|
+
// 原文 -> 实体(自动反转,无需手动写两份!)
|
|
32
|
+
encode: {}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
// 自动反转生成 encode 表(只执行一次)
|
|
36
|
+
for (const [k, v] of Object.entries(HTML_ENTITY.decode)) {
|
|
37
|
+
if (v !== '') HTML_ENTITY.encode[v] = k;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// =====================
|
|
41
|
+
// 解码:实体 → 原始字符
|
|
42
|
+
// =====================
|
|
43
|
+
export function decodeHtmlEntities(str) {
|
|
44
|
+
if (!str) return '';
|
|
45
|
+
return str
|
|
46
|
+
.replace(/&[a-z0-9]+;/gi, m => HTML_ENTITY.decode[m] || m)
|
|
47
|
+
.replace(/&#(\d+);/g, (_, n) => String.fromCharCode(n))
|
|
48
|
+
.replace(/&#x([0-9a-f]+);/gi, (_, h) => String.fromCharCode(parseInt(h, 16)));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// =====================
|
|
52
|
+
// 编码:原始字符 → 实体
|
|
53
|
+
// =====================
|
|
54
|
+
export function encodeHtmlEntities(str) {
|
|
55
|
+
if (!str) return '';
|
|
56
|
+
return str
|
|
57
|
+
// 只转义需要转义的核心字符,避免双重转义
|
|
58
|
+
.replace(/[&<>"']/g, m => HTML_ENTITY.encode[m]);
|
|
59
|
+
}
|