#
Junjie
2024-07-03 acebf48e2e139f6f298cfbba829cc3c29db3c891
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<script setup>
import { nextTick, ref, inject } from 'vue';
import { useRouter } from "vue-router";
import { get, post, postForm } from '@/utils/request.js'
import { logout } from '@/config.js';
import * as Icons from "@ant-design/icons-vue";
import { message } from 'ant-design-vue';
import {
  MenuUnfoldOutlined,
  MenuFoldOutlined,
  HomeOutlined,
  CloseOutlined,
  RedoOutlined,
  UserOutlined,
  TranslationOutlined,
  ApartmentOutlined,
} from "@ant-design/icons-vue";
import { formatMessage } from '@/utils/localeUtils.js';
 
const globalState = inject('globalState');
const selectedKeys = ref([]);
const collapsed = ref(false);
const router = useRouter();
let routerCache = ref([]);
let routerCacheList = ref([]);
let currentCache = ref(null);
let isRouterAlive = ref(true);
const menuCache = ref([]);
const hostList = ref([]);
 
const components = {
  ...Icons,
};
 
getMenu()
function getMenu() {
  get('/api/auth/menu', {}).then((result) => {
    menuCache.value = result.data.data;
  })
}
 
function menuSelect(item) {
  router.push({
    path: item.key
  })
 
  let name = item.item.name;
  currentCache.value = name;
 
  if (name != undefined && routerCache.value.indexOf(name) == -1) {
    routerCache.value.push(item.item.name)
    routerCacheList.value.push({
      key: item.key,
      languageId: item.item.languageId,
      name: item.item.name,
    })
  }
}
 
function closeTabs(param) {
  let name = param.name;
  let tmp = []
  let tmpList = [];
  routerCache.value.forEach((item) => {
    if (item != name) {
      tmp.push(item);
    }
  })
 
  routerCacheList.value.forEach((item) => {
    if (item.name != name) {
      tmpList.push(item);
    }
  })
 
  if (tmp == 0) {
    router.push({
      path: '/'
    })
    routerCache.value.push('home')
    routerCacheList.value.push({
      key: '/',
      languageId: 'common.home',
      name: '主页',
    })
    selectedKeys.value = ['/']
  } else {
    switchTabs(tmpList[0]);
  }
  routerCache.value = tmp;
  routerCacheList.value = tmpList;
}
 
function reloadTabs() {
  const hide = message.loading(formatMessage('common.loading', '加载中'));
  try {
    isRouterAlive.value = false;
    nextTick(() => {
      isRouterAlive.value = true;
      message.success(formatMessage('common.success', '加载成功'));
    })
  } catch (error) {
    message.error(formatMessage('common.fail', '加载失败'));
  } finally {
    hide();
  }
}
 
function closeAllTabs() {
  routerCache.value = [];
  routerCacheList.value = [];
  router.push({
    path: '/'
  })
}
 
function switchTabs(item) {
  router.push({
    path: item.key
  })
 
  currentCache.value = item.name;
  selectedKeys.value = [item.key]
}
 
const switchLocale = (locale) => {
  globalState.locale = locale;
  localStorage.setItem('locale', locale)
  reloadTabs()
}
 
getHostList()
function getHostList() {
  post('/api/show/host.action', {}).then((resp) => {
    let result = resp.data;
    let data = result.data;
    let hostId = data.hostId;
    if (data.root) {
      post('/api/host/list', {}).then((resp) => {
        let result = resp.data;
        let data = result.data;
        hostList.value = data;
        data.forEach((item) => {
          if (item.id == hostId) {
            globalState.currentHost = item;
          }
        })
      })
    }
  })
}
 
const switchHost = (item) => {
  globalState.currentHost = item;
  postForm('/api/root/change/host/auth', {
    hostId: item.id
  }).then((resp) => {
    let result = resp.data;
    if (result.code == 200) {
      window.location.reload();
    } else {
      message.error(formatMessage('common.fail', '加载失败'));
    }
  })
}
 
</script>
 
<template>
  <a-layout class="main">
    <a-layout-sider v-model:collapsed="collapsed" :trigger="null" collapsible>
      <div class="logo" />
      <a-menu v-for="(item, index) in menuCache" :key="index" v-model:selectedKeys="selectedKeys" @select="menuSelect"
        theme="dark" mode="inline">
        <div>
          <a-menu-item key="/" name="主页">
            <HomeOutlined /> {{ formatMessage('common.home', '主页') }}
          </a-menu-item>
        </div>
 
        <a-sub-menu v-if="item.type == 0">
          <template #title>
            <span>
              <component :is="components[ref(item.icon).value]" />
              {{ formatMessage(item.languageId, item.name) }}
            </span>
          </template>
          <div v-for="(child, idx) in item.children">
            <a-menu-item v-if="child.status == 1" :key="child.route" :name="child.name" :languageId="child.languageId">
              {{ formatMessage(child.languageId, child.name) }}
            </a-menu-item>
          </div>
        </a-sub-menu>
      </a-menu>
 
    </a-layout-sider>
    <a-layout>
      <a-layout-header style="background: #fff; padding: 0">
        <div class="header-top">
          <div class="header-top-left">
            <MenuUnfoldOutlined v-if="collapsed" class="trigger" @click="() => (collapsed = !collapsed)" />
            <MenuFoldOutlined v-else class="trigger" @click="() => (collapsed = !collapsed)" />
          </div>
          <div class="header-top-right">
            <div class="trigger" v-if="globalState.currentHost">
              <a-dropdown>
                <div>
                  <ApartmentOutlined />
                  {{ globalState.currentHost?.name }}
                </div>
                <template #overlay>
                  <a-menu>
                    <a-menu-item v-for="(item, index) in hostList" :key="index" @click="switchHost(item)"
                      :class="globalState.currentHost?.id == item.id ? 'active' : ''">{{ item.name }}</a-menu-item>
                  </a-menu>
                </template>
              </a-dropdown>
            </div>
            <div class="trigger">
              <a-dropdown>
                <TranslationOutlined />
                <template #overlay>
                  <a-menu>
                    <a-menu-item @click="switchLocale('enUS')"
                      :class="globalState.locale == 'enUS' ? 'active' : ''">English</a-menu-item>
                    <a-menu-item @click="switchLocale('zhCN')"
                      :class="globalState.locale == 'zhCN' ? 'active' : ''">简体中文</a-menu-item>
                  </a-menu>
                </template>
              </a-dropdown>
            </div>
            <div>
              <a-dropdown>
                <a class="header-user" @click.prevent>
                  <UserOutlined />
                  <span>{{ globalState.user.username }}</span>
                </a>
                <template #overlay>
                  <a-menu @click="logout">
                    <a-menu-item key="logout">{{ formatMessage('common.account.logout', '退出') }}</a-menu-item>
                  </a-menu>
                </template>
              </a-dropdown>
            </div>
          </div>
        </div>
      </a-layout-header>
      <a-layout-content class="content-view">
        <div class="tabs-fixed">
          <div v-for="(item, index) in routerCacheList" :key="index" @click="switchTabs(item)" class="tabs-item"
            :class="currentCache == item.name ? 'tabs-item-active' : ''">
            <div :class="currentCache == item.name ? '' : 'tabs-item-reload-none'" @click="reloadTabs" @click.stop>
              <RedoOutlined />
            </div>
            <div>{{ formatMessage(item.languageId, item.name) }}</div>
            <div @click="closeTabs(item)" @click.stop>
              <CloseOutlined />
            </div>
          </div>
        </div>
        <router-view v-slot="{ Component, route }" v-if="isRouterAlive">
          <keep-alive :include="routerCache">
            <component :is="Component" />
          </keep-alive>
        </router-view>
      </a-layout-content>
    </a-layout>
  </a-layout>
</template>
 
<style scoped></style>