Kaynağa Gözat

fix:添加项目切换

zhaojing 2 yıl önce
ebeveyn
işleme
2e206029dd

+ 7 - 5
src/app.tsx

@@ -6,7 +6,7 @@ import {
   reFreshCheckToken,
   getUsersInfo,
 } from '@/services/sagacare_service/environment';
-import { projectObj } from '@/config/api.js';
+// import { projectObj } from '@/config/api.js';
 import UserStorage from '@/config/sagacare/sagacare_user';
 const loginPath = '/noAuth';
 
@@ -22,7 +22,7 @@ export async function getInitialState(): Promise<{
   // settings?: Partial<LayoutSettings>;
   currentUser?: API.CurrentUser;
   fetchUser?: () => Promise<API.CurrentUser | undefined>;
-  projectObj?: any;
+  projectId?: any;
   //loading?: boolean;
   //fetchUserInfo?: () => Promise<API.CurrentUser | undefined>;
 }> {
@@ -31,7 +31,7 @@ export async function getInitialState(): Promise<{
   const checkUser = async () => {
     //获取用户信息 查看权限
     try {
-      var userres = await getUsersInfo();
+      var userres = await getUsersInfo(); //通过token获取用户信息
     } catch (err) {
       history.push('/login');
       return {};
@@ -49,6 +49,7 @@ export async function getInitialState(): Promise<{
           name: userres.username,
           phone: '',
           id: userres.userId,
+          projects: userres.projects,
         };
         var userObj = UserStorage.getInstance();
         userObj.setUser(currentUser);
@@ -114,11 +115,12 @@ export async function getInitialState(): Promise<{
       return undefined;
     }
   };
-  //debugger;
+
+  var projectId = 'Pj1101080259';
   return {
     currentUser,
     fetchUser,
-    projectObj,
+    projectId,
   };
 }
 

+ 2 - 2
src/config/api.js

@@ -2,5 +2,5 @@ export const BASE_PATH = '/api';
 //export const projectId = 'Pj3301100002';//之江
 //export const projectId = 'Pj1101080259';//博锐
 //export const projectId = 'Pj1101140020';//吉因加
-export const projectObj = { projectId: 'Pj1101080259' };
-// export const projectObj = { projectId: 'Pj3301100002' };
+export const projectObj = { projectId: '' };
+// export const projectObj = { projectId: 'Pj3301100002' };

+ 38 - 5
src/layouts/index.jsx

@@ -1,11 +1,12 @@
-import React, { useState, useEffect } from 'react';
+import React, { useState, useEffect, useMemo } from 'react';
 import { useModel, useLocation, useIntl, history } from 'umi';
 import styles from './index.less';
 import { MenuFoldOutlined, SmileOutlined } from '@ant-design/icons';
 import Icon from '@/tenants-ui/SgIcon';
 
-import { Drawer, notification, Dropdown, Menu } from 'antd';
+import { Drawer, notification, Dropdown, Menu, Select } from 'antd';
 import NavMenu from '@/sagacare_components/navMenu';
+import { projectObj } from '@/config/api.js';
 
 export default (props) => {
   const { menuVisible, closeMenu, toggleMenu } = useModel('controller');
@@ -105,15 +106,47 @@ export default (props) => {
     console.log('location', location);
   }, [location]);
 
+  const [selProject, setSelProject] = useState('');
+
+  const changProjectHandle = (item) => {
+    //debugger;
+    setSelProject(item);
+    setInitialState((s) => {
+      return { ...s, projectId: item };
+    });
+  };
+
+  const projectList = useMemo(() => {
+    //debugger;
+    setSelProject(initialState?.projectId || '');
+    var projects = initialState?.currentUser?.projects || [];
+    var projects2 = projects.map((item) => {
+      return { label: item.localName, value: item.id };
+    });
+
+    return projects2;
+  }, [initialState?.currentUser]);
+
   return (
     <div className={styles.layout}>
       <div className={styles.header}>
-        <div className={styles.title}>之江项目</div>
+        <div className={styles.title}>
+          <Select
+            options={projectList}
+            placeholder="项目"
+            value={selProject}
+            onChange={changProjectHandle}
+            size="large"
+            dropdownMatchSelectWidth={true}
+            style={{ width: '100%' }}
+            bordered={false}
+          />
+        </div>
         <div className={styles.right}>
           <div className={styles.picture}>
             <Dropdown
               trigger="click"
-              overlay={
+              menu={
                 <Menu
                   style={{ width: 80 }}
                   onClick={({ key }) => {
@@ -151,7 +184,7 @@ export default (props) => {
         placement="right"
         closable={false}
         onClose={closeMenu}
-        visible={menuVisible}
+        open={menuVisible}
         drawerStyle={
           {
             //   height: 340,

+ 7 - 7
src/pages/Equipment/components/topNavRight/index.tsx

@@ -37,12 +37,12 @@ const TopNavRight: React.FC<topNavRightProps> = ({
   //去掉全部设备的 设备列表
   const [deviceList, setDeviceList] = useState<navigatorItem[]>([]);
   useEffect(() => {
-    var navigatorListCopy:navigatorItem[] = [];
-    navigatorList.forEach((item)=>{
-      if(item.id!=='all'&&item.id!=='curtain'){
+    var navigatorListCopy: navigatorItem[] = [];
+    navigatorList.forEach((item) => {
+      if (item.id !== 'all' && item.id !== 'curtain') {
         navigatorListCopy.push(item);
       }
-    })
+    });
     //console.log('navigatorListCopy', navigatorListCopy, navigatorList);
     setDeviceList(navigatorListCopy);
   }, [navigatorList]);
@@ -71,7 +71,7 @@ const TopNavRight: React.FC<topNavRightProps> = ({
       onOk() {
         //console.log('mapListmapList', mapList,selNavObj);
         //筛选 没有开启的空间
-        if (selNavObj.id == 'light'||selNavObj.id == 'socket') {
+        if (selNavObj.id == 'light' || selNavObj.id == 'socket') {
           var filterSpaceArr = mapList.filter((item) => {
             return (item[selNavObj.id] == 0 || item[selNavObj.id] == 2) && item.spaceId;
           });
@@ -129,7 +129,7 @@ const TopNavRight: React.FC<topNavRightProps> = ({
       okText: '确认',
       cancelText: '取消',
       onOk() {
-        if (selNavObj.id == 'light'||selNavObj.id == 'socket') {
+        if (selNavObj.id == 'light' || selNavObj.id == 'socket') {
           var filterSpaceArr = mapList.filter((item) => {
             return (item[selNavObj.id] == 1 || item[selNavObj.id] == 2) && item.spaceId;
           });
@@ -236,7 +236,7 @@ const TopNavRight: React.FC<topNavRightProps> = ({
             <Dropdown
               trigger="click"
               placement="bottomLeft"
-              overlay={
+              menu={
                 <Menu style={{ width: 160 }} onClick={({ key }) => allOpenClick(key)}>
                   <Menu.Item key="open">全部开启</Menu.Item>
                 </Menu>

+ 33 - 32
src/sagacare_components/topNavigator/index.tsx

@@ -5,7 +5,7 @@ import { Select } from 'antd';
 
 import type { navigatorItem } from '@/pages/Environment/index';
 import { getBuildingList, getFloorList } from '@/services/sagacare_service/environment';
-// import { projectObj } from '@/config/api.js';
+import { projectObj } from '@/config/api.js';
 import { useModel } from 'umi';
 import UserStorage from '@/config/sagacare/sagacare_user';
 
@@ -123,40 +123,41 @@ const TopNavigator: React.FC<topNavigatorProps> = ({
     var userObj = UserStorage.getInstance();
     const user = userObj.getUser();
     //console.log('TopNavigator----getBuildingList', user, projectObj.projectId);
-    if (buildList.length == 0) {
-      getBuildingList({
-        criteria: {
-          projectId: initialState?.projectObj?.projectId,
+    projectObj.projectId = initialState?.projectId;
+    //if (buildList.length == 0) {
+    getBuildingList({
+      criteria: {
+        projectId: initialState?.projectId,
+      },
+      orders: [
+        {
+          column: 'localId',
+          asc: true,
         },
-        orders: [
-          {
-            column: 'localId',
-            asc: true,
-          },
-        ],
-      }).then((res) => {
-        var resData = res.content || [];
-        setBuildList(
-          (resData || []).map((item) => {
-            return { label: item.localName, value: item.id };
-          }),
-        );
-
-        var lastBuildId2 = lastBuildId || sessionStorage.getItem('lastBuildId') || '';
-        var lastFilter = resData.filter((item) => {
-          return item.id == lastBuildId2;
-        });
+      ],
+    }).then((res) => {
+      var resData = res.content || [];
+      setBuildList(
+        (resData || []).map((item) => {
+          return { label: item.localName, value: item.id };
+        }),
+      );
 
-        var lastBuildIdTemp = '';
-        if (lastFilter.length > 0) {
-          lastBuildIdTemp = lastBuildId2;
-        }
-        //debugger;
-        setCurrentBuild(searchBuildId || lastBuildIdTemp || (resData[0] || {}).id); //设置第一个建筑值
-        //changeBuildId && changeBuildId(lastBuildId || (resData[0] || {}).id);
+      var lastBuildId2 = lastBuildId || sessionStorage.getItem('lastBuildId') || '';
+      var lastFilter = resData.filter((item) => {
+        return item.id == lastBuildId2;
       });
-    }
-  }, [initialState]);
+
+      var lastBuildIdTemp = '';
+      if (lastFilter.length > 0) {
+        lastBuildIdTemp = lastBuildId2;
+      }
+      //debugger;
+      setCurrentBuild(searchBuildId || lastBuildIdTemp || (resData[0] || {}).id); //设置第一个建筑值
+      //changeBuildId && changeBuildId(lastBuildId || (resData[0] || {}).id);
+    });
+    //}
+  }, [initialState?.projectId]);
 
   //请求楼层列表的接口
   useEffect(() => {