index.vue 14.9 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>
dragondean@qq.com's avatar
dragondean@qq.com committed
151 152 153 154 155
          <el-col :span="12">
            <el-form-item :label="$t('角标字段')" prop="redirect">
              <el-input v-model="form.badgeField" :placeholder="$t('不清楚请留空,已设勿改')" />
            </el-form-item>
          </el-col>
sunhongwei's avatar
sunhongwei committed
156 157 158
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
Marcus's avatar
Marcus committed
159 160
        <el-button type="primary" @click="submitForm">{{ $t('确 定') }}</el-button>
        <el-button @click="cancel">{{ $t('取 消') }}</el-button>
sunhongwei's avatar
sunhongwei committed
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
      </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
206
          { required: true, message: this.$t("菜单名称不能为空"), trigger: "blur" }
sunhongwei's avatar
sunhongwei committed
207
        ],
huyufeng's avatar
huyufeng committed
208
        enName: [
Marcus's avatar
Marcus committed
209
          { required: true, message: this.$t("菜单英文名称不能为空"), trigger: "blur" }
huyufeng's avatar
huyufeng committed
210
        ],
sunhongwei's avatar
sunhongwei committed
211
        sort: [
Marcus's avatar
Marcus committed
212
          { required: true, message: this.$t("菜单顺序不能为空"), trigger: "blur" }
sunhongwei's avatar
sunhongwei committed
213 214
        ],
        path: [
Marcus's avatar
Marcus committed
215
          { required: true, message: this.$t("路由地址不能为空"), trigger: "blur" }
sunhongwei's avatar
sunhongwei committed
216 217
        ],
        status: [
Marcus's avatar
Marcus committed
218
          { required: true, message: this.$t("状态不能为空"), trigger: "blur" }
sunhongwei's avatar
sunhongwei committed
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
        ]
      },

      // 枚举
      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,
253
        label: this.$l(node, 'name'),
sunhongwei's avatar
sunhongwei committed
254 255 256 257 258
        children: node.children
      };
    },
    /** 查询菜单下拉树结构 */
    getTreeselect() {
259
      console.log('this.handleTree', this.handleTree)
sunhongwei's avatar
sunhongwei committed
260 261
      listMenu().then(response => {
        this.menuOptions = [];
Marcus's avatar
Marcus committed
262
        const menu = { id: 0, name: this.$t('主类目'), children: [] };
sunhongwei's avatar
sunhongwei committed
263 264 265 266 267 268 269 270 271 272 273 274
        menu.children = this.handleTree(response.data, "id");
        this.menuOptions.push(menu);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
275
        /* id: undefined,
sunhongwei's avatar
sunhongwei committed
276 277
        parentId: 0,
        name: undefined,
huyufeng's avatar
huyufeng committed
278
        enName: undefined,
sunhongwei's avatar
sunhongwei committed
279
        icon: undefined,
sunhongwei's avatar
sunhongwei committed
280
        isShowInMenuBar: true,
sunhongwei's avatar
sunhongwei committed
281 282
        type: SystemMenuTypeEnum.DIR,
        sort: undefined,
sunhongwei's avatar
sunhongwei committed
283
        keepalive: false,
sunhongwei's avatar
sunhongwei committed
284
        redirect: undefined,
285
        status: CommonStatusEnum.ENABLE */
sunhongwei's avatar
sunhongwei committed
286
      };
287
      /* this.resetForm("form"); */
sunhongwei's avatar
sunhongwei committed
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
    },
    /** 搜索按钮操作 */
    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
316
      this.title = this.$t("添加菜单");
sunhongwei's avatar
sunhongwei committed
317 318 319 320 321 322 323 324
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      getMenu(row.id).then(response => {
        this.form = response.data;
        this.open = true;
Marcus's avatar
Marcus committed
325
        this.title = this.$t("修改菜单");
sunhongwei's avatar
sunhongwei committed
326 327 328 329 330 331 332 333 334 335 336
      });
    },
    /** 提交按钮 */
    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
337
            if (path && path.indexOf('http://') === -1 || path.indexOf('https://') === -1) {
sunhongwei's avatar
sunhongwei committed
338 339
              // 父权限为根节点,path 必须以 / 开头
              if (this.form.parentId === 0 && path.charAt(0) !== '/') {
340
                this.$modal.msgSuccess(this.$t('前端必须以 / 开头'))
sunhongwei's avatar
sunhongwei committed
341 342
                return
              } else if (this.form.parentId !== 0 && path.charAt(0) === '/') {
343
                this.$modal.msgSuccess(this.$t('前端不能以 / 开头'))
sunhongwei's avatar
sunhongwei committed
344 345 346 347 348 349 350 351
                return
              }
            }
          }

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