index.js 3.41 KB
Newer Older
1
export { default as langUtil } from './langUtils';
水落(YangLei)'s avatar
水落(YangLei) committed
2 3 4 5 6 7 8 9 10 11
export * from './requestUtil';

const USERID_KEY = 'userId';
export function getUserId() {
    return window.sessionStorage.getItem(USERID_KEY) || '';
}

export function setUserId(val) {
    window.sessionStorage.setItem(USERID_KEY, val);
}
水落(YangLei)'s avatar
水落(YangLei) committed
12 13 14
export function clearUserId() {
    window.sessionStorage.removeItem(USERID_KEY);
}
水落(YangLei)'s avatar
水落(YangLei) committed
15 16 17 18

/**
 * 转变菜单列表为tree结构
 * @param {Array} menuList 菜单列表
19
 * @param {Boolean} filterMenu 是否过滤掉菜单,只保留目录
水落(YangLei)'s avatar
水落(YangLei) committed
20
 */
21 22 23 24 25
export function convertListToTree(menuList, filterMenu = false) {
    let tempMenu = [...menuList];
    if (filterMenu) {
        tempMenu = tempMenu.filter(m => m.menuType !== 'MENU');
    }
水落(YangLei)'s avatar
水落(YangLei) committed
26 27 28 29 30 31 32
    for (const menu of menuList) {
        if (menu.parentMenuId === 0) continue;
        const parent = menuList.find(m => m.menuId === menu.parentMenuId);
        parent.children ? parent.children.push(menu) : (parent.children = [menu]);
    }
    return tempMenu.filter(m => m.parentMenuId === 0);
}
33 34

export function EMPTY_FUN() {}
35 36

export const isFunction = val => typeof val === 'function';
陈浩玮's avatar
陈浩玮 committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
export function arrayToTree(options) {
    options = JSON.parse(JSON.stringify(options));
    this.data = options.data || [];
    this.parentKey = options.parentKey || 'parent_code';
    this.childrenKey = options.childrenKey || 'children';
    this.key = options.key || 'code';
    this.maping = options.maping || undefined;
    this.rootValue = options.rootValue || '0';
    this.treeData = [];
    this.indexStorage = {};
    this.getDataBykey = function(key) {
        return this.indexStorage[key];
    };
    this.setMapping = function() {
        for (let i = 0; i < this.data.length; i++) {
            var item = this.data[i];
            for (const x in this.maping) {
                item[this.maping[x]] = item[x];
            }
        }
    };
    if (this.maping) {
        this.setMapping();
    }
    this.setIndexStorage = function() {
        for (let i = 0; i < this.data.length; i++) {
            this.indexStorage[this.data[i][this.key]] = this.data[i]; // 以id作为索引存储元素,可以无需遍历直接定位元素
        }
    };
    this.setIndexStorage();
    // 利用数组浅拷贝
    this.toTree = function() {
        const THISDATA = JSON.parse(JSON.stringify(this.data));
        for (let i = 0; i < this.data.length; i++) {
            let currentElement = this.data[i];
            let tempCurrentElementParent = this.indexStorage[currentElement[this.parentKey]]; // 临时变量里面的当前元素的父元素
            if (tempCurrentElementParent) {
                // 如果存在父元素
                if (!tempCurrentElementParent[this.childrenKey]) {
                    // 如果父元素没有chindren键
                    tempCurrentElementParent[this.childrenKey] = []; // 设上父元素的children键
                }
                tempCurrentElementParent[this.childrenKey].push(currentElement); // 给父元素加上当前元素作为子元素
            } else {
                // 不存在父元素,意味着当前元素是一级元素
                if (this.rootValue != undefined && currentElement[this.key] === this.rootValue) {
                    this.treeData.push(currentElement);
                } else {
                    this.treeData.push(currentElement);
                }
            }
        }
        return this.treeData;
    };
    this.toTree();
    return this;
}