Browse Source

错误信息保存数据库的修改

zhaojing 3 years ago
parent
commit
06093cb4b5
2 changed files with 160 additions and 74 deletions
  1. 53 0
      requests/errorRequest.js
  2. 107 74
      requests/http.js

+ 53 - 0
requests/errorRequest.js

@@ -0,0 +1,53 @@
+
+// 错误接口信息存到后台数据库  persagy定制
+
+export default function (pobj) {
+    const API_BASE_URL = "https://duoduoenv.sagacloud.cn";
+  var params = {
+    name: pobj.name, // 类型:String  必有字段  备注:接口名字
+    type: 2, // 类型:String  必有字段  备注:类型,默认传 2 (表示异常)
+    success: false, // 类型:Boolean  必有字段  备注:默认false
+    input: pobj.input, // 类型:String  必有字段  备注:请求输入参数
+    exception: pobj.exception // 类型:String  必有字段  备注:异常信息
+  };
+  var info = {
+    url: `${API_BASE_URL}/server/systemLog/save`,
+    method: 'POST',
+    data: JSON.stringify(params)
+  };
+
+//   const token = store.state.user.token;
+//   if (token) {
+//     info.header = Object.assign({
+//       token
+//     }, info.header)
+//   }
+
+  info.timeout = info.timeout || 10 * 1000; // 默认超时为10s
+
+  return new Promise((resolve, reject) => {
+    wx.request(
+      Object.assign(info, {
+        success(res) {
+          if (res.statusCode >= 400) {
+            console.log(
+              `systemLog/save接口请求失败:code=${res.statusCode},msg=${
+                res.message || JSON.stringify(res)
+              }`
+            );
+            reject(res.errMsg);
+          } else if (res.data.code !== 1 && res.data.code) {
+            console.log('systemLog/save请求失败:' + `${res.data.msg}`);
+            reject(res.data);
+          } else {
+            resolve(res.data);
+          }
+        },
+        fail(res) {
+          reject({ ...res, reason: 'wxRequestFail' });
+          console.log(`微信接口systemLog/save调用失败:${JSON.stringify(res)}`);
+        }
+      })
+    );
+  });
+}

+ 107 - 74
requests/http.js

@@ -1,82 +1,115 @@
-import $ from "../utils/Tool.js"
-
+import $ from "../utils/Tool.js";
+import errorRequest from "./errorRequest.js";
 // const test = "http://192.168.12.20:50009"; //192.168.12.20 172.16.36.245
 const API_BASE_URL = "https://duoduoenv.sagacloud.cn";
-function get(url, data = {},testenv){
-  let _url;
-  if(testenv){
-    _url = test + url.replace('/server','');
-  }else{
-    _url = API_BASE_URL + url;
-  }
+function get(url, data = {}, testenv) {
+    let _url;
+    if (testenv) {
+        _url = test + url.replace("/server", "");
+    } else {
+        _url = API_BASE_URL + url;
+    }
+
+    data = Object.assign(data, {
+        pubname: "sagacare",
+        openid: $.storage.get("openId"),
+    });
+    return new Promise(function (resolve, reject) {
+        wx.request({
+            url: _url,
+            method: "GET",
+            data: data,
+            header: {
+                "content-type": "application/json",
+            },
+            success: function success(request) {
+                if (request.statusCode >= 400) {
+                    console.log(
+                      'systemLog/save接口请求失败',request
+                    );
+                    errorRequest({
+                        name: _url, // 类型:String  必有字段  备注:接口名字
+                        input: JSON.stringify(data) || '', // 类型:String  必有字段  备注:请求输入参数
+                        exception: JSON.stringify(request.errMsg) // 类型:String  必有字段  备注:异常信息
+                      });
+                    reject(request.errMsg);
+                  } else{
+                    resolve(request.data);
+                  }
 
-  data = Object.assign(data, {
-    pubname: "sagacare",
-    openid:$.storage.get('openId')
-  });
-  return new Promise(function (resolve, reject) {
-    wx.request({
-      url: _url,
-      method: 'GET',
-      data: data,
-      header: {
-        'content-type': 'application/json'
-      },
-      success: function success(request) {
-        resolve(request.data);
-      },
-      fail: function fail(error) {
-        wx.showModal({
-          title: '网络错误',
-          content: '网络出错,请刷新重试',
-          showCancel: false
-        })
-        reject(error);
-      },
-      complete: function complete() {
-      }
+            },
+            fail: function fail(error) {
+                wx.showModal({
+                    title: "网络错误",
+                    content: "网络出错,请刷新重试",
+                    showCancel: false,
+                });
+                errorRequest({
+                    name: _url, // 类型:String  必有字段  备注:接口名字
+                    input: JSON.stringify(data) || '', // 类型:String  必有字段  备注:请求输入参数
+                    exception: JSON.stringify(error), // 类型:String  必有字段  备注:异常信息
+                });
+                reject(error);
+            },
+            complete: function complete() {},
+        });
     });
-  });
 }
 
-function post(url, data = {},testenv) {
-  let _url;
-  if(testenv){
-    _url = test + url.replace('/server','');
-  }else{
-    _url = API_BASE_URL + url;
-  }
-  data = Object.assign(data, {
-    openid:$.storage.get('openId')
-  })
-  _url = _url + `?pubname=sagacare&openid=${$.storage.get('openId')}`
-  if (data.flag) {
-    _url = _url + `&flag=${data.flag}`
-  }
-  return new Promise(function (resolve, reject) {
-    wx.request({
-      url: _url,
-      method: 'POST',
-      data: data,
-      header: {
-        'content-type': 'application/json'
-      },
-      success: function success(request) {
-        resolve(request.data);
-      },
-      fail: function fail(error) {
-        wx.showModal({
-          title: '网络错误',
-          content: '网络出错,请刷新重试',
-          showCancel: false
-        })
-        reject(error);
-      },
-      complete: function complete() {
-      }
+function post(url, data = {}, testenv) {
+    let _url;
+    if (testenv) {
+        _url = test + url.replace("/server", "");
+    } else {
+        _url = API_BASE_URL + url;
+    }
+    data = Object.assign(data, {
+        openid: $.storage.get("openId"),
     });
-  });
-};
+    _url = _url + `?pubname=sagacare&openid=${$.storage.get("openId")}`;
+    if (data.flag) {
+        _url = _url + `&flag=${data.flag}`;
+    }
+    return new Promise(function (resolve, reject) {
+        wx.request({
+            url: _url,
+            method: "POST",
+            data: data,
+            header: {
+                "content-type": "application/json",
+            },
+            success: function success(request) {
+                if (request.statusCode >= 400) {
+                    console.log(
+                      'systemLog/save接口请求失败',request
+                    );
+                    errorRequest({
+                        name: _url, // 类型:String  必有字段  备注:接口名字
+                        input: JSON.stringify(data) || '', // 类型:String  必有字段  备注:请求输入参数
+                        exception: JSON.stringify(request.errMsg) // 类型:String  必有字段  备注:异常信息
+                      });
+                    reject(request.errMsg);
+                  } else{
+                    resolve(request.data);
+                  }
+            },
+            fail: function fail(error) {
+                wx.showModal({
+                    title: "网络错误",
+                    content: "网络出错,请刷新重试",
+                    showCancel: false,
+                });
+                errorRequest({
+                    name: _url, // 类型:String  必有字段  备注:接口名字
+                    input: JSON.stringify(data) || '', // 类型:String  必有字段  备注:请求输入参数
+                    exception: JSON.stringify(error) // 类型:String  必有字段  备注:异常信息
+                  });
+                reject(error);
+            },
+            complete: function complete() {},
+        });
+    });
+}
 module.exports = {
-  http:{get,post}
-}
+    http: { get, post },
+};