liuxiaolong
2019-05-06 8700cf1dc46c350371d865532c2914595187788e
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
<template>
  <sidenav :orientation="orientation" :class="curClasses">
 
    <!-- Brand demo (see src/demo.css) -->
    <div class="app-brand demo" v-if="orientation !== 'horizontal'">
      <span class="app-brand-logo demo">
        <img src="../assets/img/logo.png" width="100%" alt="">
      </span>
      <router-link to="/" class="app-brand-text demo sidenav-text font-weight-normal ml-2">{{$store.state.projectTitle}}</router-link>
      <!-- 左边的三个横行 -->
      <a href="javascript:void(0)" class="layout-sidenav-toggle sidenav-link text-large ml-auto" @click="toggleSidenav()">
        <i class="ion ion-md-menu align-middle pl10"></i>
      </a>
    </div>
    <div class="sidenav-divider mt-0" v-if="orientation !== 'horizontal'"></div>
 
    <!-- Links -->
    <div class="sidenav-inner" :class="{ 'py-1': orientation !== 'horizontal' }" >
      <!-- <sidenav-router-link icon="fas fa-home" to="/" :exact="true">首页</sidenav-router-link> -->
      <!-- 菜单 start menuListData -->
        <!-- <sidenav-divider class="mb-1"/> -->
        <sidenav-header
          class="small font-weight-semibold"
        >
          {{$store.state.projectTitle}}
        </sidenav-header>
        <sidenav-router-link
          v-for=" menu in menuListData "
          v-if="(!menu.child||!isChildMenu(menu.child) )&&menu.type !== 2"
          :key="menu.id"
          :icon="menu.icon"
          :to="menu.url"
          exact
          :active="isMenuActive(menu.url)"
        >
          {{menu.name}}
        </sidenav-router-link>
        <sidenav-menu
          v-for=" menu in menuListData"
          v-if="menu.child&&isChildMenu(menu.child)&&menu.type !==2"
          :key="menu.id"
          :icon="menu.icon"
          :open="isMenuOpen(menu.url)"
        >
          <template slot="link-text">{{menu.name}}</template>
          <sidenav-router-link
            v-for="iteam in menu.child"
            :key="iteam.id"
            v-if="(!iteam.child||!isChildMenu(iteam.child)&& iteam.type !== 2 )"
            :to="iteam.url"
            :exact="true"
          >
            {{iteam.name}}
          </sidenav-router-link>
          <sidenav-menu
            v-for=" iteam in menu.child "
            v-if="iteam.child&&isChildMenu(iteam.child)&& iteam.type !== 2"
            :key="iteam.id"
            :icon="iteam.icon"
            :open="isMenuOpen(iteam.url)"
          >
            <template slot="link-text">{{iteam.name}}</template>
            <sidenav-router-link
              v-for="it in iteam.child"
              :key="it.id"
              v-if="it.type !== 2"
              :to="it.url"
              :exact="true"
            >
              {{it.name}}
            </sidenav-router-link>
          </sidenav-menu>
        </sidenav-menu>
      <!-- 菜单 end-->
    </div>
    <!-- <div class="sidenav-inner" :class="{ 'py-1': orientation !== 'horizontal' }" >
      <sidenav-router-link icon="fas fa-home" to="/" :exact="true">首页</sidenav-router-link>
        <sidenav-divider class="mb-1"/>
        <sidenav-header  class="small font-weight-semibold">{{$store.state.projectTitle}}</sidenav-header>
        <sidenav-router-link v-for=" menu in menuListData " v-if="(!menu.child||!isChildMenu(menu.child) )&&menu.type !== 2" :key="menu.id" :icon="menu.icon" :to="menu.url" :exact="true">{{menu.name}}</sidenav-router-link>
        <sidenav-menu v-for=" menu in menuListData " v-if="menu.child&&isChildMenu(menu.child)&&menu.type !==2" :key="menu.id" :icon="menu.icon" :active="isMenuActive(menu.url)" :open="isMenuOpen(menu.url)">
          <template slot="link-text">{{menu.name}}</template>
          <sidenav-router-link v-for="iteam in menu.child" :key="iteam.id" v-if="iteam.type !== 2" :to="iteam.url" :exact="true">{{iteam.name}}</sidenav-router-link>
        </sidenav-menu>
    </div> -->
  </sidenav>
</template>
 
<script>
import router from '@/router'
import { getMenuListData } from '@/server/login.js'
import {
  Sidenav,
  SidenavLink,
  SidenavRouterLink,
  SidenavMenu,
  SidenavHeader,
  SidenavBlock,
  SidenavDivider
} from '@/vendor/libs/sidenav'
import { findButtonAuthoritys, findInArr } from '../components/common/util.js'
export default {
  name: 'app-layout-sidenav',
  components: {
    Sidenav,
    SidenavLink,
    SidenavRouterLink,
    SidenavMenu,
    SidenavHeader,
    SidenavBlock,
    SidenavDivider
  },
 
  props: {
    orientation: {
      type: String,
      default: 'vertical'
    }
  },
  data() {
    return {
      menuListData: []
    }
  },
  computed: {
    curClasses() {
      let bg = this.layoutSidenavBg
 
      if (
        this.orientation === 'horizontal' &&
        (bg.indexOf(' sidenav-dark') !== -1 ||
          bg.indexOf(' sidenav-light') !== -1)
      ) {
        bg = bg
          .replace(' sidenav-dark', '')
          .replace(' sidenav-light', '')
          .replace('-darker', '')
          .replace('-dark', '')
      }
 
      return (
        `bg-${bg} ` +
        (this.orientation !== 'horizontal'
          ? 'layout-sidenav'
          : 'layout-sidenav-horizontal container-p-x flex-grow-0')
      )
    }
  },
 
  methods: {
    isMenuActive(url) {
      return this.$route.matched.length > 1 && this.$route.matched[1].path === url
    },
    isMenuOpen(url) {
      return (
        this.$route.path.indexOf(url) === 0 && this.orientation !== 'horizontal'
      )
    },
    toggleSidenav() {
      this.layoutHelpers.toggleCollapsed()
    },
    isChildMenu(arr) {
      for (let iteam of arr) {
        if (iteam.type === 1) {
          return true
        }
      }
      return false
    },
    /* 获取菜单数据 */
    async getMenuList() {
      let results = await getMenuListData({
        module: this.$store.state.menuName
      })
      if (results && results.success) {
        // if (results && results.length && (this.$route.query.is_login || this.$route.path === '/')) {
        if (
          results.data &&
          results.data.length &&
          (this.$route.query.is_login || this.$route.path === '/') &&
          results.data[0].url
        ) {
          this.$router.replace(results.data[0].url)
        }
        /* 菜单数据赋值  permission权限 */
        this.menuListData = results.data
        /* 存储权限 */
        let buttonAuthoritys = []
        findButtonAuthoritys(results.data, authority => {
          if (findInArr(authority, buttonAuthoritys) === -1) {
            buttonAuthoritys.push(authority)
          }
        })
        sessionStorage.setItem('buttonAuthoritys', ',' + buttonAuthoritys + ',')
        sessionStorage.setItem('menuInfo', JSON.stringify(results.data))
      } else {
        this.$toast({
          type: 'error',
          message: '菜单获取失败'
        })
      }
    }
  },
  created() {
    // 初始化数据
    let token =
      JSON.parse(sessionStorage.getItem('loginedInfo')) &&
      JSON.parse(sessionStorage.getItem('loginedInfo')).access_token
    if (token && token !== '') {
      this.getMenuList()
    } else {
      router.push({
        path: '/login'
      })
    }
  },
  watch: {
    $route() {
      if (this.$route.path === '/') {
        let menuInfo = sessionStorage.getItem('menuInfo')
        menuInfo = JSON.parse(menuInfo)
        menuInfo && this.$router.replace(menuInfo[0].url)
      }
    }
  }
}
</script>