LayoutHeader.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <template>
  2. <div class="layout-header">
  3. <div class="logo"></div>
  4. <div class="menu">
  5. <el-menu :default-active="currentPath" ref="menuRef" class="el-menu-demo" mode="horizontal">
  6. <el-menu-item :index="item.path" v-for="item in routeList" :key="item.name" :class="{ 'external-link-item': isExternalLink(item.path) }"
  7. @click="handleMenuClick(item.path)">{{ item.title }}</el-menu-item>
  8. </el-menu>
  9. </div>
  10. <!-- 机构切换下拉 -->
  11. <div style="align-self: center; margin-right: 16px;">
  12. <el-select v-model="currentOrg" placeholder="请选择机构" style="width: 160px" @change="changeOrg">
  13. <el-option v-for="org in orgList" :key="org.id" :label="org.organ_name" :value="org.id" />
  14. </el-select>
  15. </div>
  16. <!-- 用户下拉 -->
  17. <div style="align-self: center;">
  18. <el-dropdown style="margin-top:10px">
  19. <div style="font-size:medium;font-weight: bold; color:#000">{{ user.full_name }}</div>
  20. <template #dropdown>
  21. <el-dropdown-menu>
  22. <el-dropdown-item>用户资料</el-dropdown-item>
  23. <el-dropdown-item @click="editPassShow = true">修改密码</el-dropdown-item>
  24. <el-dropdown-item @click="handleLogout">登出</el-dropdown-item>
  25. </el-dropdown-menu>
  26. </template>
  27. </el-dropdown>
  28. </div>
  29. </div>
  30. <EditPasswordDialog v-model="editPassShow" />
  31. </template>
  32. <script setup>
  33. import { ref, computed, onMounted, onBeforeUnmount,getCurrentInstance } from 'vue'
  34. import { useMenuStore } from "@/stores/menu.js"
  35. import { useRoute, useRouter } from "vue-router";
  36. import { getSessionVar, clearSessionVar, saveSessionVar } from '@/utils/session'
  37. import EditPasswordDialog from "@/components/EditPasswordDialog.vue"
  38. import { knowledgeGraphAddr } from "@/utils/config"
  39. const { updateRouteList } = useMenuStore();
  40. const { proxy } = getCurrentInstance()
  41. const route = useRoute()
  42. const router = useRouter()
  43. let timer
  44. const menuRef = ref()
  45. const { routeList } = useMenuStore()
  46. const user = ref({
  47. id: "0",
  48. full_name: 'John Doe',
  49. username: 'johndoe',
  50. })
  51. user.value = {
  52. id: getSessionVar('user_id') || "0",
  53. full_name: getSessionVar('full_name') || 'John Doe',
  54. username: getSessionVar('username') || 'johndoe',
  55. }
  56. // 机构相关
  57. const orgList = ref([])
  58. const currentOrg = ref(getSessionVar('org_id') || '') // 当前机构id
  59. // 获取机构列表
  60. const fetchOrgList = async () => {
  61. // 假设接口返回 { records: [{id, name}, ...] }
  62. const { records } = await proxy.$http.get('/open-platform/sys/loadSURO')
  63. orgList.value = records
  64. // 默认选中第一个
  65. const res = await proxy.$http.get('/open-platform/sys/currSURO')
  66. console.log('当前机构11:', res)
  67. currentOrg.value = res
  68. saveSessionVar('org_id', res)
  69. console.log('机构列表:', orgList.value)
  70. console.log('当前机构:', currentOrg.value)
  71. }
  72. // 切换机构
  73. const changeOrg = async (orgId) => {
  74. // 可调用后端切换机构接口
  75. const res = await proxy.$http.post(`/open-platform/sys/changeSURO/${orgId}`)
  76. console.log('切换机构结果:', res)
  77. saveSessionVar('org_id', orgId)
  78. // 可选:刷新页面或重新拉取权限/菜单等
  79. updateRouteList([]);
  80. let knowledageSystem = '';
  81. let routeList = [{
  82. path: '/kmplatform/home',
  83. name: 'kmplatform-home',
  84. title: "主页",
  85. children: []
  86. }]
  87. res.records[0].menu_permissions.sort((a, b) => {
  88. return a.id - b.id;
  89. });
  90. res.records[0].menu_permissions.forEach((item) => {
  91. if (item.menu_name == "知识更新管理") {
  92. knowledageSystem = 'true';
  93. routeList.push({
  94. path: knowledgeGraphAddr,
  95. name: '',
  96. title: item.name,
  97. children: item.children,
  98. });
  99. } else if (item.menu_route) {
  100. routeList.push({
  101. path: item.menu_route,
  102. name: item.menu_route.split("/")[2],
  103. title: item.name,
  104. children: item.children,
  105. });
  106. }
  107. });
  108. console.log("knowledageSystem", knowledageSystem);
  109. saveSessionVar("knowledageSystem", knowledageSystem);
  110. saveSessionVar('routeList', JSON.stringify(routeList))
  111. updateRouteList(routeList);
  112. // window.location.reload()
  113. }
  114. let editPassShow = ref(false)
  115. const currentPath = computed(() => {
  116. let temp = ""
  117. for (let i = 0; i < routeList.length; i++) {
  118. for (let j = 0; j < route.matched.length; j++) {
  119. if (routeList[i].path === route.matched[j].path) {
  120. temp = route.matched[j].path
  121. break
  122. }
  123. }
  124. if (temp) break;
  125. }
  126. return temp
  127. })
  128. const handleLogout = () => {
  129. router.push({ path: '/login' });
  130. clearSessionVar()
  131. }
  132. const isExternalLink = (path) => {
  133. return /^https?/g.test(path);
  134. };
  135. function handleMenuClick(path) {
  136. if (/^https?/g.test(path)) {
  137. const newWindow = window.open(path, '_blank');
  138. timer = setInterval(() => {
  139. newWindow?.postMessage({ type: 'login', username: getSessionVar("full_name"), userID: getSessionVar("user_id") }, "*")
  140. }, 1000)
  141. menuRef.value.updateActiveIndex(currentPath.value)
  142. } else {
  143. if (route.path.includes(path)) return
  144. router.push({ path: path })
  145. }
  146. }
  147. function handleLogin(event) {
  148. const { type, status } = event.data
  149. if (type === 'login' && status === 'ok') {
  150. clearInterval(timer)
  151. }
  152. }
  153. onMounted(() => {
  154. window.addEventListener('message', handleLogin);
  155. fetchOrgList()
  156. })
  157. onBeforeUnmount(() => {
  158. window.removeEventListener('message', handleLogin);
  159. clearInterval(timer)
  160. })
  161. </script>
  162. <style lang="less" scoped>
  163. .layout-header {
  164. display: flex;
  165. border-bottom: 1px solid #C4C2C2;
  166. // padding-bottom: 10px;
  167. .logo {
  168. width: 200px;
  169. // height: 60px;
  170. // border: 1px solid skyblue;
  171. flex: 0 0 auto;
  172. margin-right: 50px;
  173. background: url('../assets/images/logo.jpg') no-repeat center;
  174. background-size: 100% 100%;
  175. }
  176. .menu {
  177. flex: 1 1 auto;
  178. // .el-menu--horizontal>.el-menu-item.is-active {
  179. // border: none;
  180. // }
  181. .el-menu--horizontal.el-menu {
  182. border: none;
  183. }
  184. ::v-deep .el-menu {
  185. .el-menu-item {
  186. font-size: 20px;
  187. }
  188. }
  189. .external-link-item {
  190. // 覆盖激活效果
  191. &.is-active {
  192. background-color: transparent !important;
  193. color: inherit !important;
  194. }
  195. // 覆盖点击效果
  196. &:focus {
  197. background-color: transparent !important;
  198. color: inherit !important;
  199. }
  200. }
  201. }
  202. }
  203. </style>