Selaa lähdekoodia

使用 ant-design-vue 处理Ajax请求异常

YangZhiJie 2 vuotta sitten
vanhempi
sitoutus
12f92114f8

+ 30 - 0
examples/vue-monthly-picker/src/VueMonthlyPickerExample.vue

@@ -193,6 +193,10 @@ export default {
     },
     
   },
+
+  mounted(){
+    this.testDayJs();
+  },
   methods: {
     handleSelect(value) {
       console.log('Select', value);
@@ -201,6 +205,32 @@ export default {
       this.min = min;
       this.max = max;
     },
+    // 测试DayJs
+    testDayJs(){
+      // exist bug
+      console.log(dayjs('00:00:00', 'HH:mm:ss'));
+
+
+      console.log(dayjs('2024-05-09').format('YYYY-MM-DD'));
+      console.log(dayjs('2024-05-09 10:45').format('YYYY-MM-DD'));
+      console.log(dayjs('2024-05-09 10:45:01').format('YYYY-MM-DD'));
+      console.log(dayjs().format('YYYYMMDD_hhmmss'));
+
+      const nowDate = new Date();
+      const startDate = dayjs(nowDate).subtract(1, 'M').format('YYYY-MM-DD HH:mm:ss');
+      const endDate = dayjs(nowDate).add(1, 'd').format('YYYY-MM-DD HH:mm:ss');
+      console.log(startDate, endDate);
+      
+      const startDate1 = '2024-01-02';
+      const endDate1 = '2024-01-10';
+      let currentYearMonth = dayjs(startDate1).add(1, 'M').format('YYYY-MM');
+      console.log(endDate1, 'isBefore', currentYearMonth, dayjs(endDate1).isBefore(currentYearMonth));
+
+      console.log(dayjs('2024-01-25').format('YYYY-MM-DD HH:mm'));
+      console.log(dayjs('2024-01-25').format('YYYY-MM-DD'));
+      console.log(dayjs('2024-01-25').format('YYYY-MM'));
+      console.log(dayjs().format('_YYYYMMDD_hhmmss'));
+    },
   },
 };
 </script>

+ 1 - 1
package.json

@@ -1,6 +1,6 @@
 {
   "name": "pc-component-v3",
-  "version": "1.0.81",
+  "version": "1.0.82",
   "description": "",
   "main": "dist/pc-component-v3.js",
   "scripts": {

+ 51 - 7
packages/common/Common.js

@@ -1,11 +1,17 @@
 import Notify from './Notify.js';
+import { notification } from 'ant-design-vue';
 
 
 export default {
   pageSize: 20,
 
 
-  // 异常处理
+  /**
+   * 使用 BootstarpDialog 处理Ajax请求异常
+   * @param {*} XMLHttpRequest 
+   * @param {*} textStatus 
+   * @param {*} errorThrown 
+   */
   processException: function (XMLHttpRequest, textStatus, errorThrown) {
     var _self = this;
     console.log(XMLHttpRequest);
@@ -45,6 +51,49 @@ export default {
     }
   },
 
+  
+  /**
+   * 使用 ant-design-vue 处理Ajax请求异常
+   * @param {*} XMLHttpRequest 
+   */
+  processExceptionWithAntV: function (XMLHttpRequest) {
+    var _self = this;
+    console.log(XMLHttpRequest);
+    notification.error({
+      message: XMLHttpRequest.status,
+      description: XMLHttpRequest.responseText,
+    });
+
+    if (XMLHttpRequest.status == 401) {
+      var currentUrl = window.location;
+      var href = window.location.href;
+      // 当前未处于登陆的界面
+      // 系统未登录
+      if (href.indexOf('login') < 0 && href.indexOf('redirectUrl=') < 0) {
+        // 处理钉钉免登陆
+        const clientId = this.getRouteParam('clientId');
+        const appName = this.getRouteParam('appName');
+        const corpId = this.getRouteParam('corpId');
+
+        let newUrl;
+        if (clientId != null && clientId.length > 0 && appName != null && appName.length > 0 && corpId != null && corpId.length > 0) {
+          newUrl = _self.getRedirectUrl('index.html#/login?clientId=' + clientId + '&appName=' + appName + '&corpId=' + corpId + '&redirectUrl=' + encodeURIComponent(currentUrl));
+        } else {
+          newUrl = _self.getRedirectUrl('index.html#/login?redirectUrl=' + encodeURIComponent(currentUrl));
+        }
+        window.location = newUrl;
+      }
+    } else if (XMLHttpRequest.status == 500) {
+      // 500 Internal Server Error
+      if (XMLHttpRequest.responseText.indexOf('登录超时') >= 0) {
+        // 如果异常信息包含“登录超时”,则2秒后跳转到登录页面
+        setTimeout(function () {
+          window.location = _self.getRedirectUrl('index.html#/login');
+        }, 2 * 1000);
+      }
+    }
+  },
+
 
   /**
    * 获取主机地址
@@ -201,12 +250,7 @@ export default {
    * @param {*} url 
    */
   getRedirectUrl: function (url) {
-    var href = window.location.href;
-    if (href.indexOf('pcapp') >= 0) {
-      return this.getHostPageBaseURL() + '/pcapp/' + url;
-    } else {
-      return this.getHostPageBaseURL() + '/' + url;
-    }
+    return this.getHostPageBaseURL() + '/' + url;
   },
 
   clearLocalStorage: function () {

+ 1 - 1
packages/vue-monthly-picker/README.md

@@ -30,7 +30,7 @@ Vue.component('my-component', {
 </vue-monthly-picker>
 ```
 
-**Note**: `v-model` binding value need to be a **moment** object
+**Note**: `v-model` binding value need to be a **dayjs** object
 ## Available props