index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <template>
  2. <el-dialog
  3. v-model="dialogVisible"
  4. width="30%"
  5. class="overlay"
  6. :show-close="false"
  7. >
  8. <template #header>
  9. <input
  10. v-model="searchInput"
  11. class="quick-input"
  12. placeholder="请输入你需要快捷到达的功能"
  13. >
  14. </template>
  15. <div
  16. v-for="(option,index) in options"
  17. :key="index"
  18. >
  19. <div
  20. v-if="option.children.length"
  21. class="quick-title"
  22. >{{ option.label }}</div>
  23. <div
  24. v-for="(item,key) in option.children"
  25. :key="index+'-'+key"
  26. class="quick-item"
  27. @click="item.func"
  28. >
  29. {{ item.label }}
  30. </div>
  31. </div>
  32. <template #footer>
  33. <span class="dialog-footer">
  34. <el-button @click="close">关闭</el-button>
  35. </span>
  36. </template>
  37. </el-dialog>
  38. </template>
  39. <script setup>
  40. import { reactive, ref, watch } from 'vue'
  41. import { useRouter, useRoute } from 'vue-router'
  42. import { useRouterStore } from '@/pinia/modules/router'
  43. import { useUserStore } from '@/pinia/modules/user'
  44. defineOptions({
  45. name: 'CommandMenu',
  46. })
  47. const router = useRouter()
  48. const route = useRouter()
  49. const userStore = useUserStore()
  50. const routerStore = useRouterStore()
  51. const dialogVisible = ref(false)
  52. const searchInput = ref('')
  53. const options = reactive([])
  54. const deepMenus = (menus) => {
  55. const arr = []
  56. menus.forEach(menu => {
  57. if (menu.children && menu.children.length > 0) {
  58. arr.push(...deepMenus(menu.children))
  59. } else {
  60. if (menu.meta.title && menu.meta.title.indexOf(searchInput.value) > -1) {
  61. arr.push({
  62. label: menu.meta.title,
  63. func: () => changeRouter(menu)
  64. })
  65. }
  66. }
  67. })
  68. return arr
  69. }
  70. const addQuickMenu = () => {
  71. const option = {
  72. label: '跳转',
  73. children: []
  74. }
  75. const menus = deepMenus(routerStore.asyncRouters[0].children)
  76. option.children.push(...menus)
  77. options.push(option)
  78. }
  79. const addQuickOption = () => {
  80. const option = {
  81. label: '操作',
  82. children: []
  83. }
  84. const quickArr = [
  85. {
  86. label: '亮色主题',
  87. func: () => changeMode('light')
  88. }, {
  89. label: '暗色主题',
  90. func: () => changeMode('dark')
  91. }, {
  92. label: '退出登录',
  93. func: () => userStore.LoginOut()
  94. }
  95. ]
  96. option.children.push(...quickArr.filter(item => item.label.indexOf(searchInput.value) > -1))
  97. options.push(option)
  98. }
  99. addQuickMenu()
  100. addQuickOption()
  101. const open = () => {
  102. dialogVisible.value = true
  103. }
  104. const changeRouter = (e) => {
  105. const index = e.name
  106. const query = {}
  107. const params = {}
  108. routerStore.routeMap[index]?.parameters &&
  109. routerStore.routeMap[index]?.parameters.forEach((item) => {
  110. if (item.type === 'query') {
  111. query[item.key] = item.value
  112. } else {
  113. params[item.key] = item.value
  114. }
  115. })
  116. if (index === route.name) return
  117. if (e.name.indexOf('http://') > -1 || e.name.indexOf('https://') > -1) {
  118. window.open(e.name)
  119. } else {
  120. router.push({ name: index, query, params })
  121. }
  122. dialogVisible.value = false
  123. }
  124. const changeMode = (e) => {
  125. if (e === null) {
  126. userStore.changeSideMode('dark')
  127. return
  128. }
  129. userStore.changeSideMode(e)
  130. }
  131. const close = () => {
  132. dialogVisible.value = false
  133. }
  134. defineExpose({ open })
  135. watch(searchInput, () => {
  136. options.length = 0
  137. addQuickMenu()
  138. addQuickOption()
  139. })
  140. </script>
  141. <style lang="scss">
  142. .overlay {
  143. border-radius: 4px;
  144. .el-dialog__header{
  145. padding:0 !important;
  146. margin-right:0 !important;
  147. }
  148. .el-dialog__body{
  149. padding: 12px !important;
  150. height: 50vh;
  151. overflow: auto !important;
  152. }
  153. .quick-title{
  154. margin-top: 8px;
  155. font-size: 12px;
  156. font-weight: 600;
  157. color: #666;
  158. }
  159. .quick-input{
  160. color: #666;
  161. border-radius: 4px 4px 0 0;
  162. border:none;
  163. padding: 12px 16px;
  164. box-sizing: border-box;
  165. width: 100%;
  166. font-size: 16px;
  167. border-bottom: 1px solid #ddd;
  168. }
  169. .quick-item{
  170. font-size: 14px;
  171. padding: 8px;
  172. margin: 4px 0;
  173. &:hover{
  174. cursor: pointer;
  175. background: #eee;
  176. border-radius: 4px;
  177. }
  178. }
  179. }
  180. </style>