You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
2.9 KiB
117 lines
2.9 KiB
import type { Effect, Reducer } from 'umi';
|
|
import { history } from 'umi';
|
|
import { message } from 'antd';
|
|
import { loginByAccount } from './service';
|
|
import { getPageQuery, setAuthority } from './utils/utils';
|
|
import md5 from 'blueimp-md5';
|
|
|
|
export interface StateType {
|
|
status?: 'ok' | 'error';
|
|
type?: string;
|
|
currentAuthority?: 'user' | 'guest' | 'admin';
|
|
}
|
|
|
|
export interface ModelType {
|
|
namespace: string;
|
|
state: StateType;
|
|
effects: {
|
|
login: Effect;
|
|
getCaptcha: Effect;
|
|
};
|
|
reducers: {
|
|
changeLoginStatus: Reducer<StateType>;
|
|
};
|
|
}
|
|
|
|
const Model: ModelType = {
|
|
namespace: 'userAndlogin',
|
|
|
|
state: {
|
|
status: undefined,
|
|
},
|
|
|
|
effects: {
|
|
/* 登录函数 */
|
|
*login({ payload }, { call, put }) {
|
|
payload.password = md5(payload.password + 'ncstslat'); // 对密码进行加密处理
|
|
|
|
const tempToken = localStorage.getItem('TEMP_TOKEN');
|
|
console.log('password', payload.password);
|
|
console.log(payload);
|
|
|
|
const response = yield call(loginByAccount, {
|
|
...payload,
|
|
tempToken: tempToken,
|
|
});
|
|
|
|
// 显示登录失败错误消息
|
|
if (response.code !== 0) {
|
|
message.error(`[${response.code}]${response.msg}`);
|
|
return;
|
|
}
|
|
|
|
localStorage.setItem('USER_LOGIN', 'true');
|
|
|
|
// 将获得的Token存起来
|
|
localStorage.setItem('SYS_TOKEN', response.data);
|
|
|
|
const res = yield API.menu.getCurrentUserMenuAuthority.request();
|
|
const menuAuthority = res && Array.isArray(res.data) ? res.data : [];
|
|
|
|
// 修改登录状态
|
|
yield put({
|
|
type: 'changeLoginStatus',
|
|
payload: {
|
|
...response,
|
|
currentAuthority: menuAuthority,
|
|
status: 'ok',
|
|
},
|
|
});
|
|
|
|
// 成功登录
|
|
if (response.code === 0) {
|
|
message.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 = redirect;
|
|
return;
|
|
}
|
|
}
|
|
|
|
//history.replace(redirect || '/welcome');
|
|
history.replace('/welcome');
|
|
}
|
|
},
|
|
},
|
|
|
|
reducers: {
|
|
/**
|
|
* 更新用户状态
|
|
* @param state
|
|
* @param param1
|
|
*/
|
|
changeLoginStatus(state, { payload }) {
|
|
// 设置当前用户权限
|
|
setAuthority(payload.currentAuthority);
|
|
return {
|
|
...state,
|
|
status: payload.status,
|
|
type: payload.type,
|
|
};
|
|
},
|
|
},
|
|
};
|
|
|
|
export default Model;
|
|
|