zhsz-common-ui
Version:
This template should help get you started developing with Vue 3 in Vite. The template uses Vue 3 `<script setup>` SFCs, check out the [script setup docs](https://v3.vuejs.org/api/sfc-script-setup.html#sfc-script-setup) to learn more.
502 lines (462 loc) • 16.2 kB
JavaScript
/**
* 通用js方法封装处理
* Copyright (c) 2023 xz
*/
import _ from 'lodash'
import {ElMessage} from "element-plus";
import jszip from 'jszip'
import {saveAs} from 'file-saver'
const zip = new jszip()
// 日期格式化
export function parseTime(time, pattern) {
if (arguments.length === 0 || !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), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), '');
}
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()
}
const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
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
}
// 表单重置
export function resetForm(refName) {
if (this.$refs[refName]) {
this.$refs[refName].resetFields();
}
}
// 添加日期范围
export function addDateRange(params, dateRange, propName) {
let search = params;
search.params = typeof (search.params) === 'object' && search.params !== null && !Array.isArray(search.params) ? search.params : {};
dateRange = Array.isArray(dateRange) ? dateRange : [];
if (typeof (propName) === 'undefined') {
search.params['beginTime'] = dateRange[0];
search.params['endTime'] = dateRange[1];
} else {
search.params['begin' + propName] = dateRange[0];
search.params['end' + propName] = dateRange[1];
}
return search;
}
// 回显数据字典
export function selectDictLabel(datas, value) {
if (value === undefined) {
return "";
}
var actions = [];
Object.keys(datas).some((key) => {
if (datas[key].value == ('' + value)) {
actions.push(datas[key].label);
return true;
}
})
if (actions.length === 0) {
actions.push(value);
}
return actions.join('');
}
// 回显数据字典(字符串数组)
export function selectDictLabels(datas, value, separator) {
if (value === undefined || value.length === 0) {
return "";
}
if (Array.isArray(value)) {
value = value.join(",");
}
var actions = [];
var currentSeparator = undefined === separator ? "," : separator;
var temp = value.split(currentSeparator);
Object.keys(value.split(currentSeparator)).some((val) => {
var match = false;
Object.keys(datas).some((key) => {
if (datas[key].value == ('' + temp[val])) {
actions.push(datas[key].label + currentSeparator);
match = true;
}
})
if (!match) {
actions.push(temp[val] + currentSeparator);
}
})
return actions.join('').substring(0, actions.join('').length - 1);
}
// 字符串格式化(%s )
export function sprintf(str) {
var args = arguments, flag = true, 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 parseStrEmpty(str) {
if (!str || str == "undefined" || str == "null") {
return "";
}
return str;
}
// 数据合并
export function mergeRecursive(source, target) {
for (const 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'
*/
export function handleTree(data, id, parentId, children) {
let config = {
id: id || 'id',
parentId: parentId || 'parentId',
childrenList: children || 'children'
};
var childrenListMap = {};
var nodeIds = {};
var tree = [];
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) {
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;
}
// 树形解构转平铺
// data 集合数据;list 返回集合; primaryObj 原集合对象; resultObj 返回新的集合对象
// 例 primaryObj = ['name', 'age']; resultObj = ['title', 'years']; 返回的集合中将name和age属性值赋值给了title和years属性!
export function treeToTileGivenObject(data, list, props = {children: 'children'},
primaryObj = [], resultObj = []) {
data.forEach(item => {
let obj = {};
if (primaryObj.length && resultObj.length) {
primaryObj.map((m, i) => {
obj[resultObj[i]] = item[m];
});
}
if (item[props.children] && item[props.children].length) {
list.push(item);
treeToTileGivenObject(item[props.children], list, props, primaryObj, resultObj)
} else {
(primaryObj.length && resultObj.length) ? list.push(obj) : list.push(item);
}
});
return list;
}
/**
* 参数处理
* @param {*} params 参数
*/
export function tansParams(params) {
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)) {
if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
let params = propName + '[' + key + ']';
var subPart = encodeURIComponent(params) + "=";
result += subPart + encodeURIComponent(value[key]) + "&";
}
}
} else {
result += part + encodeURIComponent(value) + "&";
}
}
}
return result
}
// 返回项目路径
export function getNormalPath(p) {
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;
}
// 验证是否为blob格式
export function blobValidate(data) {
return data.type !== 'application/json'
}
/**
* 字符串转数组 数组转字符串
* @param value
* @param type arr 或者 string
* @param splitter 分割符号
*/
export function dataConversion(value, type, splitter = ',') {
if (value) {
if (type === 'arr') {
return value.join(splitter)
}
return value.split(splitter)
}
return value
}
export function formatListDataByKey(val, isMore, arr, label = 'label', value = 'value') {
if (val || val === 0) {
if (isMore) {
let moreVal = val
if (typeof val === "string") {
moreVal = val.split(',')
}
const stringVal = _.map(moreVal, (it) => String(it))
const filterData = _.filter(arr, (item) => {
return _.includes(stringVal, String(item[value]))
})
const mapLabelData = _.map(filterData, (item) => item[label])
return mapLabelData.join(',')
} else {
return _.find(arr, (item) => String(val) === String(item[value]))?.[label] || '--'
}
}
return '--'
}
export function formatListTagByKey(val, isMore, arr, label = 'label', value = 'value') {
if (val || val === 0) {
if (isMore) {
const stringVal = _.map(val, (it) => String(it))
const filterData = _.filter(arr, (item) => {
return _.includes(stringVal, String(item[value]))
})
return _.map(filterData, (item) => item[label])
} else {
return _.find(arr, (item) => String(val) === String(item[value]))
}
}
return val
}
/**
* 复制
*/
export function copyText(text) {
// 判断是否安全 https 本地可以使用 http 报错
// if (navigator.clipboard && window.isSecureContext) {
// navigator.clipboard.writeText(text)
// .then(function () {
// ElMessage.success('复制成功!')
// })
// .catch(function (error) {
// console.error('复制失败: ', error);
// });
// }
// 创建text area
const textArea = document.createElement('textarea')
textArea.value = text
// 使text area不在viewport,同时设置不可见
document.body.appendChild(textArea)
textArea.focus()
textArea.select()
ElMessage.success('复制成功!')
return new Promise((res, rej) => {
// 执行复制命令并移除文本框
document.execCommand('copy') ? res() : rej()
textArea.remove()
})
}
// 批量下载图片
const handleDownloadAll = (imgList) => {
const imgBase64 = [];
const imageSuffix = []; // 图片后缀
const fileNames = [] // 名字
const img = zip.folder('下载文件包');
for (let i = 0; i < imgList.length; i++) {
// 获取每个图片的后缀
const suffix = imgList[i].substring(imgList[i].lastIndexOf('.'));
const arr_name = imgList[i].split("/");
const file_name = arr_name[arr_name.length - 1]
imageSuffix.push(suffix)
fileNames.push(file_name)
getBase64(imgList[i])
.then(base64 => {
imgBase64.push(base64.substring(22))
}, err => {
console.log(err) // 打印异常信息
})
}
function tt() {
setTimeout(function () {
if (imgList.length === imgBase64.length) {
for (let i = 0; i < imgList.length; i++) {
// 设置文件名
img.file(fileNames[i], imgBase64[i], {base64: true})
}
zip.generateAsync({type: 'blob'}).then(function (content) {
// see FileSaver.js
saveAs(content, 'files.zip')
ElMessage.success(`成功下载${imgList.length}张图片!`)
})
} else {
tt()
}
}, 100)
}
tt()
}
// 把路径指向文件转成ArrayBuffer对象
const getBase64 = (img) => {
// width、height调用时传入具体像素值,控制大小 ,不传则默认图像大小
function getBase64Image(img, width, height) {
const canvas = document.createElement('canvas');
canvas.width = width || img.width
canvas.height = height || img.height
const ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, canvas.width, canvas.height)
return canvas.toDataURL()
}
return new Promise((resolve) => {
const image = new Image();
image.setAttribute('crossOrigin', 'anonymous')
if (img) {
image.onload = function () {
// 读取图片之后的操作
resolve(getBase64Image(image))
}
image.src = img
}
})
}
export const downAll = (imgList) => {
handleDownloadAll(imgList)
}
/**
* 数字千分符
* @param inputNum 传入的数字
* @returns {string}
*/
export const thousandthMark = (inputNum) => {
let flag = inputNum > 0;
let numArr = Math.abs(inputNum).toString().split('.');
let right = numArr[1] ? '.' + numArr[1] : ''; // 小数点右边的数字
let left = numArr[0]; // 小数点左边的数字
let temp = '';
while (left.length > 3) {
// str.slice(start, end); 截取字符串,返回截取的字符串,end取不到
temp = ',' + left.slice(-3) + temp;
left = left.slice(0, left.length - 3);
}
return flag ? left + temp + right : '-' + left + temp + right;
}
export const numToFixed = (num, digit = 2) => {
const formatNum = num ? num : 0
const number = Number(formatNum)
// "123.46" 字符串形式
// return parseFloat(formattedNum); // 123.46 转换为数字
return number.toFixed(digit)
}
// (表格数据,表格字段,合并的条件)
export const setTableRowSpan = (isLevel = false, tableData = [], fieldArr = [], effectMerge = {}) => {
let lastItem = {};
fieldArr.forEach((field, index) => {
let judgeArr = fieldArr.slice(0, index + 1);
//@ts-ignore
if (effectMerge[field]) {
//@ts-ignore
judgeArr = [...effectMerge[field], field]; //[area,field]
// console.log(judgeArr, "--==judgeAr2222", lastItem);
}
tableData.forEach((item) => {
item.mergeCell = fieldArr;
const rowSpan = `rowspan_${field}`;
const condition = isLevel ? judgeArr.every((e) => lastItem[e] === item[e] && (item[e] || item[e] === 0)) :
lastItem[field] === item[field] && (item[field] || item[field] === 0)
// 判断是否合并到上个单元格。
if (condition) {
// 判断是否所在行的列对应的值全部相同,并且此列的值不为空
// 是:合并行
item[rowSpan] = 0;
//@ts-ignore
lastItem[rowSpan] += 1;
} else {
// 否:完成一次同类合并。lastItem重新赋值,进入下一次合并计算。
item[rowSpan] = 1;
lastItem = item;
}
});
});
};
export const objectSpanMethod = ({row, column, rowIndex, columnIndex}) => {
if (row.mergeCell && row.mergeCell.includes(column.property)) {
const rowspan = row[`rowspan_${column.property}`];
if (rowspan) {
return {rowspan: rowspan, colspan: 1};
} else {
return {rowspan: 0, colspan: 0};
}
}
}