tj
2025-06-05 bba272999cc546f65781bf3d20245a3f819af67f
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
272
273
274
<template>
  <div style="height:100%;">
    <el-container>
      <el-aside :width="sidebarWidth" class="aside" :style="asideStyle">
        <sidebar :isCollapse="isCollapse" :is-phone="isPhone"></sidebar>
      </el-aside>
      <el-container>
        <el-header class="header">
          <div class="left">
            <div class="operate" ref="operate">
              <i class="iconfont icon-fold" :class="{ rotate: foldState }" @click="changeSlidebarState" />
              <nav-bar></nav-bar>
            </div>
            <el-collapse-transition> <reuse-tab ref="reuse"></reuse-tab> </el-collapse-transition>
          </div>
        </el-header>
        <el-main ref="main">
          <menu-tab></menu-tab>
          <app-main ref="appMain"></app-main>
        </el-main>
        <back-top :right="50" :bottom="50" :fontSize="34"></back-top>
      </el-container>
      <div class="sidenav-mask" :class="{ show: isPhone && isCollapse }" @click="changeSlidebarState"></div>
    </el-container>
  </div>
</template>
 
<script>
import emitter from 'lin/util/emitter'
import { NavBar, Sidebar, AppMain, ReuseTab, MenuTab, BackTop } from '@/component/layout'
 
const navBarHeight = 66 // header高度
const reuseTabHeight = 70 // 历史记录栏高度
const marginHeight = 20 // 历史记录栏与舞台的间距
const sidebarWidth = '210px'
const totalHeight = navBarHeight + reuseTabHeight + marginHeight
 
export default {
  components: {
    NavBar,
    Sidebar,
    AppMain,
    ReuseTab,
    MenuTab,
    BackTop,
  },
  data() {
    return {
      isCollapse: false, // 左侧菜单栏是否折叠
      sidebarWidth, // 左侧菜单栏展开的宽度
      clientWidth: 0, // 页面宽度
      clientHeight: 0, // 页面高度
      foldState: false, // 控制左侧菜单栏按键
      isPhone: false,
    }
  },
  mounted() {
    this.setResize()
    if (this.clientWidth < 500) {
      this.isPhone = true
    } else {
      this.isPhone = false
      // 检测屏幕宽度, 确定默认是否展开
      if (this.clientWidth <= 768) {
        emitter.emit('removeSidebarSearch')
        this.isCollapse = true
      } else {
        this.isCollapse = false
        emitter.emit('showSidebarSearch')
      }
    }
    // 监测屏幕宽度 折叠左侧菜单栏
    window.onresize = () => {
      this.setResize()
      if (this.clientWidth <= 500) {
        this.isPhone = true
      } else if (this.clientWidth <= 800) {
        this.isPhone = false
      }
 
      // if (_this.clientWidth <= 768) {
      //   // 页面宽度 768
      //   if (_this.isCollapse === false) {
      //     _this.eventBus.$emit('removeSidebarSearch')
      //     _this.isCollapse = true
      //   }
      // } else if (_this.isCollapse === true) {
      //   _this.eventBus.$emit('showSidebarSearch')
      //   _this.isCollapse = false
      // }
    }
 
    emitter.on('noReuse', () => {
      this.$refs.operate.style.height = '86px'
    })
    emitter.on('hasReuse', () => {
      this.$refs.operate.style.height = '45px'
    })
  },
  computed: {
    elMenuCollapse() {
      if (this.isPhone) {
        return false
      }
 
      return this.isCollapse
    },
    asideStyle() {
      const style = {}
      if (this.isPhone) {
        style.position = 'absolute'
        style.height = `${this.clientHeight}px`
        style.zIndex = 12
        if (this.isCollapse === false) {
          style.transform = `translateX(-${sidebarWidth})`
        } else {
          style.transform = 'translateX(0)'
        }
      }
      return style
    },
  },
  methods: {
    // 控制菜单折叠
    changeSlidebarState() {
      this.isCollapse = !this.isCollapse
      if (this.isCollapse) {
        emitter.emit('removeSidebarSearch')
      } else {
        emitter.emit('showSidebarSearch')
      }
      this.foldState = !this.foldState
    },
    // 响应页面的宽度高度变化
    setResize() {
      this.clientHeight = document.body.clientHeight
      this.clientWidth = document.body.clientWidth
      this.$refs.appMain.$el.style.minHeight = `${this.clientHeight - totalHeight + 20}px`
    },
  },
  watch: {
    isCollapse() {
      if (this.isPhone) {
        // 手机模式
        this.sidebarWidth = sidebarWidth
        if (this.isCollapse === false) {
          this.transX = 0
        } else {
          this.transX = -1 * sidebarWidth
        }
      } else {
        this.transX = 0
        this.sidebarWidth = this.isCollapse === false ? sidebarWidth : '64px'
      }
    },
    $route() {
      this.showBackTop = false
      if (this.scrollY <= 70) {
        // MenuTab组件高度
        this.backTop()
      }
      if (this.isPhone && this.isCollapse) {
        this.changeSlidebarState()
      }
    },
  },
 
  beforeUnmount() {
    emitter.off('noReuse')
    emitter.off('hasReuse')
  },
}
</script>
 
<style lang="scss" scoped>
.aside {
  background: rgb(25, 42, 94);
  overflow-x: hidden;
 
  &::-webkit-scrollbar {
    width: 0px;
    height: 0px;
  }
}
 
.header {
  padding: 0;
  background: $header-background;
  height: $header-height !important;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0px 2px 6px 0px rgba(190, 204, 216, 0.4);
  border-bottom: 1px solid rgba(190, 204, 216, 0.4);
 
  .left {
    height: 100%;
    width: 100%;
 
    .operate {
      display: flex;
      align-items: center;
      background: $header-background;
      padding-left: 20px;
      height: 86px;
 
      .iconfont {
        font-size: 16px;
        font-weight: 500;
        color: $right-side-font-color;
        cursor: pointer;
        transform: rotate(0deg);
        transition: all 0.3s linear;
        margin-right: 10px;
 
        &:hover {
          color: #3963bc;
        }
      }
 
      .rotate {
        transform: rotate(180deg);
        transition: all 0.3s linear;
      }
    }
  }
 
  .right-info {
    display: flex;
    align-items: center;
  }
}
 
.el-main {
  overflow-y: auto;
  position: relative;
  padding: 0;
}
 
.backTop {
  position: fixed;
  display: inline-block;
  text-align: center;
  cursor: pointer;
  right: 50px;
  bottom: 50px;
  width: 22px;
  height: 22px;
  line-height: 22px;
  border-radius: 50%;
  z-index: 99;
  background: #fff;
 
  .iconfont {
    font-size: 36px;
  }
}
 
.sidenav-mask {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.3);
  z-index: 10;
  display: none;
  cursor: pointer;
 
  &.show {
    display: block;
  }
}
</style>