完成调查问卷

This commit is contained in:
Zy
2026-04-15 17:32:03 +08:00
parent 40f13be704
commit cf9a1f1585
25 changed files with 2325 additions and 254 deletions

View File

@@ -0,0 +1,233 @@
<template>
<div class="app-container">
<!-- 搜索栏 -->
<el-form :model="queryParams" ref="queryFormRef" :inline="true" v-show="showSearch" label-width="68px">
<el-form-item label="问卷标题" prop="title">
<el-input v-model="queryParams.title" placeholder="请输入问卷标题" clearable @keyup.enter="handleQuery" />
</el-form-item>
<el-form-item>
<el-button type="primary" :icon="Search" @click="handleQuery">搜索</el-button>
<el-button :icon="Refresh" @click="resetQuery">重置</el-button>
</el-form-item>
</el-form>
<!-- 操作栏 -->
<el-row :gutter="10" class="mb8">
<el-col :span="1.5">
<el-button type="primary" plain :icon="Plus" @click="handleAdd">新增问卷</el-button>
</el-col>
<right-toolbar v-model:showSearch="showSearch" @queryTable="getList" />
</el-row>
<!-- 数据表格 -->
<el-table v-loading="loading" :data="questionnaireList">
<el-table-column label="问卷ID" align="center" prop="id" />
<el-table-column label="问卷标题" align="center" prop="title" />
<el-table-column label="状态" align="center" prop="status">
<template #default="scope">
<el-tag :type="scope.row.status === 1 ? 'success' : 'info'">
{{ scope.row.status === 0 ? '草稿' : scope.row.status === 1 ? '已发布' : '已停用' }}
</el-tag>
</template>
</el-table-column>
<el-table-column label="创建时间" align="center" prop="createTime" width="180">
<template #default="scope">
<span>{{ parseTime(scope.row.createTime) }}</span>
</template>
</el-table-column>
<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
<template #default="scope">
<el-button link type="primary" @click="handleView(scope.row)">查看</el-button>
<el-button link type="primary" :icon="Edit" @click="handleUpdate(scope.row)">设计</el-button>
<el-button link type="primary" :icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
<!-- 新增/修改对话框 -->
<el-dialog :title="title" v-model="open" width="80%" top="5vh" append-to-body destroy-on-close>
<questionnaire-edit ref="editRef" @success="getList" />
</el-dialog>
<!-- 详情对话框 -->
<el-dialog title="问卷详情" v-model="viewOpen" width="60%" top="5vh" append-to-body>
<div v-loading="viewLoading">
<div class="detail-header">
<h2>{{ viewData.title }}</h2>
<p style="color: #666">{{ viewData.description }}</p>
</div>
<el-divider />
<div class="detail-content">
<div v-for="(q, index) in viewContent" :key="q.cid" class="question-item">
<div class="title">
<span style="font-weight: bold">{{ index + 1 }}. {{ q.title }}</span>
<span style="color: #f56c6c; margin-left: 5px" v-if="q.required">*</span>
</div>
<div class="type-tag">
<el-tag size="small">{{ getComponentLabel(q.type) }}</el-tag>
</div>
<!-- 单选题/多选题选项展示 -->
<div v-if="q.type === 'radio' || q.type === 'checkbox'" class="options">
<div v-for="(opt, idx) in q.options" :key="idx" class="option-item">
<span>{{ opt.label }}</span>
</div>
</div>
<!-- 填空题/多行文本展示 -->
<div v-else class="input-placeholder">
<el-input type="textarea" v-model="q.placeholder" placeholder="用户输入区域" />
</div>
</div>
</div>
</div>
</el-dialog>
</div>
</template>
<script setup name="Questionnaire">
import { listQuestionnaire, delQuestionnaire, getQuestionnaire } from './api';
import QuestionnaireEdit from './QuestionnaireEdit.vue';
const { proxy } = getCurrentInstance();
const questionnaireList = ref([]);
const open = ref(false);
const viewOpen = ref(false);
const loading = ref(true);
const viewLoading = ref(false);
const showSearch = ref(true);
const title = ref('');
const queryParams = ref({
pageNum: 1,
pageSize: 10,
title: null
});
const viewData = ref({ title: '', description: '', content: [] });
const viewContent = ref([]);
// 组件库定义(用于获取类型名称)
const componentList = [
{ type: 'radio', label: '单选题' },
{ type: 'checkbox', label: '多选题' },
{ type: 'input', label: '填空题' },
{ type: 'textarea', label: '多行文本' }
];
// 辅助方法:获取组件名称
function getComponentLabel(type) {
const item = componentList.find((c) => c.type === type);
return item ? item.label : '';
}
/** 查询问卷列表 */
async function getList() {
loading.value = true;
const res = await listQuestionnaire(queryParams.value);
questionnaireList.value = res.rows;
loading.value = false;
}
/** 搜索按钮操作 */
function handleQuery() {
queryParams.value.pageNum = 1;
getList();
}
/** 重置按钮操作 */
function resetQuery() {
proxy.resetForm('queryFormRef');
handleQuery();
}
/** 新增按钮操作 */
function handleAdd() {
open.value = true;
title.value = '设计问卷';
nextTick(() => {
proxy.$refs.editRef.reset();
});
}
/** 修改按钮操作 */
function handleUpdate(row) {
open.value = true;
title.value = '修改问卷';
nextTick(() => {
proxy.$refs.editRef.init(row.id);
});
}
/** 查看按钮操作 */
async function handleView(row) {
viewOpen.value = true;
viewLoading.value = true;
try {
const res = await getQuestionnaire(row.id);
viewData.value = res.data;
// 解析JSON内容
if (typeof viewData.value.content === 'string') {
viewContent.value = JSON.parse(viewData.value.content);
} else {
viewContent.value = viewData.value.content;
}
} finally {
viewLoading.value = false;
}
}
/** 删除按钮操作 */
async function handleDelete(row) {
proxy.$modal
.confirm('是否确认删除问卷编号为"' + row.id + '"的项目?')
.then(async function () {
await delQuestionnaire(row.id);
proxy.$modal.msgSuccess('删除成功');
getList();
})
.catch(() => {});
}
/** 初始化 */
onMounted(() => {
getList();
});
</script>
<style scoped lang="scss">
.detail-header {
text-align: center;
margin-bottom: 20px;
}
.question-item {
margin-bottom: 25px;
padding: 15px;
border-radius: 8px;
background: #fafafa;
.title {
margin-bottom: 8px;
}
.type-tag {
margin-bottom: 12px;
}
.options {
.option-item {
display: flex;
align-items: center;
margin-bottom: 8px;
color: #666;
.el-icon {
margin-right: 8px;
color: #909399;
}
}
}
.input-placeholder {
margin-top: 10px;
}
}
</style>

View File

@@ -0,0 +1,311 @@
<template>
<div class="edit-container">
<el-row :gutter="20">
<!-- 左侧:组件库 -->
<el-col :span="4">
<el-card shadow="hover" class="component-panel">
<template #header>
<div class="card-header">
<span>组件库</span>
</div>
</template>
<draggable
:list="componentList"
:group="{ name: 'components', pull: 'clone', put: false }"
:clone="cloneComponent"
item-key="type"
class="component-list"
>
<template #item="{ element }">
<div class="component-item">
<el-icon><component :is="element.icon" /></el-icon>
<span>{{ element.label }}</span>
</div>
</template>
</draggable>
</el-card>
</el-col>
<!-- 中间:设计画布 -->
<el-col :span="12">
<el-card shadow="hover" class="canvas-panel">
<template #header>
<div class="card-header">
<span>设计画布</span>
</div>
</template>
<div class="question-title-area">
<el-input v-model="form.title" placeholder="请输入问卷标题" class="title-input" />
<el-input v-model="form.description" type="textarea" placeholder="请输入问卷描述(选填)" />
</div>
<draggable v-model="form.content" group="components" item-key="cid" class="question-list" @add="handleAddQuestion">
<template #item="{ element, index }">
<div class="question-item" :class="{ 'is-active': activeIndex === index }" @click="selectQuestion(index)">
<div class="question-handle">
<el-icon><Rank /></el-icon>
</div>
<div class="question-content">
<span class="question-type-tag">{{ getComponentLabel(element.type) }}</span>
<span>Q{{ index + 1 }}. {{ element.title || '未命名题目' }}</span>
</div>
<div class="question-actions">
<el-button link type="danger" :icon="Delete" @click.stop="deleteQuestion(index)">删除</el-button>
</div>
</div>
</template>
</draggable>
<el-empty v-if="form.content.length === 0" description="请从左侧拖拽组件到此处" />
</el-card>
</el-col>
<!-- 右侧:属性配置 -->
<el-col :span="8">
<el-card shadow="hover" class="property-panel" v-if="activeQuestion">
<template #header>
<div class="card-header">
<span>属性配置</span>
</div>
</template>
<el-form label-width="80px">
<el-form-item label="题目标题">
<el-input v-model="activeQuestion.title" placeholder="请输入题目标题" />
</el-form-item>
<el-form-item label="是否必填">
<el-switch v-model="activeQuestion.required" />
</el-form-item>
<!-- 单选/多选特有属性 -->
<template v-if="activeQuestion.type === 'radio' || activeQuestion.type === 'checkbox'">
<el-divider content-position="left">选项设置</el-divider>
<div v-for="(opt, idx) in activeQuestion.options" :key="idx" class="option-item">
<el-input v-model="opt.label" placeholder="选项内容" style="width: 200px; margin-right: 10px" />
<el-button link type="danger" :icon="Delete" @click="activeQuestion.options.splice(idx, 1)">删除</el-button>
</div>
<el-button type="primary" plain :icon="Plus" @click="activeQuestion.options.push({ label: '新选项' })">添加选项</el-button>
</template>
</el-form>
</el-card>
<el-empty v-else description="请点击选中一道题目" />
</el-col>
</el-row>
<!-- 底部操作栏 -->
<div class="footer-actions">
<el-button @click="cancel">取消</el-button>
<el-button type="primary" @click="submitForm">保存草稿</el-button>
<el-button type="success" @click="submitForm(1)">保存并发布</el-button>
</div>
</div>
</template>
<script setup name="QuestionnaireEdit">
import draggable from 'vuedraggable';
import { Plus, Delete, Rank, Tickets, Document, Edit } from '@element-plus/icons-vue';
import { getQuestionnaire, addQuestionnaire, updateQuestionnaire } from './api';
const emit = defineEmits(['success']);
// 组件库定义
const componentList = ref([
{ type: 'radio', label: '单选题', icon: Tickets },
{ type: 'checkbox', label: '多选题', icon: Tickets },
{ type: 'input', label: '填空题', icon: Edit },
{ type: 'textarea', label: '多行文本', icon: Document }
]);
// 表单数据
const form = ref({
id: null,
title: '',
description: '',
content: [],
status: 0
});
const activeIndex = ref(-1);
// 计算属性:当前选中的题目
const activeQuestion = computed(() => {
if (activeIndex.value === -1) return null;
return form.value.content[activeIndex.value];
});
// 辅助方法:获取组件名称
function getComponentLabel(type) {
const item = componentList.value.find((c) => c.type === type);
return item ? item.label : '';
}
// 克隆组件(用于从左侧拖入中间)
function cloneComponent(component) {
return {
cid: Date.now(), // 生成唯一ID
type: component.type,
title: '',
required: true,
options: component.type === 'radio' || component.type === 'checkbox' ? [{ label: '选项1' }, { label: '选项2' }] : []
};
}
// 选中题目
function selectQuestion(index) {
activeIndex.value = index;
}
// 添加题目后的处理
function handleAddQuestion(evt) {
// 自动选中新添加的题目
activeIndex.value = evt.newIndex;
}
// 删除题目
function deleteQuestion(index) {
form.value.content.splice(index, 1);
if (activeIndex.value === index) {
activeIndex.value = -1;
} else if (activeIndex.value > index) {
activeIndex.value--;
}
}
// 提交表单
async function submitForm(status = 0) {
form.value.status = String(status);
if (!form.value.title) {
proxy.$modal.msgError('请输入问卷标题');
return;
}
try {
form.value.content = JSON.stringify(form.value.content);
console.log(form.value.content);
if (form.value.id) {
await updateQuestionnaire(form.value);
proxy.$modal.msgSuccess('修改成功');
} else {
await addQuestionnaire(form.value);
proxy.$modal.msgSuccess('新增成功');
}
emit('success');
} catch (e) {
// 错误已由拦截器处理
}
}
// 取消
function cancel() {
emit('success');
}
// 初始化(用于修改)
function init(id) {
getQuestionnaire(id).then((res) => {
form.value = res.data;
// 注意:数据库取出的JSON字符串需转回对象
if (typeof form.value.content === 'string') {
form.value.content = JSON.parse(form.value.content);
}
});
}
// 重置
function reset() {
form.value = {
id: null,
title: '',
description: '',
content: [],
status: 0
};
activeIndex.value = -1;
}
defineExpose({ init, reset });
</script>
<style scoped lang="scss">
.edit-container {
height: calc(100vh - 200px);
overflow-y: auto;
padding-bottom: 60px;
position: relative;
.component-list {
.component-item {
display: flex;
align-items: center;
padding: 10px;
margin-bottom: 10px;
background: #f4f4f5;
border-radius: 4px;
cursor: move;
.el-icon {
margin-right: 8px;
}
}
}
.question-title-area {
margin-bottom: 20px;
padding: 10px;
border: 1px dashed #dcdfe6;
.title-input {
font-size: 18px;
font-weight: bold;
margin-bottom: 10px;
}
}
.question-list {
min-height: 200px;
.question-item {
display: flex;
align-items: center;
padding: 12px;
margin-bottom: 10px;
border: 1px solid #ebeef5;
background: #fff;
border-radius: 4px;
&:hover {
border-color: #409eff;
}
&.is-active {
border-color: #409eff;
background: #ecf5ff;
}
.question-handle {
cursor: move;
padding: 0 10px;
color: #909399;
}
.question-content {
flex: 1;
.question-type-tag {
display: inline-block;
padding: 2px 6px;
margin-right: 8px;
background: #f0f9eb;
color: #67c23a;
border-radius: 4px;
font-size: 12px;
}
}
}
}
.footer-actions {
position: fixed;
bottom: 0;
left: 0;
right: 0;
background: #fff;
padding: 10px 20px;
text-align: right;
border-top: 1px solid #dcdfe6;
z-index: 100;
}
}
</style>

View File

@@ -0,0 +1,44 @@
import request from '@/utils/request';
// 查询问卷列表
export function listQuestionnaire(query) {
return request({
url: '/questionnaire/list',
method: 'get',
params: query
});
}
// 查询问卷详细
export function getQuestionnaire(id) {
return request({
url: '/questionnaire/' + id,
method: 'get'
});
}
// 新增问卷
export function addQuestionnaire(data) {
return request({
url: '/questionnaire',
method: 'post',
data: data
});
}
// 修改问卷
export function updateQuestionnaire(data) {
return request({
url: '/questionnaire',
method: 'put',
data: data
});
}
// 删除问卷
export function delQuestionnaire(id) {
return request({
url: '/questionnaire/' + id,
method: 'delete'
});
}