You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

228 lines
7.1 KiB

3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
  1. <script setup>
  2. // 导航栏在这
  3. import { computed, ref } from 'vue'
  4. import { useRoute, useRouter } from 'vue-router'
  5. import { ElMessage } from 'element-plus'
  6. import dmmn from '../assets/link.png'
  7. import ChangePassword from '@/components/changePassword.vue'
  8. import { useAdminStore } from '@/store'
  9. import { storeToRefs } from 'pinia'
  10. import { filterMenu, getRoutePath } from "@/utils/menuUtils.js";
  11. // 存储接口返回的菜单数据
  12. const menuList = ref([])
  13. // 获取仓库实例
  14. const adminStore = useAdminStore()
  15. // 解构状态(保持响应式) 获得 adminData(用户信息) 和 menuTree(菜单树)
  16. const { adminData, menuTree } = storeToRefs(adminStore)
  17. // 筛选权限菜单 ,menuTree 是组件通信拿的
  18. menuList.value = filterMenu(menuTree.value)
  19. console.log("menuList", menuList.value)
  20. // 获取当前路由
  21. const route = useRoute()
  22. // 通用函数:从菜单树中递归找出最匹配的 index
  23. function findBestMatch(menuList, path) {
  24. let bestMatch = ''
  25. function traverse(menus) {
  26. for (const item of menus) {
  27. const itemPath = getRoutePath(item)
  28. // 如果当前菜单的 path 是当前路径的前缀,可能是候选项
  29. if (path.startsWith(itemPath) && itemPath.length > bestMatch.length) {
  30. bestMatch = itemPath
  31. }
  32. if (item.children && item.children.length > 0) {
  33. traverse(item.children)
  34. }
  35. }
  36. }
  37. traverse(menuList)
  38. return bestMatch || path // fallback 到当前路径
  39. }
  40. // 响应式高亮菜单
  41. const activeMenu = computed(() => {
  42. return findBestMatch(menuList.value, route.path)
  43. })
  44. const router = useRouter()
  45. const imgrule1 = dmmn
  46. const messageVisible = ref(false)
  47. // 查看个人信息弹出框
  48. const openMessage = function () {
  49. messageVisible.value = true
  50. }
  51. // 关闭个人信息
  52. const closeMessage = function () {
  53. messageVisible.value = false
  54. }
  55. const message = function () {
  56. openMessage()
  57. }
  58. // 显示修改密码弹窗
  59. const showPasswordDialog = ref(false)
  60. const pwdRef = ref()
  61. //打开修改密码弹窗
  62. const openChangePassword = () => {
  63. showPasswordDialog.value = true
  64. }
  65. //关闭后清空密码表单
  66. function onPwdDialogClosed() {
  67. // 调用子组件暴露的 resetFields
  68. pwdRef.value?.resetFields()
  69. }
  70. function logout() {
  71. const machineId = localStorage.getItem('machineId')
  72. localStorage.removeItem('token')
  73. adminStore.clearState()
  74. router.push('/login?machineId=' + machineId)
  75. ElMessage.success('退出成功')
  76. }
  77. </script>
  78. <template>
  79. <div>
  80. <el-container>
  81. <el-aside >
  82. <div class="logo">
  83. <img src="../assets/新logo.png" alt="logo" style="width: 9vh; height: 9vh" />
  84. </div>
  85. <el-menu :router="true" :default-active="activeMenu" style="min-height: 80vh;border:none;">
  86. <!-- 递归渲染菜单层级 -->
  87. <template v-for="menu in menuList" :key="menu.id">
  88. <!-- 有子菜单的父级菜单menuType=2 且存在children -->
  89. <el-sub-menu v-if="menu.children && menu.children.length > 0" :index="menu.id.toString()">
  90. <template #title>
  91. <el-icon>
  92. <Folder />
  93. </el-icon>
  94. <span>{{ menu.menuName }}</span>
  95. </template>
  96. <!-- 子菜单 -->
  97. <template v-for="child in menu.children" :key="child.id">
  98. <!-- 子菜单为叶子节点无children -->
  99. <el-menu-item v-if="!child.children || child.children.length === 0" :index="getRoutePath(child)">
  100. <span>{{ child.menuName }}</span>
  101. </el-menu-item>
  102. <!-- 子菜单有下级 -->
  103. <el-sub-menu v-else :index="child.id.toString()">
  104. <template #title>
  105. <span>{{ child.menuName }}</span>
  106. </template>
  107. <!-- 递归 下一级-->
  108. <template v-for="grandChild in child.children" :key="grandChild.id">
  109. <el-menu-item :index="getRoutePath(grandChild)">
  110. <span>{{ grandChild.menuName }}</span>
  111. </el-menu-item>
  112. </template>
  113. </el-sub-menu>
  114. </template>
  115. </el-sub-menu>
  116. <!-- 无子菜单的一级菜单 -->
  117. <el-menu-item v-else :index="getRoutePath(menu)">
  118. <el-icon>
  119. <Folder />
  120. </el-icon>
  121. <span>{{ menu.menuName }}</span>
  122. </el-menu-item>
  123. </template>
  124. </el-menu>
  125. </el-aside>
  126. <el-container style="margin-left: 15vw;">
  127. <el-header class="header">
  128. <el-menu class="el-menu-demo" mode="horizontal" :ellipsis="false">
  129. <el-sub-menu index="1">
  130. <template #title>
  131. <el-image :src="imgrule1" style="width: 50px; height: 50px" />
  132. <span style="margin-left: 10px">{{ adminData.name }}</span>
  133. </template>
  134. <el-menu-item @click="message()">查看个人信息</el-menu-item>
  135. <el-menu-item @click="openChangePassword">修改密码</el-menu-item>
  136. <el-menu-item @click="logout">退出登录</el-menu-item>
  137. </el-sub-menu>
  138. </el-menu>
  139. </el-header>
  140. <el-main style="margin-top: 6vh;height: auto;">
  141. <router-view></router-view>
  142. </el-main>
  143. </el-container>
  144. </el-container>
  145. <!-- 查看个人信息 -->
  146. <el-dialog v-model="messageVisible" title="查看个人信息" width="500px">
  147. <el-form :model="adminData">
  148. <el-form-item label="用户姓名" label-width="100px" label-position="left">
  149. <span class="message-font">{{ adminData.adminName }}</span>
  150. </el-form-item>
  151. <el-form-item label="精网号" label-width="100px" label-position="left">
  152. <span class="message-font">{{ adminData.account }}</span>
  153. </el-form-item>
  154. <el-form-item label="地区" label-width="100px" label-position="left">
  155. <span class="message-font">{{ adminData.markets }}</span>
  156. </el-form-item>
  157. <el-form-item label="注册时间" label-width="100px" label-position="left">
  158. <span class="message-font">{{ adminData.createTime }}</span>
  159. </el-form-item>
  160. </el-form>
  161. <template #footer>
  162. <div>
  163. <el-button text @click="closeMessage()">关闭</el-button>
  164. </div>
  165. </template>
  166. </el-dialog>
  167. <!-- 自定义密码修改弹窗组件 -->
  168. <el-dialog v-model="showPasswordDialog" :center="true" width="470px" @closed="onPwdDialogClosed">
  169. <ChangePassword ref="pwdRef" @confirm="showPasswordDialog = false" />
  170. </el-dialog>
  171. </div>
  172. </template>
  173. <style scoped>
  174. .header {
  175. position: fixed;
  176. right: 0;
  177. z-index: 80;
  178. background: white;
  179. height: 8vh;
  180. }
  181. .message-font {
  182. font-size: 16px;
  183. font-weight: bold;
  184. }
  185. .el-aside {
  186. width: 15vw;
  187. position: fixed;
  188. z-index: 100;
  189. height: 90vh;
  190. }
  191. .logo {
  192. display: flex;
  193. align-items: center;
  194. justify-content: center;
  195. height: 12vh;
  196. }
  197. .el-menu-demo {
  198. border: none;
  199. padding: 0;
  200. float: right;
  201. }
  202. </style>