167 lines
4.4 KiB
Vue
167 lines
4.4 KiB
Vue
<template>
|
|
<t-dialog
|
|
width="40%"
|
|
:header="header"
|
|
:confirmBtn="confirmBtn"
|
|
:closeOnOverlayClick="false"
|
|
@close="handleCloseDialog"
|
|
@confirm="SaveCategoryData"
|
|
>
|
|
<t-space direction="vertical" style="width: 100%">
|
|
<TForm ref="form" :data="categoryData" :rules="rules" resetType="initial">
|
|
<TFormItem label="分类名称:" name="name" help="名称长度小于5个汉字,大于2个汉字">
|
|
<t-input v-model.trim="categoryData.name" clearable placeholder="请输入分类名称" />
|
|
</TFormItem>
|
|
<TFormItem label="分类编号:" name="code" help="编号由大于4个字符的英文字母组成">
|
|
<t-input v-model.trim="categoryData.code" clearable placeholder="请输入分类编号" />
|
|
</TFormItem>
|
|
<TFormItem label="排序级别:" name="order" help="排序级别由正整数组成">
|
|
<t-input-number
|
|
:min="0"
|
|
name="order"
|
|
theme="normal"
|
|
class="!w-full"
|
|
placeholder="请输入排序级别"
|
|
v-model.trim="categoryData.order"
|
|
></t-input-number>
|
|
</TFormItem>
|
|
</TForm>
|
|
</t-space>
|
|
</t-dialog>
|
|
</template>
|
|
<script lang="ts" setup>
|
|
import type { Category } from '@/model/category'
|
|
import { computed, ref, reactive, watch } from 'vue'
|
|
import { createCategoryRequest, updateCategoryByIdRequest } from '@/api/category'
|
|
import type { FormInstanceFunctions, FormProps } from 'tdesign-vue-next'
|
|
import { MessagePlugin } from 'tdesign-vue-next'
|
|
const props = defineProps<{
|
|
isEdit?: Boolean | null
|
|
headerTitle?: String | null
|
|
category: Category | {}
|
|
}>()
|
|
const form = ref<FormInstanceFunctions | null>(null)
|
|
const header = computed(() => `${props.isEdit ? '编辑' : '创建'}${props.headerTitle || ''}`)
|
|
const confirmBtn = computed(() => (props.isEdit ? '保存' : '确定'))
|
|
const categoryData: FormProps['data'] = reactive({ name: '', order: 1 })
|
|
const rules: FormProps['rules'] = {
|
|
name: [
|
|
{
|
|
required: true,
|
|
message: '分类名称不能为空',
|
|
type: 'error',
|
|
trigger: 'blur'
|
|
},
|
|
{
|
|
required: true,
|
|
message: '分类名称不能为空',
|
|
type: 'error',
|
|
trigger: 'change'
|
|
},
|
|
{
|
|
whitespace: true,
|
|
message: '分类名称不能为空'
|
|
},
|
|
{
|
|
min: 4,
|
|
message: '输入汉字长度应在2到5之间',
|
|
type: 'error',
|
|
trigger: 'blur'
|
|
},
|
|
{
|
|
max: 10,
|
|
message: '输入汉字长度应在2到5之间',
|
|
type: 'error',
|
|
trigger: 'blur'
|
|
}
|
|
],
|
|
code: [
|
|
{
|
|
required: true,
|
|
message: '分类编号不能为空',
|
|
type: 'error',
|
|
trigger: 'blur'
|
|
},
|
|
{
|
|
required: true,
|
|
message: '分类编号不能为空',
|
|
type: 'error',
|
|
trigger: 'change'
|
|
},
|
|
{
|
|
whitespace: true,
|
|
message: '分类编号不能为空'
|
|
},
|
|
{
|
|
min: 4,
|
|
message: '输入字符应大于4个字符长度',
|
|
type: 'error',
|
|
trigger: 'blur'
|
|
},
|
|
{
|
|
pattern: /^[A-Za-z]+$/,
|
|
message: '输入字符须为英文字母',
|
|
type: 'error',
|
|
trigger: 'blur'
|
|
}
|
|
],
|
|
order: [
|
|
{
|
|
required: true,
|
|
message: '排序级别不能为空',
|
|
type: 'error',
|
|
trigger: 'blur'
|
|
},
|
|
{
|
|
required: true,
|
|
message: '排序级别不能为空',
|
|
type: 'error',
|
|
trigger: 'change'
|
|
},
|
|
{
|
|
whitespace: true,
|
|
message: '排序级别不能为空'
|
|
},
|
|
{
|
|
pattern: /^[1-9]\d*$/,
|
|
message: '排序级别必须为正整数',
|
|
type: 'error',
|
|
trigger: 'blur'
|
|
}
|
|
]
|
|
}
|
|
watch(
|
|
() => props.category,
|
|
(newCategory) => {
|
|
Object.assign(categoryData, newCategory)
|
|
},
|
|
{ deep: true }
|
|
)
|
|
const emit = defineEmits<{
|
|
success: [void]
|
|
}>()
|
|
const SaveCategoryData = async () => {
|
|
if (form.value) {
|
|
const valid = await form.value.validate()
|
|
if (valid && typeof valid === 'boolean') {
|
|
const { code, name, order, id } = categoryData
|
|
if (!props.isEdit) {
|
|
await createCategoryRequest({ code, name, order })
|
|
} else {
|
|
await updateCategoryByIdRequest(id, { code, name, order })
|
|
}
|
|
MessagePlugin.success(`${props.isEdit ? '编辑' : '创建'}分类成功`)
|
|
emit('success')
|
|
return
|
|
}
|
|
}
|
|
}
|
|
const handleCloseDialog = () => {
|
|
// 数据&&规则校验结果重置
|
|
if (form.value) {
|
|
form.value.reset()
|
|
form.value.clearValidate()
|
|
}
|
|
}
|
|
</script>
|