index.js 5.16 KB
Newer Older
1
export { default as langUtil } from './langUtils';
水落(YangLei)'s avatar
水落(YangLei) committed
2
import { getUserInfoApi } from '@/api';
水落(YangLei)'s avatar
水落(YangLei) committed
3 4 5 6 7 8 9 10 11 12
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
13 14 15
export function clearUserId() {
    window.sessionStorage.removeItem(USERID_KEY);
}
水落(YangLei)'s avatar
水落(YangLei) committed
16

水落(YangLei)'s avatar
水落(YangLei) committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
const USERINFO_KEY = 'USERINFO';
export async function setUserInfoByRequest() {
    const userInfo = await getUserInfoApi(getUserId());
    window.sessionStorage.setItem(USERINFO_KEY, JSON.stringify(userInfo));
}

export function getUserInfo() {
    const localUserInfo = window.sessionStorage.getItem(USERINFO_KEY);
    return JSON.parse(localUserInfo);
}

export function clearUserInfo() {
    window.sessionStorage.clear();
}

水落(YangLei)'s avatar
水落(YangLei) committed
32 33 34
/**
 * 转变菜单列表为tree结构
 * @param {Array} menuList 菜单列表
35
 * @param {Boolean} filterMenu 是否过滤掉菜单,只保留目录
水落(YangLei)'s avatar
水落(YangLei) committed
36
 */
37
export function convertListToTree(menuList, filterMenu = false, toMenuData = false) {
38 39
    let tempMenu = [...menuList];
    if (filterMenu) {
陈浩玮's avatar
tijiao  
陈浩玮 committed
40
        tempMenu = tempMenu.filter((m) => m.menuType !== 'MENU');
41
    }
水落(YangLei)'s avatar
水落(YangLei) committed
42
    for (const menu of menuList) {
43 44 45 46
        if (toMenuData) {
            menu.name = menu.menuName;
            menu.path = menu.menuUrl;
        }
水落(YangLei)'s avatar
水落(YangLei) committed
47
        if (menu.parentMenuId === 0) continue;
陈浩玮's avatar
tijiao  
陈浩玮 committed
48
        const parent = menuList.find((m) => m.menuId === menu.parentMenuId);
水落(YangLei)'s avatar
水落(YangLei) committed
49 50
        parent.children ? parent.children.push(menu) : (parent.children = [menu]);
    }
陈浩玮's avatar
tijiao  
陈浩玮 committed
51
    return tempMenu.filter((m) => m.parentMenuId === 0);
水落(YangLei)'s avatar
水落(YangLei) committed
52
}
53 54

export function EMPTY_FUN() {}
55

陈浩玮's avatar
tijiao  
陈浩玮 committed
56
export const isFunction = (val) => typeof val === 'function';
陈浩玮's avatar
陈浩玮 committed
57 58 59 60 61 62 63 64 65 66
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 = {};
陈浩玮's avatar
tijiao  
陈浩玮 committed
67
    this.getDataBykey = function (key) {
陈浩玮's avatar
陈浩玮 committed
68 69
        return this.indexStorage[key];
    };
陈浩玮's avatar
tijiao  
陈浩玮 committed
70
    this.setMapping = function () {
陈浩玮's avatar
陈浩玮 committed
71 72 73 74 75 76 77 78 79 80
        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();
    }
陈浩玮's avatar
tijiao  
陈浩玮 committed
81
    this.setIndexStorage = function () {
陈浩玮's avatar
陈浩玮 committed
82 83 84 85 86 87
        for (let i = 0; i < this.data.length; i++) {
            this.indexStorage[this.data[i][this.key]] = this.data[i]; // 以id作为索引存储元素,可以无需遍历直接定位元素
        }
    };
    this.setIndexStorage();
    // 利用数组浅拷贝
陈浩玮's avatar
tijiao  
陈浩玮 committed
88
    this.toTree = function () {
陈浩玮's avatar
陈浩玮 committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
        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;
}
陈浩玮's avatar
陈浩玮 committed
114 115

// select组件 数据格式化
水落(YangLei)'s avatar
水落(YangLei) committed
116
export const formatObj = (ArrObj, obj) => {
陈浩玮's avatar
陈浩玮 committed
117
    const newData = [...ArrObj];
陈浩玮's avatar
tijiao  
陈浩玮 committed
118
    const newDataOne = newData.map((i) => {
陈浩玮's avatar
陈浩玮 committed
119
        const newObj = { ...i };
陈浩玮's avatar
tijiao  
陈浩玮 committed
120
        Object.keys(obj).forEach((key) => {
陈浩玮's avatar
陈浩玮 committed
121 122 123 124 125 126
            newObj[key] = i[obj[key]];
        });
        return newObj;
    });
    return newDataOne;
};
陈浩玮's avatar
陈浩玮 committed
127 128 129 130 131

export const isObjEmpty = (data = {}) => {
    const arr = Object.keys(data);
    return arr.length === 0;
};
水落(YangLei)'s avatar
水落(YangLei) committed
132 133 134 135 136 137 138 139 140 141
/**
 * 下载文件
 * @param {String} url 下载路径
 */
export function downloadFileByUrl(url) {
    const aEle = document.createElement('a');
    aEle.setAttribute('download', true);
    aEle.setAttribute('href', url);
    aEle.click();
}
陈浩玮's avatar
tijiao  
陈浩玮 committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

/**
 * 下载文件
 * @param {data} data 文件流
 * @param {string} type 文件后缀
 */
export function downloadFile(data, type) {
    const url = window.URL.createObjectURL(data);
    // 生成一个a标签
    const link = document.createElement('a');
    link.style.display = 'none';
    link.href = url;
    // 生成时间戳
    const timestamp = new Date().getTime();
    link.download = `${timestamp}.${type}`;
    document.body.appendChild(link);
    link.click();
}