WebApp【公共组件库】@前端(For Git Submodule)
‘chensiAb’
2025-04-01 196aac76666571c60536973418bff3da63bef1d8
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/**
 * SmartAddress
 * @author Tevin
 */
 
import { $locations } from '@components/forms/chinaArea/ChinaLocations';
import { Tools } from '@components/common/Tools';
 
export class SmartAddress {
    constructor() {
        this.wordData = {};
    }
 
    parseWord(word, callback) {
        if (!this._checkLength(word)) {
            callback(false);
            return;
        }
        this.wordData = {};
        const wordArr = this._splitWord(word);
        $locations.onReady(() => {
            this._checkPhone(wordArr);
            this._checkCompany(wordArr);
            this._checkUser(wordArr);
            this._checkAddress(wordArr);
            if (Tools.isEmptyObject(this.wordData)) {
                callback(false);
            } else {
                callback({ ...this.wordData });
            }
        });
    }
 
    _checkLength(word) {
        if (!word || typeof word !== 'string') {
            return false;
        } else if (word.length < 8) {
            return false;
        } else if (word.length > 150) {
            return false;
        }
        return true;
    }
 
    _splitWord(word) {
        word = word.replace(/([::]) */g, '$1');  // 带类型名模式预处理
        const splitReg = /[ \n\r…,.;?!'"()\[\],。;?!‘’“”()【】—|]/g;
        const array = word.split(splitReg).filter(Boolean);
        return array.map(item => {
            const nextItem = { label: '', content: '' };
            if (/[::]/.test(item)) {
                const [label, content] = item.split(/[::]/g).filter(Boolean);
                nextItem.label = label;
                nextItem.content = content;
            } else {
                nextItem.content = item;
            }
            return nextItem;
        });
    }
 
    _checkPhone(list) {
        const phoneLabelReg = /^电话|联系电话|手机|手机号码$/;
        const phoneReg = /^1[3456789]\d{9}$/;
        const telReg = /\d{3.4}-\d{8}/;
        for (let i = 0, item; (item = list[i]); i++) {
            if (!item.content) {
                continue;
            }
            if (item.label) {
                if (phoneLabelReg.test(item.label)) {
                    this.wordData.phone = item.content;
                    if (this.wordData.phone.length === 13) {
                        this.wordData.phone = this.wordData.phone.replace(/-/g, '');
                    }
                    list.splice(i, 1);
                    break;
                }
            } else {
                const phone = item.content.replace(/-/g, '');
                if (phoneReg.test(phone)) {
                    this.wordData.phone = phone;
                    list.splice(i, 1);
                    break;
                } else if (telReg.test(item.content)) {
                    this.wordData.phone = phone;
                    list.splice(i, 1);
                    break;
                }
            }
        }
    }
 
    _checkCompany(list) {
        const companyLabelReg = /公司|公司名称|客户|客户名称/;
        const companyNameReg = /公司|厂|站|气|部$/;
        for (let i = 0, item; (item = list[i]); i++) {
            if (!item.content) {
                continue;
            }
            if (item.label) {
                if (companyLabelReg.test(item.label)) {
                    this.wordData.company = item.content;
                    list.splice(i, 1);
                    break;
                }
            } else {
                if (item.content.length > 5 && companyNameReg.test(item.content)) {
                    this.wordData.company = item.content;
                    list.splice(i, 1);
                    break;
                }
            }
        }
    }
 
    _checkUser(list) {
        const userLabelReg = /姓名|联系人|收货人|收件人/;
        const userReg = /^[\u4e00-\u9fa5]{2,3}$/;
        for (let i = 0, item; (item = list[i]); i++) {
            if (!item.content) {
                continue;
            }
            if (item.label) {
                if (userLabelReg.test(item.label)) {
                    this.wordData.user = item.content;
                    list.splice(i, 1);
                    break;
                }
            } else {
                if (userReg.test(item.content)) {
                    this.wordData.user = item.content;
                    list.splice(i, 1);
                    break;
                }
            }
        }
    }
 
    _checkAddress(list) {
        const adrLabelReg = /地址|详细地址|公司地址|联系地址|地区|所在地区/;
        $locations.getLocationTree(3, chinaTree => {
            for (let i = 0, item; (item = list[i]); i++) {
                if (!item.content) {
                    continue;
                }
                if (item.label) {
                    if (adrLabelReg.test(item.label)) {
                        this._parseAddress(item.content);
                        list.splice(i, 1);
                        break;
                    }
                } else {
                    if (this._isAddress(chinaTree, item.content)) {
                        this._parseAddress(chinaTree, item.content);
                        list.splice(i, 1);
                        break;
                    }
                }
            }
        });
    }
 
    _isAddress(tree, content) {
        content = content.replace('中国', '');
        const provinceNames = tree.map(lv1 => lv1.label.replace('特别行政区', '').replace('省', ''));
        for (let province of provinceNames) {
            if (content.indexOf(province) >= 0) {
                return true;
            }
        }
        return false;
    }
 
    _parseAddress(chinaTree, content) {
        content = content.replace('中国', '');
        // 省份处理 ---
        const provinceTail = '特别行政区|壮族自治区|回族自治区|维吾尔自治区|自治区|省|市';
        const provinceTailReg = new RegExp(provinceTail + '$');
        for (let province of chinaTree) {
            const provinceStr = province.label.replace(provinceTailReg, '');
            // 省份匹配
            if (content.indexOf(provinceStr) === 0) {
                // 保存省
                this.wordData.province = province.label;
                this.wordData.provinceCode = province.value;
                // 移除省份
                const provinceNameReg = new RegExp('^' + provinceStr + '(' + provinceTail + ')?');
                content = content.replace(provinceNameReg, '');
                // 市级处理 ---
                const cityTail = '市|区|县|自治县|自治州|地区';
                const cityTailReg = new RegExp(cityTail + '$');
                for (let city of province.children) {
                    const cityStr = city.label.replace(cityTailReg, '');
                    // 市级匹配
                    if (content.indexOf(cityStr) === 0) {
                        // 保存市级
                        this.wordData.city = city.label;
                        this.wordData.cityCode = city.value;
                        // 移除市级
                        const cityNameReg = new RegExp('^' + cityStr + '(' + cityTail + ')?');
                        content = content.replace(cityNameReg, '');
                        // 区级处理 ---
                        const distTail = '市|区|县|镇|乡|自治县|开发区|新区|矿区|旗|自治旗';
                        const distTailReg = new RegExp(distTail + '$');
                        for (let dist of city.children) {
                            const distStr = dist.label.replace(distTailReg, '');
                            // 区级匹配
                            if (content.indexOf(distStr) === 0) {
                                // 保存区级
                                this.wordData.dist = dist.label;
                                this.wordData.distCode = dist.value;
                                // 移除区级
                                const distNameReg = new RegExp('^' + distStr + '(' + distTail + ')?');
                                content = content.replace(distNameReg, '');
                            }
                        }
                    }
                }
            }
        }
        // 省市区都匹配上了,剩下的为街道
        if (this.wordData.province && this.wordData.city && this.wordData.dist) {
            this.wordData.street = content;
        }
    }
 
}