menu.vue 9.1 KB
Newer Older
1
<template>
2 3 4
  <div>
    <div class="button-box clearflex">
      <el-button @click="addMenu('0')" type="primary">新增根菜单</el-button>
5
    </div>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
6 7

    <!-- 由于此处菜单跟左侧列表一一对应所以不需要分页 pageSize默认999 -->
Mr.奇淼('s avatar
Mr.奇淼( 已提交
8
    <el-table :data="tableData" border row-key="ID" stripe>
9 10
      <el-table-column label="ID" min-width="100" prop="ID"></el-table-column>
      <el-table-column label="路由Name" min-width="160" prop="name"></el-table-column>
J
jinlan.du 已提交
11
      <el-table-column label="是否隐藏" min-width="100" prop="hidden">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
12 13 14 15
        <template slot-scope="scope">
          <span>{{scope.row.hidden?"隐藏":"显示"}}</span>
        </template>
      </el-table-column>
J
jinlan.du 已提交
16
      <el-table-column label="父节点" min-width="90" prop="parentId"></el-table-column>
17
      <el-table-column label="排序" min-width="70" prop="sort"></el-table-column>
J
jinlan.du 已提交
18
      <el-table-column label="文件路径" min-width="360" prop="component"></el-table-column>
19
      <el-table-column label="展示名称" min-width="120" prop="authorityName">
20 21 22 23
        <template slot-scope="scope">
          <span>{{scope.row.meta.title}}</span>
        </template>
      </el-table-column>
24
      <el-table-column label="图标" min-width="140" prop="authorityName">
25 26 27 28
        <template slot-scope="scope">
          <span>{{scope.row.meta.icon}}</span>
        </template>
      </el-table-column>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
29
      <el-table-column fixed="right" label="操作" width="300">
30
        <template slot-scope="scope">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
31
          <el-button @click="deleteMenu(scope.row.ID)" size="small" type="text">删除菜单</el-button>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
32
          <el-button @click="editMenu(scope.row.ID)" size="small" type="text">编辑菜单</el-button>
33 34 35 36 37
          <el-button @click="addMenu(scope.row.ID)" size="small" type="text">添加子菜单</el-button>
        </template>
      </el-table-column>
    </el-table>

38
    <el-dialog :before-close="handleClose" :visible.sync="dialogFormVisible" :title="dialogTitle">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
39 40
      <el-form :inline="true" :model="form" :rules="rules" label-width="85px" ref="menuForm">
        <el-form-item label="路由name" prop="path">
41
          <el-input autocomplete="off" placeholder="唯一英文字符串" v-model="form.path"></el-input>
42 43 44 45 46 47 48 49
        </el-form-item>
        <el-form-item label="是否隐藏">
          <el-select placeholder="是否在列表隐藏" v-model="form.hidden">
            <el-option :value="false" label="否"></el-option>
            <el-option :value="true" label="是"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="父节点Id">
50 51 52 53 54 55 56 57
           <el-cascader
              :disabled="!this.isEdit"
              v-model="form.parentId"
              :options="menuOption"
              :show-all-levels="false"
              :props="{ checkStrictly: true,label:'title',value:'ID',disabled:'disabled',emitPath:false}"
              filterable>
              </el-cascader>
58
        </el-form-item>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
59
        <el-form-item label="文件路径" prop="component">
60 61
          <el-input autocomplete="off" v-model="form.component"></el-input>
        </el-form-item>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
62
        <el-form-item label="展示名称" prop="meta.title">
63 64
          <el-input autocomplete="off" v-model="form.meta.title"></el-input>
        </el-form-item>
65
        <el-form-item label="图标" prop="meta.icon">
66 67 68
          <el-input autocomplete="off" v-model="form.meta.icon">
            <template slot="prepend">el-icon-</template>
          </el-input>
69
        </el-form-item>
70
        <el-form-item label="排序标记" prop="sort">
71
          <el-input autocomplete="off" v-model.number="form.sort"></el-input>
72
        </el-form-item>
73 74 75 76 77 78
        <el-form-item label="keepAlive" prop="meta.keepAlive">
          <el-select placeholder="是否keepAlive缓存页面" v-model="form.meta.keepAlive">
            <el-option :value="false" label="否"></el-option>
            <el-option :value="true" label="是"></el-option>
          </el-select>
        </el-form-item>
79
      </el-form>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
80
      <div class="warning">新增菜单需要在角色管理内配置权限才可使用</div>
81 82 83 84 85 86
      <div class="dialog-footer" slot="footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button @click="enterDialog" type="primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
87 88 89
</template>

<script>
Mr.奇淼('s avatar
注释  
Mr.奇淼( 已提交
90 91
// 获取列表内容封装在mixins内部  getTableData方法 初始化已封装完成

92
import {
R
rainyan 已提交
93
  updateBaseMenu,
94 95 96 97 98
  getMenuList,
  addBaseMenu,
  deleteBaseMenu,
  getBaseMenuById
} from '@/api/menu'
99
import infoList from '@/components/mixins/infoList'
100
export default {
101
  name: 'Menus',
102
  mixins: [infoList],
103 104
  data() {
    return {
105
      listApi: getMenuList,
106
      dialogFormVisible: false,
107
      dialogTitle:"新增菜单",
108 109 110 111 112 113
      menuOption:[
        {
          ID:"0",
          title:"根菜单"
        }
      ],
114
      form: {
115
        ID: 0,
116 117 118 119 120 121 122
        path: '',
        name: '',
        hidden: '',
        parentId: '',
        component: '',
        meta: {
          title: '',
123 124 125
          icon: '',
          defaultMenu:false,
          keepAlive:false
126
        }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
127
      },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
128 129 130 131 132 133 134 135 136
      rules: {
        path: [{ required: true, message: '请输入菜单name', trigger: 'blur' }],
        component: [
          { required: true, message: '请输入文件路径', trigger: 'blur' }
        ],
        'meta.title': [
          { required: true, message: '请输入菜单展示名称', trigger: 'blur' }
        ]
      },
137
      isEdit: false
138 139 140
    }
  },
  methods: {
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
    setOptions(){
       this.menuOption = [{
          ID:"0",
          title:"根目录"
        }]
      this.setMenuOptions(this.tableData,this.menuOption,false)
    },
    setMenuOptions(menuData,optionsData,disabled){
      menuData&&menuData.map(item=>{
        if(item.children.length){
          const option = {
            title:item.meta.title,
            ID:String(item.ID),
            disabled:disabled||item.ID == this.form.ID,
            children:[]
        }
          this.setMenuOptions(item.children,option.children,disabled||item.ID == this.form.ID)
          optionsData.push(option)
        }else{
          const option = {
              title:item.meta.title,
              ID:String(item.ID),
              disabled:disabled||item.ID == this.form.ID,
          }
          optionsData.push(option)
        }
      })
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
169
    handleClose(done) {
170 171 172
      this.initForm()
      done()
    },
173 174
    // 懒加载子菜单
    load(tree, treeNode, resolve) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
      resolve([
        {
          id: 31,
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄'
        },
        {
          id: 32,
          date: '2016-05-01',
          name: '王小虎',
          address: '上海市普陀区金沙江路 1519 弄'
        }
      ])
    },
190
    // 删除菜单
Mr.奇淼('s avatar
Mr.奇淼( 已提交
191
    deleteMenu(ID) {
192 193 194 195 196 197
      this.$confirm('此操作将永久删除所有角色下该菜单, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
198
          const res = await deleteBaseMenu({ ID })
199
          if (res.code == 0) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
200 201 202 203
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
204
            this.getTableData()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
205
          }
206 207 208 209 210 211 212 213
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
214
    // 初始化弹窗内表格方法
215
    initForm() {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
216
      this.$refs.menuForm.resetFields()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230
      this.form = {
        ID: 0,
        path: '',
        name: '',
        hidden: '',
        parentId: '',
        component: '',
        meta: {
          title: '',
          icon: '',
          defaultMenu:false,
          keepAlive:""
        }
      }
231
    },
232
    // 关闭弹窗
233
    closeDialog() {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
234
      this.initForm()
235 236
      this.dialogFormVisible = false
    },
237
    // 添加menu
238
    async enterDialog() {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
239 240 241 242 243
      this.$refs.menuForm.validate(async valid => {
        if (valid) {
          let res
          this.form.name = this.form.path
          if (this.isEdit) {
R
rainyan 已提交
244
            res = await updateBaseMenu(this.form)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
245 246 247
          } else {
            res = await addBaseMenu(this.form)
          }
248
          if (res.code == 0) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
249 250
            this.$message({
              type: 'success',
251
              message: this.isEdit?'编辑成功':'添加成功!'
Mr.奇淼('s avatar
Mr.奇淼( 已提交
252 253 254
            })
            this.getTableData()
          }
255
          this.initForm()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
256 257 258
          this.dialogFormVisible = false
        }
      })
259
    },
260
    // 添加菜单方法,id为 0则为添加根菜单
261
    addMenu(id) {
262
      this.dialogTitle = "新增菜单"
263
      this.form.parentId = String(id)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
264
      this.isEdit = false
265
      this.setOptions()
266 267
      this.dialogFormVisible = true
    },
268
    // 修改菜单方法
269
    async editMenu(id) {
270
      this.dialogTitle = "编辑菜单"
271
      const res = await getBaseMenuById({ id })
Mr.奇淼('s avatar
Mr.奇淼( 已提交
272 273
      this.form = res.data.menu
      this.isEdit = true
274 275
      this.setOptions()
      this.dialogFormVisible = true
276
    },
Mr.奇淼('s avatar
Mr.奇淼( 已提交
277
  },
278
   async created() {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
279
    this.pageSize = 999
280
    await this.getTableData()
281
  }
282 283
}
</script>
284
<style scoped lang="scss">
285 286 287 288 289 290
.button-box {
  padding: 10px 20px;
  .el-button {
    float: right;
  }
}
K
yx  
klausY 已提交
291
.warning {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
292
  color: #dc143c;
K
yx  
klausY 已提交
293
}
294
</style>