浏览代码

refactor(util): 优化文件大小格式化函数并添加安全BigInt转换工具

yz 3 天之前
父节点
当前提交
f1c016833a
共有 3 个文件被更改,包括 30 次插入39 次删除
  1. 20 3
      src/util/util.js
  2. 4 26
      src/views/forecast-summary/summaryIndex.js
  3. 6 10
      src/views/forecast-summary/types.d.ts

+ 20 - 3
src/util/util.js

@@ -372,11 +372,28 @@ export const downloadFileBase64 = (path, name) => {
  * @returns {string} 格式化后的文件大小
  */
 export const formatFileSize = (bytes) => {
-  if (!bytes || bytes === 0) return '0 B'
-
+  if (bytes === 0) return '0 B'
   const k = 1024
   const sizes = ['B', 'KB', 'MB', 'GB', 'TB']
   const i = Math.floor(Math.log(bytes) / Math.log(k))
+  const value = (bytes / Math.pow(k, i)).toFixed(2)
+  return `${value} ${sizes[i]}`
+}
 
-  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]
+/**
+ * BigInt 安全转换(兼容 stringify)
+ * @param {string|number|bigint|null|undefined} v
+ * @returns {string|null}
+ */
+export const safeBigInt = (v) => {
+  try {
+    if (v === null || v === undefined) return '0'
+    if (typeof BigInt === 'function') {
+      const n = BigInt(v)
+      return n.toString()
+    }
+    return String(v)
+  } catch (e) {
+    return v != null ? String(v) : '0'
+  }
 }

+ 4 - 26
src/views/forecast-summary/summaryIndex.js

@@ -21,6 +21,7 @@ import {
   isReadOnlyMode
 } from '@/constants/forecast'
 import { mapGetters } from 'vuex'
+import { safeBigInt } from '@/util/util'
 
 /**
  * 经销商销售预测汇总页面业务逻辑混入
@@ -530,8 +531,8 @@ import { mapGetters } from 'vuex'
              const children = Array.isArray(r.pcBladeSalesForecastSummaryList)
                 ? r.pcBladeSalesForecastSummaryList.map(it => ({
                     ...it,
-                    idBigint: this.safeBigInt(it.id),
-                    forecastMainIdBigint: it.forecastMainId != null ? this.safeBigInt(it.forecastMainId) : null,
+                    idBigint: safeBigInt(it.id),
+                    forecastMainIdBigint: it.forecastMainId != null ? safeBigInt(it.forecastMainId) : null,
                     year: Number(it.year),
                     month: Number(it.month),
                     forecastQuantity: typeof it.forecastQuantity === 'string' ? Number(it.forecastQuantity) : Number(it.forecastQuantity),
@@ -545,7 +546,7 @@ import { mapGetters } from 'vuex'
 
              return {
                ...r,
-               idBigint: this.safeBigInt(r.id),
+               idBigint: safeBigInt(r.id),
                year: Number(r.year),
                month: Number(r.month),
                pcBladeSalesForecastSummaryList: children
@@ -563,29 +564,6 @@ import { mapGetters } from 'vuex'
        }
      },
 
-     /**
-       * BigInt 安全转换(兼容 stringify)
-       * @param {string|number|bigint|null|undefined} v
-       * @returns {string|null}
-       */
-      safeBigInt(v) {
-        try {
-          if (v === null || v === undefined) return '0'
-          // 兼容旧浏览器/运行环境无 BigInt 的情况
-          if (typeof BigInt === 'function') {
-            const n = BigInt(v)
-            return n.toString()
-          }
-          return String(v)
-        } catch (e) {
-          return v != null ? String(v) : null
-        }
-      },
-
-     /**
-      * 查询主表对应的子表明细
-      * @param {string|number} id - 主表ID
-      */
      async getForecastSummaryDetail(id) {
        try {
          const res = await getForecastSummaryDetail(id)

+ 6 - 10
src/views/forecast-summary/types.d.ts

@@ -1,4 +1,4 @@
-import { ForecastSummaryRecord, SalesForecastMainListItemRecord, SalesForecastMainRecord } from "@/api/forecast/types";
+import { SalesForecastMainRecord } from "@/api/forecast/types";
 
 export interface PageConfig {
   pageSize: number;
@@ -11,14 +11,10 @@ export interface ForecastSummaryComponentData {
   query: Record<string, any>;
   loading: boolean;
   page: PageConfig;
-  // 列表数据切换为主表记录,子表在展开行中展示
   data: Array<SalesForecastMainRecord>;
-  // 选择列表也切换为主表记录
   selectionList: Array<SalesForecastMainRecord>;
   option: AvueCrudOption;
-  // 新增:子表格(展开区)配置
   childOption: AvueCrudOption;
-  // —— 导出选择层 ——
   exportDialogVisible: boolean;
   exportForm: {
     year: string | number | null;
@@ -27,6 +23,9 @@ export interface ForecastSummaryComponentData {
   exportLoading: boolean;
   exportFormRules: Record<string, any>;
   monthOptions: Array<{ label: string; value: number }>;
+  rowExporting: Record<string, boolean>;
+  permissionList: Record<string, boolean>;
+  readOnlyMode: boolean;
 }
 
 export interface ForecastSummaryComponent extends ForecastSummaryComponentData {
@@ -38,16 +37,13 @@ export interface ForecastSummaryComponent extends ForecastSummaryComponentData {
   sizeChange: (pageSize: number) => void;
   refreshChange: () => void;
   beforeOpen: (done: Function, type: string) => void;
-  getForecastSummaryDetail: (id: string | number) => Promise<ForecastSummaryRecord | null>;
-  // 新增:BigInt 安全转换工具
-  safeBigInt: (v: string | number | null | undefined) => bigint | null;
-  // 模板调用的方法补充类型
+  getForecastSummaryDetail: (id: string | number) => Promise<any[]>;
   getApprovalStatusConfig: (status: number) => { label: string; type: string };
   formatNumber: (value: string | number) => string;
   formatDateTime: (dateTime: string) => string;
   formatYearMonth: (year: number, month: number) => string;
-  // —— 导出相关 ——
   onExportTemplate: () => void;
   confirmExportByMonth: () => Promise<void>;
   cancelExportDialog: () => void;
+  handleExportByMainId: (row: any) => Promise<void>;
 }