Эх сурвалжийг харах

feat: 店铺运营默认展示最近 2 天数据

liangzhao 1 долоо хоног өмнө
parent
commit
ca2de06ddc
2 өөрчлөгдсөн 11 нэмэгдсэн , 1 устгасан
  1. BIN
      public/assets/logo.png
  2. 11 1
      public/ops.js

BIN
public/assets/logo.png


+ 11 - 1
public/ops.js

@@ -43,6 +43,12 @@
     return div.innerHTML;
   }
 
+  /** 日期加减(YYYYMMDD),用于默认「最近 N 天」 */
+  function shiftYmd(ymd, days) {
+    const d = new Date(Date.UTC(Number(ymd.slice(0, 4)), Number(ymd.slice(4, 6)) - 1, Number(ymd.slice(6, 8)) + days));
+    return `${d.getUTCFullYear()}${String(d.getUTCMonth() + 1).padStart(2, '0')}${String(d.getUTCDate()).padStart(2, '0')}`;
+  }
+
   function ymdToInput(ymd) {
     const s = String(ymd || '');
     return /^\d{8}$/.test(s) ? `${s.slice(0, 4)}-${s.slice(4, 6)}-${s.slice(6, 8)}` : '';
@@ -87,8 +93,12 @@
     const { body } = await api('/api/ops/summary');
     state.summary = body || { enabled: false, reason: 'storage-unreachable' };
     if (state.summary.enabled && state.summary.coverage) {
-      if (!state.from) state.from = state.summary.coverage.from;
+      // 默认只看最近 2 天;数据不足 2 天时从最早一天开始
       if (!state.to) state.to = state.summary.coverage.to;
+      if (!state.from) {
+        const recent = shiftYmd(state.to, -1);
+        state.from = recent < state.summary.coverage.from ? state.summary.coverage.from : recent;
+      }
     }
     return state.summary;
   }