import { createStore as create } from 'vuex'; import { Styles, Size } from '../config'; import { getByCodes, getMenus, logout } from '../api/public'; import { mapping } from '../api/role'; export class State { dict = { active_status: { A: '启用', B: '禁用', }, }; roles = {}; menus = []; roleRefresh = true; style = Styles.plain; size = Size.normal; } export class Actions { getDictMap = ({ state, commit }, codes) => { if (codes == null) { return; } const arr: Array = []; for (let i = 0; i < codes.length; i++) { const code = codes[i]; state.dict[code] || arr.push(code); } if (arr.length == 0) { return; } getByCodes({ codes: arr.join() }).then((rsp: any) => { if (rsp) { for (let i = 0; i < arr.length; i++) { const key = arr[i]; const dict = {}; if (rsp[key]) { rsp[key].forEach((item) => { dict[item.code] = item.name; }); } commit('updateDict', [key, dict]); } } }); }; getRoleMap = ({ state, commit }) => { state.roleRefresh && mapping().then((rsp) => { commit('updateState', ['roles', rsp]); commit('updateState', ['roleRefresh', false]); }); }; getMenus = ({ state, commit }) => { getMenus().then((rsp) => commit('updateState', ['menus', rsp])); }; } export class Mutations { updateState = (state, param) => { state[param[0]] = param[1]; }; initSize = (state, height) => { const size = state.size; const head = parseInt(size.headHeight); const mainPad = parseInt(size.mainPad); const searchRow = parseInt(size.searchRowHeight); const searchRowPad = parseInt(size.searchRowPad); if (height) { size.height = height; } else { height = size.height; } size.mainHeight = Math.floor(height - head) + 'px'; size.tableHeight = height - 2 * (mainPad + searchRowPad) - 3 - searchRow - head; size.pTableHeight = size.tableHeight - size.pageHeight; }; updateDict = (state, param) => { state.dict[param[0]] = param[1]; }; } export interface StoreOptions { state?: State; getters?; actions?; mutations?: Mutations; modules?; plugins?; strict?: boolean; devtools?: boolean; } export const createStore = (options) => { return create(options); };