index.js 15.9 KB
Newer Older
崔佳豪's avatar
崔佳豪 committed
1 2 3
import React, { useState, useEffect } from 'react';
import { Input, Button, notification, Spin, message, Modal } from 'antd';
import { CheckOutlined, PlusOutlined } from '@ant-design/icons';
崔佳豪's avatar
崔佳豪 committed
4
import { connect } from 'react-redux';
5
import classnames from 'classnames';
崔佳豪's avatar
崔佳豪 committed
6 7 8
import * as _ from 'lodash';
import PinyinMatch from 'pinyin-match';
import PandaEmpty from '@wisdom-components/empty';
邓晓峰's avatar
邓晓峰 committed
9
import { useHistory } from '@wisdom-utils/runtime';
10
import { actionCreators } from '@/containers/App/store';
11 12
import { appService } from '@/api';
import { savePagePartInfo } from '@/api/service/base';
崔佳豪's avatar
崔佳豪 committed
13

14 15 16
import styles from './index.less';
import thumbnail from '../../assets/images/commonMenu/常用菜单.png';
import pageLogo from '../../assets/images/commonMenu/page-logo.png';
崔佳豪's avatar
崔佳豪 committed
17
import starIcon from '../../assets/images/commonMenu/矢量智能对象 拷贝 3@2x.png';
18 19

// 是否是灰色的图标(灰色图标在白色背景中看不见,添加滤镜变色)
崔佳豪's avatar
崔佳豪 committed
20 21
const isNeedFilterIcon = (icon = '') =>
  icon && !icon.includes('一级') && !icon.includes('ios/');
邓晓峰's avatar
邓晓峰 committed
22 23 24
// Modal.config({
//   rootPrefixCls: 'panda-console-base'
// });
崔佳豪's avatar
崔佳豪 committed
25 26
const CommonMenu = props => {
  const history = useHistory();
崔佳豪's avatar
崔佳豪 committed
27
  const { menus } = props;
崔佳豪's avatar
崔佳豪 committed
28
  const [commonMenus, setCommonMenus] = useState([]); // 收藏菜单信息
崔佳豪's avatar
崔佳豪 committed
29
  const [menuList, setMenuList] = useState([]);
30 31
  const [searchInfo, setSearchInfo] = useState('');

崔佳豪's avatar
崔佳豪 committed
32 33
  const [loading, setLoading] = useState(true); // loading显示隐藏
  const [isShowMenuModal, setIsShowMenuModal] = useState(false);
34 35

  /**
崔佳豪's avatar
崔佳豪 committed
36
   * 获取收藏的菜单信息
37 38 39 40 41 42 43 44 45 46 47 48 49 50
   */
  const fetchMenus = () => {
    appService
      .getPagePartInfo({
        UserID: window?.globalConfig?.userInfo?.OID ?? '',
      })
      .then(res => {
        setLoading(false);
        if (res.say.statusCode !== '0000')
          return notification.error({
            message: '服务错误',
            description: res.say.errMsg,
          });
        const newMenus = [];
崔佳豪's avatar
崔佳豪 committed
51
        // 过滤出当前client的菜单(widget_client_xxx)
52 53 54 55 56 57
        const data = res.getMe.filter(item => {
          const client = item.PartID.split('_')[1] ?? '';
          return client === window.globalConfig.client;
        });
        data.forEach(item => {
          const newMenu = {
崔佳豪's avatar
崔佳豪 committed
58 59 60 61 62
            icon: item.icon,
            name: item.PartName,
            path: item.PartUrl,
            pic: item.BgPicUrl,
            id: item.PartID,
63
          };
崔佳豪's avatar
崔佳豪 committed
64 65 66 67
          // 所属产品
          const [product] = item.PartUrl.split('/').filter(d => !!d);
          newMenu.product = product;
          // 所属一级分组
68
          const PartIDArr = item.PartID.split('_');
崔佳豪's avatar
崔佳豪 committed
69
          newMenu.topGroup = PartIDArr.splice(2, 1).join('_');
70 71
          newMenus.push(newMenu);
        });
崔佳豪's avatar
崔佳豪 committed
72
        setCommonMenus(newMenus);
73 74 75 76 77 78 79 80 81 82 83 84 85
      })
      .catch(error => {
        notification.error({
          message: '服务错误',
          description: error,
        });
      });
  };

  useEffect(() => {
    fetchMenus();
  }, []);

崔佳豪's avatar
崔佳豪 committed
86 87
  useEffect(() => {
    const isAddedMenu = menu => {
崔佳豪's avatar
崔佳豪 committed
88
      const menuTmp = commonMenus.find(item => item.id === menu.extData.PartID);
崔佳豪's avatar
崔佳豪 committed
89 90 91
      return !!menuTmp;
    };
    const loop = (data, prePartID) => {
崔佳豪's avatar
崔佳豪 committed
92 93
      const newData = [];
      data.forEach(item => {
崔佳豪's avatar
崔佳豪 committed
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
        const { name, level, href, key, path, routes } = item;
        const newmenu = { ...item };
        newmenu.key = href ? key : path;
        newmenu.title = name;

        // 菜单进行搜索过滤
        const index = name.indexOf(searchInfo);
        const beforeStr = name.substr(0, index);
        const afterStr = name.substr(index + searchInfo.length);
        const title =
          index > -1 ? (
            <span>
              {beforeStr}
              <span className={styles.treeSearchInfo}>{searchInfo}</span>
              {afterStr}
            </span>
          ) : (
            <span>{name}</span>
          );
        newmenu.title = title;
        newmenu.extData = {
          ...item.extData,
          PartID: `${prePartID}_${name}`,
        };
        newmenu.extData.isAdded = isAddedMenu(newmenu);

        if (routes) {
          newmenu.children = loop(routes, `${prePartID}_${name}`);
        }
崔佳豪's avatar
崔佳豪 committed
123 124 125 126 127
        if (routes && newmenu.children && newmenu.children.length) {
          newData.push(newmenu);
        } else if (!routes && index > -1) {
          newData.push(newmenu);
        }
崔佳豪's avatar
崔佳豪 committed
128
      });
崔佳豪's avatar
崔佳豪 committed
129
      return newData;
崔佳豪's avatar
崔佳豪 committed
130 131 132 133 134
    };
    const newmenus = loop(menus, `widget_${window.globalConfig.client}`);
    setMenuList(newmenus);
  }, [menus, searchInfo, commonMenus]);

崔佳豪's avatar
崔佳豪 committed
135 136 137 138 139
  /**
   * 菜单跳转
   * @param {*} menu
   */
  const linkToMenu = menu => {
崔佳豪's avatar
崔佳豪 committed
140
    const { name, path, topGroup } = menu;
141
    const currentIndex = props.menus.findIndex(item => item.name === topGroup);
崔佳豪's avatar
崔佳豪 committed
142 143 144 145 146 147 148 149 150 151 152 153 154
    let currentRoutes = props.flatMenu.find(item => item.path === path && item.name === name);
    // let currentRoutes = props.menus[currentIndex] && props.menus[currentIndex].routes;
    // currentRoutes = currentRoutes.find(item => item.path === path);
    if(currentRoutes) {
      history.push(path);
      props.updateCurrentIndex(currentIndex);
      window.share && window.share.event.emit('trigger:updateMenuIndex', currentIndex);
      window.share && window.share.event && window.share.event.emit('event:updateCurrentChildrenRoutes', {
        currentPath: path,
        currentRoute: currentRoutes,
        selectedIndex: currentIndex
      });
    }
155 156
  };

崔佳豪's avatar
崔佳豪 committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
  const mdOk = submitData => {
    setLoading(true);
    savePagePartInfo({
      query: {
        UserID: window.globalConfig?.userInfo?.OID ?? '',
      },
      data: submitData,
    })
      .then(res => {
        setLoading(false);
        if (res.statusCode === '0000') {
          message.success('修改常用菜单成功!');
          setIsShowMenuModal(false);
          fetchMenus();
        } else {
          message.error('修改常用菜单失败!');
        }
      })
      .catch(err => {
        setLoading(false);
      });
崔佳豪's avatar
崔佳豪 committed
178 179
  };

崔佳豪's avatar
崔佳豪 committed
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
  const mdCancel = () => {
    setIsShowMenuModal(false);
  };

  const rednerMenuCardData = () => {
    const menuCardData = [];
    commonMenus &&
      commonMenus.forEach(item => {
        const { name } = item;
        const m = PinyinMatch.match(name, searchInfo);
        if (!m) return;
        const [before, after] = m;
        const beforeStr = name.slice(0, before);
        const centerStr = name.slice(before, after + 1);
        const afterStr = name.slice(after + 1);
        const title =
          after > -1 ? (
            <span>
              {beforeStr}
              <em style={{ fontStyle: 'normal', color: 'orange' }}>
                {centerStr}
              </em>
              {afterStr}
            </span>
          ) : (
            <span>{name}</span>
          );
        menuCardData.push(
          <MenuCard
            menu={{ ...item, title }}
            linkToMenu={linkToMenu}
            key={item.id}
          />,
        );
      });
    return menuCardData.length > 0 ? menuCardData : <PandaEmpty />;
崔佳豪's avatar
崔佳豪 committed
216 217
  };

218
  return (
崔佳豪's avatar
崔佳豪 committed
219
    <div className={styles.commonMenu}>
邓晓峰's avatar
邓晓峰 committed
220
      <Spin spinning={loading} style={{height: '100%'}}>
221
        <div className={styles.searchWrapper}>
崔佳豪's avatar
崔佳豪 committed
222
          <div className={styles.searchBox}>
223
            <div className={styles.searchTitle}>
崔佳豪's avatar
崔佳豪 committed
224 225 226 227 228
              <img
                style={{ width: '20px', marginRight: '0.5em' }}
                src={starIcon}
                alt=""
              />
229
              <span>我的常用菜单</span>
崔佳豪's avatar
崔佳豪 committed
230
              <span>{commonMenus.length}</span>
崔佳豪's avatar
崔佳豪 committed
231 232 233 234 235 236 237
              <PlusOutlined
                className={styles.addIcon}
                title="添加常用菜单"
                onClick={() => {
                  setIsShowMenuModal(true);
                }}
              />
238
            </div>
崔佳豪's avatar
崔佳豪 committed
239
            <div className={styles.searchInput}>
崔佳豪's avatar
崔佳豪 committed
240
              <Input.Search
崔佳豪's avatar
崔佳豪 committed
241 242 243 244 245 246
                maxLength={50}
                width={400}
                placeholder="搜索功能菜单"
                onChange={e => {
                  setSearchInfo(e.target.value);
                }}
247 248 249
              />
            </div>
          </div>
崔佳豪's avatar
崔佳豪 committed
250 251 252 253 254 255 256 257
          {/* <Button
            className={styles.searchBtn}
            onClick={() => {
              setIsShowMenuModal(true);
            }}
          >
            添加菜单
          </Button> */}
258
        </div>
崔佳豪's avatar
崔佳豪 committed
259 260 261 262 263 264 265 266
        <MenuAddModal
          isShowMenuModal={isShowMenuModal}
          setIsShowMenuModal={setIsShowMenuModal}
          menuList={menuList}
          mdOk={mdOk}
          mdCancel={mdCancel}
        />
        <div className={styles.menuCardWrapper}>{rednerMenuCardData()}</div>
267 268 269 270 271 272 273 274
        <div className={styles.pageLogo}>
          <img src={pageLogo} alt="" />
        </div>
      </Spin>
    </div>
  );
};

崔佳豪's avatar
崔佳豪 committed
275
// 搜藏菜单卡片展示
崔佳豪's avatar
崔佳豪 committed
276
const MenuCard = ({ menu, linkToMenu }) => {
崔佳豪's avatar
崔佳豪 committed
277
  const { icon, name, title, topGroup, menuID, path, pic } = menu;
278
  return (
崔佳豪's avatar
崔佳豪 committed
279 280
    <div className={styles.menuCard} onClick={() => linkToMenu(menu)}>
      {/* <Link to={menuUrl} title={menuName}> */}
崔佳豪's avatar
崔佳豪 committed
281
      <img className={styles.cardThumbnail} src={pic || thumbnail} alt="" />
崔佳豪's avatar
崔佳豪 committed
282 283 284 285
      <div className={styles.cardLabel}>
        <div
          className={classnames(
            styles.cardTitle,
崔佳豪's avatar
崔佳豪 committed
286
            isNeedFilterIcon(icon) ? styles.filterIconBox : '',
崔佳豪's avatar
崔佳豪 committed
287 288 289
          )}
        >
          <span className={styles.iconBox}>
崔佳豪's avatar
崔佳豪 committed
290
            <img className={styles.cardIcon} src={icon} alt="" />
崔佳豪's avatar
崔佳豪 committed
291
          </span>
崔佳豪's avatar
崔佳豪 committed
292
          <span className={styles.cardName}>{title || name}</span>
293
        </div>
崔佳豪's avatar
崔佳豪 committed
294
        <div className={styles.cardGroup}>{topGroup}</div>
崔佳豪's avatar
崔佳豪 committed
295 296
      </div>
      {/* </Link> */}
297 298 299 300
    </div>
  );
};

崔佳豪's avatar
崔佳豪 committed
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 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 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
const ModalTitle = () => <p className={styles.modalTitle}>添加常用菜单</p>;

const MenuAddModal = props => {
  const { isShowMenuModal, menuList, mdOk, mdCancel } = props;
  const [tempMenuList, setTempMenuList] = useState([]); // 模态框菜单列表数据
  const [submitData, setSubmitData] = useState([]);
  const [searchInfo, setSearchInfo] = useState('');
  useEffect(() => {
    const newSubmitData = [];
    const loop = data => {
      data &&
        data.forEach(item => {
          if (item.href && item.extData.isAdded) {
            newSubmitData.push({
              PartID: item.extData.PartID,
              PartName: item.name,
              PartUrl: item.path,
              icon: item.extData.icon,
            });
          }
          item.children && loop(item.children);
        });
    };
    loop(menuList);
    setSubmitData(newSubmitData);
  }, [menuList, isShowMenuModal]);

  useEffect(() => {
    let newTempMenuList = menuList.map(item => {
      const childMenus = [];
      const deep = (data = []) => {
        data.forEach(d => {
          if (d.href) {
            const m = PinyinMatch.match(d.name, searchInfo);
            const tempIsAdded = !!submitData.find(
              sd => sd.PartID === d.extData.PartID,
            );
            if (m)
              childMenus.push({
                ...d,
                extData: { ...d.extData, isAdded: tempIsAdded },
                match: m,
              });
          } else {
            deep(d.children);
          }
        });
      };
      if (item.href) {
        childMenus.push({ ...item });
      } else {
        deep(item.children);
      }
      return { ...item, childMenus };
    });
    newTempMenuList = newTempMenuList.filter(
      item => item.childMenus && item.childMenus.length > 0,
    );
    setTempMenuList(newTempMenuList);
  }, [menuList, isShowMenuModal, searchInfo]);

  const toggleMenu = menu => {
    const { key } = menu;
    const newTempMenuList = _.cloneDeep(tempMenuList);
    let newSubmitData = [];
    let flag = false;
    for (let i = 0; i < newTempMenuList.length; i += 1) {
      const { childMenus } = newTempMenuList[i];
      for (let j = 0; j < childMenus.length; j += 1) {
        if (childMenus[j].key === key) {
          flag = true;
          const {
            name,
            href,
            extData: { PartID, icon, isAdded },
          } = childMenus[j];
          childMenus[j].extData.isAdded = !isAdded;
          if (submitData.find(item => item.PartID === PartID)) {
            newSubmitData = submitData.filter(item => item.PartID !== PartID);
          } else {
            newSubmitData = [
              ...submitData,
              {
                PartID,
                PartName: name,
                PartUrl: href,
                icon,
              },
            ];
          }
          break;
        }
      }
      if (flag) break;
    }

    setTempMenuList(newTempMenuList);
    setSubmitData(newSubmitData);
  };
  return (
    <Modal
      wrapClassName={styles.MenuModal}
      title={<ModalTitle />}
      width={900}
      okText="确定"
      cancelText="取消"
      visible={isShowMenuModal}
      bodyStyle={{ padding: 0 }}
      onOk={() => {
        mdOk(submitData);
      }}
      onCancel={() => {
        mdCancel();
      }}
    >
      <div className={styles.modalBodyHead}>
        <Input
          allowClear
          placeholder="搜索功能菜单"
          prefix={
            <img
              src={starIcon}
              alt=""
              style={{ height: '18px', marginRight: '5px' }}
            />
            // <SearchOutlined style={{ color: '#1d8dff', fontSize: '16px' }} />
          }
          onChange={e => {
            setSearchInfo(e.target.value);
          }}
        />
      </div>
      <div className={classnames(styles.modalBodyMain)}>
        {tempMenuList.length ? (
          tempMenuList.map(item => (
            <div className={styles.menuWrapper} key={item.key}>
              <p className={styles.menuGroupTitle}>{item.name}</p>
              <div className={styles.menuList}>
崔佳豪's avatar
崔佳豪 committed
439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
                {Array.isArray(item.childMenus) &&
                  item.childMenus.map(child => {
                    const { match, name } = child;
                    const { isAdded } = child.extData;
                    let bname = '',
                      cname = '',
                      aname = name;
                    if (match) {
                      const [before, after] = match;
                      bname = after > -1 ? name.slice(0, before) : '';
                      cname =
                        after > -1 ? (
                          <em style={{ color: 'orange', fontStyle: 'normal' }}>
                                                        
                            {name.slice(before, after + 1)}
                                                      
                          </em>
                        ) : (
                          ''
                        );
                      aname =
                        after > -1 ? name.slice(after + 1, name.length) : name;
                    }

                    return (
                      <div
                        key={child.key}
                        className={classnames(
                          styles.menuItem,
                          isAdded ? styles.menuItem_added : '',
                        )}
                        onClick={() => {
                          toggleMenu(child);
                        }}
                      >
                        <div>
                          <span>
                            {bname}
                            {cname}
                            {aname}
                          </span>
                          {isAdded ? (
                            <p className={styles.addedFlag}>
                              <CheckOutlined />
                            </p>
                          ) : null}
                        </div>
崔佳豪's avatar
崔佳豪 committed
486
                      </div>
崔佳豪's avatar
崔佳豪 committed
487 488
                    );
                  })}
崔佳豪's avatar
崔佳豪 committed
489 490 491 492 493 494 495 496 497 498 499
              </div>
            </div>
          ))
        ) : (
          <PandaEmpty />
        )}
      </div>
    </Modal>
  );
};

崔佳豪's avatar
崔佳豪 committed
500
const mapStateToProps = state => ({
崔佳豪's avatar
崔佳豪 committed
501
  menus: state.getIn(['global', 'menu']),
崔佳豪's avatar
崔佳豪 committed
502
  flatMenu: state.getIn(['global', 'flatMenu']),
崔佳豪's avatar
崔佳豪 committed
503
});
504 505 506
const mapDispatchToProps = dispatch => ({
  updateCurrentIndex(index) {
    dispatch(actionCreators.updateCurrentIndex(index));
崔佳豪's avatar
崔佳豪 committed
507
  },
508
});
崔佳豪's avatar
崔佳豪 committed
509 510
export default connect(
  mapStateToProps,
511
  mapDispatchToProps,
崔佳豪's avatar
崔佳豪 committed
512
)(CommonMenu);