wms-antdvue/.svn/pristine/77/778e1548554de00aa13fc72b7bfc72a9e8b677be.svn-base
2024-11-07 16:33:03 +08:00

147 lines
3.9 KiB
Plaintext

<template>
<a-modal
v-model:visible="props.visible"
:title="props.categoryId ? '编辑' : '新增'"
width="600px"
@cancel="dialogClose"
>
<a-form ref="formRef" :model="formData" :label-col="{ style: { width: '80px' } }">
<a-form-item
label="上级分类"
name="parentId"
:rules="{ required: true, message: '请选择上级分类', trigger: 'change' }"
>
<a-tree-select
class="flex-1"
v-model:value="formData.parentId"
:tree-data="categoryOptions"
allow-clear
treeDefaultExpandAll
:fieldNames="{ children: 'children', label: 'name', value: 'id' }"
placeholder="请选择上级分类"
/>
</a-form-item>
<a-form-item
label="分类名称"
name="name"
:rules="{ required: true, message: '请输入分类名称', trigger: 'blur' }"
>
<a-input v-model:value="formData.name" placeholder="请输入分类名称" allow-clear />
</a-form-item>
<a-form-item label="分类排序" name="sort" class="flex-1">
<div>
<a-input-number v-model:value="formData.sort" :max="9999" />
<div class="form-tips">数值越小越排前</div>
</div>
</a-form-item>
<a-form-item label="分类备注" name="note">
<a-textarea
class="flex-1"
v-model:value="formData.note"
placeholder="请输入分类备注"
allow-clear
/>
</a-form-item>
</a-form>
<template #footer>
<span class="dialog-footer">
<a-button @click="dialogClose">取消</a-button>
<a-button :loading="subLoading" type="primary" @click="submit"> 确定 </a-button>
</span>
</template>
</a-modal>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'ant-design-vue';
import {
categoryAdd,
categoryUpdate,
getCategoryList,
getCategoryDetail,
} from '@/api/content/category';
import { onMounted, reactive, ref, shallowRef } from 'vue';
import { buildTree } from '@/utils/auth';
import { message } from 'ant-design-vue';
import { useLockFn } from '@/utils/useLockFn';
const props = defineProps({
visible: {
type: Boolean,
required: true,
default: false,
},
categoryId: {
type: Number,
required: true,
default: 0,
},
pid: {
type: Number,
default: 0,
},
});
const expandedKeys = ref([]);
const emit = defineEmits(['success', 'update:visible']);
const formRef = shallowRef<FormInstance>();
const formData = reactive({
id: '',
//父级id
parentId: 0,
//类型
type: 1,
//名称
name: '',
//排序
sort: 0,
note: '',
});
const dialogClose = () => {
emit('update:visible', false);
};
const categoryOptions = ref<any[]>([]);
const getMenu = async () => {
const data: any = await getCategoryList();
data.map((item) => {
expandedKeys.value.push(item.id);
});
const menu: any = [{ id: 0, name: '顶级', children: [] }];
const lists = buildTree(data);
menu[0].children.push(...lists);
categoryOptions.value = menu;
};
const handleSubmit = async () => {
await formRef.value?.validate();
props.categoryId ? await categoryUpdate(formData) : await categoryAdd(formData);
message.success('操作成功');
emit('update:visible', false);
emit('success');
};
const { isLock: subLoading, lockFn: submit } = useLockFn(handleSubmit);
const setFormData = (data: Record<any, any>) => {
for (const key in formData) {
if (data[key] != null && data[key] != undefined) {
formData[key] = data[key];
}
}
};
const getDetail = async () => {
const data = await getCategoryDetail(props.categoryId);
setFormData(data);
};
onMounted(() => {
getMenu();
if (props.categoryId) {
getDetail();
} else {
formData.parentId = props.pid;
}
});
</script>