2024-10-17 15:09:27 +08:00
|
|
|
|
<template>
|
2025-07-09 20:12:40 +08:00
|
|
|
|
<el-dialog :title="dialogTitle" :model-value="dialogVisible" @close="close" v-bind="dialogMiddle" align-center>
|
2025-10-10 13:23:40 +08:00
|
|
|
|
<el-form :model="formContent" ref="dialogFormRef" :rules="rules" class="form-two">
|
|
|
|
|
|
<el-form-item label="上级菜单" prop="pid" :label-width="100">
|
|
|
|
|
|
<el-tree-select
|
|
|
|
|
|
v-model="displayPid"
|
|
|
|
|
|
:data="functionList"
|
|
|
|
|
|
check-strictly
|
|
|
|
|
|
:render-after-expand="false"
|
|
|
|
|
|
show-checkbox
|
|
|
|
|
|
check-on-click-node
|
|
|
|
|
|
node-key="id"
|
|
|
|
|
|
:props="defaultProps"
|
|
|
|
|
|
/>
|
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
<el-form-item label="名称" prop="name" :label-width="100">
|
|
|
|
|
|
<el-input v-model="formContent.name" maxlength="32" show-word-limit />
|
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
<el-form-item label="编码" prop="code" :label-width="100">
|
|
|
|
|
|
<el-input v-model="formContent.code" maxlength="32" show-word-limit />
|
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
<el-form-item v-if="!formContent.type" label="图标" prop="icon" :label-width="100">
|
|
|
|
|
|
<IconSelect
|
|
|
|
|
|
v-model="formContent.icon"
|
|
|
|
|
|
:iconValue="formContent.icon"
|
|
|
|
|
|
@update:icon-value="iconValue => (formContent.icon = iconValue)"
|
|
|
|
|
|
placeholder="选择一个图标"
|
|
|
|
|
|
/>
|
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
<el-form-item v-if="!formContent.type" label="路由地址" prop="path" :label-width="100">
|
|
|
|
|
|
<el-input v-model="formContent.path" maxlength="32" show-word-limit />
|
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
<el-form-item v-if="!formContent.type" label="组件地址" prop="component" :label-width="100">
|
|
|
|
|
|
<el-input v-model="formContent.component" maxlength="32" show-word-limit />
|
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
<el-form-item label="排序" prop="sort" :label-width="100">
|
|
|
|
|
|
<el-input-number v-model="formContent.sort" :min="1" :max="999" />
|
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
<el-form-item label="类型" prop="type" :label-width="100">
|
|
|
|
|
|
<el-select v-model="formContent.type" clearable placeholder="请选择资源类型">
|
|
|
|
|
|
<el-option label="菜单" :value="0"></el-option>
|
|
|
|
|
|
<el-option label="按钮" :value="1"></el-option>
|
|
|
|
|
|
<!-- <el-option label="公共资源" :value="2"></el-option>
|
2024-11-18 16:02:19 +08:00
|
|
|
|
<el-option label="服务间调用资源" :value="3"></el-option> -->
|
2025-10-10 13:23:40 +08:00
|
|
|
|
</el-select>
|
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
<el-form-item label="描述" prop="remark" :label-width="100">
|
|
|
|
|
|
<el-input v-model="formContent.remark" :rows="2" type="textarea" />
|
|
|
|
|
|
</el-form-item>
|
|
|
|
|
|
</el-form>
|
|
|
|
|
|
<template #footer>
|
|
|
|
|
|
<div class="dialog-footer">
|
|
|
|
|
|
<el-button @click="close()">取 消</el-button>
|
|
|
|
|
|
<el-button type="primary" @click="save()">确 定</el-button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
2024-10-17 15:09:27 +08:00
|
|
|
|
</el-dialog>
|
2025-10-10 13:23:40 +08:00
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script lang="ts" setup name="ResourceDialog">
|
|
|
|
|
|
import { computed, type Ref, ref, watch } from 'vue'
|
|
|
|
|
|
import { dialogMiddle } from '@/utils/elementBind'
|
|
|
|
|
|
import { ElMessage, type FormInstance, type FormItemRule } from 'element-plus'
|
|
|
|
|
|
import { useDictStore } from '@/stores/modules/dict'
|
|
|
|
|
|
import type { Function } from '@/api/user/interface/function'
|
|
|
|
|
|
import { addFunction, getFunctionListNoButton, updateFunction } from '@/api/user/function/index'
|
|
|
|
|
|
import IconSelect from '@/components/SelectIcon/index.vue'
|
|
|
|
|
|
|
|
|
|
|
|
const value = ref()
|
|
|
|
|
|
// 树形节点配置
|
|
|
|
|
|
const defaultProps = {
|
2024-11-14 20:36:54 +08:00
|
|
|
|
children: 'children',
|
|
|
|
|
|
label: 'name',
|
2024-11-18 16:02:19 +08:00
|
|
|
|
value: 'id'
|
2025-10-10 13:23:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
const functionList = ref<Function.ResFunction[]>([])
|
|
|
|
|
|
const dictStore = useDictStore()
|
|
|
|
|
|
// 定义弹出组件元信息
|
|
|
|
|
|
const dialogFormRef = ref()
|
|
|
|
|
|
function useMetaInfo() {
|
2024-11-14 11:34:25 +08:00
|
|
|
|
const dialogVisible = ref(false)
|
|
|
|
|
|
const titleType = ref('add')
|
|
|
|
|
|
const formContent = ref<Function.ResFunction>({
|
2025-10-10 13:23:40 +08:00
|
|
|
|
id: '', //资源表Id
|
|
|
|
|
|
pid: '', //节点(0为根节点)
|
|
|
|
|
|
pids: '', //节点上层所有节点
|
|
|
|
|
|
name: '', //名称
|
|
|
|
|
|
code: '', //资源标识
|
|
|
|
|
|
path: '', //路径
|
|
|
|
|
|
component: '',
|
|
|
|
|
|
icon: undefined as string | undefined, // 图标
|
|
|
|
|
|
sort: 100, //排序
|
|
|
|
|
|
type: 0, //资源类型0-菜单、1-按钮、2-公共资源、3-服务间调用资源
|
|
|
|
|
|
remark: '', //权限资源描述
|
|
|
|
|
|
state: 1 //权限资源状态
|
2024-11-14 11:34:25 +08:00
|
|
|
|
})
|
|
|
|
|
|
return { dialogVisible, titleType, formContent }
|
2025-10-10 13:23:40 +08:00
|
|
|
|
}
|
2024-11-18 16:02:19 +08:00
|
|
|
|
|
2025-10-10 13:23:40 +08:00
|
|
|
|
const { dialogVisible, titleType, formContent } = useMetaInfo()
|
2024-11-14 11:34:25 +08:00
|
|
|
|
// 清空formContent
|
|
|
|
|
|
const resetFormContent = () => {
|
|
|
|
|
|
formContent.value = {
|
2025-10-10 13:23:40 +08:00
|
|
|
|
id: '', //资源表Id
|
|
|
|
|
|
pid: '', //节点(0为根节点)
|
|
|
|
|
|
pids: '', //节点上层所有节点
|
|
|
|
|
|
name: '', //名称
|
|
|
|
|
|
code: '', //资源标识
|
|
|
|
|
|
path: '', //路径
|
|
|
|
|
|
component: '',
|
|
|
|
|
|
icon: undefined, //图标
|
|
|
|
|
|
sort: 100, //排序
|
|
|
|
|
|
type: 0, //资源类型0-菜单、1-按钮、2-公共资源、3-服务间调用资源
|
|
|
|
|
|
remark: '', //权限资源描述
|
|
|
|
|
|
state: 1 //权限资源状态
|
2024-11-14 11:34:25 +08:00
|
|
|
|
}
|
2025-10-10 13:23:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
let dialogTitle = computed(() => {
|
2024-11-14 11:34:25 +08:00
|
|
|
|
return titleType.value === 'add' ? '新增菜单' : '编辑菜单'
|
2025-10-10 13:23:40 +08:00
|
|
|
|
})
|
2024-11-14 11:34:25 +08:00
|
|
|
|
|
2025-10-10 13:23:40 +08:00
|
|
|
|
// 定义规则
|
|
|
|
|
|
const formRuleRef = ref<FormInstance>()
|
|
|
|
|
|
const rules: Ref<Record<string, Array<FormItemRule>>> = ref({
|
|
|
|
|
|
name: [{ required: true, trigger: 'blur', message: '菜单名称必填!' }],
|
2025-10-15 15:20:07 +08:00
|
|
|
|
code: [{ required: true, trigger: 'blur', message: '编码必填!' }],
|
|
|
|
|
|
path : [{ required: true, trigger: 'blur', message: '路由地址必填!' }],
|
|
|
|
|
|
component :[{ required: true, trigger: 'blur', message: '组件地址必填!' }]
|
2025-10-10 13:23:40 +08:00
|
|
|
|
})
|
2024-10-30 15:19:47 +08:00
|
|
|
|
|
2025-10-15 15:20:07 +08:00
|
|
|
|
// watch(
|
|
|
|
|
|
// () => formContent.value.type,
|
|
|
|
|
|
// newVal => {
|
|
|
|
|
|
// if (newVal === 1) {
|
|
|
|
|
|
// // 选择按钮时,路由地址和组件地址无需校验
|
|
|
|
|
|
// rules.value.path = []
|
|
|
|
|
|
// rules.value.component = []
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
// // 其他情况下,路由地址和组件地址需要校验
|
|
|
|
|
|
// rules.value.path = [{ required: true, trigger: 'blur', message: '路由地址必填!' }]
|
|
|
|
|
|
// rules.value.component = [{ required: true, trigger: 'blur', message: '组件地址必填!' }]
|
|
|
|
|
|
// }
|
|
|
|
|
|
// }
|
|
|
|
|
|
// )
|
2024-11-20 11:02:50 +08:00
|
|
|
|
|
2025-10-10 13:23:40 +08:00
|
|
|
|
// 关闭弹窗
|
2024-11-14 11:34:25 +08:00
|
|
|
|
const close = () => {
|
|
|
|
|
|
dialogVisible.value = false
|
|
|
|
|
|
// 清空dialogForm中的值
|
|
|
|
|
|
resetFormContent()
|
|
|
|
|
|
// 重置表单
|
|
|
|
|
|
dialogFormRef.value?.resetFields()
|
2025-10-10 13:23:40 +08:00
|
|
|
|
}
|
2024-11-18 16:02:19 +08:00
|
|
|
|
|
|
|
|
|
|
// 计算属性,用于控制显示的 pid
|
|
|
|
|
|
const displayPid = computed({
|
2025-10-10 13:23:40 +08:00
|
|
|
|
get: () => {
|
|
|
|
|
|
return formContent.value.pid === '0' ? '' : formContent.value.pid
|
|
|
|
|
|
},
|
|
|
|
|
|
set: value => {
|
|
|
|
|
|
formContent.value.pid = value
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
2024-11-18 16:02:19 +08:00
|
|
|
|
|
2025-10-10 13:23:40 +08:00
|
|
|
|
// 保存数据
|
|
|
|
|
|
const save = () => {
|
2024-11-14 11:34:25 +08:00
|
|
|
|
try {
|
|
|
|
|
|
dialogFormRef.value?.validate(async (valid: boolean) => {
|
2025-10-10 13:23:40 +08:00
|
|
|
|
if (formContent.value.pid === undefined || formContent.value.pid === null || formContent.value.pid === '') {
|
|
|
|
|
|
formContent.value.pid = '0'
|
|
|
|
|
|
}
|
|
|
|
|
|
if (
|
|
|
|
|
|
formContent.value.pids === undefined ||
|
|
|
|
|
|
formContent.value.pids === null ||
|
|
|
|
|
|
formContent.value.pids === ''
|
|
|
|
|
|
) {
|
|
|
|
|
|
formContent.value.pids = '0'
|
|
|
|
|
|
}
|
|
|
|
|
|
if (valid) {
|
|
|
|
|
|
if (formContent.value.id) {
|
|
|
|
|
|
await updateFunction(formContent.value)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
await addFunction(formContent.value)
|
|
|
|
|
|
}
|
|
|
|
|
|
ElMessage.success({ message: `${dialogTitle.value}成功!` })
|
|
|
|
|
|
close()
|
|
|
|
|
|
// 刷新表格
|
|
|
|
|
|
await props.refreshTable!()
|
|
|
|
|
|
}
|
|
|
|
|
|
})
|
2024-11-14 11:34:25 +08:00
|
|
|
|
} catch (err) {
|
2025-10-10 13:23:40 +08:00
|
|
|
|
console.error('验证过程中出现错误', err)
|
2024-10-30 15:19:47 +08:00
|
|
|
|
}
|
2025-10-10 13:23:40 +08:00
|
|
|
|
}
|
2024-11-18 16:02:19 +08:00
|
|
|
|
|
2025-10-10 13:23:40 +08:00
|
|
|
|
// 打开弹窗,可能是新增,也可能是编辑
|
|
|
|
|
|
const open = async (sign: string, data: Function.ResFunction) => {
|
2024-11-20 15:13:50 +08:00
|
|
|
|
// 重置表单
|
|
|
|
|
|
dialogFormRef.value?.resetFields()
|
2025-10-15 15:20:07 +08:00
|
|
|
|
// 清空表单校验
|
|
|
|
|
|
dialogFormRef.value?.clearValidate()
|
2024-11-14 20:36:54 +08:00
|
|
|
|
const response = await getFunctionListNoButton()
|
|
|
|
|
|
functionList.value = response.data as unknown as Function.ResFunction[]
|
2024-11-14 11:34:25 +08:00
|
|
|
|
titleType.value = sign
|
|
|
|
|
|
dialogVisible.value = true
|
2025-10-15 15:20:07 +08:00
|
|
|
|
|
2025-10-10 13:23:40 +08:00
|
|
|
|
if (formContent.value.pid === '0') {
|
|
|
|
|
|
formContent.value.pid = ''
|
2024-11-18 16:02:19 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-14 11:34:25 +08:00
|
|
|
|
if (data.id) {
|
2025-10-10 13:23:40 +08:00
|
|
|
|
formContent.value = { ...data }
|
2024-11-14 11:34:25 +08:00
|
|
|
|
} else {
|
2025-10-10 13:23:40 +08:00
|
|
|
|
resetFormContent()
|
2024-10-17 15:09:27 +08:00
|
|
|
|
}
|
2025-10-10 13:23:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 对外映射
|
|
|
|
|
|
defineExpose({ open })
|
|
|
|
|
|
const props = defineProps<{
|
|
|
|
|
|
refreshTable: (() => Promise<void>) | undefined
|
|
|
|
|
|
}>()
|
|
|
|
|
|
</script>
|