File size: 2,441 Bytes
81d8726
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
<template>
  <el-menu
    :default-active="defaultActiveMenu"
    :collapse="menuCollapse"
    :mode="mode"
    class="menu"
    :class="{ 'horizontal_menu': mode === 'horizontal' }"
    :collapse-transition="true"
    @select="handleSelect"
  >
    <el-menu-item v-for="(item, index) in list" :key="index" :index="item.index">
      <el-icon>
        <component :is="item.icon" />
      </el-icon>
      <template #title>
        <span>{{ item.name }}</span>
      </template>
    </el-menu-item>
  </el-menu>
</template>

<script setup>
import { reactive, markRaw, getCurrentInstance, computed, watchEffect } from 'vue'
import { useRoute } from 'vue-router'
import {
  Menu as IconMenu,
  Key,
  Setting,
  ScaleToOriginal,
  ArrowRight,
  Pointer,
  FolderOpened
  // FolderOpened
} from '@element-plus/icons-vue'

const props = defineProps({
  mode: {
    type: String,
    default: 'vertical'
  }
})

const { proxy: { $router, $store } } = getCurrentInstance()

const emit = defineEmits(['select',])

const route = useRoute()

const list = reactive([
  {
    name: '实例列表',
    icon: markRaw(IconMenu),
    index: '/server'
  },
  {
    name: '远程终端',
    icon: markRaw(ScaleToOriginal),
    index: '/terminal'
  },
  {
    name: '远程桌面',
    icon: markRaw(ScaleToOriginal),
    index: '/rdp'
  },
  {
    name: '文件对传',
    icon: markRaw(FolderOpened),
    index: '/file'
  },
  {
    name: '凭据管理',
    icon: markRaw(Key),
    index: '/credentials'
  },
  {
    name: '脚本库',
    icon: markRaw(ArrowRight),
    index: '/scripts'
  },
  {
    name: '批量指令',
    icon: markRaw(Pointer),
    index: '/onekey'
  },
  {
    name: '系统设置',
    icon: markRaw(Setting),
    index: '/setting'
  },
])

const menuCollapse = computed(() => props.mode === 'horizontal' ? false : $store.menuCollapse)

// eslint-disable-next-line no-useless-escape
const regex = /^\/([^\/]+)/
const defaultActiveMenu = computed(() => {
  const match = route.path.match(regex)
  return match[0]
})

watchEffect(() => {
  const idx = route.path.match(regex)[0]
  const targetRoute = list.find(item => item.index === idx)
  $store.setTitle(targetRoute?.name || '')
})

const handleSelect = (path) => {
  // console.log(path)
  $router.push(path)
  emit('select', path)
}
</script>

<style lang="scss" scoped>
.horizontal_menu {
  :deep(.el-menu-item) {
    .el-icon {
      margin-right: 8px;
    }
  }
}
</style>