login.js 1.41 KB
Newer Older
1
import { routerRedux } from 'dva/router';
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
import { fakeAccountLogin, fakeMobileLogin } from '../services/api';

export default {
  namespace: 'login',

  state: {
    status: undefined,
  },

  effects: {
    *accountSubmit({ payload }, { call, put }) {
      yield put({
        type: 'changeSubmitting',
        payload: true,
      });
ddcat1115's avatar
ddcat1115 committed
17
      const response = yield call(fakeAccountLogin, payload);
18
      yield put({
19
        type: 'changeLoginStatus',
20 21 22 23 24 25 26
        payload: response,
      });
      yield put({
        type: 'changeSubmitting',
        payload: false,
      });
    },
afc163's avatar
afc163 committed
27
    *mobileSubmit(_, { call, put }) {
28 29 30 31 32 33
      yield put({
        type: 'changeSubmitting',
        payload: true,
      });
      const response = yield call(fakeMobileLogin);
      yield put({
34
        type: 'changeLoginStatus',
35 36 37 38 39 40 41
        payload: response,
      });
      yield put({
        type: 'changeSubmitting',
        payload: false,
      });
    },
42
    *logout(_, { put }) {
ddcat1115's avatar
fix #52  
ddcat1115 committed
43
      yield put({
44 45 46 47
        type: 'changeLoginStatus',
        payload: {
          status: false,
        },
ddcat1115's avatar
fix #52  
ddcat1115 committed
48
      });
49
      yield put(routerRedux.push('/user/login'));
ddcat1115's avatar
fix #52  
ddcat1115 committed
50
    },
51 52 53
  },

  reducers: {
54
    changeLoginStatus(state, { payload }) {
55 56 57 58 59 60 61 62 63 64 65 66 67 68
      return {
        ...state,
        status: payload.status,
        type: payload.type,
      };
    },
    changeSubmitting(state, { payload }) {
      return {
        ...state,
        submitting: payload,
      };
    },
  },
};