1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- import {stringify} from 'querystring';
- import {history, Reducer, Effect} from 'umi';
- import {fakeAccountLogin} from '@/services/login';
- import {setAuthority} from '@/utils/authority';
- import {getPageQuery} from '@/utils/utils';
- export interface StateType {
- status?: 'SUCCESS' | 'error';
- type?: string;
- currentAuthority?: '2' | '1'; // 1是管理员 ,2 是普通成员
- }
- export interface LoginModelType {
- namespace: string;
- state: StateType;
- effects: {
- login: Effect;
- logout: Effect;
- };
- reducers: {
- changeLoginStatus: Reducer<StateType>;
- };
- }
- const Model: LoginModelType = {
- namespace: 'login',
- state: {
- status: undefined,
- },
- effects: {
- * login({payload}, {call, put}) {
- // 登录
- const response = yield call(fakeAccountLogin, payload);
- // 保存登录信息,无论成功和失败
- localStorage.setItem("success", response.message);
- yield put({
- type: 'changeLoginStatus',
- payload: response,
- });
- if (response.success) {
- // 是否需要跳转
- const urlParams = new URL(window.location.href);
- const params = getPageQuery();
- let {redirect} = params as { redirect: string };
- 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 = '/';
- return;
- }
- }
- history.replace(redirect || '/');
- }
- },
- logout() {
- localStorage.removeItem("success");
- const {redirect} = getPageQuery();
- // Note: There may be security issues, please note
- if (window.location.pathname !== '/user/login' && !redirect) {
- history.replace({
- pathname: '/user/login',
- search: stringify({
- redirect: window.location.href,
- }),
- });
- }
- },
- },
- reducers: {
- changeLoginStatus(state, {payload}) {
- setAuthority(payload.data.roleId);
- return {
- ...state,
- status: payload.message,
- type: payload.type,
- };
- },
- },
- };
- export default Model;
|