liaoanqi 2 жил өмнө
parent
commit
b99b6464fa

+ 46 - 23
mall4m/utils/http.js

@@ -20,39 +20,62 @@ function request(params, isGetTonken) {
     dataType: 'json',
     responseType: params.responseType == undefined ? 'text' : params.responseType,
     success: function(res) {
-      if (res.statusCode == 200) {
-        //如果有定义了params.callBack,则调用 params.callBack(res.data)
+			const responseData = res.data
+
+      // 00000 请求成功
+      if (responseData.code === '00000') {
         if (params.callBack) {
-          params.callBack(res.data);
+          params.callBack(responseData.data);
         }
+        return
+      }
 
-      } else if (res.statusCode == 500) {
-        wx.showToast({
-          title: "服务器出了点小差",
-          icon: "none"
-        });
-      } else if (res.statusCode == 401) {
+      // A00004 未授权
+      if (responseData.code === 'A00004') {
         wx.navigateTo({
           url: '/pages/login/login',
         })
-        // // 添加到请求队列
-        // globalData.requestQueue.push(params);
-        // // 是否正在登陆
-        // if (!globalData.isLanding) {
-        //   globalData.isLanding = true
-        //   //重新获取token,再次请求接口
-        //   getToken();
-        // }
-      } else if (res.statusCode == 400) {
+				return
+      }
+
+      // A00005 服务器出了点小差
+      if (responseData.code === 'A00005') {
+        console.error('============== 请求异常 ==============')
+        console.log('接口: ', params.url)
+        console.log('异常信息: ', responseData)
+        console.error('============== 请求异常 ==============')
+        if (params.errCallBack) {
+          params.errCallBack(responseData)
+          return
+        }
         wx.showToast({
-          title: res.data,
-          icon: "none"
+          title: '服务器出了点小差~',
+          icon: 'none'
         })
+      }
 
-      } else {
-        //如果有定义了params.errCallBack,则调用 params.errCallBack(res.data)
+      // A00001 用于直接显示提示用户的错误,内容由输入内容决定
+      if (responseData.code === 'A00001') {
+        if (params.errCallBack) {
+          params.errCallBack(responseData)
+          return
+        }
+        wx.showToast({
+          title: responseData.msg || 'Error',
+          icon: 'none'
+        })
+        return
+      }
+
+      // 其他异常
+      if (responseData.code !== '00000') {
+        // console.log('params', params)
+      	wx.hideLoading();
         if (params.errCallBack) {
-          params.errCallBack(res);
+          params.errCallBack(responseData)
+        } else {
+          console.log(`接口: ${params.url}`)
+          console.log(`返回信息: `, res)
         }
       }
       if (!globalData.isLanding) {

+ 51 - 34
mall4uni/utils/http.js

@@ -37,25 +37,18 @@ function request(params, isGetTonken) {
     dataType: 'json',
     responseType: params.responseType == undefined ? 'text' : params.responseType,
     success: function (res) {
-      if (res.statusCode == 200) {
-        //如果有定义了params.callBack,则调用 params.callBack(res.data)
+			const responseData = res.data
+
+      // 00000 请求成功
+      if (responseData.code === '00000') {
         if (params.callBack) {
-          params.callBack(res.data);
+          params.callBack(responseData.data);
         }
-      } else if (res.statusCode == 500) {
-        uni.showToast({
-          title: "服务器出了点小差",
-          icon: "none"
-        });
-      } else if (res.statusCode == 401) {
-        // 添加到请求队列
-        // globalData.requestQueue.push(params); // 是否正在登陆
-
-        // if (!globalData.isLanding) {
-        //   globalData.isLanding = true; //重新获取token,再次请求接口
-
-        //   getToken();
-        // }
+        return
+      }
+
+      // A00004 未授权
+      if (responseData.code === 'A00004') {
 				uni.removeStorageSync('loginResult');
 				uni.removeStorageSync('token');
 				// #ifdef H5
@@ -136,24 +129,52 @@ function request(params, isGetTonken) {
 				// 		// #endif
 					}
 				}
-      } else if (res.statusCode == 400 && !params.errCallBack) {
-        uni.hideLoading();
-				uni.showToast({
-          title: res.data,
-          icon: "none"
-        });
-      } else {
-        //如果有定义了params.errCallBack,则调用 params.errCallBack(res.data)
+				return
+      }
+
+      // A00005 服务器出了点小差
+      if (responseData.code === 'A00005') {
+        console.error('============== 请求异常 ==============')
+        console.log('接口: ', params.url)
+        console.log('异常信息: ', responseData)
+        console.error('============== 请求异常 ==============')
         if (params.errCallBack) {
+          params.errCallBack(responseData)
+          return
+        }
+        uni.showToast({
+          title: '服务器出了点小差~',
+          icon: 'none'
+        })
+      }
 
-          params.errCallBack(res);
+      // A00001 用于直接显示提示用户的错误,内容由输入内容决定
+      if (responseData.code === 'A00001') {
+        if (params.errCallBack) {
+          params.errCallBack(responseData)
+          return
         }
-				uni.hideLoading();
+        uni.showToast({
+          title: responseData.msg || 'Error',
+          icon: 'none'
+        })
+        return
       }
 
-      // if (!globalData.isLanding) {
-      //   uni.hideLoading();
-      // }
+      // 其他异常
+      if (responseData.code !== '00000') {
+        // console.log('params', params)
+        if (params.errCallBack) {
+          params.errCallBack(responseData)
+        } else {
+          console.log(`接口: ${params.url}`)
+          console.log(`返回信息: `, res)
+        }
+      }
+			
+      if (!globalData.isLanding) {
+        wx.hideLoading();
+      }
     },
     fail: function (err) {
       uni.hideLoading();
@@ -168,10 +189,6 @@ function request(params, isGetTonken) {
 					icon: "none"
 				});
 			}, 1);
-      // uni.showToast({
-      //   title: "服务器出了点小差",
-      //   icon: "none"
-      // });
     }
   });
 } //通过code获取token,并保存到缓存

+ 51 - 1
mall4v/src/utils/httpRequest.js

@@ -28,7 +28,57 @@ http.interceptors.request.use(config => {
  * 响应拦截
  */
 http.interceptors.response.use(response => {
-  return response
+  const res = response.data
+  // 00000 请求成功
+  if (res.code === '00000') {
+    return res
+  }
+  // A00001 用于直接显示提示用户的错误,内容由输入决定
+  if (res.code === 'A00001') {
+    Message({
+      message: res.msg || 'Error',
+      type: 'error',
+      duration: 1.5 * 1000
+    })
+    return Promise.reject(res)
+  }
+  // A00002 用于直接显示提示系统的成功,内容由输入决定
+  if (res.code === 'A00002') {
+    Message({
+      message: res.msg,
+      type: 'success',
+      duration: 1.5 * 1000
+    })
+  }
+
+  // A00004 未授权
+  if (res.code === 'A00004') {
+    clearLoginInfo()
+    router.push({ name: 'login' })
+  }
+
+  // A00005 服务器异常
+  if (res.code === 'A00005') {
+    console.error('============== 请求异常 ==============')
+    console.log('接口地址: ', response.config.url.replace(process.env.VUE_APP_BASE_API, ''))
+    console.log('异常信息: ', res)
+    console.error('============== 请求异常 end ==========')
+    Message({
+      message: '服务器出了点小差,请稍后再试',
+      type: 'error',
+      duration: 1.5 * 1000,
+      customClass: 'element-error-message-zindex'
+    })
+    return Promise.reject(res)
+  }
+  if (res.code === 'A00014') {
+    Message({
+      message: res.msg,
+      type: 'error',
+      duration: 1.5 * 1000
+    })
+    return Promise.reject(res)
+  }
 }, error => {
   switch (error.response.status) {
     case 400: