From 7c2e817088e096f5b69471730f55f7a94da82db0 Mon Sep 17 00:00:00 2001 From: Tevin <tingquanren@163.com> Date: Tue, 21 Mar 2023 16:19:52 +0800 Subject: [PATCH] 允许关闭mock,使用post请求本地模拟接口 --- bases/HostBoot.js | 30 +++++++++++++++++++++++++++--- 1 files changed, 27 insertions(+), 3 deletions(-) diff --git a/bases/HostBoot.js b/bases/HostBoot.js index 41a9460..83aefda 100644 --- a/bases/HostBoot.js +++ b/bases/HostBoot.js @@ -5,6 +5,7 @@ import project from '@project'; import { Tools } from '@components/common/Tools'; +import { $localStorage } from '@components/common/LocalStorage'; export class HostBoot { @@ -13,6 +14,7 @@ */ constructor() { this._data = { + mockOff: Tools.getUrlParam('mock') === 'off', // 可用主机列表 hostList: { ...project.host.hosts, @@ -27,9 +29,9 @@ /** * 创建主机类型 * @param {string} typeName - * @param {boolean} [isDefault=false] + * @param {string} [isDefault=''] */ - createHostType(typeName, isDefault = false) { + createHostType(typeName, isDefault = '') { if (isDefault === 'default') { this._data.defaultHostType = typeName; } @@ -51,7 +53,7 @@ }; } // 如果有匹配服务器,使用指定的服务器地址 - if (typeof this._data.hostList[server] !== 'undefined') { + else if (typeof this._data.hostList[server] !== 'undefined') { this._data.activeHost[typeName] = { name: server, host: this._data.hostList[server], @@ -93,6 +95,11 @@ type: typeName, }; } + // 如果有缓存,优先使用缓存 + const storageHostTypes = $localStorage.load('HostType'); + if (typeof storageHostTypes[typeName] !== 'undefined') { + this._data.activeHost[typeName] = storageHostTypes[typeName]; + } } _matchHostName(host) { @@ -129,8 +136,18 @@ host: host2, type: typeName, }; + // 存储自定义主机配置 + const storageHostTypes = $localStorage.load('HostType'); + storageHostTypes[typeName] = this._data.activeHost[typeName]; + $localStorage.save('HostType', storageHostTypes); } + /** + * 清除主机类型缓存 + */ + cleanHostStorage() { + $localStorage.remove('HostType'); + } /** * 判断是否为本地开发模式 @@ -171,8 +188,15 @@ return this._data.activeHost[typeName].name; } + /** + * 是否开启本地 mock + * @return {boolean} + */ isOnMock() { if (this.isDevMod()) { + if (this._data.mockOff) { + return false; + } return this.getHostName() === 'lc'; } else { return false; -- Gitblit v1.9.1