commonUtils.js 1.3 KB
import store from '../redux/store';

/*
*key转小写
*/
export const typeToLowerCase = (type) => {
    if (type && typeof (type) === 'string') {
        return type.toLowerCase();
    } else {
        return type;
    }
};
/*
*reducer生成方法
*/
export const reducerFactory = (state = {}, action = {}) => {
    const { payload, type } = action;
    if (state && state.hasOwnProperty(typeToLowerCase(type))) {
        return { ...state, [typeToLowerCase(type)]: payload };
    } else {
        return state;
    }
};

export const dispatch = (action) => {
    return store.dispatch(action);
};

/* 
*修改路由
*/
export const pushRoute = (url) => {
    document.location.href = `#${url}`;
};
/*
获取当前选中的菜单
*/
export const getSystemSelect = (pathname, perms) => {
    let moduleName = '', openKeys = [], selectedKeys = [];
    if (pathname && pathname.indexOf('/main/') != -1 && pathname.split("/").length > 2) {
        const pathArray = pathname.split("/");
        moduleName = pathArray[2];
    }
    perms.map((perm, i) => {
        if (perm.module == moduleName) {
            selectedKeys = [moduleName];
            if (perm.parent_module) {
                openKeys = [perm.parent_module];
            }
        }
    });
    return {
        openKeys,
        selectedKeys
    };
};