xf-web/src/models/auth/_.ts

86 lines
2.0 KiB
TypeScript

import { message } from "antd";
import { MenuItem } from "../_/defs";
export type LoginUser = {
id: number;
loginName: string;
name: string;
roleList: string[];
tokenInfo: {
tokenValue: string;
};
}
export type AuthState = {
user: LoginUser | null | 'failed';
menu: MenuItem[];
}
export const USER_SESSION_KEY = '__usereinfo__';
export function removeLoginInfo() {
sessionStorage.removeItem(USER_SESSION_KEY);
sessionStorage.removeItem('TOKEN');
}
export function setLoginInfo(userInfo: string, token: string) {
sessionStorage.setItem(USER_SESSION_KEY, userInfo);
sessionStorage.setItem('TOKEN', token);
}
export function getUserFromSession(): LoginUser | null {
const strUser = sessionStorage.getItem(USER_SESSION_KEY);
if (!strUser) {
return null;
}
try {
const obj: LoginUser = JSON.parse(strUser);
if (obj.id && obj.tokenInfo.tokenValue) {
return obj;
}
} catch (e) { }
return null
}
export async function regByToken(token: string): Promise<LoginUser | undefined> {
const result: LoginUser | null = {
id: 1,
loginName: 'admin',
name: '管理员',
roleList: [],
tokenInfo: {
tokenValue: 'demo-token'
},
}
setLoginInfo(JSON.stringify(result), result.tokenInfo?.tokenValue);
return result;
}
export async function login(form: { username: string, password: string }): Promise<LoginUser | undefined> {
message.error('登陆失败');
return;
}
export function loadMenu(user: LoginUser): MenuItem[] {
const id = (() => {
let id = 1;
return () => `${id++}`
})();
return [
{ id: id(), title: '综合首页', path: '/mgr/zhsy', icon:"zhsy-icon.png" },
{ id: id(), title: '水源管理', path: '/mgr/sygl', icon:"szygl-icon.png" },
{ id: id(), title: '智慧水厂 ', path: '/mgr/zhsc', icon:"zhsc-icon.png" },
{ id: id(), title: '智慧管网', path: '/mgr/zhgw', icon:"zhgw-icon.png" },
{ id: id(), title: '用户服务', path: '/mgr/yhfw', icon:"yhfw-icon.png" },
].filter(Boolean);
}
export function defaultHomePage() {
return '/mgr/stinfo';
}