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.

281 lines
8.1 KiB

1 month ago
1 month ago
1 month ago
3 weeks ago
1 month 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. //打开修改密码弹窗
  61. const openChangePassword = () => {
  62. showPasswordDialog.value = true
  63. }
  64. function logout() {
  65. const machineId = localStorage.getItem('machineId')
  66. localStorage.removeItem('token')
  67. adminStore.clearState()
  68. router.push('/login?machineId=' + machineId)
  69. ElMessage.success('退出成功')
  70. }
  71. </script>
  72. <template>
  73. <div class="common-layout">
  74. <el-container>
  75. <el-aside style="
  76. width: 15%;
  77. min-width: 180px;
  78. position: fixed; /* 固定位置 */
  79. top: 0;
  80. left: 0;
  81. height: 100vh; /* 高度占满视口 */
  82. z-index: 100; /* 确保侧边栏在其他元素之上 */
  83. ">
  84. <div class="logo">
  85. <img src="../assets/新logo.png" alt="logo" style="width: 80px; height: 80px"/>
  86. <!-- <div style="font-size: 16px; font-weight: bold; color: black; text-align: center;" ><h1>海外金币管理系统</h1></div> -->
  87. </div>
  88. <el-card style="min-height: 90%;">
  89. <el-menu
  90. :router="true"
  91. class="el-menu-vertical-demo"
  92. :default-active="activeMenu"
  93. >
  94. <!-- 递归渲染菜单层级 -->
  95. <template v-for="menu in menuList" :key="menu.id">
  96. <!-- 有子菜单的父级菜单menuType=2 且存在children -->
  97. <el-sub-menu
  98. v-if="menu.children && menu.children.length > 0"
  99. :index="menu.id.toString()"
  100. >
  101. <template #title>
  102. <el-icon>
  103. <Folder/>
  104. </el-icon>
  105. <span>{{ menu.menuName }}</span>
  106. </template>
  107. <!-- 子菜单 -->
  108. <template v-for="child in menu.children" :key="child.id">
  109. <!-- 子菜单为叶子节点无children -->
  110. <el-menu-item
  111. v-if="!child.children || child.children.length === 0"
  112. :index="getRoutePath(child)"
  113. >
  114. <span>{{ child.menuName }}</span>
  115. </el-menu-item>
  116. <!-- 子菜单有下级 -->
  117. <el-sub-menu
  118. v-else
  119. :index="child.id.toString()"
  120. >
  121. <template #title>
  122. <span>{{ child.menuName }}</span>
  123. </template>
  124. <!-- 递归 下一级-->
  125. <template v-for="grandChild in child.children" :key="grandChild.id">
  126. <el-menu-item :index="getRoutePath(grandChild)">
  127. <span>{{ grandChild.menuName }}</span>
  128. </el-menu-item>
  129. </template>
  130. </el-sub-menu>
  131. </template>
  132. </el-sub-menu>
  133. <!-- 无子菜单的一级菜单 -->
  134. <el-menu-item
  135. v-else
  136. :index="getRoutePath(menu)"
  137. >
  138. <el-icon>
  139. <Folder/>
  140. </el-icon>
  141. <span>{{ menu.menuName }}</span>
  142. </el-menu-item>
  143. </template>
  144. </el-menu>
  145. </el-card>
  146. </el-aside>
  147. <el-container style="margin-left: 15%; min-width: 180px">
  148. <!-- 修改 el-header 样式 -->
  149. <el-header style="
  150. position: fixed;
  151. top: 0;
  152. left: 15%;
  153. right: 0;
  154. z-index: 80;
  155. background: white;
  156. ">
  157. <el-menu class="el-menu-demo" mode="horizontal" :ellipsis="false">
  158. <el-sub-menu index="1" class="admin">
  159. <template #title>
  160. <el-image :src="imgrule1" alt="错误" style="width: 50px; height: 50px"/>
  161. <span style="margin-left: 10px">{{ adminData.name }}</span>
  162. </template>
  163. <el-menu-item @click="message()">查看个人信息</el-menu-item>
  164. <el-menu-item @click="openChangePassword">修改密码</el-menu-item>
  165. <el-menu-item @click="logout">退出登录</el-menu-item>
  166. </el-sub-menu>
  167. </el-menu>
  168. </el-header>
  169. <el-main style="margin-top: 60px">
  170. <!-- 60px el-header 的大致高度可根据实际情况调整 -->
  171. <router-view></router-view>
  172. </el-main>
  173. </el-container>
  174. </el-container>
  175. <!-- 查看个人信息 -->
  176. <el-dialog v-model="messageVisible" title="查看个人信息" width="500px">
  177. <el-form :model="adminData">
  178. <el-form-item label="用户姓名" label-width="100px" label-position="left">
  179. <span class="message-font">{{ adminData.adminName }}</span>
  180. </el-form-item>
  181. <el-form-item label="精网号" label-width="100px" label-position="left">
  182. <span class="message-font">{{ adminData.account }}</span>
  183. </el-form-item>
  184. <el-form-item label="地区" label-width="100px" label-position="left">
  185. <span class="message-font">{{ adminData.markets }}</span>
  186. </el-form-item>
  187. <el-form-item label="注册时间" label-width="100px" label-position="left">
  188. <span class="message-font">{{ adminData.createTime }}</span>
  189. </el-form-item>
  190. </el-form>
  191. <template #footer>
  192. <div class="dialog-footer">
  193. <el-button text @click="closeMessage()">关闭</el-button>
  194. </div>
  195. </template>
  196. </el-dialog>
  197. <!-- 自定义密码修改弹窗组件 -->
  198. <el-dialog
  199. v-model="showPasswordDialog"
  200. :center="true"
  201. width="470px"
  202. >
  203. <ChangePassword @confirm="showPasswordDialog = false"/>
  204. </el-dialog>
  205. </div>
  206. </template>
  207. <style scoped>
  208. .message-font {
  209. font-size: 16px;
  210. font-weight: bold;
  211. }
  212. .item {
  213. margin-top: 20px;
  214. margin-right: 40px;
  215. }
  216. .admin {
  217. margin-left: auto;
  218. }
  219. .el-aside {
  220. min-height: 100vh;
  221. width: 200px;
  222. }
  223. /* background-color: #BFD8D2; */
  224. .logo {
  225. margin: 20px 0px 20px 20px;
  226. display: flex;
  227. }
  228. .el-menu-demo {
  229. border: none;
  230. /* 去除边框 */
  231. padding: 0;
  232. /* 去除内边距 */
  233. float: right;
  234. /* 将菜单向右浮动 */
  235. }
  236. .el-menu-vertical-demo:not(.el-menu--collapse) {
  237. width: 240px;
  238. min-height: 400px;
  239. border: none;
  240. /* 去除边框 */
  241. }
  242. </style>