forked from gzzfw/frontEnd/gzDyh

liuwh
2024-09-14 221546808c11df777199def992e4abbf1fc31b5a
gz-customerSystem/src/components/Layout/index.jsx
@@ -2,7 +2,7 @@
 * @Company: hugeInfo
 * @Author: ldh
 * @Date: 2022-03-11 14:22:43
 * @LastEditTime: 2023-12-25 16:10:51
 * @LastEditTime: 2024-09-11 09:42:38
 * @LastEditors: dminyi 1301963064@qq.com
 * @Version: 1.0.0
 * @Description: 外层layout
@@ -19,6 +19,17 @@
function getRolePowerApi(roleId) {
   return $$.ax.request({ url: `ctRole/getById?id=${roleId}`, type: 'get', service: 'cust' });
}
//对接单点登录,获取token
function gridLogin(data) {
   return $$.ax.request({ url: `ctAccount/gridLogin`, type: 'post', service: 'cust', isGrid: true, data });
}
// 角色选择
function switchRoleApi(data) {
   return $$.ax.request({ url: 'ctAccount/switchRole', type: 'get', data, service: 'cust' });
}
const Layout = ({ isNotNav, headerUserChange }) => {
   const location = useLocation() || {};
@@ -41,6 +52,9 @@
   // 当前选中的角色 及其 权限
   const [roleActive, setRoleActive] = useState({ id: null, powerMap: {} });
   //网格跳转请求状态
   const [gridStatus, setGridStatus] = useState(false)
   // 切换头部menu
   function handleClickHeaderMenu(values) {
      function loop(value) {
@@ -60,7 +74,7 @@
   // 切换角色
   function handleChangeRole(roleName, roleId) {
      navigate('/mediate/workbench');
      navigate('/mediate/visit/visitWorkBench');
      setRoleActive({ ...roleActive, id: roleId, roleName });
   }
@@ -97,17 +111,48 @@
      }
   }
   //请求网格token
   const getGridToken = async (token) => {
      const res = await gridLogin({
         token
      })
      if (res.type) {
         setGridStatus(true)
         $$.setSessionStorage('gridToken', '')
         $$.setSessionStorage('customerSystemToken', res.data?.token);
         $$.setLocal('customerSystemUser', res.data || {});
         let ctUseroleList = res.data?.ctUseroleList || [];
         const roleId = ctUseroleList[0]?.roleId
         const roleName = ctUseroleList[0]?.roleName
         const roleCode = ctUseroleList[0]?.roleCode
         $$.setSessionStorage('role', { roleId: roleId, roleName: roleName });
         setRoleActive({ id: roleId, roleName: roleName, powerMap: {} });
         const res = await switchRoleApi({ roleCode: roleCode });
         if (res.type) {
            $$.setSessionStorage('customerSystemToken', res.data);
            navigate('/mediate/visit/visitWorkBench');
         }
      }
   }
   // 初始化 格式化数据适应菜单栏
   useEffect(() => {
      let role = $$.getSessionStorage('role');
      if (role) {
         setRoleActive({ id: role.roleId, roleName: role.roleName, powerMap: {} });
         return;
      }
      let customerSystemUser = $$.getLocal('customerSystemUser');
      let ctUseroleList = customerSystemUser?.ctUseroleList || [];
      if (ctUseroleList[0]?.roleId) {
         setRoleActive({ id: ctUseroleList[0]?.roleId, roleName: ctUseroleList[0]?.roleName, powerMap: {} });
      //单点登录,网格传一个token在session,如果有就请求接口
      const gridToken = $$.getSessionStorage('gridToken');
      if (gridToken) {
         getGridToken(gridToken)
      } else {
         setGridStatus(true)
         let role = $$.getSessionStorage('role');
         if (role) {
            setRoleActive({ id: role.roleId, roleName: role.roleName, powerMap: {} });
            return;
         }
         let customerSystemUser = $$.getLocal('customerSystemUser');
         let ctUseroleList = customerSystemUser?.ctUseroleList || [];
         if (ctUseroleList[0]?.roleId) {
            setRoleActive({ id: ctUseroleList[0]?.roleId, roleName: ctUseroleList[0]?.roleName, powerMap: {} });
         }
      }
   }, []);
@@ -117,28 +162,29 @@
      async function getRolePower(roleId, roleName) {
         global.setSpinning(true);
         const res = await getRolePowerApi(roleId);
         console.log(res, 'res1111')
         global.setSpinning(false);
         if (res.type) {
            let resData = res.data?.roleTreeBOS || [];
            let menu_data = [];
            let power_data = [];
            resData.forEach((x) => {
               if (x.appClient === '1' && x.powerTag !== 'pay') {
               if (x.appClient === 1 && x.powerTag !== 'pay') {
                  menu_data.push(x);
               }
               if (x.appClient === '1' || x.powerTag === 'pay') {
               if (x.appClient === 1 || x.powerTag === 'pay') {
                  power_data.push(x);
               }
            });
            let powerMap = setPowerDataMap(power_data);
            function loop(data, arr) {
               data.forEach((x) => {
                  if (x.powerUrl && powerMap[x.powerUrl]?.show) {
                     arr.push(x.powerUrl);
                  }
                  if ( x.powerUrl && powerMap[x.powerUrl]?.update) {
                  if (x.powerUrl && powerMap[x.powerUrl]?.update) {
                     arr.push(x.powerUrl);
                     console.log(powerMap[x.powerUrl]?.update, 'res111111111');
                  }
@@ -172,11 +218,11 @@
            headerUserChange={headerUserChange}
            handleChangeRole={handleChangeRole}
         />
         {isNotNav ? (
         {gridStatus && isNotNav ? (
            <main className="layout-main" id="layoutMain">
               <Outlet />
            </main>
         ) : (
         ) : gridStatus ? (
            <main className="layout-main" id="layoutMain" ref={layoutRef}>
               <Navigation powerMap={roleActive.powerMap} roleActive={roleActive} sideMenu={sideMenu} />
               <main id="layoutChild" className="layout-child">
@@ -184,7 +230,8 @@
               </main>
               <SOP />
            </main>
         )}
         ) : <></>
         }
      </>
   );
};