/** * 通用js方法封装处理 */ import { ElMessage, ElMessageBox } from 'element-plus' // #region String 字符串---------------------------------------------------------------------- /** * 判断是否为string类型 * @param val 入参 * @returns 是返回true,反之则反 */ export const isString = (val:any) => { return typeof (val) === 'string' || val instanceof String } // #endregion // #region Number 数值------------------------------------------------------------------------ // #endregion // #region Date 日期 ------------------------------------------------------------------------- /** * 判断是否是日期 * @param val 入参 * @returns 是返回true,反之则反 */ export const isDate = (val:any) => { return (val instanceof Date) } // #endregion // #region Array 数组 ------------------------------------------------------------------------ /** * 判断是否是数组 * @param val 入参 * @returns 是返回true,反之则反 */ export function isArray(val:any) { if (typeof Array.isArray === 'undefined') { return Object.prototype.toString.call(val) === '[object Array]' } return Array.isArray(val) } /** * 根据KEY VALUE获取树形结构数组第一个匹配项 * @param data 数据源 * @param key 匹配字段 * @param val 匹配值 * @returns 有则返回匹配项,无则返回null */ export function getTreeItemByKey(data: any, key: any, val: any) { if (isNull(key)) { return null } var item = null as any get(data) return item function get(arr:any) { if (!isNull(item)) { return } for (var i = 0; i < arr.length; i++) { if (arr[i][key] === val) { item = arr[i] return } if (!isNull(arr[i].children)) { get(arr[i].children) } } } } /** * 根据KEY VALUE获取树形结构数组所有匹配项 * @param data 数据源 * @param key 匹配字段 * @param val 匹配值 * @returns 有则返回匹配项,无则返回null */ export function getAllTreeItemByKey(data: any, key: any, val: any) { if (isNull(key)) { return null } var item = [] as any get(data) return item function get(arr:any) { for (var i = 0; i < arr.length; i++) { if (arr[i][key] === val) { item.push(arr[i]) } if (!isNull(arr[i].children)) { get(arr[i].children) } } } } // #endregion // #region Object 对象------------------------------------------------------------------------ /** * 判断两个对象是否相同 * @param a 要比较的对象一 * @param b 要比较的对象二 * @returns 相同返回 true,反之则反 */ export function isEqual(a: { [key: string]: any }, b: { [key: string]: any }) { if (!a || !b) return false const aProps = Object.getOwnPropertyNames(a) const bProps = Object.getOwnPropertyNames(b) if (aProps.length !== bProps.length) return false for (let i = 0; i < aProps.length; i++) { const propName = aProps[i] const propA = a[propName] const propB = b[propName] if (!b.hasOwnProperty(propName)) return false if (propA instanceof Object) { if (!isEqual(propA, propB)) return false } else if (propA !== propB) { return false } } return true } /** * 对象深克隆 * @param val 源对象 * @returns 克隆后的对象 */ export function deepClone(val: any) { let newObj: any try { newObj = val.push ? [] : {} } catch (error) { newObj = {} } for (const attr in val) { if (val[attr] !== null && typeof val[attr] === 'object') { newObj[attr] = deepClone(val[attr]) } else { newObj[attr] = val[attr] } } return newObj } /** * 判断是否为空 * @param val 入参 * @returns 是返回true,反之则反 */ export const isNull = (val:any) => { return typeof (val) === 'undefined' || val === '' || val == null || val === 'null' || val === 'undefined' } export const isNullObj = (val:any) => { return JSON.stringify(val) === '{}' } /** * 判断是否为函数 * @param val 入参 * @returns 是返回true,反之则反 */ export function isFunction(val:any) { return typeof (val) === 'function' } // #endregion // #region download 下载----------------------------------------------------------------------- /** * 下载文件(minio使用代理转成同源) * @param url 地址 * @param name 文件名 */ export const download = (url:string, name:string) => { var domA = document.createElement('a') domA.download = name domA.style.display = 'none' url = url.split('://')[1] url = url.substring(url.indexOf('/')) domA.href = location.protocol + '//' + location.host + url document.body.appendChild(domA) domA.click() document.body.removeChild(domA) } // #endregion // #region message 消息提示相关(不能使用箭头函数)------------------------------------------------- /** * 关键词语重点提示 * @param msg 消息内容 * @param style 消息样式 * @param plain 是否简洁模式 * @returns 返回重构后的html */ export const keyContent = function(msg: string, style: string | undefined = 'info', plain: boolean | undefined = false) { if (style === 'info') { style = 'color: #444' } else if (style === 'success') { style = 'color: #3a3ad2' } else if (style === 'warning') { style = 'color: #79f904' } else if (style === 'error') { style = 'color: #ff5400' } return plain ? (' ' + msg + ' ') : ('【' + msg + '】') } /** * 消息框 * @param msg 消息内容 * @param type 类型 * @param options 属性 * @returns 返回msg */ export const msg = function(msg: string, type: string | undefined = 'success', options: any = {}) { msg = msg ? msg.replace(/\r\n/g, '
') : '' options.iconClass = options.iconClass + ' el-message__icon iconfont icon-font-' + type options.customClass = options.customClass + ' window-msg el-message--' + type const opts = Object.assign({}, { message: msg, type: type, duration: (type === 'success' ? 1000 : 3000), offset: 130, dangerouslyUseHTMLString: true, showClose: true }, options) return ElMessage(opts) } /** * 提示框 * @param msg 提示内容 * @param title 标题 * @param options 属性 * @returns 返回info */ export const info = function(msg: string, title: string | undefined = '提示', options: any = {}) { msg = msg ? msg.replace(/\r\n/g, '
') : '' options.iconClass = options.iconClass + ' el-message-box__status el-icon-info iconfont icon-font-info' options.customClass = options.customClass + ' window-alert window-info' const opts = Object.assign({}, { type: 'info', confirmButtonText: '确 定', // confirmButtonClass: 'iconfont icon-font-save', dangerouslyUseHTMLString: true, showClose: false }, options) return ElMessageBox.alert(msg, title, opts) } /** * 警告框 * @param msg 警告内容 * @param title 标题 * @param options 属性 * @returns 返回warning */ export const warning = function(msg: string, title: string | undefined = '警告', options: any = {}) { msg = msg ? msg.replace(/\r\n/g, '
').replace(/\r/g, '
').replace(/\n/g, '
') : '' options.iconClass = options.iconClass + ' el-message-box__status el-icon-warning iconfont icon-font-warning' options.customClass = options.customClass + ' window-alert window-warning' const opts = Object.assign({}, { type: 'warning', confirmButtonText: '确 定', // confirmButtonClass: 'iconfont icon-font-save', dangerouslyUseHTMLString: true, showClose: false }, options) return ElMessageBox.alert(msg, title, opts) } /** * 错误框 * @param msg 错误内容 * @param title 标题 * @param options 属性 * @returns 返回error */ export const error = function(msg: string, title: string | undefined = '错误', options: any = {}) { if (document.getElementsByClassName('window-networkError').length) { // console.info('网络异常,请稍后重试') return } if (msg === 'Network Error') { options.customClass = options.customClass + ' window-networkError' msg = '网络异常,请稍后重试' } msg = msg ? msg.replace(/\r\n/g, '
') : '' options.iconClass = options.iconClass + ' el-message-box__status el-icon-error iconfont icon-font-error' options.customClass = options.customClass + ' window-alert window-error' const opts = Object.assign({}, { type: 'error', confirmButtonText: '确 定', // confirmButtonClass: 'iconfont icon-font-save', dangerouslyUseHTMLString: true, showClose: false }, options) return ElMessageBox.alert(msg, title, opts) } /** * 确认框 (重写了elementPlus的$confirm) * @param msg 询问内容 * @param title 标题 * @param type 类型 ['question','success', 'warning', 'info', 'error'] * @param options 属性 * @returns 返回error */ export const confirm = function(msg: string, title: string | undefined = '询问', type: string, options: any = {}) { msg = msg ? msg.replace(/\r\n/g, '
') : '' // 此处这样判断原因在于elementPlus不支持question,后期根据具体情况手动更改依赖包 ['success', 'warning', 'info', 'error'] const iconType = isNull(type) || type === 'question' ? 'question' : type type = isNull(type) || type === 'question' ? 'info' : type options.iconClass = options.iconClass + ' el-message-box__status el-icon-' + type + ' iconfont icon-font-' + iconType options.customClass = options.customClass + ' window-alert window-confirm' const opts = Object.assign({}, { type: type, showCancelButton: true, confirmButtonText: '确 定', // confirmButtonClass: 'iconfont icon-font-save', cancelButtonText: '取 消', // cancelButtonClass: 'iconfont icon-font-cancel', dangerouslyUseHTMLString: true, showClose: false }, options) return ElMessageBox.alert(msg, title, opts) } // #endregion