From 219fb684a854c63d51fdad6443e65693447b4f26 Mon Sep 17 00:00:00 2001
From: Tevin <tingquanren@163.com>
Date: Mon, 03 Mar 2025 17:25:52 +0800
Subject: [PATCH] 知识库文档,数据控制层基类,补充部分说明

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

diff --git a/bases/Fetcher.js b/bases/Fetcher.js
index b2ffbd9..54a41c8 100644
--- a/bases/Fetcher.js
+++ b/bases/Fetcher.js
@@ -125,7 +125,7 @@
      * @param {String} url
      * @param {*} [data]
      * @param {object} [options]
-     * @return {Promise<any>|}
+     * @return {Promise<any>}
      */
     query(type, url, data = null, options = {}) {
         return new Promise((resolve, reject) => {
@@ -147,6 +147,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 +168,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 +323,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 +365,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
      */
@@ -570,7 +590,7 @@
             title: msg,
             icon: 'none',
             mask: false,
-            duration: type === 'fail' ? 3000 : 2000,
+            duration: (type === 'fail' || type === 'error') ? 3000 : 2000,
         });
     }
 

--
Gitblit v1.9.1