login.js 993 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import { routerRedux } from 'dva/router';
  2. import { message } from 'antd';
  3. import { login, logout } from '../services/login';
  4. import { addLocalUser } from '../utils/helper';
  5. export default {
  6. namespace: 'login',
  7. state: {
  8. loading: false,
  9. },
  10. effects: {
  11. *login({ payload }, { put, call, select }) {
  12. yield put({ type: 'save', payload: { loading: true } });
  13. const { data, success } = yield call(login, payload);
  14. if (success) {
  15. addLocalUser(data);
  16. message.success('登录成功')
  17. yield put(routerRedux.push('/'));
  18. }
  19. yield put({ type: 'save', payload: { loading: false } });
  20. },
  21. *logout(_, { put, call }) {
  22. const { success } = yield call(logout);
  23. if (success) {
  24. message.success('注销成功,请重新登录!')
  25. yield put(routerRedux.push('/user/login'));
  26. }
  27. }
  28. },
  29. reducers: {
  30. save(state, action) {
  31. return {
  32. ...state,
  33. ...action.payload,
  34. }
  35. },
  36. },
  37. }