index.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. <template>
  2. <div class="navbar-wrap d-flex align-items-center" @click.stop.prevent="handleCloseSidebar">
  3. <template v-if="authInfoLoaded">
  4. <a-tooltip :title="$t('navbar.button.menu')" placement="right">
  5. <div class="d-flex align-items-center navbar-item-trigger justify-content-center global-map-btn ml-1 flex-shrink-0 flex-grow-0" @click.stop.prevent="handleToggleSidebar">
  6. <icon type="menu" style="font-size: 24px;" />
  7. </div>
  8. </a-tooltip>
  9. </template>
  10. <template v-else>
  11. <div class="d-flex align-items-center h-100 navbar-item-trigger flex-shrink-0 flex-grow-0">
  12. <icon type="menu" style="font-size: 24px; cursor: default;" />
  13. </div>
  14. </template>
  15. <div class="flex-fill d-flex align-items-center h-100">
  16. <div class="header-logo ml-2">
  17. <img class="logo" :src="logo" />
  18. </div>
  19. <h1 class="header-title ml-3">{{ $t('common_210') }}</h1>
  20. <!-- 视图选择 -->
  21. <div class="navbar-item primary-color-hover d-flex align-items-center justify-content-end flex-shrink-0 flex-grow-0" v-if="showViewSelection">
  22. <a-popover
  23. trigger="click"
  24. v-model="viewChangePopoverVisible"
  25. destroyTooltipOnHide
  26. :getPopupContainer="triggerNode => triggerNode.parentNode">
  27. <template slot="content">
  28. <ul class="list-unstyled view-list-wrap" style="max-height: 60vh; overflow-y: auto;">
  29. <!-- 管理后台 -->
  30. <template v-if="systemProjects && systemProjects.length">
  31. <li v-if="systemProjects.length === 1" class="item-link" @click="() => projectChange(systemProjects[0].id, 'system')">
  32. <div class="d-flex h-100 align-items-center">
  33. <div class="flex-fill text-truncate">{{ $t('navbar.view.system_manager') }}</div>
  34. <div style="width: 20px;" class="ml-1">
  35. <a-icon v-show="scope === 'system' && systemProjects[0].id === userInfo.projectId" type="check-circle" theme="twoTone" twoToneColor="#52c41a" />
  36. </div>
  37. </div>
  38. </li>
  39. <li v-else>
  40. <div>{{ $t('navbar.view.system_manager') }}</div>
  41. <ul class="list-unstyled">
  42. <template v-for="item of systemProjects">
  43. <li class="item-link" :key="item.id" @click="() => projectChange(item.id, 'system')">
  44. <div class="d-flex h-100 align-items-center">
  45. <div class="flex-fill text-truncate">{{ item.name }}({{ item.domain }})</div>
  46. <div style="width: 20px;" class="ml-1">
  47. <a-icon v-show="scope === 'system' && item.id === userInfo.projectId" type="check-circle" theme="twoTone" twoToneColor="#52c41a" />
  48. </div>
  49. </div>
  50. </li>
  51. </template>
  52. </ul>
  53. </li>
  54. </template>
  55. <!-- 域管理后台 -->
  56. <template v-if="domainProjects && domainProjects.length">
  57. <li>
  58. <div>{{$t('navbar.view.domain_manager')}}</div>
  59. <ul class="list-unstyled">
  60. <template v-for="item of domainProjects">
  61. <li class="item-link" :key="item.id" @click="() => projectChange(item.id, 'domain')">
  62. <div class="d-flex h-100 align-items-center">
  63. <div class="flex-fill text-truncate" v-if="isSingleProject(domainProjects, item)">{{ item.domain }}</div>
  64. <div class="flex-fill text-truncate" v-else>{{ item.domain }}({{ item.name }})</div>
  65. <div style="width: 20px;" class="ml-1">
  66. <a-icon v-show="scope === 'domain' && item.id === userInfo.projectId" type="check-circle" theme="twoTone" twoToneColor="#52c41a" />
  67. </div>
  68. </div>
  69. </li>
  70. </template>
  71. </ul>
  72. </li>
  73. </template>
  74. <!-- 项目 -->
  75. <template v-if="projects && projects.length">
  76. <li>
  77. <div>{{$t('navbar.view.project')}}</div>
  78. <ul class="list-unstyled">
  79. <template v-for="item of projects">
  80. <li class="item-link" :key="item.id" @click="() => projectChange(item.id, 'project')">
  81. <div class="d-flex h-100 align-items-center">
  82. <div class="flex-fill text-truncate">{{ item.name }}</div>
  83. <div style="width: 20px;" class="ml-1">
  84. <a-icon v-show="scope === 'project' && item.id === userInfo.projectId" type="check-circle" theme="twoTone" twoToneColor="#52c41a" />
  85. </div>
  86. </div>
  87. </li>
  88. </template>
  89. </ul>
  90. </li>
  91. </template>
  92. </ul>
  93. </template>
  94. <div class="navbar-item-trigger d-flex align-items-center justify-content-center">
  95. <a-tooltip :title="$t('navbar.view.switch')" placement="right">
  96. <icon type="navbar-view-switch" style="font-size: 24px; line-height: normal;" />
  97. </a-tooltip>
  98. <span class="ml-2 current-view-label text-truncate" style="line-height: normal;" :title="viewLabel">{{ viewLabel }}</span>
  99. <icon type="caret-down" style="font-size: 24px; line-height: normal;" />
  100. </div>
  101. </a-popover>
  102. </div>
  103. <!-- 系统选择 -->
  104. <div class="navbar-item d-flex align-items-center justify-content-end" v-if="products">
  105. <a-dropdown :trigger="['click']">
  106. <div class="navbar-item-trigger d-flex align-items-center justify-content-center">
  107. <icon type="navbar-setting" style="font-size: 24px; line-height: 1;" />
  108. <span class="ml-2">{{$t('dictionary.endpoint')}}</span>
  109. <icon type="caret-down" style="font-size: 24px; line-height: normal;" />
  110. </div>
  111. <a-menu slot="overlay" @click="productChange">
  112. <a-menu-item v-for="item of products" :key="item.key">{{ item.label }}</a-menu-item>
  113. </a-menu>
  114. </a-dropdown>
  115. </div>
  116. </div>
  117. <!-- 资源报警 -->
  118. <alertresource v-if="showAlertresource" :res_total="alertresource.total" class="navbar-item-icon primary-color-hover" />
  119. <!-- 消息中心 -->
  120. <notify-popover class="navbar-item-icon primary-color-hover" :notifyMenuTitleUsedText="notifyMenuTitleUsedText" v-if="showNotify" />
  121. <!-- cloudshell -->
  122. <cloud-shell v-if="isAdminMode" class="navbar-item-icon primary-color-hover" />
  123. <!-- 更多 -->
  124. <more-popover class="navbar-item-icon primary-color-hover" :showMenuMap="{more: true, docs: true, about: true}" />
  125. <div class="navbar-item">
  126. <a-dropdown :trigger="['click']">
  127. <!-- <div class="navbar-item-trigger d-flex align-items-center justify-content-center">
  128. <icon type="navbar-user" style="font-size: 24px;" />
  129. </div> -->
  130. <div class="navbar-item-trigger d-flex align-items-center justify-content-center">
  131. <a-avatar style="color: #fff;" class="primary-color-bg">{{ firstNameWord }}</a-avatar>
  132. <span class="ml-2 text-truncate" style="max-width: 100px;">{{ username }}</span>
  133. </div>
  134. <a-menu slot="overlay" @click="userMenuClick">
  135. <a-sub-menu v-if="!supportLanguages.length || supportLanguages.length > 1" key="language">
  136. <span slot="title"><a-icon class="mr-2 ml-2" type="global" /><span>{{$t('common_630')}}</span></span>
  137. <a-menu-item v-if="!supportLanguages.length || supportLanguages.includes('zh-CN')" key="3" @click="settingLanguageCH">
  138. <span class="mr-2" style="cursor: pointer">简体中文</span><a-icon v-show="language === 'zh-CN'" type="check-circle" theme="twoTone" twoToneColor="#52c41a" />
  139. </a-menu-item>
  140. <a-menu-item v-if="!supportLanguages.length || supportLanguages.includes('en')" key="4" @click="settingLanguageEN">
  141. <span class="mr-2" style="cursor: pointer">English</span><a-icon v-show="language === 'en'" type="check-circle" theme="twoTone" twoToneColor="#52c41a" />
  142. </a-menu-item>
  143. <a-menu-item v-if="!supportLanguages.length || supportLanguages.includes('ja-JP')" key="5" @click="settingLanguageJP">
  144. <span class="mr-2" style="cursor: pointer">日本語</span><a-icon v-show="language === 'ja-JP'" type="check-circle" theme="twoTone" twoToneColor="#52c41a" />
  145. </a-menu-item>
  146. </a-sub-menu>
  147. <a-menu-item key="toClouduser" v-if="showClouduser"><a-icon class="mr-2 ml-2" type="cloud-upload" />{{ $t('scope.cloudid') }}</a-menu-item>
  148. <a-menu-item key="toCredentials"><icon class="mr-2 ml-2" type="access-credentials" />{{ $t('common_631') }}</a-menu-item>
  149. <a-menu-item key="handleUpdatePassword"><a-icon class="mr-2 ml-2" type="usergroup-delete" />{{ $t('scope.text_5') }}</a-menu-item>
  150. <a-menu-item key="logout"><a-icon class="mr-2 ml-2" type="logout" />{{ $t('scope.text_6') }}</a-menu-item>
  151. </a-menu>
  152. </a-dropdown>
  153. </div>
  154. </div>
  155. </template>
  156. <script>
  157. import get from 'lodash/get'
  158. import * as R from 'ramda'
  159. import { mapGetters, mapState } from 'vuex'
  160. import storage from '@/utils/storage'
  161. import Alertresource from '@/sections/Navbar/components/Alertresource'
  162. import { setLanguage } from '@/utils/common/cookie'
  163. import CloudShell from '@/sections/Navbar/components/CloudShell'
  164. import NotifyPopover from '@/sections/Navbar/components/NotifyPopover'
  165. import MorePopover from '@/sections/Navbar/components/MorePopover'
  166. import WindowsMixin from '@/mixins/windows'
  167. import { hasSetupKey } from '@/utils/auth'
  168. export default {
  169. name: 'Navbar',
  170. components: {
  171. CloudShell,
  172. Alertresource,
  173. NotifyPopover,
  174. MorePopover,
  175. },
  176. mixins: [WindowsMixin],
  177. props: {
  178. showViewSelection: {
  179. type: Boolean,
  180. default: true,
  181. },
  182. showNotify: {
  183. type: Boolean,
  184. default: true,
  185. },
  186. notifyMenuTitleUsedText: {
  187. type: Boolean,
  188. default: false,
  189. },
  190. },
  191. data () {
  192. return {
  193. viewChangePopoverVisible: false,
  194. }
  195. },
  196. computed: {
  197. ...mapGetters(['isAdminMode', 'userInfo', 'scope', 'logo', 'permission', 'scopeResource', 'setting']),
  198. ...mapState('app', {
  199. alertresource: state => state.alertrecords,
  200. }),
  201. username () {
  202. return this.userInfo.displayname || this.userInfo.name || 'OneCloud'
  203. },
  204. firstNameWord () {
  205. const word = this.username.split('')[0]
  206. return word && word.toUpperCase()
  207. },
  208. products () {
  209. if (this.userInfo.menus && this.userInfo.menus.length > 0) {
  210. const menus = this.userInfo.menus.map(item => {
  211. return {
  212. key: item.url,
  213. label: item.name,
  214. }
  215. })
  216. return menus
  217. }
  218. return null
  219. },
  220. projects () {
  221. return R.sort((a, b) => {
  222. return a.name.localeCompare(b.name)
  223. }, this.userInfo.projects)
  224. },
  225. systemProjects () {
  226. return this.projects.filter(v => v.system_capable === true)
  227. },
  228. domainProject () {
  229. return R.find(R.propEq('domain_capable', true))(this.projects)
  230. },
  231. domainProjects () {
  232. const ret = this.projects.filter(v => v.domain_capable === true)
  233. return R.uniqWith((a, b) => {
  234. return this.isSingleProject(ret, a) && this.isSingleProject(ret, b) &&
  235. a.domain_id === b.domain_id && R.equals(a.domain_policies, b.domain_policies)
  236. })(ret)
  237. },
  238. viewLabel () {
  239. if (this.reLogging) return '-'
  240. if (this.$store.getters['auth/isAdmin']) {
  241. return this.$t('navbar.view.system_manager')
  242. }
  243. if (this.$store.getters['auth/isDomain']) {
  244. return this.isOperation ? this.$t('navbar.view.manager') : this.$t('navbar.view.domain_manager_1var', { domain: this.userInfo.projectDomain || '-' })
  245. }
  246. return this.userInfo.projectName || '-'
  247. },
  248. // 认证信息加载完毕
  249. authInfoLoaded () {
  250. return !!this.userInfo.roles && !!this.permission && !!this.scopeResource
  251. },
  252. language () {
  253. return this.setting.language
  254. },
  255. showAlertresource () {
  256. if (this.isAdminMode) {
  257. if (this.alertresource) {
  258. return this.alertresource.total > 0
  259. }
  260. }
  261. return false
  262. },
  263. showClouduser () {
  264. return hasSetupKey(['public', 'hcso'])
  265. },
  266. globalSettingSetupKeys () {
  267. const { globalSetting } = this.$store.state
  268. if (globalSetting && globalSetting.value && globalSetting.value.setupKeys) {
  269. return globalSetting.value.setupKeys
  270. }
  271. return []
  272. },
  273. supportLanguages () {
  274. const setupKeys = this.globalSettingSetupKeys || []
  275. const languages = setupKeys.filter(item => ['zh-CN', 'en', 'ja-JP'].includes(item))
  276. return languages
  277. },
  278. },
  279. watch: {
  280. userInfo: {
  281. handler (val, oldVal = {}) {
  282. if (val.id !== oldVal.id) {
  283. if ((R.isNil(val.projects) || R.isEmpty(val.projects)) && (R.isNil(val.projectId) || R.isEmpty(val.projectId))) {
  284. this.$router.push('/no-project')
  285. }
  286. this.fetchOEM(val)
  287. }
  288. },
  289. immediate: true,
  290. },
  291. supportLanguages: {
  292. handler: function (val) {
  293. this.setSupportLanguages(val)
  294. if (val.length && !val.includes(this.language)) {
  295. const navigatorL = navigator.language || navigator.userLanguage
  296. setLanguage(val.includes(navigatorL) ? navigatorL : val[0])
  297. window.location.reload()
  298. }
  299. },
  300. immediate: true,
  301. },
  302. },
  303. created () {
  304. this.pushApiServerUrlAlert(this.userInfo.id)
  305. this.cronjobFetchAlerts()
  306. },
  307. methods: {
  308. async userMenuClick (item) {
  309. if (item.key === 'logout') {
  310. try {
  311. await this.$store.dispatch('auth/logout')
  312. this.$router.push('/auth/login')
  313. } catch (error) {
  314. throw error
  315. }
  316. } else if (item.key === 'handleUpdatePassword') {
  317. this.createDialog('UpdateUserPasswordDialog')
  318. } else if (item.key === 'toClouduser') {
  319. this.$router.push('/clouduser')
  320. } else if (item.key === 'toCredentials') {
  321. this.$router.push('/credentials')
  322. }
  323. },
  324. projectChange (id, scope) {
  325. this.viewChangePopoverVisible = false
  326. if (this.userInfo.projectId === id && this.scope === scope) return
  327. this.reLogin(id, scope)
  328. },
  329. productChange (item) {
  330. window.open(item.key, '_blank')
  331. },
  332. async reLogin (projectId, scope) {
  333. this.reLogging = true
  334. try {
  335. await this.$store.dispatch('auth/login', {
  336. tenantId: projectId,
  337. })
  338. await this.$store.commit('auth/SET_SCOPE', scope)
  339. await this.$store.commit('auth/SET_TENANT', projectId)
  340. await this.$store.commit('auth/UPDATE_HISTORY_USERS', {
  341. key: this.$store.getters['auth/currentLoggedUserKey'],
  342. value: {
  343. tenant: projectId,
  344. scope,
  345. },
  346. })
  347. await this.$store.commit('auth/UPDATE_HISTORY_USERS', {
  348. key: this.$store.getters['auth/currentLoggedUserKey'],
  349. value: {
  350. tenant: projectId,
  351. scope,
  352. },
  353. })
  354. await this.$store.dispatch('scopedPolicy/get', {
  355. category: ['sub_hidden_menus'],
  356. })
  357. window.location.reload()
  358. return true
  359. } catch (error) {
  360. throw error
  361. } finally {
  362. this.reLogging = false
  363. }
  364. },
  365. handleToggleSidebar () {
  366. const drawerVisible = !(get(this.$store.getters, 'common.sidebar.drawerVisible', false))
  367. this.$store.dispatch('common/updateObject', {
  368. name: 'sidebar',
  369. data: {
  370. drawerVisible,
  371. },
  372. })
  373. },
  374. handleCloseSidebar () {
  375. this.$store.dispatch('common/updateObject', {
  376. name: 'sidebar',
  377. data: {
  378. drawerVisible: false,
  379. },
  380. })
  381. },
  382. settingLanguageCH () {
  383. setLanguage('zh-CN')
  384. window.location.reload()
  385. },
  386. settingLanguageEN () {
  387. setLanguage('en')
  388. window.location.reload()
  389. },
  390. settingLanguageJP () {
  391. setLanguage('ja-JP')
  392. window.location.reload()
  393. },
  394. isSingleProject (projects, item) {
  395. const sameDomainProjects = projects.filter(v => v.domain_id === item.domain_id)
  396. const num = sameDomainProjects?.length
  397. if (num > 1) {
  398. const isAll = sameDomainProjects.every(v => {
  399. return R.equals(v.domain_policies, item.domain_policies)
  400. })
  401. if (isAll) {
  402. return true
  403. }
  404. }
  405. return num === 1
  406. },
  407. cronjobFetchAlerts () { // 定时5分钟请求一次
  408. const userInfo = this.$store.getters.userInfo
  409. if ((R.isNil(userInfo.projects) || R.isEmpty(userInfo.projects)) && (R.isNil(userInfo.projectId) || R.isEmpty(userInfo.projectId))) {
  410. return
  411. }
  412. this.$store.dispatch('app/fetchAlertingrecords')
  413. setInterval(() => {
  414. this.$store.dispatch('app/fetchAlertingrecords')
  415. }, 5 * 60 * 1000)
  416. if (this.isAdminMode && this.$store._actions['app/fetchAlertresource']) {
  417. // this.$store.dispatch('app/fetchAlertresource')
  418. // setInterval(() => {
  419. // this.$store.dispatch('app/fetchAlertresource')
  420. // }, 5 * 60 * 1000)
  421. } else {
  422. this.$store.commit('app/SET_ALERTRESOURCE', {
  423. data: [],
  424. total: 0,
  425. })
  426. }
  427. },
  428. async pushApiServerUrlAlert (id) {
  429. if (!id) return
  430. try {
  431. const params = {}
  432. if (this.$store.getters.scope !== 'system') {
  433. params.domain = this.$store.getters.userInfo.projectDomain
  434. }
  435. const regions = await this.$store.dispatch('auth/getRegions', params)
  436. const currentHost = window.location.hostname
  437. const apiServer = regions.api_server || ''
  438. if (apiServer) {
  439. if (!apiServer.includes(currentHost)) {
  440. this.$store.dispatch('common/updateObject', {
  441. name: 'topAlert',
  442. data: {
  443. apiServer: {
  444. messageOptions: [
  445. this.$t('common_222'),
  446. ['a', { attrs: { href: apiServer } }, apiServer],
  447. this.$t('common_223'),
  448. ],
  449. interval: 1000 * 60 * 60 * 24,
  450. },
  451. },
  452. })
  453. }
  454. }
  455. } catch (error) {
  456. throw error
  457. }
  458. },
  459. fetchOEM (val) {
  460. if (val) {
  461. this.$store.dispatch('app/fetchOEM')
  462. }
  463. },
  464. setSupportLanguages (val) {
  465. storage.set('__oc_support_languages__', val)
  466. },
  467. },
  468. }
  469. </script>
  470. <style lang="less" scoped>
  471. @import '../../../src/styles/less/theme';
  472. .navbar-wrap {
  473. color: #606266;
  474. height: 60px;
  475. box-shadow: 0 2px 4px 0 hsla(0,0%,93%,.5), 0 2px 4px 0 hsla(0,0%,93%,.5);
  476. padding: 0;
  477. position: fixed;
  478. top: 0;
  479. left: 0;
  480. right: 0;
  481. z-index: 99;
  482. background-color: #fff;
  483. }
  484. .navbar-item {
  485. height: 100%;
  486. // border-left: 1px solid #f5f5f5;
  487. }
  488. .navbar-item-icon {
  489. width: 40px;
  490. height: 40px;
  491. margin-left: 5px;
  492. margin-right: 5px;
  493. }
  494. .navbar-item-trigger {
  495. height: 100%;
  496. padding: 0 20px;
  497. cursor: pointer;
  498. text-decoration: none;
  499. }
  500. .header-logo {
  501. line-height: 1;
  502. img {
  503. width: 30px;
  504. border-radius: 50%;
  505. }
  506. }
  507. .header-title {
  508. margin: 0;
  509. padding: 0;
  510. font-weight: 400;
  511. font-size: 18px;
  512. color: rgba(0,0,0,.85);
  513. }
  514. .products-label {
  515. max-width: 150px;
  516. }
  517. .current-view-label {
  518. max-width: 150px;
  519. }
  520. .view-list-wrap {
  521. max-width: 200px;
  522. margin: 0;
  523. padding: 0;
  524. > li {
  525. padding: 5px 0;
  526. > ul {
  527. margin-top: 5px;
  528. > li {
  529. padding-left: 10px;
  530. height: 24px;
  531. }
  532. }
  533. }
  534. .item-link {
  535. cursor: pointer;
  536. &:hover {
  537. color: @link-color;
  538. }
  539. }
  540. }
  541. .global-map-btn {
  542. width: 50px;
  543. height: 50px;
  544. padding: 0 !important;
  545. position: relative;
  546. &::after {
  547. content: "";
  548. left: 0;
  549. top: 0;
  550. right: 0;
  551. bottom: 0;
  552. position: absolute;
  553. background: #fff;
  554. border-radius: 50%;
  555. z-index: -1;
  556. transition: all .3s ease;
  557. }
  558. &:hover {
  559. &::after {
  560. background-color: rgb(241, 241, 241);
  561. }
  562. }
  563. }
  564. </style>