Bläddra i källkod

feat: 添加请求 token 机制以处理异步请求,避免数据错位和残留

xieyong 4 dagar sedan
förälder
incheckning
7c218ed85c
1 ändrade filer med 17 tillägg och 3 borttagningar
  1. 17 3
      src/views/rulesManagement/warehouseInventory/components/selectType.vue

+ 17 - 3
src/views/rulesManagement/warehouseInventory/components/selectType.vue

@@ -234,7 +234,9 @@
         curType: '',
         classificationId: '',
         memoSelection: [],
-        newColumns: [] // 动态表头
+        newColumns: [], // 动态表头
+        // 请求 token:每次发起新请求 +1,响应回来比对,旧的请求结果直接丢弃
+        _fetchToken: 0
       };
     },
     created() {
@@ -286,15 +288,23 @@
         this.multipleSelection = val;
       },
       handleNodeClick(data) {
+        // 切分类时回到第一页,避免上次的 pageNum 让新分类数据错位
+        this.pages.pageNum = 1;
         this.classificationId = data.id;
         this.curType = data.type;
         this.initData();
       },
       closeWindows() {
+        // 完整重置:清掉表格 / 搜索 / 选中 / 当前分类,避免下次打开残留上次的数据
+        this.tableData = [];
         this.multipleSelection = [];
+        this.total = 0;
+        this.value = '';
+        this.classificationId = '';
+        this.curType = '';
+        this.pages.pageNum = 1;
         this.$emit('update:loadTree', false);
         this.$emit('update:visibleDialog', false);
-        this.pages.pageNum = 1;
       },
       handleCurrentChange(e) {
         this.initData();
@@ -307,6 +317,8 @@
         this._getInformation();
       },
       async _getInformation() {
+        // 每次发起新请求 +1,作为这一次的"身份"
+        const token = ++this._fetchToken;
         const params = {
           ...this.pages,
           keyWord: this.value,
@@ -319,7 +331,9 @@
         } else {
           res = await getBatchList(params);
         }
-        if (res && this.classificationId == params.categoryLevelIds) {
+        // 期间又有更新的请求发起过,本次的响应直接丢弃
+        if (token !== this._fetchToken) return;
+        if (res) {
           this.tableData = res.list;
           this.total = res.count;
         }