index.vue 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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 languages = this.globalSettingSetupKeys.filter(item => ['zh-CN', 'en', 'ja-JP'].includes(item))
  275. return languages
  276. },
  277. },
  278. watch: {
  279. userInfo: {
  280. handler (val, oldVal = {}) {
  281. if (val.id !== oldVal.id) {
  282. if ((R.isNil(val.projects) || R.isEmpty(val.projects)) && (R.isNil(val.projectId) || R.isEmpty(val.projectId))) {
  283. this.$router.push('/no-project')
  284. }
  285. this.fetchOEM(val)
  286. }
  287. },
  288. immediate: true,
  289. },
  290. supportLanguages: {
  291. handler: function (val) {
  292. this.setSupportLanguages(val)
  293. if (val.length && !val.includes(this.language)) {
  294. const navigatorL = navigator.language || navigator.userLanguage
  295. setLanguage(val.includes(navigatorL) ? navigatorL : val[0])
  296. window.location.reload()
  297. }
  298. },
  299. immediate: true,
  300. },
  301. },
  302. created () {
  303. this.pushApiServerUrlAlert(this.userInfo.id)
  304. this.cronjobFetchAlerts()
  305. },
  306. methods: {
  307. async userMenuClick (item) {
  308. if (item.key === 'logout') {
  309. try {
  310. await this.$store.dispatch('auth/logout')
  311. this.$router.push('/auth/login')
  312. } catch (error) {
  313. throw error
  314. }
  315. } else if (item.key === 'handleUpdatePassword') {
  316. this.createDialog('UpdateUserPasswordDialog')
  317. } else if (item.key === 'toClouduser') {
  318. this.$router.push('/clouduser')
  319. } else if (item.key === 'toCredentials') {
  320. this.$router.push('/credentials')
  321. }
  322. },
  323. projectChange (id, scope) {
  324. this.viewChangePopoverVisible = false
  325. if (this.userInfo.projectId === id && this.scope === scope) return
  326. this.reLogin(id, scope)
  327. },
  328. productChange (item) {
  329. window.open(item.key, '_blank')
  330. },
  331. async reLogin (projectId, scope) {
  332. this.reLogging = true
  333. try {
  334. await this.$store.dispatch('auth/login', {
  335. tenantId: projectId,
  336. })
  337. await this.$store.commit('auth/SET_SCOPE', scope)
  338. await this.$store.commit('auth/SET_TENANT', projectId)
  339. await this.$store.commit('auth/UPDATE_HISTORY_USERS', {
  340. key: this.$store.getters['auth/currentLoggedUserKey'],
  341. value: {
  342. tenant: projectId,
  343. scope,
  344. },
  345. })
  346. await this.$store.commit('auth/UPDATE_HISTORY_USERS', {
  347. key: this.$store.getters['auth/currentLoggedUserKey'],
  348. value: {
  349. tenant: projectId,
  350. scope,
  351. },
  352. })
  353. await this.$store.dispatch('scopedPolicy/get', {
  354. category: ['sub_hidden_menus'],
  355. })
  356. window.location.reload()
  357. return true
  358. } catch (error) {
  359. throw error
  360. } finally {
  361. this.reLogging = false
  362. }
  363. },
  364. handleToggleSidebar () {
  365. const drawerVisible = !(get(this.$store.getters, 'common.sidebar.drawerVisible', false))
  366. this.$store.dispatch('common/updateObject', {
  367. name: 'sidebar',
  368. data: {
  369. drawerVisible,
  370. },
  371. })
  372. },
  373. handleCloseSidebar () {
  374. this.$store.dispatch('common/updateObject', {
  375. name: 'sidebar',
  376. data: {
  377. drawerVisible: false,
  378. },
  379. })
  380. },
  381. settingLanguageCH () {
  382. setLanguage('zh-CN')
  383. window.location.reload()
  384. },
  385. settingLanguageEN () {
  386. setLanguage('en')
  387. window.location.reload()
  388. },
  389. settingLanguageJP () {
  390. setLanguage('ja-JP')
  391. window.location.reload()
  392. },
  393. isSingleProject (projects, item) {
  394. const sameDomainProjects = projects.filter(v => v.domain_id === item.domain_id)
  395. const num = sameDomainProjects?.length
  396. if (num > 1) {
  397. const isAll = sameDomainProjects.every(v => {
  398. return R.equals(v.domain_policies, item.domain_policies)
  399. })
  400. if (isAll) {
  401. return true
  402. }
  403. }
  404. return num === 1
  405. },
  406. cronjobFetchAlerts () { // 定时5分钟请求一次
  407. const userInfo = this.$store.getters.userInfo
  408. if ((R.isNil(userInfo.projects) || R.isEmpty(userInfo.projects)) && (R.isNil(userInfo.projectId) || R.isEmpty(userInfo.projectId))) {
  409. return
  410. }
  411. this.$store.dispatch('app/fetchAlertingrecords')
  412. setInterval(() => {
  413. this.$store.dispatch('app/fetchAlertingrecords')
  414. }, 5 * 60 * 1000)
  415. if (this.isAdminMode && this.$store._actions['app/fetchAlertresource']) {
  416. // this.$store.dispatch('app/fetchAlertresource')
  417. // setInterval(() => {
  418. // this.$store.dispatch('app/fetchAlertresource')
  419. // }, 5 * 60 * 1000)
  420. } else {
  421. this.$store.commit('app/SET_ALERTRESOURCE', {
  422. data: [],
  423. total: 0,
  424. })
  425. }
  426. },
  427. async pushApiServerUrlAlert (id) {
  428. if (!id) return
  429. try {
  430. const params = {}
  431. if (this.$store.getters.scope !== 'system') {
  432. params.domain = this.$store.getters.userInfo.projectDomain
  433. }
  434. const regions = await this.$store.dispatch('auth/getRegions', params)
  435. const currentHost = window.location.hostname
  436. const apiServer = regions.api_server || ''
  437. if (apiServer) {
  438. if (!apiServer.includes(currentHost)) {
  439. this.$store.dispatch('common/updateObject', {
  440. name: 'topAlert',
  441. data: {
  442. apiServer: {
  443. messageOptions: [
  444. this.$t('common_222'),
  445. ['a', { attrs: { href: apiServer } }, apiServer],
  446. this.$t('common_223'),
  447. ],
  448. interval: 1000 * 60 * 60 * 24,
  449. },
  450. },
  451. })
  452. }
  453. }
  454. } catch (error) {
  455. throw error
  456. }
  457. },
  458. fetchOEM (val) {
  459. if (val) {
  460. this.$store.dispatch('app/fetchOEM')
  461. }
  462. },
  463. setSupportLanguages (val) {
  464. storage.set('__oc_support_languages__', val)
  465. },
  466. },
  467. }
  468. </script>
  469. <style lang="less" scoped>
  470. @import '../../../src/styles/less/theme';
  471. .navbar-wrap {
  472. color: #606266;
  473. height: 60px;
  474. box-shadow: 0 2px 4px 0 hsla(0,0%,93%,.5), 0 2px 4px 0 hsla(0,0%,93%,.5);
  475. padding: 0;
  476. position: fixed;
  477. top: 0;
  478. left: 0;
  479. right: 0;
  480. z-index: 99;
  481. background-color: #fff;
  482. }
  483. .navbar-item {
  484. height: 100%;
  485. // border-left: 1px solid #f5f5f5;
  486. }
  487. .navbar-item-icon {
  488. width: 40px;
  489. height: 40px;
  490. margin-left: 5px;
  491. margin-right: 5px;
  492. }
  493. .navbar-item-trigger {
  494. height: 100%;
  495. padding: 0 20px;
  496. cursor: pointer;
  497. text-decoration: none;
  498. }
  499. .header-logo {
  500. line-height: 1;
  501. img {
  502. width: 30px;
  503. border-radius: 50%;
  504. }
  505. }
  506. .header-title {
  507. margin: 0;
  508. padding: 0;
  509. font-weight: 400;
  510. font-size: 18px;
  511. color: rgba(0,0,0,.85);
  512. }
  513. .products-label {
  514. max-width: 150px;
  515. }
  516. .current-view-label {
  517. max-width: 150px;
  518. }
  519. .view-list-wrap {
  520. max-width: 200px;
  521. margin: 0;
  522. padding: 0;
  523. > li {
  524. padding: 5px 0;
  525. > ul {
  526. margin-top: 5px;
  527. > li {
  528. padding-left: 10px;
  529. height: 24px;
  530. }
  531. }
  532. }
  533. .item-link {
  534. cursor: pointer;
  535. &:hover {
  536. color: @link-color;
  537. }
  538. }
  539. }
  540. .global-map-btn {
  541. width: 50px;
  542. height: 50px;
  543. padding: 0 !important;
  544. position: relative;
  545. &::after {
  546. content: "";
  547. left: 0;
  548. top: 0;
  549. right: 0;
  550. bottom: 0;
  551. position: absolute;
  552. background: #fff;
  553. border-radius: 50%;
  554. z-index: -1;
  555. transition: all .3s ease;
  556. }
  557. &:hover {
  558. &::after {
  559. background-color: rgb(241, 241, 241);
  560. }
  561. }
  562. }
  563. </style>