index.js 3.27 KB
Newer Older
邹绪超's avatar
邹绪超 committed
1
import { store, event } from '@wisdom-utils/utils';
xuchaozou's avatar
xuchaozou committed
2 3
import BaseDirective from '../BaseDirective';
import CloseSingleDirective from './CloseSingleDirective';
xuchaozou's avatar
xuchaozou committed
4
import config from '../../config';
5 6

class CloseMenuDirective extends BaseDirective {
邹绪超's avatar
邹绪超 committed
7
  static name = '关闭菜单或者模糊匹配关闭菜单';
xuchaozou's avatar
xuchaozou committed
8
  static beforeLoadDirectives = [CloseSingleDirective]
邹绪超's avatar
邹绪超 committed
9 10 11
  match() {
    const { text } = this;
    let result = null;
xuchaozou's avatar
xuchaozou committed
12
    if(!result) {
13
      result = text.match(new RegExp(`关闭(所有).*(?=(?:[${Array.from(config.punctuationMark).join("|")}]?))`))
xuchaozou's avatar
xuchaozou committed
14
    }
邹绪超's avatar
邹绪超 committed
15
    if (!result) {
16
      result = text.match(new RegExp(`关闭(当前).*(?=(?:[${Array.from(config.punctuationMark).join("|")}]?))`))
xuchaozou's avatar
xuchaozou committed
17
    }
邹绪超's avatar
邹绪超 committed
18 19
    if (!result) {
      result = text.match(/关闭(.*)(?=(?:菜单|功能|路径))/);
20
    }
邹绪超's avatar
邹绪超 committed
21
    if (!result) {
22
      result = text.match(new RegExp(`关闭(.*)(?=(?:[${Array.from(config.punctuationMark).join("|")}]?))`))
23
    }
邹绪超's avatar
邹绪超 committed
24 25
    if (result && result.length >= 2 && !this.matchResult) {
      this.matchResult = result[1];
26
    }
邹绪超's avatar
邹绪超 committed
27 28 29
    return !!this.matchResult;
  }

30
  async excute({
xuchaozou's avatar
xuchaozou committed
31
    sendMsg = function () { }
32
  }) {
邹绪超's avatar
邹绪超 committed
33 34
    let widgets = null;
    if (this.matchResult == '所有') {
35
      return sendMsg(this.closeAllMenu());
邹绪超's avatar
邹绪超 committed
36 37
    }
    if (this.matchResult == '当前') {
38
      return sendMsg(this.closeCurrentMenu());
39
    }
邹绪超's avatar
邹绪超 committed
40 41 42 43 44
    widgets = this.getIsOneWdiget();
    if (!widgets) {
      widgets = this.getCompleteMenus();
    }
    if (!widgets) {
45
      return sendMsg('已打开菜单中没有查找到此菜单,请检查语音是否正确');
邹绪超's avatar
邹绪超 committed
46 47 48
    }
    if (widgets.length == 1) {
      event.emit('event:closeMenuByName', widgets[0].name);
49
      return sendMsg(`关闭${widgets[0].name}菜单成功`);
邹绪超's avatar
邹绪超 committed
50 51 52 53
    }
    this.data = {};
    this.data.type = 'widgets';
    this.data.widgets = widgets;
54
    return sendMsg(this.widgetsToMsg(widgets));
邹绪超's avatar
邹绪超 committed
55 56 57 58 59 60 61 62 63 64 65 66
  }

  widgetsToMsg(widgets) {
    return `有以下${widgets.length}个菜单打开\n${widgets
      .map((widget, index) => `${index + 1}.${widget.name}`)
      .join('\n')},\n请语音确认一下关闭第几个菜单`;
  }

  closeAllMenu() {
    event.emit('event:closeAllMenu', true);
    return `成功关闭所有菜单`;
  }
67

邹绪超's avatar
邹绪超 committed
68 69 70 71 72
  closeCurrentMenu() {
    const menus = this.getOpenedMenus();
    const menu = location.pathname.match(/\/civbase(.*)/);
    if (!menu || menu.length <= 1) {
      return '系统发生匹配错误';
73
    }
邹绪超's avatar
邹绪超 committed
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
    const currentMenu = menus.find(item => item.location.pathname == decodeURI(menu[1]));
    if (!currentMenu) {
      return '系统发生异常';
    }
    const isHome = currentMenu.name == '首页';
    if (isHome) {
      return `当前菜单为首页,不能被关闭`;
    }
    event.emit('event:closeMenuByName', currentMenu.name);
    return `关闭${currentMenu.name}菜单成功`;
  }

  getCompleteMenus() {
    const widgets = this.getOpenedMenus();
    const reg = new RegExp(this.matchResult);
    const results = widgets.filter(i => reg.test(i.name));
    return results.length > 0 ? results : null;
  }

  getIsOneWdiget() {
    const widgets = this.getOpenedMenus();
    const widget = widgets.find(i => i.name == this.matchResult);
    return widget ? [widget] : null;
  }

  getOpenedMenus() {
    const menus = store.get('event:openedMenus');
    return Array.from(menus.current.values());
  }
103 104
}

xuchaozou's avatar
xuchaozou committed
105 106

export default CloseMenuDirective