model.ts 1.45 KB
Newer Older
陈帅's avatar
陈帅 committed
1
import { routerRedux } from 'dva/router';
2 3
import { getPageQuery } from './utils/utils';
import { fakeAccountLogin, getFakeCaptcha } from './service';
4 5

export default {
6
  namespace: 'BLOCK_NAME_CAMEL_CASE',
7 8 9 10 11 12

  state: {
    status: undefined,
  },

  effects: {
afc163's avatar
afc163 committed
13
    *login({ payload }, { call, put }) {
ddcat1115's avatar
ddcat1115 committed
14
      const response = yield call(fakeAccountLogin, payload);
15
      yield put({
16
        type: 'changeLoginStatus',
17 18
        payload: response,
      });
afc163's avatar
afc163 committed
19 20
      // Login successfully
      if (response.status === 'ok') {
21
        const urlParams = new URL(window.location.href);
yoyo837's avatar
yoyo837 committed
22 23
        const params = getPageQuery();
        let { redirect } = params;
24 25 26 27
        if (redirect) {
          const redirectUrlParams = new URL(redirect);
          if (redirectUrlParams.origin === urlParams.origin) {
            redirect = redirect.substr(urlParams.origin.length);
28
            if (redirect.match(/^\/.*#/)) {
xiaoiver's avatar
xiaoiver committed
29
              redirect = redirect.substr(redirect.indexOf('#') + 1);
yoyo837's avatar
yoyo837 committed
30
            }
31 32 33 34 35
          } else {
            window.location.href = redirect;
            return;
          }
        }
36
        yield put(routerRedux.replace(redirect || '/'));
37
      }
ddcat1115's avatar
fix #52  
ddcat1115 committed
38
    },
陈帅's avatar
陈帅 committed
39

40 41 42
    *getCaptcha({ payload }, { call }) {
      yield call(getFakeCaptcha, payload);
    },
43 44 45
  },

  reducers: {
46
    changeLoginStatus(state, { payload }) {
ddcat1115's avatar
ddcat1115 committed
47
      setAuthority(payload.currentAuthority);
48 49 50 51 52 53 54 55
      return {
        ...state,
        status: payload.status,
        type: payload.type,
      };
    },
  },
};