authority.vue 11.0 KB
Newer Older
1
<template>
2
  <div class="authority">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
3
    <warning-bar title="注:右上角头像下拉可切换角色" />
Mr.奇淼('s avatar
Mr.奇淼( 已提交
4 5
    <div class="gva-table-box">
      <div class="gva-btn-list">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
6
        <el-button size="small" type="primary" icon="plus" @click="addAuthority('0')">新增角色</el-button>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
7 8 9 10 11 12 13
      </div>
      <el-table
        :data="tableData"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
        row-key="authorityId"
        style="width: 100%"
      >
Mr.奇淼('s avatar
Mr.奇淼( 已提交
14
        <el-table-column label="角色ID" min-width="180" prop="authorityId" />
Mr.奇淼('s avatar
Mr.奇淼( 已提交
15 16
        <el-table-column align="left" label="角色名称" min-width="180" prop="authorityName" />
        <el-table-column align="left" label="操作" width="460">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
17
          <template #default="scope">
P
piexlmax 已提交
18
            <el-button
P
piexlmax 已提交
19
              icon="setting"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
20
              size="small"
P
piexlmax 已提交
21 22 23
              type="text"
              @click="opdendrawer(scope.row)"
            >设置权限</el-button>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
24
            <el-button
P
piexlmax 已提交
25
              icon="plus"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
26
              size="small"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
27 28 29 30
              type="text"
              @click="addAuthority(scope.row.authorityId)"
            >新增子角色</el-button>
            <el-button
P
piexlmax 已提交
31
              icon="copy-document"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
32
              size="small"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
33
              type="text"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
34
              @click="copyAuthorityFunc(scope.row)"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
35 36
            >拷贝</el-button>
            <el-button
P
piexlmax 已提交
37
              icon="edit"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
38
              size="small"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
39 40 41 42
              type="text"
              @click="editAuthority(scope.row)"
            >编辑</el-button>
            <el-button
P
piexlmax 已提交
43
              icon="delete"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
44
              size="small"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
45 46 47 48 49 50
              type="text"
              @click="deleteAuth(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
51
    </div>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
52
    <!-- 新增角色弹窗 -->
53
    <el-dialog v-model="dialogFormVisible" :title="dialogTitle">
P
piexlmax 已提交
54
      <el-form ref="authorityForm" :model="form" :rules="rules" label-width="80px">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
55 56
        <el-form-item label="父级角色" prop="parentId">
          <el-cascader
何秀钢 已提交
57
            v-model="form.parentId"
P
piexlmax 已提交
58
            style="width:100%"
Mr.奇淼('s avatar
Mr.奇淼( 已提交
59 60 61 62 63
            :disabled="dialogType=='add'"
            :options="AuthorityOption"
            :props="{ checkStrictly: true,label:'authorityName',value:'authorityId',disabled:'disabled',emitPath:false}"
            :show-all-levels="false"
            filterable
何秀钢 已提交
64
          />
65
        </el-form-item>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
66
        <el-form-item label="角色ID" prop="authorityId">
何秀钢 已提交
67
          <el-input v-model="form.authorityId" :disabled="dialogType=='edit'" autocomplete="off" />
68
        </el-form-item>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
69
        <el-form-item label="角色姓名" prop="authorityName">
何秀钢 已提交
70
          <el-input v-model="form.authorityName" autocomplete="off" />
71 72
        </el-form-item>
      </el-form>
73 74
      <template #footer>
        <div class="dialog-footer">
P
piexlmax 已提交
75 76
          <el-button size="small" @click="closeDialog">取 消</el-button>
          <el-button size="small" type="primary" @click="enterDialog">确 定</el-button>
77 78
        </div>
      </template>
79
    </el-dialog>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
80

81
    <el-drawer v-if="drawer" v-model="drawer" :with-header="false" size="40%" title="角色配置">
Mr.奇淼('s avatar
Mr.奇淼( 已提交
82
      <el-tabs :before-leave="autoEnter" class="role-box" type="border-card">
83
        <el-tab-pane label="角色菜单">
84
          <Menus ref="menus" :row="activeRow" @changeRow="changeRow" />
85 86
        </el-tab-pane>
        <el-tab-pane label="角色api">
87
          <Apis ref="apis" :row="activeRow" @changeRow="changeRow" />
88
        </el-tab-pane>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
89
        <el-tab-pane label="资源权限">
90
          <Datas ref="datas" :authority="tableData" :row="activeRow" @changeRow="changeRow" />
Mr.奇淼('s avatar
Mr.奇淼( 已提交
91
        </el-tab-pane>
92 93
      </el-tabs>
    </el-drawer>
94 95 96
  </div>
</template>

Mr.奇淼('s avatar
Mr.奇淼( 已提交
97
<script setup>
Mr.奇淼('s avatar
Mr.奇淼( 已提交
98 99 100
import {
  getAuthorityList,
  deleteAuthority,
101
  createAuthority,
102
  updateAuthority,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
103
  copyAuthority
何秀钢 已提交
104
} from '@/api/authority'
105

Mr.奇淼('s avatar
Mr.奇淼( 已提交
106 107 108
import Menus from '@/view/superAdmin/authority/components/menus.vue'
import Apis from '@/view/superAdmin/authority/components/apis.vue'
import Datas from '@/view/superAdmin/authority/components/datas.vue'
P
piexlmax 已提交
109
import warningBar from '@/components/warningBar/warningBar.vue'
110

Mr.奇淼('s avatar
Mr.奇淼( 已提交
111 112
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
Mr.奇淼('s avatar
Mr.奇淼( 已提交
113

Mr.奇淼('s avatar
Mr.奇淼( 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 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
const mustUint = (rule, value, callback) => {
  if (!/^[0-9]*[1-9][0-9]*$/.test(value)) {
    return callback(new Error('请输入正整数'))
  }
  return callback()
}

const AuthorityOption = ref([
  {
    authorityId: '0',
    authorityName: '根角色'
  }
])
const drawer = ref(false)
const dialogType = ref('add')
const activeRow = ref({})

const dialogTitle = ref('新增角色')
const dialogFormVisible = ref(false)
const apiDialogFlag = ref(false)
const copyForm = ref({})

const form = ref({
  authorityId: '',
  authorityName: '',
  parentId: '0'
})
const rules = ref({
  authorityId: [
    { required: true, message: '请输入角色ID', trigger: 'blur' },
    { validator: mustUint, trigger: 'blur' }
  ],
  authorityName: [
    { required: true, message: '请输入角色名', trigger: 'blur' }
  ],
  parentId: [
    { required: true, message: '请选择请求方式', trigger: 'blur' }
  ]
})

const page = ref(1)
const total = ref(0)
const pageSize = ref(999)
const tableData = ref([])
const searchInfo = ref({})

// 查询
const getTableData = async() => {
  const table = await getAuthorityList({ page: page.value, pageSize: pageSize.value, ...searchInfo.value })
  if (table.code === 0) {
    tableData.value = table.data.list
    total.value = table.data.total
    page.value = table.data.page
    pageSize.value = table.data.pageSize
  }
}

getTableData()

const changeRow = (key, value) => {
  activeRow.value[key] = value
}
const menus = ref(null)
const apis = ref(null)
const datas = ref(null)
const autoEnter = (activeName, oldActiveName) => {
  const paneArr = [menus, apis, datas]
  if (oldActiveName) {
    if (paneArr[oldActiveName].value.needConfirm) {
      paneArr[oldActiveName].value.enterAndNext()
      paneArr[oldActiveName].value.needConfirm = false
何秀钢 已提交
185
    }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
  }
}
// 拷贝角色
const copyAuthorityFunc = (row) => {
  setOptions()
  dialogTitle.value = '拷贝角色'
  dialogType.value = 'copy'
  for (const k in form.value) {
    form.value[k] = row[k]
  }
  copyForm.value = row
  dialogFormVisible.value = true
}
const opdendrawer = (row) => {
  drawer.value = true
  activeRow.value = row
}
// 删除角色
const deleteAuth = (row) => {
  ElMessageBox.confirm('此操作将永久删除该角色, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(async() => {
      const res = await deleteAuthority({ authorityId: row.authorityId })
      if (res.code === 0) {
        ElMessage({
          type: 'success',
          message: '删除成功!'
        })
        if (tableData.value.length === 1 && page.value > 1) {
          page.value--
219
        }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
220
        getTableData()
221
      }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
222 223 224 225 226
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '已取消删除'
227
      })
Mr.奇淼('s avatar
Mr.奇淼( 已提交
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 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
    })
}
// 初始化表单
const authorityForm = ref(null)
const initForm = () => {
  if (authorityForm.value) {
    authorityForm.value.resetFields()
  }
  form.value = {
    authorityId: '',
    authorityName: '',
    parentId: '0'
  }
}
// 关闭窗口
const closeDialog = () => {
  initForm()
  dialogFormVisible.value = false
  apiDialogFlag.value = false
}
// 确定弹窗

const enterDialog = () => {
  if (form.value.authorityId === '0') {
    ElMessage({
      type: 'error',
      message: '角色id不能为0'
    })
    return false
  }
  authorityForm.value.validate(async valid => {
    if (valid) {
      switch (dialogType.value) {
        case 'add':
          {
            const res = await createAuthority(form.value)
            if (res.code === 0) {
              ElMessage({
                type: 'success',
                message: '添加成功!'
              })
              getTableData()
              closeDialog()
            }
          }
          break
        case 'edit':
          {
            const res = await updateAuthority(form.value)
            if (res.code === 0) {
              ElMessage({
                type: 'success',
                message: '添加成功!'
              })
              getTableData()
              closeDialog()
            }
          }
          break
        case 'copy': {
          const data = {
            authority: {
              authorityId: 'string',
              authorityName: 'string',
              datauthorityId: [],
              parentId: 'string'
            },
            oldAuthorityId: 0
          }
          data.authority.authorityId = form.value.authorityId
          data.authority.authorityName = form.value.authorityName
          data.authority.parentId = form.value.parentId
          data.authority.dataAuthorityId = copyForm.value.dataAuthorityId
          data.oldAuthorityId = copyForm.value.authorityId
          const res = await copyAuthority(data)
何秀钢 已提交
303
          if (res.code === 0) {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
304
            ElMessage({
何秀钢 已提交
305
              type: 'success',
Mr.奇淼('s avatar
Mr.奇淼( 已提交
306
              message: '复制成功!'
何秀钢 已提交
307
            })
Mr.奇淼('s avatar
Mr.奇淼( 已提交
308
            getTableData()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
309
          }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
310
        }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
311
      }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
312

Mr.奇淼('s avatar
Mr.奇淼( 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
      initForm()
      dialogFormVisible.value = false
    }
  })
}
const setOptions = () => {
  AuthorityOption.value = [
    {
      authorityId: '0',
      authorityName: '根角色'
    }
  ]
  setAuthorityOptions(tableData.value, AuthorityOption.value, false)
}
const setAuthorityOptions = (AuthorityData, optionsData, disabled) => {
  form.value.authorityId = String(form.value.authorityId)
  AuthorityData &&
Mr.奇淼('s avatar
Mr.奇淼( 已提交
330
        AuthorityData.forEach(item => {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
331 332 333 334
          if (item.children && item.children.length) {
            const option = {
              authorityId: item.authorityId,
              authorityName: item.authorityName,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
335
              disabled: disabled || item.authorityId === form.value.authorityId,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
336
              children: []
何秀钢 已提交
337
            }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
338
            setAuthorityOptions(
Mr.奇淼('s avatar
Mr.奇淼( 已提交
339 340
              item.children,
              option.children,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
341
              disabled || item.authorityId === form.value.authorityId
何秀钢 已提交
342 343
            )
            optionsData.push(option)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
344 345 346 347
          } else {
            const option = {
              authorityId: item.authorityId,
              authorityName: item.authorityName,
Mr.奇淼('s avatar
Mr.奇淼( 已提交
348
              disabled: disabled || item.authorityId === form.value.authorityId
何秀钢 已提交
349 350
            }
            optionsData.push(option)
351
          }
何秀钢 已提交
352
        })
Mr.奇淼('s avatar
Mr.奇淼( 已提交
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
}
// 增加角色
const addAuthority = (parentId) => {
  initForm()
  dialogTitle.value = '新增角色'
  dialogType.value = 'add'
  form.value.parentId = parentId
  setOptions()
  dialogFormVisible.value = true
}
// 编辑角色
const editAuthority = (row) => {
  setOptions()
  dialogTitle.value = '编辑角色'
  dialogType.value = 'edit'
  for (const key in form.value) {
    form.value[key] = row[key]
370
  }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
371 372 373 374 375 376 377 378 379 380
  setOptions()
  dialogFormVisible.value = true
}

</script>

<script>

export default {
  name: 'Authority'
何秀钢 已提交
381
}
382
</script>
何秀钢 已提交
383

384 385
<style lang="scss">
.authority {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
386
  .el-input-number {
387
    margin-left: 15px;
Mr.奇淼('s avatar
Mr.奇淼( 已提交
388
    span {
389 390 391
      display: none;
    }
  }
392
}
393
.role-box {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
394
  .el-tabs__content {
P
piexlmax 已提交
395
    height: calc(100vh - 72px);
Mr.奇淼('s avatar
Mr.奇淼( 已提交
396
    overflow: auto;
397
  }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
398
}
何秀钢 已提交
399
</style>