// eslint-disable-next-line eslint-comments/disable-enable-pair /* eslint-disable no-bitwise */ import pathToRegexp from 'path-to-regexp'; import { parse } from 'qs'; /** * 获取链接的query url部分 */ export function getPageQuery() { return parse(window.location.href.split('?')[1]); } export const generateUUID = () => { let d = new Date().getTime(); const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => { const r = (d + Math.random() * 16) % 16 | 0; d = Math.floor(d / 16); return (c === 'x' ? r : (r & 0x3) | 0x8).toString(16); }); return uuid; }; /** * 检查是否为正确的移动的电话号码 */ export const checkMobilePhone = mobilePhone => { if (!/^1(3|4|5|7|8)\d{9}$/.test(mobilePhone)) { return false; } return true; }; /** * 检查是否为正确的邮箱 */ export const checkEmail = mobilePhone => { const reg = new RegExp( '^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$', ); if (!reg.test(mobilePhone)) { return false; } return true; }; /** * 检查是否为正确的密码 * @param {*} password */ export const checkPassword = password => { const reg = new RegExp(/^(\w){6,20}$/); if (!reg.test(password)) { return false; } return true; }; /** * 转换后端返回的表格数据成前端使用的表格数据 * @param {object} data 后端数据 * @param {object} option * @param {string} option.keyCode 指定唯一标识字段 * @param {function} option.onChange 分页回调 */ export const trimTableData = (data = {}, option = {}) => { const { records = [], current = 1, pageSize = 1, total = 1 } = data; const { keyCode, onChange } = option; let tableData = records; const pageData = { current, pageSize, total, }; if (keyCode) { tableData = records.map(item => ({ key: item[keyCode], ...item })); } if (typeof onChange === 'function') { pageData.onChange = onChange; } return { tableData, pageData, }; }; /** * 转换后端返回的静态数据为key、value格式 * @param {object} staticData 后端静态数据 * @param {string} staticKey 需要处理的静态数据唯一ID */ export const trimStaticData = (staticData = {}, staticKey) => (staticData[staticKey] || []) // .filter(item => item.paramLocale === "zh_CN") .map(item => ({ key: item.paramValue, value: item.paramName, })); /** * 处理后端返回的组织数据为供Select组件使用的带结构数据 * @param {object} data */ export const trimOrgaData = data => { const trimData = clone( data.map(item => ({ ...item, title: item.orgName, value: item.orgId, key: item.orgId, })), ); const orgaData = trimData.filter(item => item.orgType === 1); const depaData = trimData.filter(item => item.orgType === 2); // 处理部门数据 depaData.forEach(item => { const orgaKey = orgaData.findIndex(orgaItem => orgaItem.orgId === item.parentOrgId); if (orgaKey > -1) { const children = orgaData[orgaKey].children || []; children.push(item); orgaData[orgaKey].children = [...children]; } }); // 处理公司数据 // 目前只支持两层数据 return orgaData.reduce((container, item) => { const orgaKey = container.findIndex(orgaItem => orgaItem.orgId === item.parentOrgId); if (orgaKey > -1) { const children = container[orgaKey].children || []; children.push(item); container[orgaKey].children = [...children]; } else { container.push(item); } return container; }, []); }; export const treeDataTransform = (data = [], key = 'id', parentKey = 'parendId', rootPid = -1) => { const midObj = {}; const arr = []; data.sort((a, b) => b[parentKey] - a[parentKey]); // eslint-disable-next-line const translator = data => { if (!data || !data.length) { return data; } data.forEach((item, index) => { const nowPid = item[parentKey]; const nowId = item[key]; if (nowPid !== rootPid) { translator(item); delete data[index]; } // 建立当前节点的父节点的children 数组 if (midObj[nowPid]) { midObj[nowPid].push(item); } else { midObj[nowPid] = []; midObj[nowPid].push(item); } // 将children 放入合适的位置 if (midObj[nowId]) { item.children = midObj[nowId]; delete midObj[nowId]; if (item && data[index]) { arr.push(item); } } }); }; translator(data); return arr; }; export const treeTransform1 = (data = [], key = 'id', parentKey = 'parendId', rootPid = -1) => { const parents = data.filter( item => item[parentKey] === rootPid || item[parentKey] === 'undefined', ); const children = data.filter( item => item[parentKey] !== rootPid && item[parentKey] !== 'undefined', ); // eslint-disable-next-line const translator = (parents, children) => { parents.forEach(parent => { children.forEach((current, index) => { if (current[parentKey] !== parent[key]) { return; } const temp = JSON.parse(JSON.stringify(children)); temp.splice(index, 1); if (typeof parent.children !== 'undefined') { parent.children.push(current); } else { parent.children = [current]; } return translator([current], temp); }); }); }; translator(parents, children); return parents; }; /** * 平级数组菜单列表转成树形菜单列表 * @param {平级数组菜单列表} flatMenuList */ export const toTreeMenuList = (flatMenuList = []) => { const tree = []; const key = 'menuId'; const pKey = 'pMenuId'; const topFlag = 0; if (flatMenuList) { const dict = {}; // add rood node flatMenuList.forEach(item => { dict[item[key]] = item; if (equals(item[pKey], topFlag) || item[pKey] === '') { item.level = 0; item.key = String(item[key]); item.icon = item.menuIcon; item.link = item.menuUrl; item.title = item.menuName; tree[tree.length] = item; // 添加根节点,可能有多个节点 } }); flatMenuList.forEach(item => { const child = item; if (equals(child[pKey], topFlag) || child[pKey] === '') { return; } const parent = dict[child[pKey]]; if (parent) { // child.parent = parent; if (!parent.children) { parent.children = []; } child.key = String(child[key]); child.level = parent.level + 1; child.icon = child.menuIcon; child.link = child.menuUrl; child.title = child.menuName; parent.children[parent.children.length] = child; } }); return tree; } }; /** * 平级组织部门转换成树形组织部门 * @param {*} flatList */ export const toDepaTree = (flatList = []) => flatListToTree(flatList, 'orgId', 'parentOrgId', 0, { title: 'orgName', value: 'orgId', }); /** * 平级数组部门-组织列表转成树形部门-组织列表 * @param {评级列表} flatList * @param {*} key * @param {*} pKey * @param {*} topFlag * @param {*} extentProcess */ export const flatListToTree = ( flatList = [], key = 'id', pKey = 'pId', topFlag = 0, mappingData = {}, ) => { const tree = []; if (flatList) { const dict = {}; // add rood node flatList.forEach(item => { dict[item[key]] = item; if (equals(item[pKey], topFlag) || item[pKey] === '') { item.level = 0; item.key = String(item[key]); Object.keys(mappingData).forEach(k => { item[k] = item[mappingData[k]]; }); tree[tree.length] = item; // 添加根节点,可能有多个节点 } }); flatList.forEach(item => { const child = item; if (equals(child[pKey], topFlag) || child[pKey] === '') { return; } const parent = dict[child[pKey]]; if (parent) { // child.parent = parent; if (!parent.children) { parent.children = []; } child.key = String(child[key]); child.level = parent.level + 1; Object.keys(mappingData).forEach(k => { child[k] = child[mappingData[k]]; }); parent.children[parent.children.length] = child; } }); return tree; } }; /** * url转成数组 * /userinfo/2144/id => ['/userinfo','/useinfo/2144,'/userindo/2144/id'] * @param {url} url */ export function urlToList(url) { const urllist = url.split('/').filter(i => i); return urllist.map((urlItem, index) => `/${urllist.slice(0, index + 1).join('/')}`); } /** * 判断路径是否匹配了菜单旅游业经 * @param {*} flatMenuList * @param {*} path */ export const getMenuMatches = (flatMenuList, path) => flatMenuList.filter(item => { if (item.menuUrl) { return pathToRegexp(item.menuUrl).test(path); } return false; }); /** * 获得当前选中的菜单key * @param {*} flatMenuList * @param {*} path */ export const getSelectedkey = (flatMenuList = [], pathname) => { const items = urlToList(pathname) .map(item => getMenuMatches(flatMenuList, item)[0]) .filter(item => item); return items && items.length > 0 ? String(items[items.length - 1].menuId) : ''; }; export const getDefaultOpenKeys = (flatMenuList = [], pathname) => { const openKeys = []; const topFlag = 0; if (flatMenuList.length > 0) { const key = getSelectedkey(flatMenuList, pathname); const menuIdMap = {}; const pMenuIdMap = {}; flatMenuList.forEach(item => { menuIdMap[item.menuId] = item; pMenuIdMap[item.pMenuId] = item; }); if (key === '') { openKeys.push(String(pMenuIdMap[topFlag].menuId)); } else { let t = key; for (let i = 0; i < flatMenuList.length; i += 1) { if (equals(t, topFlag)) { break; } if (menuIdMap[t]) { openKeys.push(String(menuIdMap[t].menuId)); t = menuIdMap[t].pMenuId; } } } } return openKeys; }; /** * 文件下载 * @param {*} url * @param {*} fileNames * @param {...any} params */ export async function downloadFile(url, params) { const token = storage.get('token'); const formEle = document.createElement('form'); formEle.method = 'get'; formEle.style = 'display:none;'; formEle.action = url; const tokenEle = document.createElement('input'); tokenEle.type = 'hidden'; tokenEle.name = 'token'; tokenEle.value = token; formEle.appendChild(tokenEle); for (const key in params) { if (key && params[key]) { const paramsEle = document.createElement('input'); paramsEle.type = 'hidden'; paramsEle.name = key; paramsEle.value = params[key]; formEle.appendChild(paramsEle); } } document.body.appendChild(formEle); formEle.submit(); document.body.removeChild(formEle); }