| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- /**
- * 全局model
- */
- import { routerRedux } from 'dva/router'
- import { message } from 'antd'
- const code = window.localStorage.getItem('usercode');
- const account = window.localStorage.getItem('account');
- const password = window.localStorage.getItem('password');
- const name = window.localStorage.getItem('username');
- const role = window.localStorage.getItem('userrole');
- export default {
- namespace: 'main',
- state: {
- authenticated: false,
- currentUser: {
- code,
- account,
- password,
- name,
- role,
- },
- currentPage: ''
- },
- reducers: {
- setCurrentPage(state, action) {
- return { ...state, currentPage: action.page || 'home' };
- },
- setCurrentUser(state, action) {
- const { user } = action;
- return { ...state, currentUser: {
- code: user.code,
- account: user.account,
- password: user.password,
- name: user.name,
- role: user.role
- } };
- },
- setAuthenticated(state, action) {
- const { authenticated } = action;
- return { ...state, authenticated };
- }
- },
- effects: {
- * redirect (action, { put }) {
- const { path, reload } = action;
- yield put(routerRedux.push(path || '/'));
- if(reload) {
- window.location.reload();
- }
- },
- * goBack (action, { put }) {
- const { reload, path } = action;
- if(window.history.length === 1) {
- yield put(routerRedux.push(path || '/'));
- }else {
- yield put(routerRedux.goBack());
- }
- if(reload) {
- window.location.reload();
- }
- },
- *logout( action, { put, call, select }) {
- try {
- window.localStorage.removeItem("username");
- window.localStorage.removeItem("userrole");
- window.localStorage.removeItem("usercode");
- window.localStorage.removeItem("loginTime");
- window.localStorage.removeItem("token");
- window.localStorage.removeItem("expireTime");
- yield put({ type: 'dataConnect/list', list: [] });
- yield put({ type: 'dataSource/list', list: [] });
- yield put({ type: 'chart/list', list: [] });
- yield put({ type: 'dashboard/list', list: [] });
- yield put({ type: 'user/list', list: [] });
- yield put({ type: 'userGroup/list', list: [] });
- yield put({ type: 'recent/listRecentChart', recentChart: [] });
- yield put({ type: 'recent/listRecentDashboard', recentDashboard: [] });
- }catch(e) {
- console.log(e);
- message.error('注销失败: ' + e);
- }
- },
- },
- subscriptions: {
- setup({ dispatch, history }) {
- message.config({
- top: 60,
- duration: 2,
- maxCount: 3,
- });
- return history.listen(({ pathname, query }) => {
- let page = pathname.match(/\/(\w*)/)[1];
- dispatch({ type: 'setCurrentPage', page });
- })
- }
- }
- };
|