CloseMenuDirective.js 4.28 KB
Newer Older
邹绪超's avatar
邹绪超 committed
1 2 3
import { store, event } from '@wisdom-utils/utils';
import BaseDirective from './BaseDirective';
import Utils from '../utils/Utils';
4 5

class CloseMenuDirective extends BaseDirective {
邹绪超's avatar
邹绪超 committed
6
  static name = '关闭菜单或者模糊匹配关闭菜单';
xuchaozou's avatar
xuchaozou committed
7

邹绪超's avatar
邹绪超 committed
8 9 10
  match() {
    const { text } = this;
    let result = null;
xuchaozou's avatar
xuchaozou committed
11

邹绪超's avatar
邹绪超 committed
12 13 14 15 16 17 18 19
    if (this.getBeforeLastGroupDirective()) {
      result = text.match(/第([一|二|三|四|五|六|七|八|九|十]+)个/);
      if (result && result.length >= 2) {
        this.matchResult = Utils.chineseToNumber(result[1]);
      } else if (!result) {
        result = text.match(/第[\u4e00-\u9fa5]?(\d+)个/);
        if (result && result.length >= 2) {
          this.matchResult = Number(result[1]);
20
        }
邹绪超's avatar
邹绪超 committed
21
      }
22
    }
邹绪超's avatar
邹绪超 committed
23 24 25
    result = text.match(/关闭(所有).*(?=(?:,|,|。))/);
    if (!result) {
      result = text.match(/关闭(当前).*(?=(?:,|,|。))/);
xuchaozou's avatar
xuchaozou committed
26
    }
邹绪超's avatar
邹绪超 committed
27 28
    if (!result) {
      result = text.match(/关闭(.*)(?=(?:菜单|功能|路径))/);
29
    }
邹绪超's avatar
邹绪超 committed
30 31
    if (!result) {
      result = text.match(/关闭(.*)(?=(?:,|,|。))/);
32
    }
邹绪超's avatar
邹绪超 committed
33 34
    if (result && result.length >= 2 && !this.matchResult) {
      this.matchResult = result[1];
35
    }
邹绪超's avatar
邹绪超 committed
36 37 38 39 40 41 42 43 44 45
    return !!this.matchResult;
  }

  getBeforeLastGroupDirective() {
    const lastMenu = this.directives.at(-1);
    if (!lastMenu) return false;
    if (!(lastMenu instanceof CloseMenuDirective)) return false;
    if (!lastMenu.data || lastMenu.data.type != 'widgets' || lastMenu.data.widgets.length == 0) return false;
    return true;
  }
46

47 48 49
  async excute({
    sendMsg = function () {}
  }) {
邹绪超's avatar
邹绪超 committed
50 51 52 53 54 55 56
    let widgets = null;
    if (typeof this.matchResult === 'number') {
      const widgets = this.getOpenedMenus();
      if (this.matchResult == 0 || this.matchResult > widgets.length) {
        this.data = {};
        this.data.type = 'widgets';
        this.data.widgets = widgets;
57
        return sendMsg(`索引超出了界限,请重新语音选择关闭第几个菜单`);
邹绪超's avatar
邹绪超 committed
58 59 60
      }
      const menu = widgets[this.matchResult];
      event.emit('event:closeMenuByName', menu.name);
61
      return sendMsg(`关闭${menu.name}菜单成功`);
邹绪超's avatar
邹绪超 committed
62 63
    }
    if (this.matchResult == '所有') {
64
      return sendMsg(this.closeAllMenu());
邹绪超's avatar
邹绪超 committed
65 66
    }
    if (this.matchResult == '当前') {
67
      return sendMsg(this.closeCurrentMenu());
68
    }
邹绪超's avatar
邹绪超 committed
69 70 71 72 73
    widgets = this.getIsOneWdiget();
    if (!widgets) {
      widgets = this.getCompleteMenus();
    }
    if (!widgets) {
74
      return sendMsg('已打开菜单中没有查找到此菜单,请检查语音是否正确');
邹绪超's avatar
邹绪超 committed
75 76 77
    }
    if (widgets.length == 1) {
      event.emit('event:closeMenuByName', widgets[0].name);
78
      return sendMsg(`关闭${widgets[0].name}菜单成功`);
邹绪超's avatar
邹绪超 committed
79 80 81 82
    }
    this.data = {};
    this.data.type = 'widgets';
    this.data.widgets = widgets;
83
    return sendMsg(this.widgetsToMsg(widgets));
邹绪超's avatar
邹绪超 committed
84 85 86 87 88 89 90 91 92 93 94 95
  }

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

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

邹绪超's avatar
邹绪超 committed
97 98 99 100 101 102 103
  closeCurrentMenu() {
    console.log(store);
    console.log(window.location);
    const menus = this.getOpenedMenus();
    const menu = location.pathname.match(/\/civbase(.*)/);
    if (!menu || menu.length <= 1) {
      return '系统发生匹配错误';
104
    }
邹绪超's avatar
邹绪超 committed
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
    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());
  }
134 135
}

邹绪超's avatar
邹绪超 committed
136
export default CloseMenuDirective;