沈丘营商办后台前端项目
王恒
5 天以前 cd95b584fe7da1ea63476871eabf512213a31e69
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import lodash from "lodash";
/**
 * 通用ts方法封装处理
 * Copyright (c) 2019 ruoyi
 */
const baseURL = import.meta.env.VITE_APP_BASE_API;
 
/**
 * 日期格式化
 *
 * @param {string} time 日期
 * @param {string} pattern 格式化类型
 * @returns
 */
export const parseTime = (time: string | number | Date, pattern: string) => {
  if (!time) {
    return null;
  }
  const format = pattern || "{y}-{m}-{d} {h}:{i}:{s}";
  let date;
  if (typeof time === "object") {
    date = time;
  } else {
    if (typeof time === "string" && /^[0-9]+$/.test(time)) {
      time = parseInt(time);
    } else if (typeof time === "string") {
      time = time.replace(new RegExp(/-/gm), "/");
    }
    // prettier-ignore
    if (typeof time === "number" && time.toString().length === 10) {
            time = time * 1000;
        }
    date = new Date(time);
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay(),
  };
  // prettier-ignore
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result: string | any[], key: string) => {
        let value = formatObj[key] ;
        // Note: getDay() returns 0 on Sunday
        if (key === "a") {
            return ["日", "一", "二", "三", "四", "五", "六"][value];
        }
        if (result.length > 0 && value < 10) {
            value = "0" + value;
        }
        return value || 0;
    });
  return time_str;
};
 
/**
 * 日期时间截取
 *
 * @param dateTime 日期时间
 * @returns
 */
export const dateTimeSub = (dateTime: any) => {
  if (!dateTime) {
    return "";
  }
  if (dateTime instanceof String) {
    if (dateTime.length > 10) {
      return dateTime.substring(0, 10);
    } else {
      return dateTime;
    }
  } else {
    const str = dateTime.toString();
    if (dateTime.length > 10) {
      return str.substring(0, 10);
    } else {
      return str;
    }
  }
};
 
/**
 * 表单重置
 *
 * @param {string} formRef
 */
export const resetForm = (formRef: any) => {
  formRef.value?.resetFields();
};
 
/**
 * 清除表格选中
 *
 * @param tableRef 表格ref
 */
export const cleanTableSelection = (tableRef: any) => {
  tableRef.value?.clearSelection();
};
 
/**
 * 设置表格行是否为选中状态
 *
 * @param tableRef  表格ref
 * @param row       表格行
 * @param selected  是否选中
 */
// prettier-ignore
export const setTableRowSelected = (tableRef: any, row: any, selected: boolean) => {
    // 设置当前行被选中
    tableRef.value?.toggleRowSelection(row, selected);
};
 
// 添加日期范围
// prettier-ignore
export const addDateRange = (params: any, dateRange: any[], propName: string) => {
    let search = params;
      search.params = typeof (search.params) === 'object' && search.params !== null && !Array.isArray(search.params) ? search.params : {};
      dateRange = Array.isArray(dateRange) ? dateRange : [];
    if (dateRange) {
        if (typeof propName !== "undefined") {
            const firstCode = propName.substring(0, 1);
            if (!isUpCase(firstCode)) {
                propName = firstCode.toUpperCase() + propName.substring(1);
            } else {
                propName = firstCode + propName.substring(1);
            }
            search.params["begin" + propName] = dateRange[0];
            search.params["end" + propName] = dateRange[1];
        } else {
            search.params["beginTime"] = dateRange[0];
            search.params["endTime"] = dateRange[1];
        }
    }
    return search;
};
 
// 回显数据字典
export const selectDictLabel = (datas: any, value: string | undefined, separator: undefined) => {
  if (!value || !datas) {
    return "";
  }
  let actions: any = [];
  Object.keys(datas).some((key) => {
    if (datas[key].dictValue == "" + value) {
      actions.push(datas[key].dictLabel);
      return true;
    }
  });
  if (actions.length === 0) {
    actions.push(value);
  }
  return actions.join("");
};
 
// 回显数据字典(字符串数组)
export const selectDictLabels = (datas: any, value: string | undefined, separator: undefined) => {
  if (value === undefined || !datas) {
    return "";
  }
  let actions: any = [];
  var currentSeparator = undefined === separator ? "," : separator;
  var temp = value.split(currentSeparator);
  Object.keys(value.split(currentSeparator)).some((val: any) => {
    Object.keys(datas).some((key) => {
      if (datas[key].dictValue == "" + temp[val]) {
        actions.push(datas[key].dictLabel + currentSeparator);
      }
    });
  });
  return actions.join("").substring(0, actions.join("").length - 1);
};
 
// 通用下载方法
export const download = (fileName: string) => {
  // prettier-ignore
  console.log("通用下载方法,文件名", fileName);
  // prettier-ignore
  window.location.href = baseURL + "/common/download?fileName=" + encodeURI(fileName) + "&delete=" + true;
};
 
// 字符串格式化(%s )
export const sprintf = (str: string) => {
  let args: any[];
  let flag = true;
  let i = 1;
  str = str.replace(/%s/g, function () {
    var arg = args[i++];
    if (typeof arg === "undefined") {
      flag = false;
      return "";
    }
    return arg;
  });
  return flag ? str : "";
};
 
// 转换字符串,undefined,null等转化为""
export function praseStrEmpty(str: string) {
  if (!str || str == "undefined" || str == "null") {
    return "";
  }
  return str;
}
 
// 数据合并
// prettier-ignore
export const mergeRecursive = (source: { [x: string]: any; }, target: { [x: string]: any; }) => {
    for (var p in target) {
        try {
            if (target[p].constructor == Object) {
                source[p] = mergeRecursive(source[p], target[p]);
            } else {
                source[p] = target[p];
            }
        } catch (e) {
            source[p] = target[p];
        }
    }
    return source;
};
 
/**
 * 构造树型结构数据
 * @param {*} data 数据源
 * @param {*} id id字段 默认 'id'
 * @param {*} parentId 父节点字段 默认 'parentId'
 * @param {*} children 孩子节点字段 默认 'children'
 */
// prettier-ignore
export const handleTree = (data: any, id: any, parentId: any, children: any) => {
    let config = {
        id: id || "id",
        parentId: parentId || "parentId",
        childrenList: children || "children",
    };
 
    var childrenListMap = {} as any;
    var nodeIds = {} as any;
    var tree = [] as any[];
 
    for (let d of data) {
        let parentId = d[config.parentId];
        if (childrenListMap[parentId] == null) {
            childrenListMap[parentId] = [];
        }
        nodeIds[d[config.id]] = d;
        childrenListMap[parentId].push(d);
    }
 
    for (let d of data) {
        let parentId = d[config.parentId];
        if (nodeIds[parentId] == null) {
            tree.push(d);
        }
    }
 
    for (let t of tree) {
        adaptToChildrenList(t);
    }
 
    function adaptToChildrenList(o: { [x: string]: any; }) {
        if (childrenListMap[o[config.id]] !== null) {
            o[config.childrenList] = childrenListMap[o[config.id]];
        }
        if (o[config.childrenList]) {
            for (let c of o[config.childrenList]) {
                adaptToChildrenList(c);
            }
        }
    }
    return tree;
};
 
/**
 * 参数处理
 *
 * @param {*} params  参数
 */
export function tansParams(params: { [x: string]: any }) {
  let result = "";
  for (const propName of Object.keys(params)) {
    const value = params[propName];
    var part = encodeURIComponent(propName) + "=";
    if (value !== null && value !== "" && typeof value !== "undefined") {
      if (typeof value === "object") {
        for (const key of Object.keys(value)) {
          // prettier-ignore
          if (value[key] !== null && value !== "" && typeof value[key] !== "undefined") {
                        let params = propName + "[" + key + "]";
                        var subPart = encodeURIComponent(params) + "=";
                        result += subPart + encodeURIComponent(value[key]) + "&";
                    }
        }
      } else {
        result += part + encodeURIComponent(value) + "&";
      }
    }
  }
  return result;
}
 
// 验证是否为blob格式
export const blobValidate = async (data: any) => {
  let b = false;
  try {
    const text = await data.text();
    JSON.parse(text);
    b = false;
  } catch (error) {
    b = true;
  }
  return b;
};
 
/**
 * 判断字母是否是大写
 *
 * @param {string} str
 * @returns
 */
export const isUpCase = (str: any) => {
  if (!str) {
    return false;
  }
  let bool = false;
  const strCode = str.charCodeAt();
  // 大写
  if (strCode >= 65 && strCode <= 90) {
    bool = false;
  } else if (strCode >= 97 && strCode <= 122) {
    // 小写
    bool = true;
  } else {
    console.error(`不是字母`);
    throw new Error(`不是字母`);
  }
  return bool;
};
 
// 返回项目路径
export const getNormalPath = (p: string) => {
  if (p.length === 0 || !p || p == "undefined") {
    return p;
  }
  let res = p.replace("//", "/");
  if (res[res.length - 1] === "/") {
    return res.slice(0, res.length - 1);
  }
  return res;
};
 
/**
 * 通用防抖
 *
 * @param callback 回调函数
 * @param wait     等待毫秒数
 * @param type     回调函数额外参数
 * @param options  防抖额外参数
 * @returns
 */
// prettier-ignore
export const lodashFunc = (callback: Function, wait: number, type?: any, options?: any) => {
    return lodash.debounce(function () {
        type ? callback(type) : callback();
    }, wait, options);
};
 
/**
 * 随机返回数组中的某个元素
 *
 * @param arr 数组
 * @returns
 */
export const getRadomForArr = <T>(arr: Array<T>) => {
  return arr[Math.floor(Math.random() * arr.length)];
};