text: import { query, logout } from '../services/app'
  import * as menusService from '../services/menus'
  import { routerRedux } from 'dva/router'
  import { parse } from 'qs'
  import config from 'config'
  import { EnumRoleType } from 'enums'
  const { prefix } = config;
  
  export default {
    namespace: 'app',
    state: {
      user: {},
      permissions: {
        visit: [],
      },
      menu: [
        {
          id: 1,
          icon: 'laptop',
          name: 'Dashboard',
          router: '/dashboard',
        },
      ],
      menuPopoverVisible: false,
      siderFold: localStorage.getItem(`${prefix}siderFold`) === 'true',
      darkTheme: localStorage.getItem(`${prefix}darkTheme`) === 'true',
      isNavbar: document.body.clientWidth < 769,
      navOpenKeys: JSON.parse(localStorage.getItem(`${prefix}navOpenKeys`)) || [],
    },
    subscriptions: {
  
      setup ({ dispatch }) {
        dispatch({ type: 'query' });
        let tid;
        window.onresize = () => {
          clearTimeout(tid);
          tid = setTimeout(() => {
            dispatch({ type: 'changeNavbar' })
          }, 300)
        }
      },
  
    },
    effects: {
  
      *query ({
        payload,
      }, { call, put }) {
        const { success, user } = yield call(query, payload);
        if (success && user) {
          const { list } = yield call(menusService.query);
          const { permissions } = user;
          let menu = list;
          if (permissions.role === EnumRoleType.ADMIN || permissions.role === EnumRoleType.DEVELOPER) {
            permissions.visit = list.map(item => item.id)
          } else {
            menu = list.filter(item => {
              const cases = [
                permissions.visit.includes(item.id),
                item.mpid ? permissions.visit.includes(item.mpid) || item.mpid === '-1' : true,
                item.bpid ? permissions.visit.includes(item.bpid) : true,
              ];
              return cases.every(_ => _)
            })
          }
          yield put({
            type: 'updateState',
            payload: {
              user,
              permissions,
              menu,
            },
          });
          if (location.pathname === '/login') {
            yield put(routerRedux.push('/dashboard'))
          }
        } else {
          if (config.openPages && config.openPages.indexOf(location.pathname) < 0) {
            let from = location.pathname;
            window.location = `${location.origin}/login?from=${from}`
          }
        }
      },
  
      *logout ({
        payload,
      }, { call, put }) {
        const data = yield call(logout, parse(payload));
        if (data.success) {
          yield put({ type: 'query' })
        } else {
          throw (data)
        }
      },
  
      *changeNavbar ({
        payload,
      }, { put, select }) {
        const { app } = yield(select(_ => _));
        const isNavbar = document.body.clientWidth < 769;
        if (isNavbar !== app.isNavbar) {
          yield put({ type: 'handleNavbar', payload: isNavbar })
        }
      },
  
    },
    reducers: {
      updateState (state, { payload }) {
        return {
          ...state,
          ...payload,
        }
      },
  
      switchSider (state) {
        localStorage.setItem(`${prefix}siderFold`, !state.siderFold);
        return {
          ...state,
          siderFold: !state.siderFold,
        }
      },
  
      switchTheme (state) {
        localStorage.setItem(`${prefix}darkTheme`, !state.darkTheme);
        return {
          ...state,
          darkTheme: !state.darkTheme,
        }
      },
  
      switchMenuPopver (state) {
        return {
          ...state,
          menuPopoverVisible: !state.menuPopoverVisible,
        }
      },
  
      handleNavbar (state, { payload }) {
        return {
          ...state,
          isNavbar: payload,
        }
      },
  
      handleNavOpenKeys (state, { payload: navOpenKeys }) {
        return {
          ...state,
          ...navOpenKeys,
        }
      },
    },
  }
  