WebApp【公共组件库】@前端(For Git Submodule)
Tevin
2025-04-09 e9799a5b4b9f221447cba49e8fbf7bea6b6c0c42
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
/**
 * HostBoot
 * @author Tevin
 */
 
import project from '@project';
import { Tools } from '@components/common/Tools';
import { $localStorage } from '@components/common/LocalStorage';
 
export class HostBoot {
 
    /**
     * @constructor
     */
    constructor() {
        this._data = {
            // 可用主机列表
            hostList: {
                ...project.host.hosts,
            },
            // 当前主机
            activeHost: {},
            // 默认服务类型
            defaultHostType: '',
        };
    }
 
    /**
     * 创建主机类型
     * @param {string} typeName
     * @param {string} [isDefault='']
     */
    createHostType(typeName, isDefault = '') {
        if (isDefault === 'default') {
            this._data.defaultHostType = typeName;
        }
        const defaultHostName = this.isDevMod() ? project.host.devHost : project.host.buildHost;
        // 网页模式
        if (process.env.TARO_ENV === 'h5') {
            // 如果网址参数有指定服务器
            const server = Tools.getUrlParam('server');
            if (server) {
                // 如果是完整网址,使用网址对应的域名
                if (server.indexOf('http') >= 0) {
                    const portal = server.split('//')[0];
                    const domain = server.split('//')[1].split('/')[0];
                    const host = portal + '//' + domain;
                    this._data.activeHost[typeName] = {
                        name: this._matchHostName(host),
                        host,
                        type: typeName,
                    };
                }
                // 如果有匹配服务器,使用指定的服务器地址
                else if (typeof this._data.hostList[server] !== 'undefined') {
                    this._data.activeHost[typeName] = {
                        name: server,
                        host: this._data.hostList[server],
                        type: typeName,
                    };
                }
                // 否则使用默认
                else {
                    this._data.activeHost[typeName] = {
                        name: defaultHostName,
                        host: this._data.hostList[defaultHostName],
                        type: typeName,
                    };
                }
            }
            // 网页域名提取服务器地址
            else if (window.location.protocol.indexOf('http') >= 0) {
                const host = window.location.protocol + '//' + window.location.host;
                this._data.activeHost[typeName] = {
                    name: this._matchHostName(host),
                    host,
                    type: typeName,
                };
            }
            // 既不指定server也不是域名访问,使用设置的服务器地址
            else {
                this._data.activeHost[typeName] = {
                    name: defaultHostName,
                    host: this._data.hostList[defaultHostName],
                    type: typeName,
                };
            }
        }
        // 小程序模式
        else if (process.env.TARO_ENV === 'weapp') {
            this._data.activeHost[typeName] = {
                name: defaultHostName,
                host: this._data.hostList[defaultHostName],
                type: typeName,
            };
        }
        // 如果有缓存,优先使用缓存
        const storageHostTypes = $localStorage.load('HostType');
        if (typeof storageHostTypes[typeName] !== 'undefined') {
            this._data.activeHost[typeName] = storageHostTypes[typeName];
        }
    }
 
    _matchHostName(host) {
        const domain = host.split('//')[1];
        let hostName = '';
        if (/127|192|localhost/.test(domain)) {
            hostName = 'lc';
        } else {
            hostName = domain.substr(0, 2);
        }
        // 尝试匹配已有主机名称
        Object.keys(this._data.hostList).forEach(key => {
            if (this._data.hostList[key] === host) {
                hostName = key;
            }
        });
        return hostName;
    }
 
    /**
     * 更新主机类型
     * @param {string} typeName
     * @param {string} host
     */
    updateHostType(typeName, host) {
        if (!typeName) {
            return;
        }
        const portal = host.split('//')[0];
        const domain = host.split('//')[1].split('/')[0];
        const host2 = portal + '//' + domain;
        this._data.activeHost[typeName] = {
            name: this._matchHostName(host),
            host: host2,
            type: typeName,
        };
        // 存储自定义主机配置
        const storageHostTypes = $localStorage.load('HostType');
        storageHostTypes[typeName] = this._data.activeHost[typeName];
        $localStorage.save('HostType', storageHostTypes);
    }
 
    /**
     * 清除主机类型缓存
     */
    cleanHostStorage() {
        $localStorage.remove('HostType');
    }
 
    /**
     * 判断是否为本地开发模式
     * @return {Boolean}
     */
    isDevMod() {
        // 开发编译
        if (process.env.NODE_ENV === 'development') {
            return true;
        }
        // 生产编译
        else if (process.env.NODE_ENV === 'production') {
            return false;
        }
    }
 
    /**
     * 获取当前主机地址
     * @param {string} [typeName]
     * @return {string}
     */
    getHost(typeName = this._data.defaultHostType) {
        if (typeof this._data.activeHost[typeName] === 'undefined') {
            return '';
        }
        return this._data.activeHost[typeName].host;
    }
 
    /**
     * 获取当前主机类型简称
     * @param {string} [typeName]
     * @return {string}
     */
    getHostName(typeName = this._data.defaultHostType) {
        if (typeof this._data.activeHost[typeName] === 'undefined') {
            return '';
        }
        return this._data.activeHost[typeName].name;
    }
 
    /**
     * 是否开启本地 mock
     * @return {boolean}
     */
    isOnMock() {
        if (this.isDevMod()) {
            return this.getHostName() === 'lc';
        } else {
            return false;
        }
    }
 
}
 
export const $hostBoot = new HostBoot();