Browse Source

fix:970 仓储管理看板首先新需求 调整统计口径-2

mlchen 1 week ago
parent
commit
0ba8821c13

+ 81 - 2
scripts/check-warehouse-dashboard.cjs

@@ -1419,14 +1419,21 @@ assert.equal(page.data().homeLoading, true, 'homeLoading 必须默认 true:首
 const createdBlock = dashSource.slice(dashSource.indexOf('async created()'), dashSource.indexOf('activated()'));
 assert.match(createdBlock, /this\.homeLoading = false;/, 'created 里首屏(筛选项 + 汇总)结束后必须撤掉遮罩');
 assert.ok(createdBlock.indexOf('this.homeLoading = false') > createdBlock.indexOf('await this.refresh()'), '必须先 await refresh() 再撤遮罩,否则数据还没到就露出来了');
-assert.ok((dashSource.match(/this\.homeLoading = false/g) || []).length === 1, 'homeLoading 只能在首屏置一次 false:定时刷新与筛选项切换不该闪遮罩');
+// 切仓库会把卡片数据整块清空:这一轮 refresh 必须自己撤遮罩(不能只靠 created 那一次),
+// 否则遮罩会从「切仓库时该出现」变成「永久盖住页面」。
+const readRouteSource = dashSource.slice(dashSource.indexOf('readRoute()'), dashSource.indexOf('route(view, filters'));
+assert.match(readRouteSource, /if \(this\.view === 'home'\) this\.homeLoading = true;/, '切仓库清空数据后必须立刻盖遮罩,否则用户看到的是一片没有任何提示的空白');
+const refreshSource = dashSource.slice(dashSource.indexOf('async refresh()'), dashSource.indexOf('async loadSummary()'));
+assert.match(refreshSource, /finally\s*\{[\s\S]*?this\.homeLoading = false;/, 'refresh 必须在 finally 里撤遮罩:筛选项失败、指标失败、提前 return 都要走到');
+assert.match(refreshSource, /if \(this\.active && generation === this\.generation\) this\.homeLoading = false;/, '撤遮罩必须带 generation 守卫:被新一轮取代的旧请求不能提前揭开空卡片');
+assert.equal((dashSource.match(/this\.homeLoading = false/g) || []).length, 2, '撤遮罩只允许两处:首屏 created 与 refresh 的 finally,别处不得乱撤');
 const layoutScss = stripCodeComments(fs.readFileSync(path.join(folder, 'dashboard-layout.scss'), 'utf8'));
 assert.match(layoutScss, /\.wms-home\s*\{[^}]*position:\s*relative/, '.wms-home 必须是定位上下文,否则遮罩会跑去页面别处');
 assert.match(layoutScss, /\.wms-home \.home-loading\s*\{[^}]*position:\s*absolute/, '遮罩必须绝对定位在内容区内');
 assert.doesNotMatch(layoutScss, /\.wms-home \.home-loading\s*\{[^}]*position:\s*fixed/, '遮罩不得用 fixed:会把系统页头、菜单、标签栏一起盖掉');
 assert.match(layoutScss, /\.wms-home \.home-loading\s*\{[^}]*z-index:\s*\d+/, '遮罩必须显式抬层级,否则盖不住卡片');
 assert.match(layoutScss, /@keyframes\s+home-loading-spin\s*\{[\s\S]{0,120}?rotate\(360deg\)/, '转圈动画必须存在,否则 spinner 静态不转');
-console.log('K1 首页首屏遮罩断言通过:homeLoading 默认 true、created 中 await refresh() 之后置 false(且只置一次),遮罩仅在首页视图出现并带中文文案;样式为 .wms-home 内的 absolute 遮罩(非 fixed,不盖系统页头/菜单)+ z-index + 转圈关键帧。');
+console.log('K1 首页首屏遮罩断言通过:homeLoading 默认 true、created 中 await refresh() 之后置 false,切仓库清空数据时重新置 true 并由 refresh 的 finally(带 generation 守卫)撤掉;遮罩仅在首页视图出现并带中文文案;样式为 .wms-home 内的 absolute 遮罩(非 fixed,不盖系统页头/菜单)+ z-index + 转圈关键帧。');
 
 // 首页指标拆分:每张卡/图一条路由,限流并保持单项失败隔离。
 const metricCalls = [];
@@ -1467,3 +1474,75 @@ assert.doesNotMatch(dashboardTplSource, /homeSummarySection/, '首页不得再
   assert.equal(metricCalls.filter(metric => metric === 'C6').length, 1, '质检明细保留 C6 数据');
   console.log('首页指标拆分断言通过:13 项独立请求、质检明细保留 C6、并发不超过 4,C2 失败不覆盖 C1。');
 })().catch(error => { console.error(error); process.exitCode = 1; });
+
+// —— K2 切仓库遮罩:清空即盖、本轮落定才撤、别的刷新不闪 ——
+// dashboard.vue 的 readRoute/refresh 在 load() 里是真实方法,这里用桩驱动,验证的是遮罩状态机而不是源码字符串。
+(async () => {
+  const loadingBindings = () => ({
+    HomeOverview: {}, DashboardDetail: {}, DashboardActions: {}, ...utils,
+    screens: vm.runInContext('screens', screens)
+  });
+  const makeState = (page, overrides = {}) => ({
+    active: true, generation: 0, requests: {}, options: {}, optionsLoaded: false, summary: {}, result: {}, error: '',
+    updatedAt: '', loading: false, view: 'home', warehouseId: '', filters: {}, homeFilters: null, homeLoading: false,
+    $route: { query: {} },
+    loadSummary: page.methods.loadSummary,
+    parameters() { return this.filters; },
+    ...overrides
+  });
+  const calls = { metrics: 0 };
+  const { value: loadingPage } = load('dashboard.vue', {
+    ...loadingBindings(),
+    homeOptions: async () => ({ warehouses: [{ id: '1' }, { id: '2' }] }),
+    homeSummaryMetric: async () => { calls.metrics++; return { inventoryBatchCount: 1, capturedAt: '2026-11-09 10:00:00' }; }
+  });
+
+  // 1) 切仓库:readRoute 清空数据后立刻盖遮罩,本轮 13 项指标落定才撤
+  const switched = makeState(loadingPage, { $route: { query: { dashboard: '1', dashboardView: 'home', warehouseId: '2' } } });
+  loadingPage.methods.readRoute.call(switched);
+  assert.equal(switched.warehouseId, '2');
+  assert.equal(switched.homeLoading, true, '切仓库清空数据后必须立刻盖遮罩,否则用户看到的是一片空白');
+  await loadingPage.methods.refresh.call(switched);
+  assert.equal(switched.homeLoading, false, '这一轮指标全部落定后必须撤掉遮罩');
+  assert.ok(calls.metrics > 0, '切仓库必须真的重新请求首页指标');
+  assert.equal(switched.summary.inventoryBatchCount, 1, '撤遮罩时数据必须已经落到 summary');
+
+  // 2) 同一仓库重复选择:不清数据,不闪遮罩
+  const same = makeState(loadingPage, { warehouseId: '2', summary: { inventoryBatchCount: 1 }, $route: { query: { dashboard: '1', dashboardView: 'home', warehouseId: '2' } } });
+  loadingPage.methods.readRoute.call(same);
+  assert.equal(same.homeLoading, false, '同仓库重复选择不清空数据,不得闪遮罩');
+
+  // 3) 15 分钟定时刷新 / 手动重试:直接调 refresh,不经过 readRoute,不盖遮罩
+  const timer = makeState(loadingPage, { warehouseId: '2', optionsLoaded: true, summary: { inventoryBatchCount: 1 } });
+  await loadingPage.methods.refresh.call(timer);
+  assert.equal(timer.homeLoading, false, '定时刷新与手动重试不闪遮罩');
+
+  // 4) 筛选项失败(refresh 里提前 return 的分支):遮罩同样要撤,交给错误条 + 重试
+  const { value: optionsFailPage } = load('dashboard.vue', {
+    ...loadingBindings(),
+    homeOptions: async () => { throw new Error('筛选项接口挂了'); },
+    homeSummaryMetric: async () => ({})
+  });
+  const optionsFail = makeState(optionsFailPage, { homeLoading: true, warehouseId: '2' });
+  await optionsFailPage.methods.refresh.call(optionsFail);
+  assert.equal(optionsFail.homeLoading, false, '筛选项加载失败也必须撤遮罩,不能永久盖住');
+  assert.match(optionsFail.error, /筛选项接口挂了/, '筛选项失败要把原因交给错误条');
+
+  // 5) 13 项指标全部失败:遮罩撤掉,错误条接管
+  const { value: metricFailPage } = load('dashboard.vue', {
+    ...loadingBindings(),
+    homeOptions: async () => ({ warehouses: [] }),
+    homeSummaryMetric: async () => { throw new Error('指标接口挂了'); }
+  });
+  const metricFail = makeState(metricFailPage, { homeLoading: true, warehouseId: '2' });
+  await metricFailPage.methods.refresh.call(metricFail);
+  assert.equal(metricFail.homeLoading, false, '指标全部失败也必须撤遮罩');
+  assert.match(metricFail.error, /指标接口挂了/, '指标失败要把原因交给错误条');
+
+  // 6) 已停用/离开看板(active=false)时 refresh 早退:不许把遮罩状态改坏
+  const inactive = makeState(loadingPage, { active: false, homeLoading: true });
+  await loadingPage.methods.refresh.call(inactive);
+  assert.equal(inactive.homeLoading, true, '看板已停用时不发请求也不动遮罩状态(由下一次 activated 的 refresh 决定)');
+
+  console.log('K2 切仓库遮罩断言通过:切仓库清空数据即盖遮罩、本轮 refresh 落定后撤掉;同仓库重复选择、定时刷新、手动重试都不闪;筛选项失败/指标失败/看板停用各分支都走到 finally,遮罩不会永久留在页面上。');
+})().catch(error => { console.error(error); process.exitCode = 1; });

+ 20 - 9
src/views/warehouseManagement/index/dashboard.vue

@@ -106,6 +106,10 @@ export default {
       this.warehouseId = this.filters.warehouseId || '';
       if (previousWarehouseId !== this.warehouseId) {
         this.summary = {}; this.updatedAt = ''; this.options = {}; this.optionsLoaded = false;
+        // 切仓库把卡片/图表/待办条的数据整块清空了,而首页要等 13 项指标全部回来才有内容:
+        // 不盖遮罩的话,用户看到的就是一片空白且没有任何进度提示(看起来像卡死在这里)。
+        // 只在「数据确实被清空」时置位——15 分钟定时刷新、同仓库重复选择都不该闪遮罩。
+        if (this.view === 'home') this.homeLoading = true;
       }
       // 首页筛选快照只保留仓库:单位维度随后端契约一起删除,不再有默认单位兜底。
       if (this.view === 'home') this.homeFilters = { warehouseId: this.filters.warehouseId || '' };
@@ -145,17 +149,24 @@ export default {
       if (!this.active) return;
       this.generation++; this.requests = {}; this.error = ''; this.loading = false;
       const generation = this.generation;
-      if (!this.optionsLoaded) {
-        try {
-          const options = await homeOptions({ warehouseId:this.warehouseId });
-          if (!this.active || generation !== this.generation) return;
-          this.options = options; this.optionsLoaded = true;
-        } catch (e) {
-          if (this.active && generation === this.generation) this.error = e.message || '筛选项加载失败,请重试';
-          return;
+      try {
+        if (!this.optionsLoaded) {
+          try {
+            const options = await homeOptions({ warehouseId:this.warehouseId });
+            if (!this.active || generation !== this.generation) return;
+            this.options = options; this.optionsLoaded = true;
+          } catch (e) {
+            if (this.active && generation === this.generation) this.error = e.message || '筛选项加载失败,请重试';
+            return;
+          }
         }
+        await Promise.allSettled([this.loadSummary(), this.businessComponent || this.view === 'home' || this.view === 'flow' ? Promise.resolve() : this.loadDetail()]);
+      } finally {
+        // 撤遮罩放在 finally:筛选项失败、指标失败、被新一次 refresh 取代(generation 变化)都必须走到,
+        // 否则切仓库后那块遮罩会永久盖住页面——从「看起来卡死」变成「确实卡死」。
+        // generation 相等才撤:旧一轮请求回来时不能把新一轮刚盖上的遮罩提前撤掉,露出半截空卡片。
+        if (this.active && generation === this.generation) this.homeLoading = false;
       }
-      await Promise.allSettled([this.loadSummary(), this.businessComponent || this.view === 'home' || this.view === 'flow' ? Promise.resolve() : this.loadDetail()]);
     },
     async loadSummary() {
       const generation = this.generation; const seq = (this.requests.summary || 0) + 1; this.requests.summary = seq;