/**
|
* 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();
|