index.vue 14.6 KB
Newer Older
sunhongwei's avatar
sunhongwei committed
1 2
<template>
  <div class="app-container">
Marcus's avatar
Marcus committed
3
<!--    <doc-alert :title="$t('功能权限')" url="https://doc.iocoder.cn/resource-permission" />-->
sunhongwei's avatar
sunhongwei committed
4
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
Marcus's avatar
Marcus committed
5 6
      <el-form-item :label="$t('菜单名称')" prop="name">
        <el-input v-model="queryParams.name" :placeholder="$t('请输入菜单名称')" clearable @keyup.enter.native="handleQuery"/>
sunhongwei's avatar
sunhongwei committed
7
      </el-form-item>
Marcus's avatar
Marcus committed
8 9
      <el-form-item :label="$t('状态')" prop="status">
        <el-select v-model="queryParams.status" :placeholder="$t('菜单状态')" clearable>
sunhongwei's avatar
sunhongwei committed
10 11 12 13
          <el-option v-for="dict in statusDictDatas" :key="parseInt(dict.value)" :label="dict.label" :value="parseInt(dict.value)"/>
        </el-select>
      </el-form-item>
      <el-form-item>
Marcus's avatar
Marcus committed
14 15
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">{{ $t('重置') }}</el-button>
sunhongwei's avatar
sunhongwei committed
16 17 18 19 20 21
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
Marcus's avatar
Marcus committed
22
                   v-hasPermi="['system:menu:create']">{{ $t('新增') }}</el-button>
sunhongwei's avatar
sunhongwei committed
23 24
      </el-col>
      <el-col :span="1.5">
25
        <el-button type="info" plain icon="el-icon-sort" size="mini" @click="toggleExpandAll">{{$t('展开')}}/{{$t('折叠')}}</el-button>
sunhongwei's avatar
sunhongwei committed
26 27 28 29 30 31
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-if="refreshTable" v-loading="loading" :data="menuList" row-key="id" :default-expand-all="isExpandAll"
              :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
32 33 34 35 36
      <el-table-column prop="name" :label="$t('菜单名称')" :show-overflow-tooltip="true" width="250">
        <template slot-scope="{row}">
          {{ $l(row, 'name') }}
        </template>
      </el-table-column>
Marcus's avatar
Marcus committed
37
      <el-table-column prop="icon" :label="$t('图标')" align="center" width="100">
sunhongwei's avatar
sunhongwei committed
38 39 40 41
        <template slot-scope="scope">
          <svg-icon :icon-class="scope.row.icon" />
        </template>
      </el-table-column>
Marcus's avatar
Marcus committed
42 43 44 45
      <el-table-column prop="sort" :label="$t('排序')" width="60"></el-table-column>
      <el-table-column prop="permission" :label="$t('权限标识')" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="component" :label="$t('组件路径')" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column prop="status" :label="$t('状态')" width="80">
sunhongwei's avatar
sunhongwei committed
46 47 48 49
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.COMMON_STATUS" :value="scope.row.status"/>
        </template>
      </el-table-column>
Marcus's avatar
Marcus committed
50
      <el-table-column :label="$t('创建时间')" align="center" prop="createTime">
sunhongwei's avatar
sunhongwei committed
51 52 53 54
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
Marcus's avatar
Marcus committed
55
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width">
sunhongwei's avatar
sunhongwei committed
56 57
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
Marcus's avatar
Marcus committed
58
                     v-hasPermi="['system:menu:update']">{{ $t('修改') }}</el-button>
sunhongwei's avatar
sunhongwei committed
59
          <el-button size="mini" type="text" icon="el-icon-plus" @click="handleAdd(scope.row)"
Marcus's avatar
Marcus committed
60
                     v-hasPermi="['system:menu:create']">{{ $t('新增') }}</el-button>
sunhongwei's avatar
sunhongwei committed
61
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
Marcus's avatar
Marcus committed
62
                     v-hasPermi="['system:menu:delete']">{{ $t('删除') }}</el-button>
sunhongwei's avatar
sunhongwei committed
63 64 65 66 67 68 69 70 71
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改菜单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
Marcus's avatar
Marcus committed
72
            <el-form-item :label="$t('上级菜单')">
sunhongwei's avatar
sunhongwei committed
73
              <treeselect v-model="form.parentId" :options="menuOptions" :normalizer="normalizer" :show-count="true"
Marcus's avatar
Marcus committed
74
                          :placeholder="$t('选择上级菜单')"/>
sunhongwei's avatar
sunhongwei committed
75 76 77
            </el-form-item>
          </el-col>
          <el-col :span="24">
Marcus's avatar
Marcus committed
78
            <el-form-item :label="$t('菜单类型')" prop="type">
sunhongwei's avatar
sunhongwei committed
79 80
              <el-radio-group v-model="form.type">
                <el-radio v-for="dict in menuTypeDictDatas" :key="parseInt(dict.value)" :label="parseInt(dict.value)">
81
                  {{$l(dict, 'label')}}</el-radio>
sunhongwei's avatar
sunhongwei committed
82 83 84
              </el-radio-group>
            </el-form-item>
          </el-col>
85
          <el-col :span="12" v-if="form.type == 2 || form.type == 1">
Marcus's avatar
Marcus committed
86
            <el-form-item :label="$t('显示菜单')" prop="isShowInMenuBar">
87 88 89
              <dict-selector v-model="form.isShowInMenuBar" :type="DICT_TYPE.INFRA_BOOLEAN_STRING" formatter="bool" form-type="radio" />
            </el-form-item>
          </el-col>
90 91
          <!--同一个组件可能会配置多个路由,通过路由设置keepalive会产生冲突,此字段废弃 22-09-17 @老丁 -->
          <!-- <el-col :span="12" v-if="form.type == 2">
Marcus's avatar
Marcus committed
92
            <el-form-item :label="$t('保活')" prop="keepalive">
sunhongwei's avatar
sunhongwei committed
93 94
              <dict-selector v-model="form.keepalive" :type="DICT_TYPE.INFRA_BOOLEAN_STRING" formatter="bool" form-type="radio" />
            </el-form-item>
95
          </el-col> -->
sunhongwei's avatar
sunhongwei committed
96
          <el-col :span="24">
Marcus's avatar
Marcus committed
97
            <el-form-item v-if="form.type != '3'" :label="$t('菜单图标')">
sunhongwei's avatar
sunhongwei committed
98 99
              <el-popover placement="bottom-start" width="460" trigger="click" @show="$refs['iconSelect'].reset()">
                <IconSelect ref="iconSelect" @selected="selected" />
Marcus's avatar
Marcus committed
100
                <el-input slot="reference" v-model="form.icon" :placeholder="$t('点击选择图标')" readonly>
sunhongwei's avatar
sunhongwei committed
101 102 103 104 105 106 107 108
                  <svg-icon v-if="form.icon" slot="prefix" :icon-class="form.icon" class="el-input__icon"
                            style="height: 32px;width: 16px;"/>
                  <i v-else slot="prefix" class="el-icon-search el-input__icon" />
                </el-input>
              </el-popover>
            </el-form-item>
          </el-col>
          <el-col :span="12">
Marcus's avatar
Marcus committed
109 110
            <el-form-item :label="$t('菜单名称')" prop="name">
              <el-input v-model="form.name" :placeholder="$t('请输入菜单名称')" />
sunhongwei's avatar
sunhongwei committed
111 112
            </el-form-item>
          </el-col>
huyufeng's avatar
huyufeng committed
113
          <el-col :span="12">
Marcus's avatar
Marcus committed
114 115
            <el-form-item :label="$t('英文名称')" prop="nameEn">
              <el-input v-model="form.nameEn" :placeholder="$t('请输入菜单英文名称')" />
huyufeng's avatar
huyufeng committed
116 117
            </el-form-item>
          </el-col>
sunhongwei's avatar
sunhongwei committed
118
          <el-col :span="12">
Marcus's avatar
Marcus committed
119
            <el-form-item :label="$t('显示排序')" prop="sort">
sunhongwei's avatar
sunhongwei committed
120 121 122 123
              <el-input-number v-model="form.sort" controls-position="right" :min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
Marcus's avatar
Marcus committed
124 125
            <el-form-item v-if="form.type != '3'" :label="$t('路由地址')" prop="path">
              <el-input v-model="form.path" :placeholder="$t('请输入路由地址')" />
sunhongwei's avatar
sunhongwei committed
126 127 128
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.type == '2'">
Marcus's avatar
Marcus committed
129 130
            <el-form-item :label="$t('组件路径')" prop="component">
              <el-input v-model="form.component" :placeholder="$t('请输入组件路径')" />
sunhongwei's avatar
sunhongwei committed
131 132 133
            </el-form-item>
          </el-col>
          <el-col :span="12">
Marcus's avatar
Marcus committed
134 135
            <el-form-item v-if="form.type != '1'" :label="$t('权限标识')">
              <el-input v-model="form.permission" :placeholder="$t('请权限标识')" maxlength="50" />
sunhongwei's avatar
sunhongwei committed
136 137 138
            </el-form-item>
          </el-col>
          <el-col :span="12">
Marcus's avatar
Marcus committed
139
            <el-form-item :label="$t('菜单状态')">
sunhongwei's avatar
sunhongwei committed
140 141
              <el-radio-group v-model="form.status">
                <el-radio v-for="dict in this.getDictDatas(DICT_TYPE.COMMON_STATUS)"
142
                          :key="dict.value" :label="parseInt(dict.value)">{{$l(dict, 'label')}}</el-radio>
sunhongwei's avatar
sunhongwei committed
143 144 145
              </el-radio-group>
            </el-form-item>
          </el-col>
sunhongwei's avatar
sunhongwei committed
146
          <el-col :span="12">
Marcus's avatar
Marcus committed
147 148
            <el-form-item v-if="form.type != '3'" :label="$t('重定向')" prop="redirect">
              <el-input v-model="form.redirect" :placeholder="$t('请输入重定向地址')" />
sunhongwei's avatar
sunhongwei committed
149 150
            </el-form-item>
          </el-col>
sunhongwei's avatar
sunhongwei committed
151 152 153
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
Marcus's avatar
Marcus committed
154 155
        <el-button type="primary" @click="submitForm">{{ $t('确 定') }}</el-button>
        <el-button @click="cancel">{{ $t('取 消') }}</el-button>
sunhongwei's avatar
sunhongwei committed
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
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listMenu, getMenu, delMenu, addMenu, updateMenu } from "@/api/system/menu";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import IconSelect from "@/components/IconSelect";

import { SystemMenuTypeEnum, CommonStatusEnum } from '@/utils/constants'
import { getDictDatas, DICT_TYPE } from '@/utils/dict'

export default {
  name: "Menu",
  components: { Treeselect, IconSelect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 菜单表格树数据
      menuList: [],
      // 菜单树选项
      menuOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开,默认全部折叠
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        name: undefined,
        visible: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
Marcus's avatar
Marcus committed
201
          { required: true, message: this.$t("菜单名称不能为空"), trigger: "blur" }
sunhongwei's avatar
sunhongwei committed
202
        ],
huyufeng's avatar
huyufeng committed
203
        enName: [
Marcus's avatar
Marcus committed
204
          { required: true, message: this.$t("菜单英文名称不能为空"), trigger: "blur" }
huyufeng's avatar
huyufeng committed
205
        ],
sunhongwei's avatar
sunhongwei committed
206
        sort: [
Marcus's avatar
Marcus committed
207
          { required: true, message: this.$t("菜单顺序不能为空"), trigger: "blur" }
sunhongwei's avatar
sunhongwei committed
208 209
        ],
        path: [
Marcus's avatar
Marcus committed
210
          { required: true, message: this.$t("路由地址不能为空"), trigger: "blur" }
sunhongwei's avatar
sunhongwei committed
211 212
        ],
        status: [
Marcus's avatar
Marcus committed
213
          { required: true, message: this.$t("状态不能为空"), trigger: "blur" }
sunhongwei's avatar
sunhongwei committed
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
        ]
      },

      // 枚举
      MenuTypeEnum: SystemMenuTypeEnum,
      CommonStatusEnum: CommonStatusEnum,
      // 数据字典
      menuTypeDictDatas: getDictDatas(DICT_TYPE.SYSTEM_MENU_TYPE),
      statusDictDatas: getDictDatas(DICT_TYPE.COMMON_STATUS)
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 选择图标
    selected(name) {
      this.form.icon = name;
    },
    /** 查询菜单列表 */
    getList() {
      this.loading = true;
      listMenu(this.queryParams).then(response => {
        this.menuList = this.handleTree(response.data, "id");
        this.loading = false;
      });
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
248
        label: this.$l(node, 'name'),
sunhongwei's avatar
sunhongwei committed
249 250 251 252 253
        children: node.children
      };
    },
    /** 查询菜单下拉树结构 */
    getTreeselect() {
254
      console.log('this.handleTree', this.handleTree)
sunhongwei's avatar
sunhongwei committed
255 256
      listMenu().then(response => {
        this.menuOptions = [];
Marcus's avatar
Marcus committed
257
        const menu = { id: 0, name: this.$t('主类目'), children: [] };
sunhongwei's avatar
sunhongwei committed
258 259 260 261 262 263 264 265 266 267 268 269
        menu.children = this.handleTree(response.data, "id");
        this.menuOptions.push(menu);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
270
        /* id: undefined,
sunhongwei's avatar
sunhongwei committed
271 272
        parentId: 0,
        name: undefined,
huyufeng's avatar
huyufeng committed
273
        enName: undefined,
sunhongwei's avatar
sunhongwei committed
274
        icon: undefined,
sunhongwei's avatar
sunhongwei committed
275
        isShowInMenuBar: true,
sunhongwei's avatar
sunhongwei committed
276 277
        type: SystemMenuTypeEnum.DIR,
        sort: undefined,
sunhongwei's avatar
sunhongwei committed
278
        keepalive: false,
sunhongwei's avatar
sunhongwei committed
279
        redirect: undefined,
280
        status: CommonStatusEnum.ENABLE */
sunhongwei's avatar
sunhongwei committed
281
      };
282
      /* this.resetForm("form"); */
sunhongwei's avatar
sunhongwei committed
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      this.getTreeselect();
      if (row != null && row.id) {
        this.form.parentId = row.id;
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
Marcus's avatar
Marcus committed
311
      this.title = this.$t("添加菜单");
sunhongwei's avatar
sunhongwei committed
312 313 314 315 316 317 318 319
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      getMenu(row.id).then(response => {
        this.form = response.data;
        this.open = true;
Marcus's avatar
Marcus committed
320
        this.title = this.$t("修改菜单");
sunhongwei's avatar
sunhongwei committed
321 322 323 324 325 326 327 328 329 330 331
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 若权限类型为目录或者菜单时,进行 path 的校验,避免后续拼接出来的路由无法跳转
          if (this.form.type === SystemMenuTypeEnum.DIR
            || this.form.type === SystemMenuTypeEnum.MENU) {
            // 如果是外链,则不进行校验
            const path = this.form.path
dragondean@qq.com's avatar
dragondean@qq.com committed
332
            if (path && path.indexOf('http://') === -1 || path.indexOf('https://') === -1) {
sunhongwei's avatar
sunhongwei committed
333 334
              // 父权限为根节点,path 必须以 / 开头
              if (this.form.parentId === 0 && path.charAt(0) !== '/') {
335
                this.$modal.msgSuccess(this.$t('前端必须以 / 开头'))
sunhongwei's avatar
sunhongwei committed
336 337
                return
              } else if (this.form.parentId !== 0 && path.charAt(0) === '/') {
338
                this.$modal.msgSuccess(this.$t('前端不能以 / 开头'))
sunhongwei's avatar
sunhongwei committed
339 340 341 342 343 344 345 346
                return
              }
            }
          }

          // 提交
          if (this.form.id !== undefined) {
            updateMenu(this.form).then(response => {
347
              this.$modal.msgSuccess(this.$t("修改成功"));
sunhongwei's avatar
sunhongwei committed
348 349 350 351 352
              this.open = false;
              this.getList();
            });
          } else {
            addMenu(this.form).then(response => {
353
              this.$modal.msgSuccess(this.$t("新增成功"));
sunhongwei's avatar
sunhongwei committed
354 355 356 357 358 359 360 361 362
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
363
      this.$modal.confirm(this.$t('是否确认删除此项?')).then(function() {
sunhongwei's avatar
sunhongwei committed
364 365 366
          return delMenu(row.id);
        }).then(() => {
          this.getList();
367
          this.$modal.msgSuccess(this.$t("删除成功"));
sunhongwei's avatar
sunhongwei committed
368 369 370 371 372
      }).catch(() => {});
    }
  }
};
</script>