巴林闲侠 3 years ago
parent
commit
f7f9b309f0
  1. 226
      web/client/src/layout/components/sider/index.jsx
  2. 459
      web/client/src/layout/containers/layout/index.jsx
  3. 4
      web/client/src/sections/auth/containers/login.jsx
  4. 12
      web/client/src/sections/example/nav-item.jsx
  5. 12
      web/client/src/sections/example1/nav-item.jsx

226
web/client/src/layout/components/sider/index.jsx

@ -9,133 +9,117 @@ import "./index.less";
let scrollbar = null let scrollbar = null
const homePath = '/example/e1/c1' const homePath = '/example/e1/c1'
const Sider = (props) => { const Sider = (props) => {
const { collapsed, clientHeight, dispatch, pathname,leftItems,leftChange } = props const { collapsed, clientHeight, dispatch, pathname, leftItems, leftChange } = props
const [items, setItems] = useState([]) const [items, setItems] = useState([])
const [selectedKeys, setSelectedKeys] = useState([]) const [selectedKeys, setSelectedKeys] = useState([])
const [openKeys, setOpenKeys] = useState([]) const [openKeys, setOpenKeys] = useState([])
useEffect(() => { useEffect(() => {
const { sections, dispatch, user } = props; const { sections, dispatch, user } = props;
let nextItems = leftItems let nextItems = leftItems
let pathname_ = pathname == '/' ? homePath : pathname // let pathname_ = pathname == '/' ? homePath : pathname
const initKeys = (items, lastKeys) => { // const initKeys = (items, lastKeys) => {
console.log('1111111',items); // // console.log('1111111',items);
for (let it of items) { // for (let it of items) {
if (it.to && it.to == pathname_) { // if (it.to && it.to == pathname_) {
lastKeys.selectedKeys.push(it.itemKey) // // console.log('22222222222',it.itemKey);
return lastKeys // lastKeys.selectedKeys.push(it.itemKey)
} else if (it.items && it.items.length) { // return lastKeys
const preLastKeys = JSON.parse(JSON.stringify(lastKeys)) // } else if (it.items && it.items.length) {
preLastKeys.openKeys.push(it.itemKey) // const preLastKeys = JSON.parse(JSON.stringify(lastKeys))
const nextKeys = initKeys(it.items, JSON.parse(JSON.stringify(preLastKeys))) // preLastKeys.openKeys.push(it.itemKey)
if (nextKeys.selectedKeys.length > preLastKeys.selectedKeys.length || nextKeys.openKeys.length > preLastKeys.openKeys.length) { // const nextKeys = initKeys(it.items, JSON.parse(JSON.stringify(preLastKeys)))
return nextKeys // if (nextKeys.selectedKeys.length > preLastKeys.selectedKeys.length || nextKeys.openKeys.length > preLastKeys.openKeys.length) {
} // return nextKeys
} // }
} // }
return lastKeys // }
return { // return lastKeys
selectedKeys: [], // return {
openKeys: [] // selectedKeys: [],
} // openKeys: []
} // }
setItems(nextItems) // }
const { selectedKeys, openKeys } = initKeys( setItems(nextItems)
nextItems, // const { selectedKeys, openKeys } = initKeys(
{ // nextItems,
selectedKeys: [], // {
openKeys: [] // selectedKeys: [],
} // openKeys: []
) // }
if (selectedKeys.length || openKeys.length) { // )
setSelectedKeys(selectedKeys) // if (selectedKeys.length || openKeys.length) {
setOpenKeys(openKeys) // setSelectedKeys(selectedKeys)
} else { // setOpenKeys(openKeys)
const lastSelectedKeys = localStorage.getItem('vcmp_selected_sider') // } else {
if (lastSelectedKeys) { // const lastSelectedKeys = localStorage.getItem('vcmp_selected_sider')
setSelectedKeys(JSON.parse(lastSelectedKeys)) // if (lastSelectedKeys) {
} // setSelectedKeys(JSON.parse(lastSelectedKeys))
const lastOpenKeys = localStorage.getItem('vcmp_open_sider') // }
if (lastOpenKeys) { // const lastOpenKeys = localStorage.getItem('vcmp_open_sider')
setOpenKeys(JSON.parse(lastOpenKeys)) // if (lastOpenKeys) {
} // setOpenKeys(JSON.parse(lastOpenKeys))
} // }
// }
scrollbar = new PerfectScrollbar('#page-slider', { suppressScrollX: true }); scrollbar = new PerfectScrollbar('#page-slider', { suppressScrollX: true });
if (pathname == '/') { if (pathname == '/') {
dispatch(push(homePath)) dispatch(push(homePath))
} }
}, [leftItems]) }, [leftItems])
let routeSelectedKey = [useLocation().pathname.split('/')[1]]// let routeSelectedKey = [useLocation().pathname.split('/')[1]]//
let routeSelectedKeys = [useLocation().pathname.split('/')[2]]// let routeSelectedKeys = [useLocation().pathname.split('/')[2]]//
let routeSelectedKeyss = [useLocation().pathname.split('/')[3]]// let routeSelectedKeyss = [useLocation().pathname.split('/')[3]]//
useEffect(()=>{ useEffect(() => {
let pathname_ = pathname == '/' ? homePath : pathname if (routeSelectedKeyss[0]) {
const initKeys = (items, lastKeys) => { setSelectedKeys(routeSelectedKeyss)
for (let it of items) { }
if (it.to && it.to == pathname_) { else if (routeSelectedKeys[0]) {
lastKeys.selectedKeys.push(it.itemKey) setSelectedKeys(routeSelectedKeys)
return lastKeys }
} else if (it.items && it.items.length) { else {
const preLastKeys = JSON.parse(JSON.stringify(lastKeys)) setSelectedKeys(routeSelectedKey)
preLastKeys.openKeys.push(it.itemKey) }
const nextKeys = initKeys(it.items, JSON.parse(JSON.stringify(preLastKeys))) const lastOpenKeys = localStorage.getItem('vcmp_open_sider')
if (nextKeys.selectedKeys.length > preLastKeys.selectedKeys.length || nextKeys.openKeys.length > preLastKeys.openKeys.length) { if (lastOpenKeys) {
return nextKeys setOpenKeys(JSON.parse(lastOpenKeys))
} }
} }, [window.localStorage.vcmp_open_sider, window.localStorage.vcmp_selected_sider, leftChange])
}
return lastKeys
}
if(routeSelectedKeyss[0]){
setSelectedKeys(routeSelectedKeyss)
}
else if(routeSelectedKeys[0]){
setSelectedKeys(routeSelectedKeys)
}
else{
setSelectedKeys(routeSelectedKey)
}
const lastOpenKeys = localStorage.getItem('vcmp_open_sider')
if (lastOpenKeys) {
setOpenKeys(JSON.parse(lastOpenKeys))
}
},[window.localStorage.vcmp_open_sider,window.localStorage.vcmp_selected_sider,leftChange])
useEffect(() => { useEffect(() => {
if (scrollbar) { if (scrollbar) {
scrollbar.update(); scrollbar.update();
} }
}) })
return ( return (
<div id={'page-slider'} style={{ height: clientHeight, position: 'relative',background:'#101531',width:180 }}> <div id={'page-slider'} style={{ height: clientHeight, position: 'relative', background: '#101531', width: 180 }}>
<Nav <Nav
style={{background:'#101531',width:180,padding:0}} style={{ background: '#101531', width: 180, padding: 0 }}
selectedKeys={selectedKeys} selectedKeys={selectedKeys}
openKeys={openKeys} openKeys={openKeys}
onSelect={({ selectedItems, selectedKeys, }) => { onSelect={({ selectedItems, selectedKeys, }) => {
const selectItem = selectedItems[0] const selectItem = selectedItems[0]
if (selectItem.to) { if (selectItem.to) {
dispatch(push(selectItem.to)) dispatch(push(selectItem.to))
} }
setSelectedKeys(selectedKeys) setSelectedKeys(selectedKeys)
localStorage.setItem('vcmp_selected_sider', JSON.stringify(selectedKeys)) localStorage.setItem('vcmp_selected_sider', JSON.stringify(selectedKeys))
}} }}
onOpenChange={({ openKeys }) => { onOpenChange={({ openKeys }) => {
setOpenKeys(openKeys) setOpenKeys(openKeys)
localStorage.setItem('vcmp_open_sider', JSON.stringify(openKeys)) localStorage.setItem('vcmp_open_sider', JSON.stringify(openKeys))
}} }}
items={items} items={items}
/> />
</div> </div>
) )
} }
function mapStateToProps (state) { function mapStateToProps (state) {
const { global } = state; const { global } = state;
return { return {
clientHeight: global.clientHeight, clientHeight: global.clientHeight,
}; };
} }
export default connect(mapStateToProps)(Sider); export default connect(mapStateToProps)(Sider);

459
web/client/src/layout/containers/layout/index.jsx

@ -11,7 +11,7 @@ import * as NProgress from 'nprogress';
import PerfectScrollbar from 'perfect-scrollbar'; import PerfectScrollbar from 'perfect-scrollbar';
NProgress.configure({ NProgress.configure({
template: ` template: `
<div class="bar" style="height:2px" role="bar"> <div class="bar" style="height:2px" role="bar">
<div class="peg"></div> <div class="peg"></div>
</div> </div>
@ -24,250 +24,251 @@ NProgress.configure({
let scrollbar let scrollbar
const LayoutContainer = props => { const LayoutContainer = props => {
const { const {
dispatch, msg, user, copyright, children, sections, clientWidth, clientHeight, dispatch, msg, user, copyright, children, sections, clientWidth, clientHeight,
location, match, routes, history, socket, location, match, routes, history, socket,
} = props } = props
const [collapsed, setCollapsed] = useState(false) const [collapsed, setCollapsed] = useState(false)
NProgress.start(); NProgress.start();
const resize_ = () => { const resize_ = () => {
dispatch(resize( dispatch(resize(
document.getElementById('PomsApp').clientHeight, document.getElementById('PomsApp').clientHeight,
document.getElementById('PomsApp').clientWidth - (collapsed ? 120 : 240) document.getElementById('PomsApp').clientWidth - (collapsed ? 120 : 240)
)); ));
} }
function deepCopy(data) {// function deepCopy (data) {//
//string,number,bool,null,undefined,symbol //string,number,bool,null,undefined,symbol
//object,array,date //object,array,date
if (data && typeof data === "object") { if (data && typeof data === "object") {
// //
if (typeof data === "function") { if (typeof data === "function") {
let tempFunc = data.bind(null); let tempFunc = data.bind(null);
tempFunc.prototype = deepCopy(data.prototype); tempFunc.prototype = deepCopy(data.prototype);
return tempFunc; return tempFunc;
} }
switch (Object.prototype.toString.call(data)) { switch (Object.prototype.toString.call(data)) {
case "[object String]": case "[object String]":
return data.toString(); return data.toString();
case "[object Number]": case "[object Number]":
return Number(data.toString()); return Number(data.toString());
case "[object Boolean]": case "[object Boolean]":
return new Boolean(data.toString()); return new Boolean(data.toString());
case "[object Date]": case "[object Date]":
return new Date(data.getTime()); return new Date(data.getTime());
case "[object Array]": case "[object Array]":
var arr = []; var arr = [];
for (let i = 0; i < data.length; i++) { for (let i = 0; i < data.length; i++) {
arr[i] = deepCopy(data[i]); arr[i] = deepCopy(data[i]);
} }
return arr; return arr;
//js //js
case "[object Object]": case "[object Object]":
var obj = {}; var obj = {};
for (let key in data) { for (let key in data) {
//hasOwnProperty obj.hasOwnProperty(prop) //hasOwnProperty obj.hasOwnProperty(prop)
obj[key] = deepCopy(data[key]); obj[key] = deepCopy(data[key]);
} }
return obj; return obj;
} }
} else { } else {
//string,number,bool,null,undefined,symbol //string,number,bool,null,undefined,symbol
return data; return data;
}
} }
} const [allItems, setAllItems] = useState([])
const [allItems, setAllItems] = useState([]) const [headerItems, setHeaderItems] = useState([])
const [headerItems, setHeaderItems] = useState([]) const [leftItems, setLeftItems] = useState([])
const [leftItems, setLeftItems] = useState([]) const [leftChange, setLeftChange] = useState(true)
const [leftChange, setLeftChange] = useState(true) const [leftShow, setLeftShow] = useState(false)
const [leftShow, setLeftShow] = useState(false) useEffect(() => {
useEffect(() => { let topItems = []//
let topItems = []// // let siderItems = []//
// let siderItems = []// let nextItems = []//
let nextItems = []// for (let c of sections) {
for (let c of sections) { if (typeof c.getNavItem == 'function') {
if (typeof c.getNavItem == 'function') { let item = c.getNavItem(user, dispatch);
let item = c.getNavItem(user, dispatch); if (item) {
if (item) { nextItems.push.apply(nextItems, item)
nextItems.push.apply(nextItems, item) if (item.length > 0) {
if (item.length > 0) { for (let j = 0; j < item.length; j++) {
let itm = deepCopy(item[0]); let itm = deepCopy(item[j]);
let itms=deepCopy(item[0]); if (itm.hasOwnProperty('items')) {
if (itm.hasOwnProperty('items')) { for (let i = 0; i < itm.items.length; i++) {
// siderItems.push.apply(siderItems, itms.items) itm.items[i].fatherKey=itm.itemKey
for (let i = 0; i < itm.items.length; i++) { delete itm.items[i].items
delete itm.items[i].items }
} topItems.push(itm)
topItems.push(itm) }
} else {
else { topItems.push.apply(topItems, item)
topItems.push.apply(topItems, item) }
} }
} }
}
} }
} }
} setLeftShow(false)
setLeftShow(false) setAllItems(nextItems)
setAllItems(nextItems) setHeaderItems(topItems)
setHeaderItems(topItems) // setLeftItems(siderItems)
// setLeftItems(siderItems)
window.addEventListener('resize', resize_); window.addEventListener('resize', resize_);
return () => { return () => {
window.removeEventListener('resize', resize_); window.removeEventListener('resize', resize_);
} }
}, []) }, [])
useEffect(() => { useEffect(() => {
NProgress.done(); NProgress.done();
if ((!user || !user.authorized)) { if ((!user || !user.authorized)) {
history.push('/signin'); history.push('/signin');
} }
if (msg) { if (msg) {
if (msg.done) { if (msg.done) {
Notification.success({ Notification.success({
// title: msg.done, // title: msg.done,
content: msg.done, content: msg.done,
duration: 2, duration: 2,
}) })
} }
if (msg.error) { if (msg.error) {
Notification.error({ Notification.error({
// title: msg.error, // title: msg.error,
content: msg.error, content: msg.error,
duration: 2, duration: 2,
}) })
} }
} }
const dom = document.getElementById('page-content'); const dom = document.getElementById('page-content');
if (dom) { if (dom) {
if (!scrollbar) { if (!scrollbar) {
scrollbar = new PerfectScrollbar('#page-content', { suppressScrollX: true }); scrollbar = new PerfectScrollbar('#page-content', { suppressScrollX: true });
scrollbar.update(); scrollbar.update();
} else { } else {
scrollbar.update(); scrollbar.update();
dom.scrollTop = 0; dom.scrollTop = 0;
} }
} }
}) })
// websocket 使 // websocket 使
useEffect(() => { useEffect(() => {
console.log(socket) console.log(socket)
if (socket) { if (socket) {
socket.on('CAMERA_ONLINE', function (msg) { socket.on('CAMERA_ONLINE', function (msg) {
console.info(msg); console.info(msg);
if (msg.online == 'ON') { if (msg.online == 'ON') {
Notification.success({ Notification.success({
title: 'Hi', title: 'Hi',
content: (<div><div>{msg.name}</div><div style={{ marginTop: 5 }}>已上线</div></div>), content: (<div><div>{msg.name}</div><div style={{ marginTop: 5 }}>已上线</div></div>),
duration: 2, duration: 2,
}) })
}
if (msg.online == 'OFF') {
Notification.error({
title: 'Hi',
content: (<div><div>{msg.name}</div><div style={{ marginTop: 5 }}>发生离线</div></div>),
duration: 2,
})
}
});
return () => {
socket.off("CAMERA_ONLINE");
} }
if (msg.online == 'OFF') { }
Notification.error({ }, [socket])
title: 'Hi',
content: (<div><div>{msg.name}</div><div style={{ marginTop: 5 }}>发生离线</div></div>),
duration: 2,
})
}
});
return () => {
socket.off("CAMERA_ONLINE");
}
}
}, [socket])
return ( return (
<Layout id="layout" style={{ height: '100%' }}> <Layout id="layout" style={{ height: '100%' }}>
{ {
<> <>
<Layout.Header> <Layout.Header>
<Header <Header
headerItems={headerItems} headerItems={headerItems}
user={user} user={user}
pathname={location.pathname} pathname={location.pathname}
toggleCollapsed={() => { toggleCollapsed={() => {
setCollapsed(!collapsed); setCollapsed(!collapsed);
}} }}
collapsed={collapsed} collapsed={collapsed}
history={history} history={history}
tochange={(val) => { tochange={(val) => {
setLeftChange(!leftChange) setLeftChange(!leftChange)
if(val.fatherKey){ if (val.fatherKey) {
for (let i = 0; i < allItems.length; i++) { for (let i = 0; i < allItems.length; i++) {
if(val.fatherKey==allItems[i].itemKey){ if (val.fatherKey == allItems[i].itemKey) {
setLeftItems(allItems[i].items) setLeftItems(allItems[i].items)
}
}
setLeftShow(true)
} }
} else {
setLeftShow(true) setLeftShow(false)
} }
else{ history.push(val.to);
setLeftShow(false) }}
} />
history.push(val.to); </Layout.Header>
}} <Layout style={{ height: 'calc(100% - 60px)' }}>
/> {leftShow ? (<Layout.Sider>
</Layout.Header> <Sider
<Layout style={{ height: 'calc(100% - 60px)' }}> sections={sections}
{leftShow?(<Layout.Sider> leftItems={leftItems}
<Sider dispatch={dispatch}
sections={sections} user={user}
leftItems={leftItems} leftChange={leftChange}
dispatch={dispatch} pathname={location.pathname}
user={user} collapsed={collapsed}
leftChange={leftChange} />
pathname={location.pathname} </Layout.Sider>) : ('')}
collapsed={collapsed} <Layout.Content>
/> <div style={{
</Layout.Sider>):('')} margin: '12px 12px 0px',
<Layout.Content> background: "#F6FAFF",
<div style={{ }}>
margin: '12px 12px 0px', <div id="page-content" style={{
background: "#F2F3F5", height: clientHeight - 12,
}}> minWidth: 520,
<div id="page-content" style={{ position: 'relative',
height: clientHeight - 12, }}>
minWidth: 520, <div style={{
position: 'relative', minHeight: clientHeight - 32 - 12,
}}> position: 'relative',
<div style={{ padding: '12px 8px',
minHeight: clientHeight - 32 - 12, }}>
position: 'relative', {children}
padding: '12px 8px', </div>
}}> <Layout.Footer>
{children} <Footer />
</div> </Layout.Footer>
<Layout.Footer> </div>
<Footer /> </div>
</Layout.Footer> </Layout.Content>
</div> </Layout>
</div> </>
</Layout.Content> }
</Layout> </Layout >
</> )
}
</Layout >
)
} }
function mapStateToProps (state) { function mapStateToProps (state) {
const { global, auth, ajaxResponse, webSocket } = state; const { global, auth, ajaxResponse, webSocket } = state;
return { return {
title: global.title, title: global.title,
copyright: global.copyright, copyright: global.copyright,
sections: global.sections, sections: global.sections,
actions: global.actions, actions: global.actions,
clientWidth: global.clientWidth, clientWidth: global.clientWidth,
clientHeight: global.clientHeight, clientHeight: global.clientHeight,
msg: ajaxResponse.msg, msg: ajaxResponse.msg,
user: auth.user, user: auth.user,
socket: webSocket.socket socket: webSocket.socket
}; };
} }
export default connect(mapStateToProps)(LayoutContainer); export default connect(mapStateToProps)(LayoutContainer);

4
web/client/src/sections/auth/containers/login.jsx

@ -20,9 +20,7 @@ const Login = props => {
useEffect(() => { useEffect(() => {
if (user && user.authorized) { if (user && user.authorized) {
dispatch(push('/example/e1/c1')); dispatch(push('/console'));
localStorage.setItem('vcmp_selected_sider', JSON.stringify(['nvr']))
localStorage.setItem('vcmp_open_sider', JSON.stringify(['equipmentWarehouse']))
} }
}, [user]) }, [user])

12
web/client/src/sections/example/nav-item.jsx

@ -14,7 +14,6 @@ export function getNavItem (user, dispatch) {
text: '举个棒子1', text: '举个棒子1',
icon: <IconCode />, icon: <IconCode />,
to: '/example/e1/c1', to: '/example/e1/c1',
fatherKey:'example',
items: [{ items: [{
itemKey: 'c1', to: '/example/e1/c1', text: '举个锤子1' itemKey: 'c1', to: '/example/e1/c1', text: '举个锤子1'
}] }]
@ -23,7 +22,6 @@ export function getNavItem (user, dispatch) {
text: '举个棒子2', text: '举个棒子2',
icon: <IconCode />, icon: <IconCode />,
to: '/example/e2/c2', to: '/example/e2/c2',
fatherKey:'example',
items: [{ items: [{
itemKey: 'c2', to: '/example/e2/c2', text: '举个锤子2' itemKey: 'c2', to: '/example/e2/c2', text: '举个锤子2'
}] }]
@ -31,15 +29,5 @@ export function getNavItem (user, dispatch) {
] ]
}, },
] ]
// ,[
// {
// itemKey: 'equipmentWarehouse', text: '', icon:<iconpark-icon style={{width:20,height:20}} name="she-1"></iconpark-icon>,
// items: [
// { itemKey: 'nvr', to: '/equipmentWarehouse/nvr', text: 'NVR' },
// { itemKey: 'camera', to: '/equipmentWarehouse/camera', text: '' },
// { itemKey: 'recycle', to: '/equipmentWarehouse/recycle', text: '' },
// ]
// },
// ]
); );
} }

12
web/client/src/sections/example1/nav-item.jsx

@ -14,7 +14,6 @@ export function getNavItem (user, dispatch) {
text: '举个棒子3', text: '举个棒子3',
icon: <IconCode />, icon: <IconCode />,
to: '/example/e3/c3', to: '/example/e3/c3',
fatherKey:'example1',
items: [{ items: [{
itemKey: 'c3', to: '/example/e3/c3', text: '举个锤子3' itemKey: 'c3', to: '/example/e3/c3', text: '举个锤子3'
}] }]
@ -23,7 +22,6 @@ export function getNavItem (user, dispatch) {
text: '举个棒子4', text: '举个棒子4',
icon: <IconCode />, icon: <IconCode />,
to: '/example/e4/c4', to: '/example/e4/c4',
fatherKey:'example1',
items: [{ items: [{
itemKey: 'c4', to: '/example/e4/c4', text: '举个锤子4' itemKey: 'c4', to: '/example/e4/c4', text: '举个锤子4'
}] }]
@ -31,15 +29,5 @@ export function getNavItem (user, dispatch) {
] ]
}, },
] ]
// ,[
// {
// itemKey: 'equipmentWarehouse', text: '', icon:<iconpark-icon style={{width:20,height:20}} name="she-1"></iconpark-icon>,
// items: [
// { itemKey: 'nvr', to: '/equipmentWarehouse/nvr', text: 'NVR' },
// { itemKey: 'camera', to: '/equipmentWarehouse/camera', text: '' },
// { itemKey: 'recycle', to: '/equipmentWarehouse/recycle', text: '' },
// ]
// },
// ]
); );
} }
Loading…
Cancel
Save