Files
admin-ui/src/views/customerService/product/component/editRole.vue

249 lines
5.5 KiB
Vue
Raw Normal View History

2025-11-22 16:39:01 +08:00
<template>
2025-11-25 17:02:31 +08:00
<div class="system-edit-role-container">
<el-dialog :title="(formData.id === 0 ? '添加' : '修改') + '产品'" v-model="isShowDialog" width="1000px">
<el-form ref="formRef" :model="formData" :rules="rules" size="default" label-width="90px">
<el-row :gutter="35">
<!-- 产品名称输入框 -->
<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
<el-form-item label="产品名称" prop="name">
<el-input v-model="formData.name" placeholder="请输入产品名称" clearable />
</el-form-item>
</el-col>
<!-- 富文本编辑器 -->
<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
<el-form-item label="产品详情" prop="content">
<Editor v-model="formData.content" height="400px" placeholder="请输入产品详细描述..." />
</el-form-item>
</el-col>
</el-row>
</el-form>
<!-- 对话框底部按钮 -->
<template #footer>
<span class="dialog-footer">
<el-button @click="onCancel" size="default"> </el-button>
<el-button type="primary" @click="onSubmit" size="default" :loading="loading">
{{ formData.id === 0 ? '新 增' : '修 改' }}
</el-button>
</span>
</template>
</el-dialog>
</div>
2025-11-22 16:39:01 +08:00
</template>
2025-11-25 17:02:31 +08:00
<script lang="ts" setup>
import { ref, reactive, toRefs, nextTick } from 'vue';
import { ElMessage, type FormInstance, type FormRules } from 'element-plus';
2025-11-28 17:17:07 +08:00
import { getproductAdd, updateProduct, getproductOne } from '/@/api/customerService/product';
2025-11-22 16:39:01 +08:00
import Editor from '/@/components/editor/index.vue';
2025-11-28 17:17:07 +08:00
import { Session } from '/@/utils/storage';
import { log } from 'node:console';
2025-11-22 16:39:01 +08:00
2025-11-25 17:02:31 +08:00
/**
* 产品表单数据接口
*/
interface ProductFormData {
id: number; // 产品ID
name: string; // 产品名称
content?: string; // 产品详情(富文本内容)
2025-11-28 17:17:07 +08:00
creator: '';
modifier: '';
2025-11-22 16:39:01 +08:00
}
2025-11-25 17:02:31 +08:00
/**
* 组件状态接口
*/
interface ComponentState {
loading: boolean; // 提交加载状态
isShowDialog: boolean; // 对话框显示状态
formData: ProductFormData; // 表单数据
2025-11-22 16:39:01 +08:00
}
2025-11-25 17:02:31 +08:00
// 定义组件事件
const emit = defineEmits<{
/**
* 获取产品列表事件 - 用于刷新父组件列表
*/
(e: 'getRoleList'): void;
}>();
// 响应式状态
const state = reactive<ComponentState>({
loading: false,
isShowDialog: false,
formData: {
id: 0,
name: '',
content: '',
2025-11-28 17:17:07 +08:00
creator: '',
modifier: '',
2025-11-25 17:02:31 +08:00
},
});
// 表单验证规则
const rules: FormRules = {
name: [
{ required: true, message: '产品名称不能为空', trigger: 'blur' },
{ min: 2, max: 50, message: '产品名称长度在 2 到 50 个字符', trigger: 'blur' },
],
content: [{ required: true, message: '产品详情不能为空', trigger: 'blur' }],
};
// 模板引用
const formRef = ref<FormInstance>();
// 解构状态数据,便于模板使用
const { loading, isShowDialog, formData } = toRefs(state);
/**
* 打开对话框
* @param row - 可选的编辑数据如果传入则为编辑模式否则为新增模式
*/
const openDialog = async (row?: ProductFormData) => {
// 重置表单
resetForm();
try {
if (row && row.id) {
// 编辑模式:获取产品详情
await handleEditMode(row.id);
}
// 显示对话框
state.isShowDialog = true;
} catch (error) {
console.error('打开对话框失败:', error);
ElMessage.error('初始化数据失败');
}
};
/**
* 处理编辑模式的数据获取
* @param id - 产品ID
*/
const handleEditMode = async (id: number) => {
try {
2025-11-28 17:17:07 +08:00
const res = await getproductOne({ id: id });
if (res.data) {
2025-11-25 17:02:31 +08:00
// 填充表单数据
2025-11-28 18:03:52 +08:00
// state.formData.content=
2025-11-28 17:17:07 +08:00
console.log(res.data, 'for');
2025-11-25 17:02:31 +08:00
}
} catch (error) {
console.error('获取产品详情失败:', error);
throw new Error('获取产品详情失败');
}
};
/**
* 关闭对话框
*/
const closeDialog = () => {
state.isShowDialog = false;
};
/**
* 取消操作
*/
const onCancel = () => {
closeDialog();
};
/**
* 提交表单
*/
const onSubmit = async () => {
if (!formRef.value) {
ElMessage.error('表单引用不存在');
return;
}
try {
// 表单验证
const valid = await formRef.value.validate();
if (!valid) {
ElMessage.warning('请完善表单信息');
return;
}
// 设置加载状态
state.loading = true;
// 根据ID判断是新增还是修改
if (state.formData.id === 0) {
// 新增产品
2025-11-28 17:17:07 +08:00
await getproductAdd(state.formData);
2025-11-25 17:02:31 +08:00
ElMessage.success('产品添加成功');
} else {
// 修改产品
2025-11-28 17:17:07 +08:00
await updateProduct(state.formData);
2025-11-25 17:02:31 +08:00
ElMessage.success('产品修改成功');
}
// 关闭对话框并刷新列表
closeDialog();
emit('getRoleList');
} catch (error) {
console.error('提交失败:', error);
ElMessage.error(state.formData.id === 0 ? '添加失败' : '修改失败');
} finally {
state.loading = false;
}
};
/**
* 重置表单
*/
const resetForm = () => {
// 重置表单数据
state.formData = {
id: 0,
name: '',
content: '',
2025-11-28 18:03:52 +08:00
creator: '',
modifier: '',
2025-11-25 17:02:31 +08:00
};
// 在下一个DOM更新周期后清除表单验证
nextTick(() => {
formRef.value?.clearValidate();
});
};
// 暴露方法给父组件使用
defineExpose({
/**
* 打开对话框方法
*/
openDialog,
/**
* 关闭对话框方法
*/
closeDialog,
2025-11-22 16:39:01 +08:00
});
</script>
<style scoped lang="scss">
2025-11-25 17:02:31 +08:00
.system-edit-role-container {
// 对话框内容区域样式
:deep(.el-dialog__body) {
padding: 20px;
}
// 底部按钮区域样式
.dialog-footer {
display: flex;
justify-content: flex-end;
align-items: center;
gap: 10px;
}
2025-11-22 16:39:01 +08:00
}
2025-11-25 17:02:31 +08:00
// 间距工具类
.mb20 {
margin-bottom: 20px;
2025-11-22 16:39:01 +08:00
}
</style>