import { ElMessage } from 'element-plus' function _basePath(path: string): string[] { if (Array.isArray(path)) return path return path.replace(/\[/g, '.').replace(/\]/g, '').split('.') } export function get(object: any, path: string, defaultValue = undefined): { key: string; value: any } { if (typeof object !== 'object') return defaultValue const resPath = _basePath(path) return { key: resPath.at(-1), value: resPath.reduce((o, k) => (o || {})[k], object) || defaultValue } } export function set(object: any, path: string, value: any) { if (typeof object !== 'object') return object _basePath(path).reduce((o, k, i, _) => { if (i === (_ as []).length - 1) { // 若遍历结束直接赋值 o[k] = value return null } else if (k in o) { // 若存在对应路径,则返回找到的对象,进行下一次遍历 return o[k] } else { // 若不存在对应路径,则创建对应对象,若下一路径是数字,新对象赋值为空数组,否则赋值为空对象 o[k] = /^[0-9]{1,}$/.test(_[i + 1]) ? [] : {} return o[k] } }, object) return object } // 导出文件 export async function getExportFil(fun, params = {}, name) { try { const res = await fun({ ...params }) const blob = new Blob([res], { type: 'application/vnd.ms-excel;charset=UTF-8' }) const fileName = name + '.xls' const url = window.URL.createObjectURL(blob) if ('download' in document.createElement('a')) { const a = document.createElement('a') a.href = url a.download = fileName a.style.display = 'none' document.body.appendChild(a) a.click() URL.revokeObjectURL(a.href) document.body.removeChild(a) } else { const nav = window.navigator as any nav.msSaveBlob(blob, fileName) } // let link = document.createElement('a') // link.href = url // link.setAttribute('download', fileName) // link.click() // link = null ElMessage({ type: 'success', message: '导出成功' }) } catch (err) { ElMessage({ type: 'error', message: err }) } }