123

71 阅读3分钟

import { menuRoutes } from '@/services/system'; import { copyright, getLogout } from '@/services/user'; import { message } from 'antd'; import p from 'immer'; import md5 from 'js-md5'; import { set as objectSet } from 'lodash'; import TreeNodeUtils from 'tree-node-utils'; import { history } from 'umi'; import create from 'zustand'; // const menuRoutes = () => {}; import { updatePassword } from '@/services/user'; // const updatePassword = () => { };

const treeNodeUtils = new TreeNodeUtils({ childrenField: 'children', // e.g. customise the field for children nodes. e.g. 'subItems', default 'children' keyField: 'id', // e.g. customise the field for node key. e.g. 'id', default 'key' }); const defaultState = { isReady: false, //当获取完路由后后为true

menuData: [], //左侧菜单信息 leftSideMenuCollapsed: false, leftSideMenuSelectedKeys: [], leftSideMenuSelectedItems: [],

confirmLoading: false, //确认按钮加载中

copyrightInfo: {}, menuReady: false, passwordModal: { title: '修改密码', visible: false, },

userInfo: null, };

export default create((set, get) => { return { ...defaultState, actions: { setState(key, value) { if (typeof key === 'string') { if (key.indexOf('.') !== -1 || key.indexOf('[') !== -1) { set( p((state) => { objectSet(state, key, value); }), ); } else { set({ [key]: value, }); } } else if (typeof key === 'function') { set( p((state) => { key(state); }), ); } else if (Object.prototype.toString.call(key) === '[object Object]' && !value) { set(key); } }, clearState() { set({ ...defaultState, }); }, sleep(seconds) { return new Promise((resolve) => { setTimeout(resolve, seconds * 1000); }); }, flatData(data) { const { actions } = get(); let result = []; data.forEach((item) => { result.push(item); if (item.children?.length) { result = result.concat(actions.flatData(item.children)); } }); return result; }, async fetchMenuData() { // await get().actions.sleep(1); // set({ // isReady: true, // menuData: [ // { // id: '1', // name: 'welcome', // path: '/welcome', // }, // { // id: '2', // name: 'GraphvizRender', // path: '/GraphvizRender', // }, // { // id: '3', // name: 'GraphView', // path: '/GraphView', // }, // { // id: '8', // name: '数据搜索', // path: '/DataSearch', // icon: 'icon-sousuo', // }, // { // id: '4', // name: '模型管理', // path: '/ModelManagement', // icon: 'icon-moxingguanli', // }, // { // id: '9', // name: '数据管理', // path: '/DataManagement', // icon: 'icon-shujuguanli', // },

    // {
    //   id: '7',
    //   name: '审计',
    //   path: '/Comptroller',
    //   icon: 'icon-shenjiguanli',
    // },
    // {
    //   id: '5',
    //   name: '基线管理',
    //   path: '/BaseLineManagement',
    //   icon: 'icon-jixian',
    // },
    //{
    //  id: '12',
    //  name: '快照管理',
    //  path: '/Snapshot',
    // icon: 'icon-shejikuaizhao',
    //},
    // {
    //   id: '6',
    //   name: '基础数据管理',
    //   path: '/BasicDataManagement',
    //   icon: 'icon-shuju',
    // },
    // {
    //   id: '10',
    //   name: '视图',
    //   path: '/View',
    //   icon: 'icon-shitu',
    // },
    // {
    //   id: '11',
    //   name: '统计报表',
    //   path: '/StatisticalReport',
    //   icon: 'icon-baobiaoguanli',
    // },
    // {
    //   id: '12',
    //   name: '标签管理',
    //   path: '/TagManagement',
    //   icon: 'icon-biaoqian1',
    // },
    // {
    //   id: '13',
    //   name: '校验规则管理',
    //   path: '/CheckRulesManagement',
    //   icon: 'icon-xiaoyan_huaban1',
    // },
    // {
    //   id: '14',
    //   name: '快照管理',
    //   path: '/SnapshotManagement',
    //   icon: 'icon-kuaizhao',
    // },
    // {
    //   id: '14',
    //   name: '调和引擎',
    //   path: '/Reconcile',
    //   icon: 'icon-yinqingguanli',
    // },
    // { path: '/dagTest', name: 'dagTest', id: '5454' },
    // {
    //   id: '16',
    //   name: '平台管理',
    //   icon: 'icon-xitong',
    //   children: [
    //     {
    //       id: '17',
    //       name: '系统管理',
    //       children: [
    //         {
    //           id: '18',
    //           name: '用户管理',
    //           path: '/system/user',
    //         },
    //         {
    //           id: '19',
    //           name: '机构管理',
    //           path: '/system/dept',
    //         },
    //         {
    //           id: '20',
    //           name: '菜单管理',
    //           path: '/system/menu',
    //         },
    //         {
    //           id: '21',
    //           name: '岗位管理',
    //           path: '/system/post',
    //         },
    //         {
    //           id: '22',
    //           name: '参数管理',
    //           path: '/system/param',
    //         },
    //         {
    //           id: '23',
    //           name: '租户管理',
    //           path: '/system/tenant',
    //         },
    //         {
    //           id: '24',
    //           name: '客户端',
    //           path: '/system/client',
    //         },
    //         {
    //           id: '25',
    //           name: '系统字典',
    //           path: '/system/dict',
    //         },
    //       ],
    //     },
    //     {
    //       id: '26',
    //       name: '权限管理',
    //       children: [
    //         {
    //           id: '27',
    //           name: '角色管理',
    //           path: '/authority/role',
    //         },
    //         {
    //           id: '28',
    //           name: '数据权限',
    //           path: '/authority/datascope',
    //         },
    //         {
    //           id: '29',
    //           name: '接口权限',
    //           path: '/authority/apiscope',
    //         },
    //       ],
    //     },
    //     {
    //       id: '30',
    //       name: '日志管理',
    //       children: [
    //         {
    //           id: '31',
    //           name: '行为日志',
    //           path: '/log/api',
    //         },
    //         {
    //           id: '32',
    //           name: '错误日志',
    //           path: '/log/error',
    //         },
    //       ],
    //     },
    //     {
    //       id: '33',
    //       name: '证书管理',
    //       path: '/certificateManage',
    //     },
    //     {
    //       id: '34',
    //       name: '版权管理',
    //       path: '/copyRightManage',
    //     },
    //     {
    //       id: '35',
    //       name: '节点管理',
    //       path: '/anyV/chartNodeManagement',
    //     },
    //   ],
    // },
    //   ],
    // });
    // return;
    // setTimeout(async () => {
    const res = await menuRoutes({ app: 'icmc' });
    if (res?.success) {
      set({
        menuData: Array.isArray(res.data) ? res.data : [],
        isReady: true,
      });
    }
    // }, 2000);
  },
  async fetchCopyright() {
    const res = await copyright();
    if (res?.success) {
      set({
        copyrightInfo: res.data,
      });
      return res.data;
    }
  },
  async fetchLoginInfo() {
    // console.log('开始请求登录用户信息');
    //await get().actions.sleep(3);
    // console.log('用户已登录');
  },
  handleLeftMenuJump(route, routeChildren, isOpen, isInterior) {
    if (isOpen == 2) {
      window.open(route);
    } else {
      if (isInterior === 0) {
        history.push(`/OtherView?src=${route}`);
      } else {
        history.push(route);
      }
    }
  },
  setMenuSelected(location) {
    if (location.pathname?.length <= 1) return;
    const {
      actions: { setState },
    } = get();
    let path = location.pathname;
    if (location.pathname === '/OtherView') {
      path = location.query.src;
    }

    let leftSideMenuSelectedKeys = [];

    setState((state) => {
      treeNodeUtils.mapNodes(state.menuData, (item, parents) => {
        if (path === item.path || path.indexOf(item.path) !== -1) {
          leftSideMenuSelectedKeys = [item.id];
          state.leftSideMenuSelectedItems = parents.concat([item]);
        }
        return item;
      });

      state.leftSideMenuSelectedKeys = leftSideMenuSelectedKeys;
      state.menuReady = true;
    });
  },
  handleToggleSideMenu() {
    set((state) => ({
      leftSideMenuCollapsed: !state.leftSideMenuCollapsed,
    }));
  },
  async logout() {
    const res = await getLogout();
    if (res?.success) {
      set({
        userInfo: null,
      });
      localStorage.removeItem('userInfo');
      history.replace('/login');
    }
  },
  async handleUpdatePasswordOk(values) {
    const {
      actions: { logout },
    } = get();
    let currentUser = JSON.parse(localStorage.getItem('userInfo'));
    set({ confirmLoading: true });
    let res = await updatePassword({
      newPassword: md5(values.newPassword),
      newPassword1: md5(values.newPassword1),
      oldPassword: md5(values.oldPassword),
      userId: currentUser.user_id,
    });
    if (res?.success) {
      message.success('修改成功');
      set({
        passwordModal: {
          title: '修改密码',
          visible: false,
        },
      });
      logout();
    }
    set({ confirmLoading: false });
  },
},

}; });