From b2b8dad778d1767ef9e609734694de97537762cf Mon Sep 17 00:00:00 2001 From: Tevin <tingquanren@163.com> Date: Wed, 07 Apr 2021 11:47:09 +0800 Subject: [PATCH] 添加模拟数据模式 --- bases/Fetcher.js | 38 ++++++++++++++++++++++++++------------ 1 files changed, 26 insertions(+), 12 deletions(-) diff --git a/bases/Fetcher.js b/bases/Fetcher.js index f227744..b683b42 100644 --- a/bases/Fetcher.js +++ b/bases/Fetcher.js @@ -18,7 +18,11 @@ this._data = { urlPrefix: options.urlPrefix || ['/api/common/', '/api/common/'], }; - this._defaultConfig.url = Fetcher.host; + if (project.host.mock) { + this._defaultConfig.url = Fetcher.host + project.host.assetsPath.replace('/assets', '/mocks'); + } else { + this._defaultConfig.url = Fetcher.host; + } } /** @@ -45,13 +49,20 @@ */ spellURL(devSuffix, serSuffix) { let url = ''; - // 开发环境地址 - if (Fetcher.inDevMod) { - url = this._data.urlPrefix[0] + devSuffix; + // mock 模式 + if (project.host.mock) { + url = this._data.urlPrefix[0].replace('api/', '') + devSuffix + '.json'; } - // 生产环境地址 + // 正常模式 else { - url = this._data.urlPrefix[1] + (serSuffix || devSuffix); + // 开发环境地址 + if (Fetcher.inDevMod) { + url = this._data.urlPrefix[0] + devSuffix; + } + // 生产环境地址 + else { + url = this._data.urlPrefix[1] + (serSuffix || devSuffix); + } } const fixReg = /[a-zA-Z0-9]+\/\.\.\//; while (url.indexOf('../') >= 0) { @@ -88,6 +99,9 @@ * @return {Promise<any>} */ post(url, data, remap = [], options = null) { + if (project.host.mock) { + return this.get(url, data, remap = [], options); + } const params = Qs.stringify(data); const data2 = {}; params.split('&').forEach(param => { @@ -540,13 +554,13 @@ if (sever && typeof project.host.hosts[sever] !== 'undefined') { return project.host.hosts[sever]; } - // 使用设置的默认服务器地址 - else if (project.host.server) { - return project.host.server; - } // 网页域名提取服务器地址 - else { + else if (window.location.protocol.indexOf('http') >= 0) { return window.location.protocol + '//' + window.location.host; + } + // 非 http 协议打开时,使用设置的服务器地址 + else if (project.host.server) { + return project.host.hosts[project.host.severType]; } } } @@ -558,7 +572,7 @@ } // 生产 else { - return project.host.server; + return project.host.hosts[project.host.severType]; } } })(); -- Gitblit v1.9.1