index.jsx 12.7 KB
Newer Older
wuhao's avatar
wuhao committed
1 2 3 4 5 6 7 8 9 10 11 12 13
/* eslint-disable react-hooks/exhaustive-deps */
/* eslint-disable no-param-reassign */
import React, { useState, Suspense, useRef, useMemo } from 'react';
import { Layout, Menu, Skeleton, Input, Row, Col, Empty } from 'antd';
import { Link, history, useModel, useLocation } from '@umijs/max';
import styles from './index.less';
import { Scrollbars } from 'react-custom-scrollbars';
import { collectPerms, cancelCollectPerms, mtosave } from '@/services/login';
import * as Ant4Icons from '@ant-design/icons';

const { Sider } = Layout;

function strToHump(str) {
wuhao's avatar
wuhao committed
14
  if (!str) return;
wuhao's avatar
wuhao committed
15 16 17 18 19 20 21 22 23 24 25
  let strArr = str.split('-');
  for (let i = 0; i < strArr.length; i++) {
    strArr[i] = strArr[i].charAt(0).toUpperCase() + strArr[i].substr(1);
  }
  let res = strArr.join('');
  return res + 'Outlined';
}

let CardItems = (datav, i, getstar, addHistory) => {
    let name = datav.name,
      collected = datav.isExist == '1',
TZW's avatar
TZW committed
26 27
      icon = datav.icon ? strToHump(datav.icon) : 'ToolOutlined',
      ItemIcon = icon ? Ant4Icons[icon] : Ant4Icons['ToolOutlined'];
wuhao's avatar
wuhao committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    return (
      <div
        key={i}
        className={styles.carditem}
        onClick={async () => {
          history.push(datav.path);
          await addHistory(datav.key);
        }}
      >
        <a className={styles.diylink}>
          {' '}
          {React.createElement(Ant4Icons[icon], { style: { marginRight: 6 } })}
          {name}
        </a>
        {collected ? (
          <Ant4Icons.StarFilled
            className={styles.collected}
            onClick={async (e) => {
              getstar(e, collected, datav.key);
            }}
          />
        ) : (
          <Ant4Icons.StarOutlined
            className={styles.tocollect}
            onClick={async (e) => {
              getstar(e, collected, datav.key);
            }}
          />
        )}
      </div>
    );
  },
  coles = { xs: 24, sm: 24, md: 12, lg: 8, xl: 6, xxl: 4 };

function SiderMenu({ logo, collapsed, onCollapse, fixSiderbar, theme, isMobile, name, menuData }) {
  let location = useLocation();
  const [anchor, setanchor] = useState();
  const [search, setsearch] = useState();
  const scrollRef = useRef();

  const { alive, changealive } = useModel('useGlobal');
  const {
    initialState: { getmenuData, newMenu },
    setInitialState,
  } = useModel('@@initialState');

  const getMenuData = newMenu?.userHavePermList ?? [],
    collectPerm = newMenu?.collectPerm ?? [],
    recentUsePermList = newMenu?.recentUsePermList ?? [];
  let scrollToAnchor = (anchorName) => {
    if (anchorName) {
      setanchor(anchorName);
      let anchorElement = document.getElementById(anchorName);
      if (scrollRef) {
        // anchorElement.scrollIntoView({ block: 'start', behavior: 'smooth' });
        scrollRef?.current?.scrollTop(anchorElement.offsetTop);
      }
    }
  };

  let filterMenu = (alldata, search) => {
    if (!search) {
      return alldata;
    }
    alldata = alldata ?? [];
    let newdata = alldata.filter((item) => {
      return (
        item.routes &&
        item.routes.some((it) => {
          return it.name.indexOf(search) != -1;
        })
      );
    });

    return newdata.map((item) => {
      return {
        ...item,
        routes: item.routes.filter((it) => {
          return it.name.indexOf(search) != -1;
        }),
      };
    });
  };
  const refreshs = async () => {
    const menuDatas = await getmenuData();
    if (menuDatas) {
      await setInitialState((s) => {
        return { ...s, newMenu: menuDatas };
      });
    }
  };
  async function getstar(e, collected, permissionId) {
    e.stopPropagation();
    if (collected) {
      let cancelData = await cancelCollectPerms({ permissionId });
      if (cancelData.code == '0000') {
        await refreshs();
      }
    } else {
      let collectData = await collectPerms({ permissionId });
      if (collectData.code == '0000') {
        await refreshs();
      }
    }
  }

  async function addHistory(permissionId) {
    let cancelData = await mtosave({ permissionId });
    if (cancelData.code == '0000') {
      changealive(false);
      await refreshs();
    }
  }

  const collectList = useMemo(() => {
    return collectPerm.map((item, i) => {
wuhao's avatar
wuhao committed
144
      const icon = item.icon ? strToHump(item.icon) : 'ToolOutlined',
TZW's avatar
TZW committed
145
        ItemIcon = icon ? Ant4Icons[icon] : Ant4Icons.ToolOutlined;
wuhao's avatar
wuhao committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
      return {
        key: item.path,
        icon: <ItemIcon />,
        label: (
          <div
            style={{
              flex: 1,
              display: 'flex',
              justifyContent: 'space-between',
              alignItems: 'center',
            }}
          >
            {item.name}
            <Ant4Icons.CloseOutlined
              className={styles.closeicon}
              style={{ fontSize: 12 }}
              onClick={async (e) => {
                e.stopPropagation();
                await getstar(e, true, item.key);
              }}
            />
          </div>
        ),
      };
    });
  }, [collectPerm]);

  return (
    <Sider
      trigger={null}
      collapsible
      collapsed={collapsed}
      width={208}
      theme={'dark'}
      collapsedWidth={48}
    >
      <div
        style={{
          height: '100vh',
          display: 'flex',
          flexDirection: 'column',
          position: 'relative',
          top: 0,
        }}
      >
        <Suspense fallback={<div style={{ width: '100vw', height: '100vh' }} />}>
          <div style={{ height: 'calc(100vh - 48px)', overflow: 'auto' }}>
            {
              <div className={styles.fate}>
                <Scrollbars
                  thumbMinSize={10}
                  autoHide
                  style={{
                    width: '100%',
                    height: '100%',
                    position: 'absolute',
                    left: 0,
                    zIndex: 999,
                    backgroundColor: '#001529',
                  }}
                  hideTracksWhenNotNeeded={true}
                >
                  <div className={styles.logo} id="logo">
                    <Link to="/welcome">
                      <img src={logo} alt="logo" />
                      <h1 style={{ fontSize: 12 }}>{name}</h1>
                    </Link>
                  </div>
                  <Menu
                    collapsedWidth={48}
                    mode="inline"
                    theme="dark"
                    selectedKeys={[location.pathname]}
                    collapsed={collapsed.toString()}
                    onClick={async ({ item, key, keyPath, domEvent }) => {
                      if (key == '0') {
                        changealive(!alive);
                        return;
                      }
TZW's avatar
TZW committed
225
                      // await addHistory(item.key);
TZW's avatar
TZW committed
226
                      changealive(false);
wuhao's avatar
wuhao committed
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
                      history.push(key);
                    }}
                    items={[
                      {
                        key: '0',
                        icon: alive ? (
                          <Ant4Icons.RightCircleOutlined />
                        ) : (
                          <Ant4Icons.DownCircleOutlined />
                        ),
                        label: '产品与服务',
                        type: 'sub1',
                      },
                      ...collectList,
                    ]}
                  />
                </Scrollbars>

                <div
                  className={alive ? styles.containbox : styles.containboxs}
                  style={{
                    transition: 'all 0.4s',
                    display: 'flex',
                    flexDirection: 'column',
                  }}
                >
                  <Skeleton active loading={false}>
                    <header
                      style={{
                        display: 'flex',
                        justifyContent: 'space-between',
                        alignItems: 'center',
                        borderBottom: '#ddd solid 1px',
                        paddingBottom: 8,
                      }}
                    >
                      <Input
                        prefix={<Ant4Icons.SearchOutlined style={{ color: 'rgba(0,0,0,1)' }} />}
                        placeholder="输入关键词查找模块"
                        allowClear
                        className={styles.diyinput}
                        value={search}
                        onChange={(e) => {
                          setsearch(e.target.value);
                        }}
                      />
                      <Ant4Icons.CloseOutlined
                        style={{ fontSize: 16, marginLeft: 24, color: '#ff6800' }}
                        onClick={() => {
                          changealive(false);
                        }}
                      />
                    </header>
                    <section
                      style={{
                        display: 'flex',
                        justifyContent: 'space-between',
                        flex: 1,
                        overflow: 'hidden',
                      }}
                    >
                      <div
                        style={{
                          flex: 1,
                          height: '100%',
                          overflow: 'auto',
                          overflowX: 'hidden',
                        }}
                      >
                        <Scrollbars
                          thumbMinSize={10}
                          autoHide
                          style={{ width: '100%', height: '100%' }}
                          ref={scrollRef}
                          hideTracksWhenNotNeeded={true}
                        >
                          <div className={styles.section} style={{ marginBottom: 18 }}>
                            <h2>历史记录</h2>
                            <Row gutter={0}>
                              {recentUsePermList &&
                                recentUsePermList.map((item, i) => {
                                  return (
                                    <Col key={i} {...coles}>
                                      {CardItems(item, i, getstar, addHistory)}
                                    </Col>
                                  );
                                })}
                            </Row>
                          </div>
                          {search && <p>搜索条件:{search},搜索结果:</p>}
                          <div className={styles.column}>
                            {getMenuData &&
                              filterMenu(getMenuData, search).map((item, i) => {
wuhao's avatar
wuhao committed
320
                                const icon = item.icon ? strToHump(item.icon) : 'ToolOutlined',
TZW's avatar
TZW committed
321
                                  ItemIcon = icon ? Ant4Icons[icon] : Ant4Icons['ToolOutlined'];
wuhao's avatar
wuhao committed
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
                                return (
                                  <div className={styles.item} key={i}>
                                    <h2
                                      id={item.key}
                                      className={anchor == item.key ? styles.activeh2 : ''}
                                    >
                                      {' '}
                                      <ItemIcon style={{ marginRight: 6 }} /> {item.name}
                                    </h2>
                                    {item.routes &&
                                      item.routes.map((it, ins) => {
                                        return it ? CardItems(it, ins, getstar, addHistory) : null;
                                      })}
                                  </div>
                                );
                              })}
                          </div>
                          {filterMenu(getMenuData, search).length == 0 && <Empty />}
                        </Scrollbars>
                      </div>
                      <div className={styles.rt}>
                        {getMenuData &&
                          getMenuData.map((item, i) => {
wuhao's avatar
wuhao committed
345
                            const icon = item.icon ? strToHump(item.icon) : 'ToolOutlined',
TZW's avatar
TZW committed
346
                              ItemIcon = icon ? Ant4Icons[icon] : Ant4Icons['ToolOutlined'];
wuhao's avatar
wuhao committed
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
                            return (
                              <p
                                className={styles.rtcarditem}
                                key={i}
                                onClick={() => {
                                  scrollToAnchor(item.key);
                                }}
                              >
                                <ItemIcon style={{ marginRight: 6 }} /> {item.name}
                              </p>
                            );
                          })}
                      </div>
                    </section>
                  </Skeleton>
                </div>
              </div>
            }
          </div>
        </Suspense>
      </div>
    </Sider>
  );
}

export default SiderMenu;