From 58f1c9e8d8bc52ea71c6a03e18472fa401ad8ad8 Mon Sep 17 00:00:00 2001
From: Tevin <tingquanren@163.com>
Date: Thu, 08 Apr 2021 10:49:43 +0800
Subject: [PATCH] 再次优化请求策略

---
 bases/Fetcher.js |   34 ++++++++++++++++++++++++++--------
 1 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/bases/Fetcher.js b/bases/Fetcher.js
index 8392590..aae3313 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 === 'on') {
+            this._defaultConfig.url = Fetcher.host + project.host.assetsPath.replace('/assets', '/mocks');
+        } else {
+            this._defaultConfig.url = Fetcher.host;
+        }
     }
 
     /**
@@ -45,13 +49,24 @@
      */
     spellURL(devSuffix, serSuffix) {
         let url = '';
-        // 开发环境地址
-        if (Fetcher.inDevMod) {
-            url = this._data.urlPrefix[0] + devSuffix;
+        // mock 模式
+        if (project.host.mock === 'on') {
+            url = this._data.urlPrefix[0].replace('api/', '') + devSuffix + '.json';
         }
-        // 生产环境地址
-        else {
+        // 强制实际请求模式
+        else if (project.host.mock === 'real') {
             url = this._data.urlPrefix[1] + (serSuffix || devSuffix);
+        }
+        // 正常模式
+        else {
+            // 开发环境地址
+            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 +103,9 @@
      * @return {Promise<any>}
      */
     post(url, data, remap = [], options = null) {
+        if (project.host.mock === 'on') {
+            return this.get(url, data, remap = [], options);
+        }
         const params = Qs.stringify(data);
         const data2 = {};
         params.split('&').forEach(param => {
@@ -546,7 +564,7 @@
                 }
                 // 非 http 协议打开时,使用设置的服务器地址
                 else if (project.host.server) {
-                    return project.host.server;
+                    return project.host.hosts[project.host.serverType];
                 }
             }
         }
@@ -558,7 +576,7 @@
             }
             // 生产
             else {
-                return project.host.server;
+                return project.host.hosts[project.host.serverType];
             }
         }
     })();

--
Gitblit v1.9.1