12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- import { routerRedux } from 'dva/router';
- import { accountLogin, accountLogout } from '../services/user';
- import { setAuthority, setLocalUser } from '../utils/authority';
- import { reloadAuthorized } from '../utils/Authorized';
- export default {
- namespace: 'login',
- state: {
- status: undefined,
- },
- effects: {
- *login({ payload }, { call, put }) {
- const response = yield call(accountLogin, payload);
- yield put({
- type: 'changeLoginStatus',
- payload: response,
- });
- // Login successfully
- if (response.success) {
- reloadAuthorized();
- yield put(routerRedux.push('/dashboard/sold'));
- }
- },
- *logout(_, { call, put }) {
- const response = yield call(accountLogout);
- if (response.success) {
- reloadAuthorized();
- yield put(routerRedux.push('/user/login'));
- }
- },
- },
- reducers: {
- changeLoginStatus(state, { payload }) {
- if (payload.data) {
- // 将用户信息存储到localStorage
- setLocalUser(payload.data);
- // 分析登录用户的角色
- const { id, cp, merchant, platForm } = payload.data;
- let userType = '';
- if (platForm && id === '1') {
- userType = 'admin';
- } else if (platForm && id !== '1') {
- userType = 'platform';
- } else if (cp) {
- userType = 'cp';
- } else if (merchant) {
- userType = 'channel';
- }
- // 设置用户权限
- setAuthority(userType);
- }
- return {
- ...state,
- status: payload.success,
- repMsg: payload.message,
- };
- },
- },
- };
|