import util from '../../../../libs/util';

/**
 * 权限
 * */
export default {
    namespaced: true,
    state: {
        // 用户权限
        userPermissions: null
    },
    getters: {},
    actions: {
        /**
         * @description 加载权限
         * @param {Object} state vuex state
         * @param {*} key 权限Key
         */
        async load({ state }, key) {
            if (state.userPermissions == null) {
                state.userPermissions = {};
                state.userPermissions = await util.db.get('permission').value();
            }
        },
        /**
         * @description 设置权限
         * @param {Object} state vuex state
         * @param {*} data 内容
         */
        async set({ state }, data) {
            let permissions = {};
            data.forEach(item => {
                permissions[item.url] = true;
            });

            util.db.set('permission', permissions).write();
            state.userPermissions = permissions;
        }
    }
};
