main.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * 全局model
  3. */
  4. import { routerRedux } from 'dva/router'
  5. import { message } from 'antd'
  6. const code = window.localStorage.getItem('usercode');
  7. const account = window.localStorage.getItem('account');
  8. const password = window.localStorage.getItem('password');
  9. const name = window.localStorage.getItem('username');
  10. const role = window.localStorage.getItem('userrole');
  11. export default {
  12. namespace: 'main',
  13. state: {
  14. authenticated: false,
  15. currentUser: {
  16. code,
  17. account,
  18. password,
  19. name,
  20. role,
  21. },
  22. currentPage: ''
  23. },
  24. reducers: {
  25. setCurrentPage(state, action) {
  26. return { ...state, currentPage: action.page || 'home' };
  27. },
  28. setCurrentUser(state, action) {
  29. const { user } = action;
  30. return { ...state, currentUser: {
  31. code: user.code,
  32. account: user.account,
  33. password: user.password,
  34. name: user.name,
  35. role: user.role
  36. } };
  37. },
  38. setAuthenticated(state, action) {
  39. const { authenticated } = action;
  40. return { ...state, authenticated };
  41. }
  42. },
  43. effects: {
  44. * redirect (action, { put }) {
  45. const { path, reload } = action;
  46. yield put(routerRedux.push(path || '/'));
  47. if(reload) {
  48. window.location.reload();
  49. }
  50. },
  51. * goBack (action, { put }) {
  52. const { reload, path } = action;
  53. if(window.history.length === 1) {
  54. yield put(routerRedux.push(path || '/'));
  55. }else {
  56. yield put(routerRedux.goBack());
  57. }
  58. if(reload) {
  59. window.location.reload();
  60. }
  61. },
  62. *logout( action, { put, call, select }) {
  63. try {
  64. window.localStorage.removeItem("username");
  65. window.localStorage.removeItem("userrole");
  66. window.localStorage.removeItem("usercode");
  67. window.localStorage.removeItem("loginTime");
  68. window.localStorage.removeItem("token");
  69. window.localStorage.removeItem("expireTime");
  70. yield put({ type: 'dataConnect/list', list: [] });
  71. yield put({ type: 'dataSource/list', list: [] });
  72. yield put({ type: 'chart/list', list: [] });
  73. yield put({ type: 'dashboard/list', list: [] });
  74. yield put({ type: 'user/list', list: [] });
  75. yield put({ type: 'userGroup/list', list: [] });
  76. yield put({ type: 'recent/listRecentChart', recentChart: [] });
  77. yield put({ type: 'recent/listRecentDashboard', recentDashboard: [] });
  78. }catch(e) {
  79. console.log(e);
  80. message.error('注销失败: ' + e);
  81. }
  82. },
  83. },
  84. subscriptions: {
  85. setup({ dispatch, history }) {
  86. message.config({
  87. top: 60,
  88. duration: 2,
  89. maxCount: 3,
  90. });
  91. return history.listen(({ pathname, query }) => {
  92. let page = pathname.match(/\/(\w*)/)[1];
  93. dispatch({ type: 'setCurrentPage', page });
  94. })
  95. }
  96. }
  97. };