首頁 > 軟體

vue後臺系統管理專案之角色許可權分配管理功能(範例詳解)

2022-09-28 14:00:35

vue後臺系統管理專案:

  • 技術選型:vue + element-ui
  • 選單許可權管理模組功能
  • 角色列表查詢,通過(角色名稱;角色編號;狀態:啟用、禁用)進行角色資料搜尋。
  • 查詢、重置、新建角色功能
  • 角色列表分頁實現
  • 角色編輯,角色禁用,角色刪除操作
  • 新建角色功能包括對角色名稱、選單許可權資訊的儲存提交

角色許可權分配管理功能

element-ui tree元件注意事項

node-key 每個樹節點用來作為唯一標識的屬性,整棵樹應該是唯一的

check-strictly 在顯示核取方塊的情況下,是否嚴格的遵循父子不互相關聯的做法,預設為 false

expand-on-click-node 是否在點選節點的時候展開或者收縮節點, 預設值為 true,如果為 false,則只有點箭頭圖示的時候才會展開或者收縮節點。

check-change 節點選中狀態發生變化時的回撥,共三個引數,依次為:傳遞給data屬性的陣列中該節點所對應的物件、節點本身是否被選中、節點的子樹中是否有被選中的節點

props

role.vue模組

1.查詢重置搜尋

 <!-- 查詢重置搜尋 -->
    <el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm" size="35">
      <el-row>
        <el-col :span="8">
          <div class="grid-content bg-purple-light">
            <el-form-item label="角色名稱" prop="roleName">
              <el-input v-model="ruleForm.roleName" placeholder="請輸入角色名稱"></el-input>
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content bg-purple-light">
            <el-form-item label="角色編號" prop="id">
              <el-input v-model="ruleForm.id" placeholder="請輸入角色編號"></el-input>
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content bg-purple-light">
            <el-form-item label="狀態" prop="disable">
              <el-select v-model="ruleForm.disable" placeholder="請選擇狀態">
                <el-option label="啟用" value="false"></el-option>
                <el-option label="禁用" value="true"></el-option>
              </el-select>
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="24">
          <div class="grid-content bg-purple-light ">
            <el-form-item>
              <el-button type="primary" @click="submitForm('ruleForm')">查詢</el-button>
              <el-button @click="resetForm('ruleForm')">重置</el-button>
              <el-button @click="addNewRole" type="primary">新建角色</el-button>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
    </el-form>

2.table列表

<!--列表-->
    <el-table :data="tableData" border highlight-current-row v-loading="loading" element-loading-text="拼命載入中"
              style="width: 100%;height:auto;" :header-cell-style="{textAlign: 'center',background:'#fafafa'}"
              :cell-style="{ textAlign: 'center' }">
      <el-table-column prop="id" label="角色編號"></el-table-column>
      <el-table-column prop="roleName" label="角色名稱"></el-table-column>
      <el-table-column prop="disable" label="狀態">
        <template slot-scope="scope">{{ scope.row.disable == false ? '啟用' : '禁用' }}</template>
      </el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button @click="handleEditClick(scope.row)" type="text" size="small">編輯</el-button>
          <!-- <el-button @click="handleLimitClick(scope.row)" type="text" size="small">許可權設定</el-button> -->
          <el-button :class="scope.row.disable==false?'isCanUse':''" @click="handleDisableClick(scope.row)" type="text"
                     size="small">{{ scope.row.disable == true ? '啟用' : '禁用' }}
          </el-button>
          <el-button class="isCanUse" @click="delFun(scope.row)" type="text" size="small">刪除</el-button>
        </template>
      </el-table-column>
    </el-table>

3.分頁

<el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                   :current-page="pagination.page" :page-sizes="[10, 20, 30, 40,50]" :page-size="pagination.pageSize"
                   layout="total, sizes, prev, pager, next, jumper" :total="pagination.total"></el-pagination>

4.新建/編輯角色彈窗

 <!-- 新建傳送彈框 -->
    <el-dialog title="新建角色" :visible.sync="dialogFormUser">
      <el-form ref="newRoleForm" :rules="rules" :model="newRoleForm" label-width="100px">
        <!-- <el-form-item label="角色編號" prop="userName">
          <el-input v-model="newRoleForm.userName" placeholder="自動生成" :disabled="true"></el-input>
        </el-form-item>-->
        <el-form-item label="角色名稱" prop="roleName">
          <el-input v-model="newRoleForm.roleName" placeholder="請輸入角色名稱"></el-input>
        </el-form-item>
        <el-form-item label="選單許可權">
          <div>
            <el-tree :data="data2" node-key="id" ref="tree" :check-strictly="checkStrictly" :expand-on-click-node="true"
                     @check-change="handleChecked" :props="defaultProps" :show-checkbox="true"></el-tree>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button @click="saveRole('newRoleForm')" type="primary">立即儲存</el-button>
          <el-button @click="dialogFormUser=false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

5.介面引入

import {
  getRolePage,//獲取角色列表
  addRole,//新增角色
  allMenu,//選單列表
  disableRoleEnable,//禁用角色
  roleEditInfo,//角色編輯
  updateEditRole, //更新角色
  delSysRole//刪除角色
} from "../../api/userMG";

6.data定義

data() {
    return {
      loading: false, //是顯示載入
      isCanUse: false,
      dialogFormUser: false,
      checkStrictly: true,
      pagination: {
        page: 1,
        pageSize: 10,
        total: 0
      },
      ruleForm: {
        roleName: "",
        id: "",
        disable: ""
      },
      newRoleForm: {
        roleName: ""
      },
      rules: {
        roleName: [
          {required: true, message: "請輸入角色名稱", trigger: "blur"}
        ]
      },
      treeCheckedData: [],
      CheckedData: [], //選擇新建角色的勾選
      tableData: [],
      data2: [],
      roleId: "",
      defaultProps: {
        children: "childMenu",
        label: "name"
      }
    };
  },

7.methods方法

查詢資料

submitForm(ruleForm) {
      this.$refs[ruleForm].validate(valid => {
        if (valid) {
          this.pagination.page = 1;
          this.getRolePageFun();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

重置資料

resetForm(ruleForm) {
      this.pagination.page = 1;
      this.$refs[ruleForm].resetFields();
      (this.ruleForm.roleName = ""),
        (this.ruleForm.id = ""),
        (this.ruleForm.disable = ""),
        (this.ruleForm.pageNum = 1);
      this.ruleForm.pageSize = this.pagination.pageSize;
      this.getRolePageFun();
    },

選擇一頁幾條資料

handleSizeChange(val) {
      this.pagination.pageSize = val;
      this.pagination.page = 1;
      this.getRolePageFun();
    },

選擇第幾頁

handleCurrentChange(val) {
      this.pagination.page = val;
      this.getRolePageFun();
    },

許可權設定

handleLimitClick(val) {
      this.dialogFormUser = true;
    },

啟用和禁用角色

handleDisableClick(row) {
      console.log(row, "row");
      this.$confirm(
        row.disable == true ? "是否將此使用者開啟使用?" : "是否將此使用者禁止使用?",
        "提示",
        {
          confirmButtonText: "確定",
          cancelButtonText: "取消",
          type: "warning"
        }
      )
        .then(() => {
          let params = {
            disable: row.disable == true ? "false" : "true",
            id: row.id
          };
          disableRoleEnable(params)
            .then(res => {
              this.loading = false;
              if (res.code == 200) {
                console.log("開啟或者禁用");
                this.$message.success(
                  row.disable == true ? "啟用成功" : "禁用成功"
                );
                this.reload();
              } else {
                this.$message.error(res.msg);
              }
            })
            .catch(err => {
              this.loading = false;
              this.$message.error("選單載入失敗,請稍後再試!");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
    },

刪除角色,根據角色id

delFun(row) {
      this.$confirm(
        "是否將此使用者刪除?",
        "提示",
        {
          confirmButtonText: "確定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).then(() => {
        delSysRole({id: row.id}).then(res =>{
          if (res.code === 200) {
            this.$message.success("操作成功");
            this.reload();
          }else {
            this.$message.warning(res.msg);
          }
        })
      }).catch(() => {
        this.$message.info("取消操作");
      })
    },

新增角色

addNewRole() {
      this.dialogFormUser = true;
      this.treeCheckedData = [];
      this.newRoleForm.roleName = "";
      this.roleId = "";
      this.getAllMenu();
    },

點選節點選中 (節點選中狀態發生變化時的回撥)

handleChecked(data) {
      this.CheckedData = this.$refs.tree.getCheckedKeys();
    },

立即儲存角色

saveRole() {
      this.addRoleFun();
    },

點選編輯

handleEditClick(row) {
      console.log(row, "row");
      this.dialogFormUser = true;
      // /sysRole/info// 根據id檢視角色詳情
      roleEditInfo(row.id)
        .then(res => {
          this.loading = false;
          if (res.code == 200) {
            console.log(res.data, "根據id檢視角色詳情");
            this.getAllMenu();
            this.roleId = res.data.id;
            // this.newRoleForm.id = res.data.id;
            this.newRoleForm.roleName = res.data.roleName;
            let that = this;
            setTimeout(function () {
              res.data.menuIds.forEach(value => {
                that.$refs.tree.setChecked(value, true, false);
              });
            }, 1000);
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch(err => {
          this.loading = false;
          this.$message.error("選單載入失敗,請稍後再試!");
        });
    },

新建角色

getRolePageFun() {
      this.ruleForm.pageNum = this.pagination.page;
      this.ruleForm.pageSize = this.pagination.pageSize;
      getRolePage(this.ruleForm)
        .then(res => {
          this.loading = false;
          if (res.code == 200) {
            console.log(res.data, "角色列表函數");
            this.tableData = res.data.records;
            this.pagination.total = res.data.total;
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch(err => {
          this.loading = false;
          this.$message.error("選單載入失敗,請稍後再試!");
        });
    },

新增角色儲存函數

addRoleFun(newRoleForm) {
      console.log(this.newRoleForm, "this.newRoleForm");
      if (this.newRoleForm.roleName == "") {
        this.$message.error("角色名稱不能為空");
      } else if (this.CheckedData.length == 0) {
        this.$message.error("許可權不能為空");
      } else {
        if (this.roleId == "") {
          this.newRoleForm.menuIds = this.CheckedData;
          addRole(this.newRoleForm)
            .then(res => {
              this.loading = false;
              console.log(res, "新增角色");
              if (res.code == 200) {
                this.$message.success("新增成功");
                this.reload();
              } else {
                this.$message.error(res.msg);
              }
            })
            .catch(err => {
              this.loading = false;
              this.$message.error("選單載入失敗,請稍後再試!");
            });
        } else {
          this.newRoleForm.menuIds = this.CheckedData;
          this.newRoleForm.id = this.roleId;
          console.log(this.newRoleForm.menuIds, "id獲取");
          updateEditRole(this.newRoleForm)
            .then(res => {
              this.loading = false;
              console.log(res, "編輯角色");
              if (res.code == 200) {
                this.$message.success("編輯成功");
                this.reload();
              } else {
                this.$message.error(res.msg);
              }
            })
            .catch(err => {
              this.loading = false;
              this.$message.error("選單載入失敗,請稍後再試!");
            });
        }
      }
    },

獲取所有的選單函數

getAllMenu() {
      allMenu()
        .then(res => {
          this.loading = false;
          console.log(res.data, "獲取所有的選單");
          if (res.code == 200) {
            this.data2 = res.data;
          } else {
            this.$message.error(res.msg);
          }
        })
        .catch(err => {
          this.loading = false;
          this.$message.error("選單載入失敗,請稍後再試!");
        });
    }

8.created載入角色列表

created() {
    // 角色列表
    this.getRolePageFun();
  },

到此這篇關於vue後臺系統管理專案-角色許可權分配管理功能的文章就介紹到這了,更多相關vue角色許可權分配管理內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com