lxl
2022-11-07 c6a9b11ff0783bcd81a043a179fbc27f685eee70
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
<template>
  <div class="leftMenu">
    <div class="leftTopWrapper">
      <div class="logo">
        <img src="../assets/img/www.terra-it.cn.png" alt="" />
      </div>
      <div class="menu">
        <el-menu
          active-text-color="#ffd04b"
          class="el-menu-vertical-demo"
          :default-active="activeIndex"
          background-color="#586884"
          text-color="#fff"
          @select="handleselect"
        >
          <customElMenu :menuData="menuList"></customElMenu>
        </el-menu>
      </div>
    </div>
    <div class="leftBotWrapper">
      <div class="userInfo">
        <img src="../assets/img/user.png" alt="" />
        <span>admin</span>
        <span @click="logOut">注销</span>
        &nbsp;
        <span @click="switchLang"> 切换语言</span>
      </div>
      <div class="btnBox">
        <div><img src="../assets/img/leftBtn.png" alt="" /></div>
        <div><img src="../assets/img/rightBtn.png" alt="" /></div>
      </div>
    </div>
  </div>
</template>
 
<script>
import { logout } from '@/api/api';
import { removeToken, getToken } from '@/utils/auth';
import customElMenu from '../components/customElMenu.vue';
import { queryMenuTree, getPerms } from '../api/api';
 
export default {
  name: 'navMenu',
  //import引入的组件需要注入到对象中才能使用
  components: {
    customElMenu,
  },
  data() {
    return {
      oriData: [], //原始树数据
      dirData: [], //el树数据
      newData: [], //拖动后数据
      lang: 'zh',
      activeIndex: '/',
      menuList: [],
      editTitle: '',
      showPopover: false,
      showEditInfoWrapper: false,
      showEdit: false,
      editMenu: false,
      editCatalogue: false,
      editUnit: false,
      itemdetail: {},
      formLabelWidth: '70px',
    };
  },
  mounted() {
    this.getUserPerms();
    this.getMenuTree();
  },
  computed: {
    // 我们使用计算属性来获取到当前点击的菜单的路由路径,然后设置default-active中的值
    // 使得菜单在载入时就能对应高亮
    // activeIndex() {
    //   const route = this.$route;
    //   const { meta, path } = route;
    //   // if set path, the sidebar will highlight the path you set
    //   // 可以在路由配置文件中设置自定义的路由路径到meta.activeMenu属性中,来控制菜单自定义高亮显示
    //   if (meta.activeMenu) {
    //     return meta.activeMenu;
    //   }
    //   return path;
    // },
  },
  methods: {
    getMenuTree() {
      //获取目录树最大ID,新建节点使用
      // queryMaxId().then((res) => {
      //   this.id = res.data;
      // });
      // 获取目录树数据
      queryMenuTree().then((res) => {
        if (res.code == 200) {
          if (res.result.length != 0) {
            let menuLists = res.result.filter((value) => {
              return value.type == 1;
            });
            this.menuList = this.treeData(menuLists);
          } else {
            alert('暂无菜单栏数据');
          }
        } else {
          console.log('接口报错');
        }
      });
    },
    treeData(source) {
      let cloneData = JSON.parse(JSON.stringify(source)); // 对源数据深度克隆
      // console.log(cloneData);
      if (cloneData.length != 0) {
        return cloneData.filter((father) => {
          // 循环所有项
          let branchArr = cloneData.filter((child) => father.id == child.pid); // 对比ID,分别上下级菜单,并返回数据
          branchArr.length > 0 ? (father.children = branchArr) : ''; // 给父级添加一个children属性,并赋值
          // 属于同一对象问题,例如:令 a=b、c=1 ,然后再令 b.c=c , 那么 a.c=b.c=c=1 ;同理,后续令 c.d=2 ,那么 a.c.d 也是=2;
          // 由此循环多次后,就能形成相应的树形数据结构
          return father.pid == 1; // 返回一级菜单
        });
      } else {
        alert('暂无菜单栏数据');
      }
    },
    logOut() {
      this.$confirm('确认是否退出登录?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(async () => {
          const data = await logout({ token: getToken() });
          // console.log(data);
          if (data.code != 200) {
            return this.$message.error('退出登录失败');
          }
          removeToken();
          this.$router.push('/login');
          this.$message({
            message: '退出登录成功',
            type: 'success',
          });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消',
          });
        });
    },
    switchLang() {
      //当前en
      if (this.lang == 'en') {
        //语言换成zh
        this.lang = 'zh';
        //菜单换为zh
        this.$store.commit('changeLang', 'zh');
        //i18换成zh
        this.$i18n.locale = this.lang;
      }
      //当前zh
      else {
        this.lang = 'en';
        this.$i18n.locale = this.lang;
        this.$store.commit('changeLang', 'en'); //传递点击的节点
      }
    },
    handleselect(index, indexPath, e) {
      // console.log(e.$attrs.perms);
      this.$store.commit('currentPerms', e.$attrs.perms.perms);
      if (Window.ws != null) {
        Window.ws.close();
        Window.ws.onclose = () => {
          console.log('服务器关闭');
        };
        Window.ws = null;
      }
 
      if (index.indexOf('http') != -1) {
        this.$router.push('/databaseMonitoring');
        this.$store.commit('getIframe', index);
      } else if (isNaN(Number(index))) {
        this.$router.push(index);
      }
    },
    getUserPerms() {
      getPerms().then((res) => {
        if (res.code == 200) this.$store.commit('getPermsEntity', res.result);
      });
    },
  },
  watch: {
    $route() {
      let str = this.$route.path;
      if (str[0] == '/') {
        this.activeIndex = str.slice(1);
      }
    },
  },
  created() {
    let str = this.$route.path;
    if (str[0] == '/') {
      this.activeIndex = str.slice(1);
    }
  },
};
</script>
<style lang="less" scoped>
//@import url(); 引入公共css类
.leftMenu {
  // width: 300px;
  height: 99%;
  // background-color: #bfa;
}
.leftTopWrapper {
  width: 100%;
  height: 100%;
  .logo {
    // background-color: rgb(139, 0, 0);
    width: 249px;
    height: 52px;
    img {
      width: 100%;
    }
  }
  .menu {
    height: 90%;
    margin-top: 22px;
    overflow: auto;
    // background-color: rgb(120, 121, 120);
    .el-menu {
      height: 100%;
      width: 280px;
      border-right: none;
      /deep/ .el-submenu {
        margin-bottom: 10px;
        .el-submenu__title {
          background-color: transparent !important;
        }
      }
      .faSub {
        background-color: #586884;
      }
    }
  }
}
.leftBotWrapper {
  width: 258px;
  position: absolute;
  left: 19px;
  bottom: 17px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  .userInfo {
    // width: 138px;
    font-size: 16px;
    font-family: Microsoft YaHei;
    font-weight: 400;
    color: #fcfcfc;
    display: flex;
    justify-content: space-between;
    align-items: center;
    cursor: pointer;
  }
  .btnBox {
    width: 65px;
    display: flex;
    justify-content: space-between;
  }
}
</style>