Blame view

src/layouts/default/LayoutHeader.tsx 7.75 KB
1
import { defineComponent, unref, computed, ref } from 'vue';
2
vben authored
3
import { Layout, Tooltip, Badge } from 'ant-design-vue';
陈文彬 authored
4
5
6
7
import Logo from '/@/layouts/Logo.vue';
import UserDropdown from './UserDropdown';
import LayoutMenu from './LayoutMenu';
import LayoutBreadcrumb from './LayoutBreadcrumb';
8
9
import LockAction from './actions/LockActionItem';
import NoticeAction from './actions/notice/NoticeActionItem.vue';
陈文彬 authored
10
11
12
13
14
15
import {
  RedoOutlined,
  FullscreenExitOutlined,
  FullscreenOutlined,
  GithubFilled,
  LockOutlined,
vben authored
16
  BugOutlined,
陈文彬 authored
17
} from '@ant-design/icons-vue';
18
陈文彬 authored
19
20
import { useFullscreen } from '/@/hooks/web/useFullScreen';
import { useTabs } from '/@/hooks/web/useTabs';
21
import { useWindowSizeFn } from '/@/hooks/event/useWindowSize';
22
import { useRouter } from 'vue-router';
23
24
25
26
27
28
29
import { useModal } from '/@/components/Modal/index';

import { appStore } from '/@/store/modules/app';
import { errorStore } from '/@/store/modules/error';

import { MenuModeEnum, MenuSplitTyeEnum, MenuTypeEnum } from '/@/enums/menuEnum';
import { GITHUB_URL } from '/@/settings/siteSetting';
陈文彬 authored
30
31
32
export default defineComponent({
  name: 'DefaultLayoutHeader',
  setup() {
33
    const widthRef = ref(200);
34
35
    let logoEl: Element | null;
36
37
    const { refreshPage } = useTabs();
    const { push } = useRouter();
陈文彬 authored
38
39
    const [register, { openModal }] = useModal();
    const { toggleFullscreen, isFullscreenRef } = useFullscreen();
40
陈文彬 authored
41
42
43
    const getProjectConfigRef = computed(() => {
      return appStore.getProjectConfig;
    });
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
    const showTopMenu = computed(() => {
      const getProjectConfig = unref(getProjectConfigRef);
      const {
        menuSetting: { mode, split: splitMenu },
      } = getProjectConfig;
      return mode === MenuModeEnum.HORIZONTAL || splitMenu;
    });

    useWindowSizeFn(
      () => {
        if (!unref(showTopMenu)) return;
        let width = 0;
        if (!logoEl) {
          logoEl = document.querySelector('.layout-header__logo');
        }
        if (logoEl) {
          width += logoEl.clientWidth;
        }
        widthRef.value = width + 60;
      },
      200,
      { immediate: true }
    );
陈文彬 authored
68
69
70
71
72
73
74
75
76

    function goToGithub() {
      window.open(GITHUB_URL, '__blank');
    }

    const headerClass = computed(() => {
      const theme = unref(getProjectConfigRef).headerSetting.theme;
      return theme ? `layout-header__header--${theme}` : '';
    });
vben authored
77
78
79

    function handleToErrorList() {
      errorStore.commitErrorListCountState(0);
80
      push('/exception/error-log');
vben authored
81
82
    }
陈文彬 authored
83
84
85
86
87
88
    /**
     * @description: 锁定屏幕
     */
    function handleLockPage() {
      openModal(true);
    }
89
陈文彬 authored
90
91
92
    return () => {
      const getProjectConfig = unref(getProjectConfigRef);
      const {
vben authored
93
        useErrorHandle,
陈文彬 authored
94
        showLogo,
chen-xt authored
95
96
97
98
99
100
101
102
        headerSetting: {
          theme: headerTheme,
          useLockPage,
          showRedo,
          showGithub,
          showFullScreen,
          showNotice,
        },
陈文彬 authored
103
104
        menuSetting: { mode, type: menuType, split: splitMenu, topMenuAlign },
        showBreadCrumb,
105
        showBreadCrumbIcon,
陈文彬 authored
106
107
108
      } = getProjectConfig;

      const isSidebarType = menuType === MenuTypeEnum.SIDEBAR;
109
110
      const width = unref(widthRef);
111
陈文彬 authored
112
      return (
113
        <Layout.Header class={['layout-header', 'flex p-0 px-4 ', unref(headerClass)]}>
陈文彬 authored
114
115
          {() => (
            <>
116
              <div class="layout-header__content ">
陈文彬 authored
117
118
119
                {showLogo && !isSidebarType && <Logo class={`layout-header__logo`} />}

                {mode !== MenuModeEnum.HORIZONTAL && showBreadCrumb && !splitMenu && (
120
                  <LayoutBreadcrumb showIcon={showBreadCrumbIcon} />
陈文彬 authored
121
                )}
122
123
                {unref(showTopMenu) && (
                  <div
124
                    class={[`layout-header__menu `]}
125
126
                    style={{ width: `calc(100% - ${unref(width)}px)` }}
                  >
陈文彬 authored
127
                    <LayoutMenu
128
129
                      isTop={true}
                      class={`justify-${topMenuAlign}`}
陈文彬 authored
130
131
132
133
134
135
136
137
138
139
                      theme={headerTheme}
                      splitType={splitMenu ? MenuSplitTyeEnum.TOP : MenuSplitTyeEnum.NONE}
                      menuMode={splitMenu ? MenuModeEnum.HORIZONTAL : null}
                      showSearch={false}
                    />
                  </div>
                )}
              </div>

              <div class={`layout-header__action`}>
vben authored
140
141
142
143
144
145
146
147
                {useErrorHandle && (
                  <Tooltip>
                    {{
                      title: () => '错误日志',
                      default: () => (
                        <Badge
                          count={errorStore.getErrorListCountState}
                          offset={[0, 10]}
148
                          dot
vben authored
149
150
151
152
153
154
155
156
157
158
159
160
161
                          overflowCount={99}
                        >
                          {() => (
                            <div class={`layout-header__action-item`} onClick={handleToErrorList}>
                              <BugOutlined class={`layout-header__action-icon`} />
                            </div>
                          )}
                        </Badge>
                      ),
                    }}
                  </Tooltip>
                )}
陈文彬 authored
162
163
164
165
166
167
168
169
170
171
172
173
                {showGithub && (
                  <Tooltip>
                    {{
                      title: () => 'github',
                      default: () => (
                        <div class={`layout-header__action-item`} onClick={goToGithub}>
                          <GithubFilled class={`layout-header__action-icon`} />
                        </div>
                      ),
                    }}
                  </Tooltip>
                )}
vben authored
174
                {useLockPage && (
陈文彬 authored
175
176
177
178
179
180
181
182
183
184
185
                  <Tooltip>
                    {{
                      title: () => '锁定屏幕',
                      default: () => (
                        <div class={`layout-header__action-item`} onClick={handleLockPage}>
                          <LockOutlined class={`layout-header__action-icon`} />
                        </div>
                      ),
                    }}
                  </Tooltip>
                )}
chen-xt authored
186
187
188
189
                {showNotice && (
                  <div>
                    <Tooltip>
                      {{
190
191
                        title: () => '消息通知',
                        default: () => <NoticeAction />,
chen-xt authored
192
193
194
195
                      }}
                    </Tooltip>
                  </div>
                )}
陈文彬 authored
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
                {showRedo && (
                  <Tooltip>
                    {{
                      title: () => '刷新',
                      default: () => (
                        <div class={`layout-header__action-item`} onClick={refreshPage}>
                          <RedoOutlined class={`layout-header__action-icon`} />
                        </div>
                      ),
                    }}
                  </Tooltip>
                )}
                {showFullScreen && (
                  <Tooltip>
                    {{
                      title: () => (unref(isFullscreenRef) ? '退出全屏' : '全屏'),
                      default: () => {
                        const Icon: any = !unref(isFullscreenRef) ? (
                          <FullscreenOutlined />
                        ) : (
                          <FullscreenExitOutlined />
                        );
                        return (
                          <div class={`layout-header__action-item`} onClick={toggleFullscreen}>
                            <Icon class={`layout-header__action-icon`} />
                          </div>
                        );
                      },
                    }}
                  </Tooltip>
                )}
                <UserDropdown class={`layout-header__user-dropdown`} />
              </div>
              <LockAction onRegister={register} />
            </>
          )}
        </Layout.Header>
      );
    };
  },
});