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/Fetcher.js |   38 +++++++++++++++++++++++++++++++-------
 1 files changed, 31 insertions(+), 7 deletions(-)

diff --git a/bases/Fetcher.js b/bases/Fetcher.js
index b2ffbd9..c81008e 100644
--- a/bases/Fetcher.js
+++ b/bases/Fetcher.js
@@ -64,8 +64,12 @@
     spellURL(devSuffix, serSuffix) {
         let url = '';
         // mock地址
-        if ($hostBoot.isOnMock()) {
-            url = this._data.urlPrefix[0].replace('api/', '') + devSuffix + '.json';
+        if ($hostBoot.isDevMod()) {
+            if ($hostBoot.isOnMock())  {
+                url = this._data.urlPrefix[0].replace('api/', '') + devSuffix + '.json';
+            } else {
+                url = this._data.urlPrefix[0] + devSuffix;
+            }
         }
         // 正常地址
         else {
@@ -147,6 +151,13 @@
                     if (process.env.TARO_ENV === 'weapp') {
                         this._saveCookies(response.cookies);
                     }
+                    // 捕获响应
+                    options && options.onCapture && options.onCapture({
+                        url: this._createUrlPrefix(options) + url,
+                        request: data,
+                        response: { ...response.data },
+                        httpCode: response.statusCode,
+                    });
                     /**
                      * @type {{state: {code, http, msg}, data: Object}}
                      * @example response.state.code
@@ -161,9 +172,16 @@
                     resolve(this._transformResponseData(responseData, options));
                 },
                 fail: error => {
-                    if (typeof options.silence === 'undefined' || !options.silence) {
-                        this._resolveCaughtNetErr(error);
-                    }
+                    // 处理响应
+                    this._resolveCaughtNetErr(error, options, msg => {
+                        // 捕获响应
+                        options && options.onCapture && options.onCapture({
+                            url: this._createUrlPrefix(options) + url,
+                            request: data,
+                            httpCode: error && error.status,
+                            httpMsg: msg + (error.message ? (' / ' + error.message) : ''),
+                        });
+                    });
                     reject(null);
                 },
             });
@@ -309,9 +327,11 @@
     /**
      * 解析捕获的网络错误
      * @param err
+     * @param options
+     * @param callback
      * @private
      */
-    _resolveCaughtNetErr(err) {
+    _resolveCaughtNetErr(err, options, callback) {
         let msg = '';
         if (err && err.status) {
             switch (err.status) {
@@ -349,14 +369,18 @@
         } else {
             msg += '解析通讯数据异常!';
         }
+        callback(msg);
         setTimeout(() => {
-            this._message('fail', msg);
+            if (typeof options.silence === 'undefined' || !options.silence) {
+                this._message('fail', msg);
+            }
         }, 20);
     }
 
     /**
      * 转换响应体
      * @param response
+     * @param options
      * @returns {Object|{}|null}
      * @private
      */

--
Gitblit v1.9.1