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

300 lines
8.3 KiB
TypeScript
Raw Normal View History

2024-12-10 10:28:55 +08:00
import { message } from 'antd'
import { config } from '../../config'
import { wait } from '../../utils/common'
import { httpget, httppost, xyt_httpget2 } from '../../utils/request'
import { MenuItem } from '../_'
import { apiPaths } from '../_/apipath'
import apiurl from '../../service/apiurl'
import { store } from '../store'
// @ts-ignore
import cookie from 'react-cookies'
export type LoginUser = {
id: number
loginName: string
name: string
roleList: string[]
tokenInfo: {
tokenValue: string
}
}
export type AuthState = {
user: LoginUser | null | -1
menu: MenuItem[]
roleObj: any,
role: any
}
export const USER_SESSION_KEY = '__usereinfo__'
export function removeLoginInfo() {
let keysToKeep = ["checked", "loginNamePwd"];
let keysToRemove = [];
// 遍历 localStorage
for (let i = 0; i < localStorage.length; i++) {
let key: any = localStorage.key(i);
if (!keysToKeep.includes(key)) {
keysToRemove.push(key);
}
}
for (let i = 0; i < keysToRemove.length; i++) {
localStorage.removeItem(keysToRemove[i]);
}
/*localStorage.removeItem(USER_SESSION_KEY);
localStorage.removeItem('TOKEN');*/
// localStorage.clear()
sessionStorage.clear()
cookie.remove('Admin-Token')
}
export function setLoginInfo(userInfo: string, token: string) {
sessionStorage.setItem(USER_SESSION_KEY, userInfo)
localStorage.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 = await httppost(
apiPaths.auth.registerByToken,
{ token }
)
if (!result) {
message.error('登陆失败')
return
}
setLoginInfo(JSON.stringify(result), result.tokenInfo?.tokenValue)
return result
}
//user: string, pw: string, captcha: string
/*export async function login(form: { user: string, pw: string }): Promise<LoginUser | undefined> {
const result = await httppost('/shzh/'+store.getState().systemSwitch.address+apiurl.upgramurl.login.authOld, {
user: form.user,
pw: form.pw,
//captcha: form.captcha
});
if(result.code === 200){
localStorage.setItem('TOKEN', result.data.token);
localStorage.setItem('account', result.data.name);
}else{
message.error('登陆失败');
//return;
}
//setLoginInfo(JSON.stringify(result), result.data.token?.token);
return result;
}*/
function idgen() {
let id = 1
return () => `${id++}`
}
const handelTreeData = (data: any, id: any) => {
if (data.length > 0) {
data.forEach((item: any) => {
item.id = id()
item.title = item.menuName;
item.icon = item.icon;
if (item.menuType === 'M' && item.children && item.children.length > 0) {//目录
item.redirect = item.path;
delete item.path
handelTreeData(item.children, id);
}
if (item.menuType === 'C') {//菜单
item.rule = item.children;
delete item.children
}
});
}
}
const buildTree = (data: any, parentId: any) => {
let tree: any = [];
data.forEach((node: any) => {
if (node.parentId === parentId) {
let children = buildTree(data, node.menuId);
if (children.length) {
node.children = children;
}
tree.push(node);
}
});
return tree;
}
export function loadRole(data: any) {
const roleObj: any = {}
const handelTreeData2 = (data: any) => {
if (data.length > 0) {
data.forEach((item: any) => {
if (item.children && item.children.length > 0) {
//是目录
handelTreeData2(item.children);
} else {
//是菜单
roleObj[item.path] = item
}
});
}
}
handelTreeData2(data)
return roleObj
}
export async function loadMenu(): Promise<MenuItem[]> {
await wait(200)
const { code, data } = await xyt_httpget2(apiurl.login.role)
if( code!== 200){
message.error('获取用户权限失败')
return []
}
//获取到菜单后遍历
const id = idgen()
// handelTreeData(data,id)
const tree = buildTree(data,0)
const tree2 = tree?.filter((item:any) =>(item.menuId != "1" && item.menuId != "2" && item.menuId != "3"))
handelTreeData(tree2,id)
return tree2
2024-12-10 10:28:55 +08:00
// const id = idgen()
// return [
// { id: id(), title: '首页', path: '/home', icon: 'yzt' },
// {
// id: id(), title: '监测数据', redirect: '/mgr/jcsj/jcsj', icon: 'sz',
// children: [
// {
// id: id(), title: '监测数据', path: '/mgr/jcsj/jcsj',
// },
// {
// id: id(), title: '报警管理', redirect: '/mgr/jcsj/bjgl/bjjl',
// children: [
// { id: id(), title: '报警记录', path: '/mgr/jcsj/bjgl/bjjl' },
// { id: id(), title: '报警规则配置', path: '/mgr/jcsj/bjgl/bjgzpz' },
// ]
// },
// ]
// },
// { id: id(), title: '安全监测', path: '/mgr/aqjc', icon: 'sz' },
// {
// id: id(), title: '设备维护', redirect: '/mgr/sbwh/wxyhgl/wxfabz', icon: 'sz',
// children: [
// {
// id: id(), title: '维修养护管理', redirect: '/mgr/sbwh/wxyhgl/wxfabz',
// children: [
// { id: id(), title: '维护方案编制', path: '/mgr/sbwh/wxyhgl/wxfabz' },
// { id: id(), title: '实施过程记录', path: '/mgr/sbwh/wxyhgl/ssgcjl' },
// { id: id(), title: '维护项目验收', path: '/mgr/sbwh/wxyhgl/whxmys' },
// ]
// },
// {
// id: id(), title: '设备物资管理', redirect: '/mgr/sbwh/sbwzgl/cgtzgl',
// children: [
// { id: id(), title: '采购台账管理', path: '/mgr/sbwh/sbwzgl/cgtzgl' },
// { id: id(), title: '库存核算管理', path: '/mgr/sbwh/sbwzgl/kchsgl' },
// { id: id(), title: '备品备件管理', path: '/mgr/sbwh/sbwzgl/bpbjgl' },
// { id: id(), title: '综合分析考核', path: '/mgr/sbwh/sbwzgl/zhfxkh' },
// ]
// },
// ]
// },
// {
// id: id(), title: '工程运行', redirect: '/mgr/gcyx/xxcx/jbqk', icon: 'sz',
// children: [
// {
// id: id(), title: '信息查询', redirect: '/mgr/gcyx/xxcx/jbqk',
// children: [
// { id: id(), title: '基本情况', path: '/mgr/gcyx/xxcx/jbqk' },
// { id: id(), title: '运行情况', path: '/mgr/gcyx/xxcx/yxqk' },
// { id: id(), title: '统计报表', path: '/mgr/gcyx/xxcx/tjbb' },
// { id: id(), title: '操作日志', path: '/mgr/gcyx/xxcx/czrz' },
// ]
// },
// ]
// },
// {
// id: id(), title: '系统管理', redirect: '/mgr/xtgl/yhxx', icon: 'sz',
// children: [
// {
// id: id(), title: '用户信息', path: '/mgr/xtgl/yhxx',
// },
// {
// id: id(), title: '部门管理', path: '/mgr/xtgl/bmgl',
// },
// {
// id: id(), title: '角色管理', path: '/mgr/xtgl/jsgl',
// },
// {
// id: id(), title: '菜单管理', path: '/mgr/xtgl/cdgl',
// },
// ]
// },
// ]
2024-12-10 10:28:55 +08:00
}
export function defaultHomePage() {
return '/mgr/home'
}
export async function currentBreadcrumbs() {
let url = window.location.href
let index = url.lastIndexOf('/')
let str = url.substring(index + 1, url.length)
let menuData: any = await loadMenu()
//console.log("menuData",menuData);
let breadcrumbsArray: any = []
menuData.map(function (item: any) {
if (item.children && item.children.length > 0) {
item.children.map(function (item1: any) {
if (item1.children && item1.children.length > 0) {
item1.children.map(function (item2: any) {
let index = item2.path.lastIndexOf('/')
if (str === item2.path.substring(index + 1, url.length)) {
breadcrumbsArray = [item.title, item1.title, item2.title]
}
})
} else {
let index = item1.path.lastIndexOf('/')
if (str === item1.path.substring(index + 1, url.length)) {
breadcrumbsArray = [item.title, item1.title]
}
}
})
}
})
return breadcrumbsArray
}