Newer
Older
operation_web / src / components / sys / role.vue
<template>
  <div id="role">
    <div id="roleTop">
      <el-input
        placeholder="请输入角色名称"
        v-model="roleValue"
        clearable
      ></el-input>
      <el-button
        type="primary"
        icon="el-icon-search"
        @click="loadGridData()"
        v-has="'Search'"
        >搜索</el-button
      >
      <el-button
        type="primary"
        icon="el-icon-search"
        @click="AddTop()"
        v-has="'m9-3-b1'"
        >新增角色</el-button
      >
    </div>
    <div
      id="roleCentent"
      v-loading="loading"
      element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(255, 255, 255, 0.3)"
    >
      <el-table :data="TableData" style="width: 100%;height:calc(100% - 45px)">
        <el-table-column prop="roleName" label="角色名称"></el-table-column>
        <el-table-column prop="roleId" label="角色ID"></el-table-column>
        <el-table-column prop="remark" label="备注"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="EditRole(scope.$index, scope.row)"
              style="margin:0 10px"
              v-has="'m9-3-b2'"
              >修改角色</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="DelRole(scope.$index, scope.row)"
              style="margin:0 10px"
              v-has="'m9-3-b3'"
              >删除角色</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="ALSizeChange"
        @current-change="ALCurrentChange"
        :current-page="ALPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="ALSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="ALDatagridDataLength"
        style="margin-top:10px;"
      ></el-pagination>
    </div>
    <el-dialog :title="DialogTitle" :visible.sync="AddDialog" width="400px">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="80px"
      >
        <el-form-item label="角色名称" prop="roleName" style="width:100%">
          <el-input
            size="small"
            v-model="ruleForm.roleName"
            clearable
            placeholder="请输入角色名称"
          ></el-input>
          <el-input
            size="small"
            v-model="ruleForm.roleId"
            clearable
            v-show="false"
          ></el-input>
        </el-form-item>
        <el-form-item label="角色类型" prop="type" style="width:100%">
          <el-select
            v-model="ruleForm.type"
            clearable
            placeholder="请选择角色类型"
          >
            <el-option
              v-for="(role, index) in roleTypeList"
              :key="index"
              :label="role.label"
              :value="role.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark" style="width:100%">
          <el-input
            size="small"
            v-model="ruleForm.remark"
            clearable
            placeholder="请输入备注"
          ></el-input>
        </el-form-item>
      </el-form>
      <p id="TreeTitle">权限分配</p>
      <div id="TreeBox">
        <el-tree
          :data="Treedata"
          show-checkbox
          node-key="menuId"
          :default-checked-keys="checklist"
          :props="defaultProps"
          ref="tree"
          v-loading="loading2"
          element-loading-text="拼命加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(255, 255, 255, 0.3)"
        ></el-tree>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="AddDialog = false">取 消</el-button>
        <el-button type="primary" @click="DialogUpdata('ruleForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { message } from "./../../util/item";
export default {
  name: "role",
  data: function() {
    return {
      roleValue: "",
      loading: false,
      loading2: false,
      TableData: [], //表格数据
      ALPage: 1, //分页默认显示页
      ALDatagridDataLength: 0, //分页上显示的数据总条数
      ALSize: 10, //分页上显示的每页的条数
      DialogTitle: "新增",
      AddDialog: false,
      ruleForm: {
        roleName: "",
        remark: "",
        type: undefined,
        roleId: "" //修改的时候才用
      },
      rules: {
        roleName: [
          {
            required: true,
            message: "此项为必填项",
            trigger: "blur"
          }
        ],
        type: [
          {
            required: true,
            message: "此项为必填项",
            trigger: "change"
          }
        ],
        remark: [
          {
            required: true,
            message: "此项为必填项",
            trigger: "blur"
          }
        ]
      },
      Treedata: [], //树形结构
      defaultProps: {
        children: "children",
        label: "name"
      }, //树的相关设置
      checklist: [], //默认勾选的节点
      CheckRoleId: "", //选中的角色的id
      addAndEditURL: "", //新增一级提交时的url
      roleTypeList: [
        //角色类型列表
        { label: "查询角色", value: 0 },
        { label: "操作角色", value: 1 },
        { label: "管理角色", value: 2 }
      ]
    };
  },
  methods: {
    ALSizeChange(val) {
      // 改变每页的条数
      this.ALSize = val;
      this.loadGridData();
    },
    ALCurrentChange(val) {
      //改变页数
      this.ALPage = val;
      this.loadGridData();
      this.loadUserHasList();
    },
    // 获取所有的角色
    loadGridData() {
      this.loading = true;
      this.TableData = [];
      this.$http
        .post(this.nozzle.roleGetRoles, {
          current: this.ALPage,
          size: this.ALSize,
          data: {
            searchStr: ""
          }
        })
        .then(response => {
          this.loading = false;
          if (response.data.code === 1) {
            this.TableData = response.data.data;
            this.ALDatagridDataLength = response.data.total;
          } else {
            message(response);
          }
        })
        .catch(response => {
          this.loading = false;
          message(response);
        });
    },
    // 新增
    AddTop() {
      this.AddDialog = true;
      this.$nextTick(function() {
        this.$refs["ruleForm"].resetFields();
        this.resetForm();
      });
      this.ruleForm.roleId = "";
      this.loadTreeData();
      this.addAndEditURL = this.nozzle.roleAddRole;
    },
    // 修改
    EditRole(index, rows) {
      this.AddDialog = true;
      console.log(rows);
      this.ruleForm = { ...{}, ...rows };
      this.loadTreeData();
      this.loadUserHasList(rows.roleId);
      this.addAndEditURL = this.nozzle.roleUpdateRole;
    },
    // 提交
    DialogUpdata(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          var AllChecksList = this.$refs.tree
            .getCheckedNodes()
            .concat(this.$refs.tree.getHalfCheckedNodes());
          var updataCheckList = [];
          for (var i = 0; i < AllChecksList.length; i++) {
            updataCheckList.push({
              menuId: AllChecksList[i].menuId
            });
          }
          this.ruleForm.menus = updataCheckList;
          this.$http
            .post(this.addAndEditURL, this.ruleForm)
            .then(response => {
              if (response.data.code === 1) {
                this.$message({
                  message: response.data.msg,
                  type: "success"
                });
              } else {
                this.$message({
                  message: response.data.msg,
                  type: "warning"
                });
              }
              this.loadGridData();
              this.AddDialog = false;
              this.$refs[formName].resetFields();
            })
            .catch(response => {
              this.loading = false;
              message(response);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 删除
    DelRole(index, rows) {
      this.$confirm("此操作将永久删除该信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          var DelList = [rows.roleId];
          this.$http
            .post(this.nozzle.roleDeleteRole, DelList)
            .then(response => {
              if (response.data.code === 1) {
                this.$message({
                  message: response.data.msg,
                  type: "success"
                });
              } else {
                this.$message({
                  message: response.data.msg,
                  type: "warning"
                });
              }
              this.loadGridData();
            })
            .catch(response => {
              this.loading = false;
              message(response);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    // 获取所有的树状数据
    loadTreeData() {
      this.loading2 = true;
      this.Treedata = [];
      this.checklist = [];
      this.$http
        .post(this.nozzle.menuGetMenuTree)
        .then(response => {
          this.loading2 = false;
          if (response.data.code === 1) {
            this.Treedata = response.data.data;
          } else {
            message(response);
          }
        })
        .catch(response => {
          this.loading2 = false;
          message(response);
        });
    },
    // 获取当前角色已分配的权限列表
    loadUserHasList(id) {
      this.checklist = [];
      this.$http
        .post(this.nozzle.roleGetRoleMenums, {
          roleId: id
        })
        .then(response => {
          if (response.data.code === 1) {
            this.checklist = response.data.data;
          } else {
            message(response);
          }
        })
        .catch(response => {
          message(response);
        });
    },
    resetForm() {
      this.ruleForm = {
        roleName: "",
        remark: "",
        type: undefined,
        roleId: "" //修改的时候才用
      };
    }
  },
  mounted: function() {
    this.loadGridData();
  }
};
</script>

<style scoped>
#role {
  width: 100%;
  height: 100%;
}
/* 顶部搜索 */
#roleTop {
  width: 100%;
  height: 60px;
  line-height: 60px;
  text-align: left;
}
#roleTop .el-input {
  width: 250px;
  margin: 0 10px;
}
/* 表格 */
#roleCentent {
  width: 100%;
  height: calc(100% - 70px);
  margin-top: 10px;
  /* background: rgba(53, 53, 53, 0.5); */
}
#TreeBox {
  width: 100%;
  height: 410px;
  overflow: auto;
}
#TreeTitle {
  width: 100%;
  height: 30px;
  line-height: 30px;
  text-align: center;
  margin-bottom: 10px;
}
/deep/ .el-select {
  width: 100%;
}
</style>
<style>
.el-form-item__label {
  text-align: left !important;
}
</style>