model.ts 2.65 KB
Newer Older
陈帅's avatar
陈帅 committed
1
import { routerRedux } from 'dva/router';
陈帅's avatar
陈帅 committed
2
import { getPageQuery, setAuthority } from './utils/utils';
3
import { fakeAccountLogin, getFakeCaptcha } from './service';
陈帅's avatar
陈帅 committed
4
import { stringify } from 'qs';
陈帅's avatar
陈帅 committed
5 6 7 8 9 10 11 12 13 14 15 16
import { Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';
import { AnyAction } from 'redux';

export interface IStateType {
  status?: 'ok' | 'error';
  type?: string;
  currentAuthority?: 'user' | 'guest' | 'admin';
}

export type Effect = (
  action: AnyAction,
陈帅's avatar
陈帅 committed
17
  effects: EffectsCommandMap & { select: <T>(func: (state: IStateType) => T) => T },
陈帅's avatar
陈帅 committed
18 19 20 21 22 23 24 25
) => void;

export interface ModelType {
  namespace: string;
  state: IStateType;
  effects: {
    login: Effect;
    getCaptcha: Effect;
陈帅's avatar
陈帅 committed
26
    logout: Effect;
陈帅's avatar
陈帅 committed
27 28 29 30 31 32 33
  };
  reducers: {
    changeLoginStatus: Reducer<IStateType>;
  };
}

const Model: ModelType = {
34
  namespace: 'BLOCK_NAME_CAMEL_CASE',
35 36 37 38 39 40

  state: {
    status: undefined,
  },

  effects: {
afc163's avatar
afc163 committed
41
    *login({ payload }, { call, put }) {
ddcat1115's avatar
ddcat1115 committed
42
      const response = yield call(fakeAccountLogin, payload);
43
      yield put({
44
        type: 'changeLoginStatus',
45 46
        payload: response,
      });
afc163's avatar
afc163 committed
47 48
      // Login successfully
      if (response.status === 'ok') {
49
        const urlParams = new URL(window.location.href);
yoyo837's avatar
yoyo837 committed
50
        const params = getPageQuery();
陈帅's avatar
陈帅 committed
51
        let { redirect } = params as { redirect: string };
52 53 54 55
        if (redirect) {
          const redirectUrlParams = new URL(redirect);
          if (redirectUrlParams.origin === urlParams.origin) {
            redirect = redirect.substr(urlParams.origin.length);
56
            if (redirect.match(/^\/.*#/)) {
xiaoiver's avatar
xiaoiver committed
57
              redirect = redirect.substr(redirect.indexOf('#') + 1);
yoyo837's avatar
yoyo837 committed
58
            }
59 60 61 62 63
          } else {
            window.location.href = redirect;
            return;
          }
        }
64
        yield put(routerRedux.replace(redirect || '/'));
65
      }
ddcat1115's avatar
fix #52  
ddcat1115 committed
66
    },
陈帅's avatar
陈帅 committed
67

68 69 70
    *getCaptcha({ payload }, { call }) {
      yield call(getFakeCaptcha, payload);
    },
陈帅's avatar
陈帅 committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92

    *logout(_, { put }) {
      yield put({
        type: 'changeLoginStatus',
        payload: {
          status: false,
          currentAuthority: 'guest',
        },
      });
      const { redirect } = getPageQuery();
      // redirect
      if (window.location.pathname !== '/user/login' && !redirect) {
        yield put(
          routerRedux.replace({
            pathname: '/user/login',
            search: stringify({
              redirect: window.location.href,
            }),
          }),
        );
      }
    },
93 94 95
  },

  reducers: {
96
    changeLoginStatus(state, { payload }) {
陈帅's avatar
陈帅 committed
97
      setAuthority(payload.currentAuthority);
98 99 100 101 102 103 104 105
      return {
        ...state,
        status: payload.status,
        type: payload.type,
      };
    },
  },
};
陈帅's avatar
陈帅 committed
106 107

export default Model;