浏览代码

角色权限

ZaiZai 1 年之前
父节点
当前提交
d8c38b9e1b
共有 3 个文件被更改,包括 201 次插入13 次删除
  1. 2 8
      src/api/request/index.js
  2. 176 0
      src/views/authority/modules/role/auth.vue
  3. 23 5
      src/views/authority/role.vue

+ 2 - 8
src/api/request/index.js

@@ -1,4 +1,4 @@
-import { HcWarMsg, httpApi } from 'hc-vue3-ui'
+import { httpApi } from 'hc-vue3-ui'
 import router from '~src/router/index'
 import pinia from '~src/store/init'
 import { useAppStore } from '~src/store'
@@ -8,7 +8,7 @@ import website from '~src/config'
 const store = useAppStore(pinia)
 
 //封装的请求
-export const HcApi = async (obj, msg) => {
+export const HcApi = async (obj) => {
     return new Promise((resolve) => {
         //处理统一的请求头
         obj.headers = obj.headers ?? {}
@@ -29,11 +29,5 @@ const getResData = async ({ code }) => {
     if (code === 401) {
         window.$message?.error('身份失效,请重新登录!')
         router.push({ path: '/login' }).then()
-    } else if (code !== 404 && code !== 500 && code !== 504) {
-        await HcWarMsg({
-            type: 'warning',
-            title: '服务器异常,请稍后重试',
-            text: '服务器异常了,如有需要,请联系管理员!',
-        })
     }
 }

+ 176 - 0
src/views/authority/modules/role/auth.vue

@@ -0,0 +1,176 @@
+<template>
+    <hc-new-dialog v-model="isShow" widths="800px" :padding="false" title="角色权限配置" is-table is-footer-center @close="dialogClose">
+        <el-tabs tab-position="left" class="hc-role-auth-dialog h-full w-full">
+            <template v-for="(item, index) in tabPaneData" :key="index">
+                <el-tab-pane :label="item.name">
+                    <hc-data-tree
+                        :ref="(el) => setTreeRefs(el, item)" tree-key="id" :h-props="treeProps"
+                        show-checkbox :datas="authTree[item.key]" :default-checked-keys="roleData[item.key]"
+                    />
+                </el-tab-pane>
+            </template>
+        </el-tabs>
+        <template #footer>
+            <el-button hc-btn @click="dialogClose">取消</el-button>
+            <el-button hc-btn type="primary" :loading="submitLoading" @click="dialogSubmit">提交</el-button>
+        </template>
+    </hc-new-dialog>
+</template>
+
+<script setup>
+import { ref, watch } from 'vue'
+import { arrIndex, getArrValue, getObjValue, isNullES } from 'js-fast-way'
+import mainApi from '~api/authority/role'
+
+const props = defineProps({
+    info: {
+        type: Object,
+        default: () => ({}),
+    },
+})
+
+//事件
+const emit = defineEmits(['close'])
+
+//双向绑定
+const isShow = defineModel('modelValue', {
+    default: false,
+})
+
+//监听内容
+const rowInfo = ref(props.info)
+watch(() => props.info, (data) => {
+    rowInfo.value = data
+}, { immediate: true, deep: true })
+
+//监听显示
+watch(isShow, (val) => {
+    if (val) {
+        getDataApi()
+    }
+})
+
+//权限列表
+const tabPaneData = ref([
+    { key: 'menu', name: '后台管理', form: 'menuIds' },
+    { key: 'usermenu', name: '质检试验', form: 'menuClientIds' },
+    { key: 'archivesMenu', name: '档案管理', form: 'menuArchivesIds' },
+    { key: 'larMenu', name: '征拆管理', form: 'menularIds' },
+    { key: 'measureMenu', name: '计量支付', form: 'menuMeasureIds' },
+    { key: 'hacMenu', name: '内控系统', form: 'menuHacIds' },
+    { key: 'tableOwners', name: '表单权限', form: 'tableOwners' },
+    { key: 'dataScope', name: '数据权限', form: 'dataScopeIds' },
+    { key: 'apiScope', name: '接口权限', form: 'apiScopeIds' },
+])
+
+//获取数据
+const getDataApi = () => {
+    const { id } = rowInfo.value
+    if (isNullES(id)) return
+    getGrantTree()
+    getRoleData(id)
+}
+
+//设置权限树的属性
+const treeProps = {
+    label: 'title',
+    children: 'children',
+}
+const treeRefs = ref([])
+const setTreeRefs = (el, { key }) => {
+    if (!el) return
+    let index = arrIndex(treeRefs.value, 'id', key)
+    if (index !== -1) {
+        treeRefs.value[index].ref = el
+    } else {
+        treeRefs.value.push({ id: key, ref: el })
+    }
+}
+const getTreeRef = async (key) => {
+    const itemRef = getArrValue(treeRefs.value)
+    if (itemRef.length <= 0) return ''
+    const index = arrIndex(itemRef, 'id', key)
+    if (index === -1) return ''
+    const obj = getObjValue(itemRef[index])
+    return obj?.ref
+}
+
+
+//获取权限树的数据
+const authTree = ref({})
+const getGrantTree = async () => {
+    const { data } = await mainApi.grantTree()
+    authTree.value = getObjValue(data)
+}
+
+//获取选中的数据
+const roleData = ref({})
+const getRoleData = async (id) => {
+    const { data } = await mainApi.getRole(id)
+    const res = getObjValue(data)
+    let newObjData = {}
+    Object.keys(res).forEach((key) => {
+        newObjData[key] = []
+        const arr = getArrValue(res[key])
+        for (let i = 0; i < arr.length; i++) {
+            let strs = arr[i].split('---')
+            if (strs[1] === 'all') {
+                newObjData[key].push(strs[0])
+            }
+        }
+    })
+    roleData.value = newObjData
+}
+
+//提交表单
+const submitLoading = ref(false)
+const dialogSubmit = async () => {
+    const paneData = tabPaneData.value
+    let newObjData = { roleIds: [rowInfo.value.id] }
+    for (let i = 0; i < paneData.length; i++) {
+        const item = paneData[i]
+        newObjData[item.form] = []
+        const refs = await getTreeRef(item.key)
+        const all = refs?.getRef()?.getCheckedKeys()
+        const half = refs?.getRef()?.getHalfCheckedKeys()
+        for (let j = 0; j < all.length; j++) {
+            newObjData[item.form].push(all[j] + '---all')
+        }
+        for (let j = 0; j < half.length; j++) {
+            newObjData[item.form].push(half[j] + '---all')
+        }
+    }
+    console.log(newObjData)
+}
+
+//关闭弹窗
+const dialogClose = () => {
+    isShow.value = false
+    submitLoading.value = false
+    emit('close')
+}
+</script>
+
+<style lang="scss">
+.el-tabs.el-tabs--left.hc-role-auth-dialog {
+    position: relative;
+    .el-tabs__header.is-left {
+        margin-right: 0;
+    }
+    .el-tabs__content {
+        position: relative;
+        height: 100%;
+        .el-tab-pane {
+            position: relative;
+            height: 100%;
+            width: 100%;
+            overflow: auto;
+            .el-tree.hc-tree-node-v2 .data-custom-tree-node .label.level-name,
+            .hc-tree-node .data-custom-tree-node .label.level-name {
+                font-size: 16px;
+                font-weight: initial;
+            }
+        }
+    }
+}
+</style>

+ 23 - 5
src/views/authority/role.vue

@@ -21,7 +21,7 @@
         >
             <template #tenantId="{ row }">{{ getTenantName(row) }}</template>
             <template #action="{ row }">
-                <el-link type="primary">权限设置</el-link>
+                <el-link type="primary" @click="authRowClick(row)">权限设置</el-link>
                 <el-link type="warning" @click="editRowClick(row)">修改</el-link>
                 <el-link type="success" @click="addRowClick(row)">添加</el-link>
                 <el-link type="danger" @click="delRowClick(row)">删除</el-link>
@@ -52,6 +52,8 @@
                 <el-button hc-btn type="primary" :loading="submitLoading" @click="dialogSubmit">提交</el-button>
             </template>
         </hc-new-dialog>
+        <!-- 权限配置 -->
+        <HcRoleAuth v-model="isRoleAuthShow" :info="roleAuthRow" @close="roleAuthClose" />
     </hc-new-card>
 </template>
 
@@ -59,9 +61,9 @@
 import { nextTick, onActivated, ref } from 'vue'
 import { delMessage } from 'hc-vue3-ui'
 import { arrToId, formValidate, getArrValue, isNullES } from 'js-fast-way'
+import HcRoleAuth from './modules/role/auth.vue'
 import tenantApi from '~api/system/tenant'
 import mainApi from '~api/authority/role'
-import { reloadPage } from '~uti/tools.js'
 
 //激活
 onActivated(() => {
@@ -216,7 +218,7 @@ const dialogSubmit = async () => {
     if (!error && code === 200) {
         dialogClose()
         window?.$message?.success('操作成功')
-        reloadPage()
+        getTableData()
     } else {
         window?.$message?.error(msg ?? '操作失败')
     }
@@ -236,7 +238,7 @@ const delRowClick = (row) => {
         const { code, msg } = await mainApi.del(row.id)
         if (code === 200) {
             window.$message.success('删除成功')
-            reloadPage()
+            getTableData()
         } else {
             window.$message.error(msg ?? '删除失败')
         }
@@ -256,10 +258,26 @@ const delClick = () => {
         const { code, msg } = await mainApi.del(ids)
         if (code === 200) {
             window.$message.success('删除成功')
-            reloadPage()
+            getTableData()
         } else {
             window.$message.error(msg ?? '删除失败')
         }
     })
 }
+
+//权限配置弹窗
+const isRoleAuthShow = ref(false)
+const roleAuthRow = ref({})
+const authRowClick = (row) => {
+    roleAuthRow.value = row
+    nextTick(() => {
+        isRoleAuthShow.value = true
+    })
+}
+
+//关闭权限配置弹窗
+const roleAuthClose = () => {
+    isRoleAuthShow.value = false
+    roleAuthRow.value = {}
+}
 </script>