1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- import { routerRedux } from 'dva/router';
- import { stringify } from 'querystring';
- // import { fakeAccountLogin, getFakeCaptcha } from '@/services/login';
- // import { setAuthority } from '@/utils/authority';
- import { getPageQuery } from '@/utils/utils';
- import { fetch, apis } from '@/utils/request';
-
- const signin = fetch(apis.user.signin);
- const signout = fetch(apis.user.signout);
-
- const Model = {
- namespace: 'login',
- state: {
- status: undefined,
- },
- effects: {
- *login({ payload }, { call, put }) {
- try {
- const response = yield call(signin, { data: payload });
- } catch (e) {
- return e;
- }
-
- yield put({
- type: 'changeLoginStatus',
- payload: { type: '' },
- }); // Login successfully
-
- window.localStorage.setItem('showSwiperIndex', 1)
-
- const urlParams = new URL(window.location.href);
- const params = getPageQuery();
- let { redirect } = params;
-
- if (redirect) {
- const redirectUrlParams = new URL(redirect);
-
- if (redirectUrlParams.origin === urlParams.origin) {
- redirect = redirect.substr(urlParams.origin.length);
-
- if (redirect.match(/^\/.*#/)) {
- redirect = redirect.substr(redirect.indexOf('#') + 1);
- }
- } else {
- window.location.href = redirect;
- return;
- }
- }
-
- yield put(routerRedux.replace(redirect || '/'));
- },
-
- *logout(_, { put, call }) {
- const { redirect } = getPageQuery(); // redirect
-
- yield call(signout);
-
- if (window.location.pathname !== '/user/login' && !redirect) {
- yield put(
- routerRedux.replace({
- pathname: '/user/login',
- search: stringify({
- redirect: window.location.href,
- }),
- }),
- );
- }
- },
- },
- reducers: {
- changeLoginStatus(state, { payload }) {
- // setAuthority((payload.user.roles || []).map(x => x.roleId));
- return { ...state, status: 'ok', type: payload.type };
- },
- },
- };
- export default Model;
|