广丰卷烟厂数采质量分析系统
zhuguifei
2026-03-02 80ff784bf60637cd348ae665fc907f7b1e527dd8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import { AcceptType } from '@/enum/business';
import { $t } from '@/locales';
/**
 * Transform record to option
 *
 * @example
 *   ```ts
 *   const record = {
 *     key1: 'label1',
 *     key2: 'label2'
 *   };
 *   const options = transformRecordToOption(record);
 *   // [
 *   //   { value: 'key1', label: 'label1' },
 *   //   { value: 'key2', label: 'label2' }
 *   // ]
 *   ```;
 *
 * @param record
 */
export function transformRecordToOption<T extends Record<string, string>>(record: T) {
  return Object.entries(record).map(([value, label]) => ({
    value,
    label
  })) as CommonType.Option<keyof T, T[keyof T]>[];
}
 
export function transformRecordToNumberOption<T extends Record<string, string>>(record: T) {
  return Object.entries(record).map(([value, label]) => ({
    value,
    label
  })) as CommonType.Option<keyof T>[];
}
 
/**
 * Translate options
 *
 * @param options
 */
export function translateOptions(options: CommonType.Option<string, App.I18n.I18nKey>[]) {
  return options.map(option => ({
    ...option,
    label: $t(option.label)
  }));
}
 
/**
 * Toggle html class
 *
 * @param className
 */
export function toggleHtmlClass(className: string) {
  function add() {
    document.documentElement.classList.add(className);
  }
 
  function remove() {
    document.documentElement.classList.remove(className);
  }
 
  return {
    add,
    remove
  };
}
 
/* 驼峰转换下划线 */
export function humpToLine(str: string, line: string = '-') {
  let temp = str.replace(/[A-Z]/g, match => {
    return `${line}${match.toLowerCase()}`;
  });
  // 如果首字母是大写,执行replace时会多一个_,这里需要去掉
  if (temp.slice(0, 1) === line) {
    temp = temp.slice(1);
  }
  return temp;
}
 
/** 判断是否为空 */
export function isNotNull(value: any) {
  return value !== undefined && value !== null && value !== '';
}
 
/** 判断是否为空 */
export function isNull(value: any) {
  return value === undefined || value === null || value === '';
}
 
/** 判断是否为图片类型 */
export function isImage(suffix: string) {
  return AcceptType.Image.split(',').includes(suffix.toLowerCase());
}
 
/**
 * 构造树型结构数据
 *
 * @param {T[]} data 数据源
 * @param {TreeConfig} config 配置选项
 * @returns {T[]} 树形结构数据
 */
export const handleTree = <T>(data: T[], config: CommonType.TreeConfig<T> = {}): { tree: T[]; flatData: T[] } => {
  if (!data?.length) {
    return {
      tree: [],
      flatData: []
    };
  }
 
  const {
    idField = 'id',
    parentIdField = 'parentId',
    childrenField = 'children',
    // 添加过滤函数,默认为不过滤
    filterFn = () => true
  } = config;
 
  // filter flat data
  const flatData = data.filter(filterFn) || [];
 
  // 使用 Map 替代普通对象,提高性能
  const childrenMap = new Map<T[keyof T], T[]>();
  const nodeMap = new Map<T[keyof T], T>();
  const tree: T[] = [];
 
  // 第一遍遍历:构建节点映射
  for (const item of flatData) {
    const id = item[idField as keyof T];
    const parentId = item[parentIdField as keyof T];
 
    nodeMap.set(id, item);
 
    if (!childrenMap.has(parentId)) {
      childrenMap.set(parentId, []);
    }
    childrenMap.get(parentId)!.push(item);
  }
 
  // 第二遍遍历:找出根节点
  for (const item of flatData) {
    const parentId = item[parentIdField as keyof T];
    if (!nodeMap.has(parentId)) {
      tree.push(item);
    }
  }
 
  // 递归构建树形结构
  const buildTree = (node: T) => {
    const id = node[idField as keyof T];
    const children = childrenMap.get(id);
 
    if (children?.length) {
      // 使用类型断言确保类型安全
      (node as any)[childrenField] = children;
      for (const child of children) {
        buildTree(child);
      }
    } else {
      // 如果没有子节点,设置为 undefined
      (node as any)[childrenField] = undefined;
    }
  };
 
  // 从根节点开始构建树
  for (const root of tree) {
    buildTree(root);
  }
 
  return {
    tree: tree || [],
    flatData
  };
};
 
/**
 * 将对象转换为 URLSearchParams
 *
 * @param obj
 */
export function transformToURLSearchParams(obj: Record<string, any>, excludeKeys: string[] = []) {
  const searchParams = new URLSearchParams();
  if (!isNotNull(obj)) {
    return searchParams;
  }
  Object.entries(obj).forEach(([key, value]) => {
    if (excludeKeys.includes(key)) {
      return;
    }
    if (typeof value === 'object') {
      transformToURLSearchParams(value).forEach((v, k) => {
        searchParams.append(`${key}[${k}]`, v);
      });
      return;
    }
    if (!isNotNull(value)) {
      return;
    }
    searchParams.append(key, value);
  });
  return searchParams;
}
 
/** 判断两个数组是否相等 */
export function arraysEqualSet(arr1: Array<any>, arr2: Array<any>) {
  return (
    arr1.length === arr2.length &&
    new Set(arr1).size === arr1.length &&
    new Set(arr2).size === arr2.length &&
    [...arr1].sort().join() === [...arr2].sort().join()
  );
}