/* * @Author: cclu * @Date: 2021-12-13 11:01:23 * @LastEditors: cclu 1106109051@qq.com * @LastEditTime: 2024-09-12 09:22:47 * @FilePath: \cloud-platform\src\models\login.ts * @Description: 登录redux */ import type { Reducer, Effect } from 'umi'; import { history } from 'umi'; import { accountLogin } from '@/services/login'; import { setAuthority } from '@/utils/authority'; import { message } from 'antd'; import session from '@/utils/session'; import { wrapTreeNode } from '@/utils/format'; export type StateType = { status?: 'ok' | 'error'; type?: string; currentAuthority?: 'user' | 'guest' | 'admin'; }; export type LoginModelType = { namespace: string; state: StateType; effects: { login: Effect; logout: Effect; }; reducers: { changeLoginStatus: Reducer; }; subscriptions: { } }; const Model: LoginModelType = { namespace: 'login', state: { status: undefined, }, effects: { // 登录方法 * login({ payload }, { call, put }) { const response = yield call(accountLogin, payload); console.log('responseresponseresponseresponse', response); yield put({ type: 'changeLoginStatus', payload: response, }); // Login successfully if (response.status === 'ok') { // yield put({ type: 'global/getMenuData', payload: response.currentUser.ID }); // 生成左侧菜单 // 这里拿一下用户信息 顺便判断一下 当前用户 是不是密码等级过低 if (!response?.passwordRes) { message.error('密码过于简单,请重新设置密码!') history.push('/user/forgetPassword'); return } yield put({ type: 'user/saveCurrentUser', payload: { ...response.currentUser } }); // 存储用户信息 message.success('🎉 🎉 🎉 登录成功!'); if (!history) return; const { query } = history.location; const { redirect } = query as { redirect: string; }; if (response.currentUser.UserPattern === 3000) { // 如果是游客,则跳转至个人中心页面 history.push(redirect || '/account/center') } else if (response.currentUser.UserPattern === 1000) { const nodeMenu: any = response.currentUser.UserModuleList let menu: any = [] if (nodeMenu && nodeMenu.length > 0) { menu = wrapTreeNode(nodeMenu) } // 是业主 判断有没有权限跳转到审批流程的那个页面 let isHave: boolean = false if (menu && menu.length > 0) { menu.forEach((item: any) => { if (item.SYSTEMMODULE_URL === '/examine/monthSearch') { isHave = true } if (item.SystemModuleList && item.SystemModuleList.length > 0) { item.SystemModuleList.forEach((subItem: any) => { if (subItem.SYSTEMMODULE_URL === '/examine/monthSearch') { isHave = true } }) } }) } if (isHave) { history.push('/examine/monthSearch', 'login'); } else { history.push('/'); } } else { // 如果是商户 跳转至工作台 history.push(redirect || (response.currentUser.UserPattern === 2000 ? '/merchantManagement/workplace' : '/')); // 2024-08-16 更新 商户跳转项目结算审批页面 // history.push(redirect || (response.currentUser.UserPattern === 2000 ? '/busniessproject/reports/settlementAccount' : '/')); } } else { message.error(response.status); } }, *logout(_, { put }) { // 退出登录 yield put({ type: 'global/changeTabsRoutes', payload: { data: [], action: 'removeAll' }, }) // Note: There may be security issues, please note const { query = {} } = history.location; const { redirect } = query; // Note: There may be security issues, please note if (window.location.pathname !== '/user/login' && !redirect) { history.replace({ pathname: '/user/login', }); } session.clearAll(); }, }, reducers: { changeLoginStatus(state, { payload }) { setAuthority(payload?.currentUser.UserPattern); return { ...state, status: payload.status, type: payload.type, }; }, } }; export default Model;