| | |
| | | constructor() { |
| | | this._data = { |
| | | // 可用主机列表 |
| | | hostList: { ...project.host.hosts }, |
| | | hostList: { |
| | | ...project.host.hosts, |
| | | }, |
| | | // 当前主机 |
| | | activeHost: {}, |
| | | defaultHostName: '', |
| | | // 默认服务类型 |
| | | defaultHostType: '', |
| | | }; |
| | | } |
| | | |
| | | /** |
| | | * 创建主机地址 |
| | | * @param {string} hostName |
| | | * @param {string} [isDefault=false] |
| | | * 创建主机类型 |
| | | * @param {string} typeName |
| | | * @param {string} [isDefault=''] |
| | | */ |
| | | createHost(hostName, isDefault = false) { |
| | | createHostType(typeName, isDefault = '') { |
| | | if (isDefault === 'default') { |
| | | this._data.defaultHostName = hostName; |
| | | this._data.defaultHostType = typeName; |
| | | } |
| | | const defaultTypeName = this.isDevMod() ? project.host.devType : project.host.serverType; |
| | | const defaultHostName = this.isDevMod() ? project.host.devHost : project.host.buildHost; |
| | | // 网页模式 |
| | | if (process.env.TARO_ENV === 'h5') { |
| | | // 如果网址参数有指定服务器 |
| | |
| | | const portal = server.split('//')[0]; |
| | | const domain = server.split('//')[1].split('/')[0]; |
| | | const host = portal + '//' + domain; |
| | | this._data.activeHost[hostName] = { |
| | | typeName: this._matchTypeName(host), |
| | | this._data.activeHost[typeName] = { |
| | | name: this._matchHostName(host), |
| | | host, |
| | | type: typeName, |
| | | }; |
| | | } |
| | | // 如果有匹配服务器,使用指定的服务器地址 |
| | | if (typeof this._data.hostList[server] !== 'undefined') { |
| | | this._data.activeHost[hostName] = { |
| | | typeName: server, |
| | | this._data.activeHost[typeName] = { |
| | | name: server, |
| | | host: this._data.hostList[server], |
| | | type: typeName, |
| | | }; |
| | | } |
| | | // 否则使用默认 |
| | | else { |
| | | this._data.activeHost[hostName] = { |
| | | typeName: defaultTypeName, |
| | | host: this._data.hostList[defaultTypeName], |
| | | 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[hostName] = { |
| | | typeName: this._matchTypeName(host), |
| | | this._data.activeHost[typeName] = { |
| | | name: this._matchHostName(host), |
| | | host, |
| | | type: typeName, |
| | | }; |
| | | } |
| | | // 既不指定server也不是域名访问,使用设置的服务器地址 |
| | | else { |
| | | this._data.activeHost[hostName] = { |
| | | typeName: defaultTypeName, |
| | | host: this._data.hostList[defaultTypeName], |
| | | this._data.activeHost[typeName] = { |
| | | name: defaultHostName, |
| | | host: this._data.hostList[defaultHostName], |
| | | type: typeName, |
| | | }; |
| | | } |
| | | } |
| | | // 小程序模式 |
| | | else if (process.env.TARO_ENV === 'weapp') { |
| | | this._data.activeHost[hostName] = { |
| | | typeName: defaultTypeName, |
| | | host: this._data.hostList[defaultTypeName], |
| | | this._data.activeHost[typeName] = { |
| | | name: defaultHostName, |
| | | host: this._data.hostList[defaultHostName], |
| | | type: typeName, |
| | | }; |
| | | } |
| | | } |
| | | |
| | | _matchTypeName(host) { |
| | | _matchHostName(host) { |
| | | const domain = host.split('//')[1]; |
| | | let typeName = ''; |
| | | let hostName = ''; |
| | | if (/127|192|localhost/.test(domain)) { |
| | | typeName = 'lc'; |
| | | hostName = 'lc'; |
| | | } else { |
| | | typeName = domain.substr(0, 2); |
| | | hostName = domain.substr(0, 2); |
| | | } |
| | | // 尝试匹配已有主机名称 |
| | | Object.keys(this._data.hostList).forEach(key => { |
| | | if (this._data.hostList[key] === host) { |
| | | typeName = key; |
| | | hostName = key; |
| | | } |
| | | }); |
| | | return typeName; |
| | | return hostName; |
| | | } |
| | | |
| | | /** |
| | | * 更新主机 |
| | | * @param {string} hostName |
| | | * 更新主机类型 |
| | | * @param {string} typeName |
| | | * @param {string} host |
| | | */ |
| | | updateHost(hostName, host) { |
| | | hostName = hostName ? hostName : this._data.defaultHostName; |
| | | this._data.activeHost[hostName] = { |
| | | typeName: this._matchTypeName(host), |
| | | 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, |
| | | }; |
| | | } |
| | | |
| | |
| | | |
| | | /** |
| | | * 获取当前主机地址 |
| | | * @param {string} [hostName] |
| | | * @param {string} [typeName] |
| | | * @return {string} |
| | | */ |
| | | getHost(hostName = this._data.defaultHostName) { |
| | | if (typeof this._data.activeHost[hostName] === 'undefined') { |
| | | getHost(typeName = this._data.defaultHostType) { |
| | | if (typeof this._data.activeHost[typeName] === 'undefined') { |
| | | return ''; |
| | | } |
| | | return this._data.activeHost[hostName].host; |
| | | return this._data.activeHost[typeName].host; |
| | | } |
| | | |
| | | /** |
| | | * 获取当前主机类型简称 |
| | | * @param {string} [hostName] |
| | | * @param {string} [typeName] |
| | | * @return {string} |
| | | */ |
| | | getTypeName(hostName = this._data.defaultHostName) { |
| | | if (typeof this._data.activeHost[hostName] === 'undefined') { |
| | | getHostName(typeName = this._data.defaultHostType) { |
| | | if (typeof this._data.activeHost[typeName] === 'undefined') { |
| | | return ''; |
| | | } |
| | | return this._data.activeHost[hostName].typeName; |
| | | return this._data.activeHost[typeName].name; |
| | | } |
| | | |
| | | isOnMock(){ |
| | | isOnMock() { |
| | | if (this.isDevMod()) { |
| | | return this.getTypeName() === 'lc'; |
| | | return this.getHostName() === 'lc'; |
| | | } else { |
| | | return false; |
| | | } |