/** * 通用工具类 */ export default class func { /** * 不为空 * @param val * @returns {boolean} */ static notEmpty(val) { return !this.isEmpty(val); } /** * 是否为定义 * @param val * @returns {boolean} */ static isUndefined(val) { return val === null || typeof val === 'undefined'; } /** * 为空 * @param val * @returns {boolean} */ static isEmpty(val) { if ( val === null || typeof val === 'undefined' || (typeof val === 'string' && val === '' && val !== 'undefined') ) { return true; } return false; } /** * 强转int型 * @param val * @param defaultValue * @returns {number} */ static toInt(val, defaultValue) { if (this.isEmpty(val)) { return defaultValue === undefined ? -1 : defaultValue; } const num = parseInt(val, 0); return Number.isNaN(num) ? (defaultValue === undefined ? -1 : defaultValue) : num; } /** * Json强转为Form类型 * @param obj * @returns {FormData} */ static toFormData(obj) { const data = new FormData(); Object.keys(obj).forEach(key => { data.append(key, Array.isArray(obj[key]) ? obj[key].join(',') : obj[key]); }); return data; } /** * date类转为字符串格式 * @param date * @param format * @returns {null} */ static format(date, format = 'YYYY-MM-DD HH:mm:ss') { return date ? date.format(format) : null; } /** * 根据逗号联合 * @param arr * @returns {string} */ static join(arr) { return arr ? arr.join(',') : ''; } /** * 根据逗号分隔 * @param str * @returns {string} */ static split(str) { return str ? String(str).split(',') : ''; } /** * 转换空字符串 * @param str * @returns {string|*} */ static toStr(str) { if (typeof str === 'undefined' || str === null) { return ""; } return str; } /** * 判断是否为对象并不为空对象 * @param value * @returns {Boolean} */ static isValuableObj(value) { return typeof value === 'object' && value !== null && Object.keys(value).length > 0; } /** * 获取流文件进行文件下 * @param value * @returns {Boolean} */ static downloadFileByBlobHandler(res) { const fileNames = res.headers['content-disposition'].split(";"); let characterSet = fileNames[2].split("filename*=")[1]; let fileName = decodeURI(fileNames[1].split("filename=")[1],characterSet); // 下载的文件名 let blob = new Blob([res.data], { type: res.data.type + ";charset=utf-8", }); const url = URL.createObjectURL(blob); // 生成下载链接 const link = document.createElement("a"); link.href = url; link.download = fileName; // 指定文件名和格式 document.body.appendChild(link); // 添加下载链接到页面 link.click(); // 触发下载 document.body.removeChild(link); //删除下载 window.URL.revokeObjectURL(url); //释放掉blob对象 } /** * 获取流文件进行文件下 * @param value * @returns {Boolean} */ static downloadFileByBlob(blobData, name) { const url = URL.createObjectURL(blobData); // 生成下载链接 const link = document.createElement("a"); link.href = url; link.download = name; // 指定文件名和格式 document.body.appendChild(link); // 添加下载链接到页面 link.click(); // 触发下载 document.body.removeChild(link); //删除下载 } /** * 将数组中的属性拼接成字符串 * @param {数组} array * @param {属性} attr * @returns */ static joinedString(array,attr) { return array.reduce((accumulator, item) => { return accumulator + (accumulator ? ',' : '') + item[attr]; }, ''); } /** * 比对数组中的对象的某个属性是否全部一致 * @param {数组} array * @param {属性} attr * @returns 一致返回fasle,不一致返回true */ static isAttrInconsistent(array,attr) { return Object.values(array).some((item, index, array) => { return item[attr] !== array[0][attr]; }); } }