• liang's avatar
    init · dad95e78
    liang authored
    dad95e78
index.vue 6.55 KB
<template>
  <div class="container">
    <direct-search
      ref="form"
      :label-position="'right'"
      :forms="searchList"
      :style="{ textAlign: 'left' }"
      @handleSearch="handleFormSearch"
    />
    <el-table-self
      ref="table"
      :table-data="tableData"
      :columns="columns"
      :list-loading="listLoading"
      :current-page="pageIndex"
      :total-count="total"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      @pageSizeChange="handleSizeChange"
      @currentPageChange="handleCurrentChange"
    />
    <dialog-form
      width="450px"
      ref="dialog"
      :title="title"
      :form-edit="formEdit"
      :form-data="formData"
      :tree-list="treeList"
      @handleConfirm="handleConfirm"
    ></dialog-form>
  </div>
</template>

<script>
import paginationMixin from "@/components/TabComponents/mixin"
import {
  getHprolePage,
  delHprole,
  addHprole,
  getHproleDetail,
} from "@/api/user"
import { getMenuPage } from "@/api/menu"
import { translateListToTree } from "@/utils/handleRoutes"

export default {
  name: "role",
  data() {
    return {
      listLoading: false,
      isAdd: false,
      id: null,
      // 查询列表
      searchList: [
        {
          label: "角色名",
          type: "input",
          prop: "roleName",
          placeholder: "请输入角色名",
        },

        {
          label: "状态",
          type: "select",
          prop: "status",
          opts: [
            {
              label: "启用",
              value: 1,
            },
            {
              label: "禁用",
              value: 0,
            },
          ],
        },
        {
          type: "button",
          value: "查询",
          icon: "el-icon-search",
        },
        {
          type: "button",
          color: "primary",
          icon: "el-icon-plus",
          value: "添加",
          func: this.handleAdd,
        },
      ],
      columns: [
        {
          label: "角色名",
          minWidth: 120,
          value: "roleName",
        },
        {
          label: "角色代码",
          minWidth: 120,
          value: "roleCode",
        },
        {
          label: "备注",
          value: "note",
        },
        {
          label: "状态",
          value: "status",
          minWidth: 80,
          formatter(row) {
            return row.status === 1 ? "启用" : "禁用"
          },
        },
        {
          label: "操作",
          width: 280,
          fixed: "right",
          operType: "button",
          operations: [
            {
              func: this.handleAdd,
              formatter(row) {
                return {
                  label: "编辑",
                  type: "primary",
                }
              },
            },
            {
              func: this.handleDel,
              formatter(row) {
                return {
                  label: "删除",
                  type: "warning",
                }
              },
            },
          ],
        },
      ],
      tableData: [],
      cacheForm: {},
      title: "",
      formData: [
        {
          type: "input",
          label: "角色名",
          prop: "roleName",
          placeholder: "请输入角色名",
          rules: [{ required: true, message: "请输入角色名" }],
        },
        {
          type: "input",
          label: "角色代码",
          placeholder: "请输入角色代码",
          prop: "roleCode",
          rules: [{ required: true, message: "请输入角色代码" }],
          disabled: true,
        },
        {
          type: "input",
          placeholder: "请输入备注",
          label: "备注",
          prop: "note",
        },
        {
          type: "tree",
          label: "菜单权限",
        },
      ],
      formEdit: {},
      treeList: [],
      isDepart: false,
    }
  },
  mixins: [paginationMixin],

  methods: {
    handleDel(row) {
      this.$confirm(`是否删除【${row.roleName}】?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delHprole(row.id).then((res) => {
            if (res.code === 1) {
              this.$message({
                type: "success",
                message: "删除成功!",
              })
              this.handleSearch()
            }
          })
        })
        .catch(() => {})
    },
    async handleAdd({ id, roleName, roleCode, note }) {
      this.title = id ? "编辑" : "新增"
      this.formEdit = id ? { id, roleName, roleCode, note } : {}
      this.formData[1].disabled = Boolean(id)
      if (id) {
        this.listLoading = true
        const res = await getHproleDetail(id)
        if (res.code === 1) {
          this.listLoading = false
          this.formEdit.treeList = res.data.menuList
          this.$refs.dialog.open()
        }
      } else {
        this.$refs.dialog.open()
      }
    },
    handleConfirm(form) {
      const data = { role: {} }
      for (let k in form) {
        if (k === "treeList") {
          data["menuList"] = form[k].map((_) => {
            return {
              id: _,
            }
          })
        } else {
          data["role"][k] = form[k]
        }
      }
      addHprole(data).then((res) => {
        if (res.code === 1) {
          this.$message.success("操作成功")
          this.handleSearch()
          this.$refs.dialog.close()
        }
      })
    },
    // 查询
    handleFormSearch(form) {
      this.pageIndex = 1
      this.handleSearch(form)
    },
    handleSearch(form) {
      this.cacheForm = Object.assign(this.cacheForm, form)
      this.listLoading = true
      const params = Object.assign({}, this.cacheForm)
      for (let key in params) {
        if (params[key] === "") {
          delete params[key]
        }
      }
      params.current = this.pageIndex
      params.size = this.pageSize
      getHprolePage(params).then((res) => {
        this.listLoading = false
        if (res.code === 1) {
          const d = res.data
          this.tableData = d.records || []
          this.total = Number(d.total)
        }
      })
    },
    getMenuPage() {
      getMenuPage({ current: 1, size: 100 }).then((res) => {
        this.listLoading = false
        if (res.code === 1) {
          const d = res.data
          this.treeList =
            translateListToTree(
              d.records.sort((a, b) => {
                return a.sortIdx - b.sortIdx
              })
            ) || []
        }
      })
    },
  },
  created() {
    this.getMenuPage()
    this.handleFormSearch()
  },
}
</script>