<template>
<div class="wjgl-container">
<!-- 索引筛选区 -->
<div class="filter-bar" style="margin-bottom: 15px;">
<el-select
v-model="statusFilter"
placeholder="状态筛选"
clearable
style="width: 150px; margin-right: 15px;"
@change="handleFilterChange"
>
<el-option label="草稿" value="draft"></el-option>
<el-option label="发布" value="published"></el-option>
<el-option label="结束" value="closed"></el-option>
</el-select>
<el-date-picker
v-model="dateRange"
type="daterange"
range-separator="至"
start-placeholder="开始日期"
end-placeholder="结束日期"
style="width: 300px; margin-right: 20px;"
@change="handleFilterChange"
></el-date-picker>
<el-input
v-model="searchKeyword"
placeholder="搜索问卷名称"
style="width: 300px;"
clearable
@keyup.enter.native="handleSearch"
>
<el-button
slot="append"
icon="el-icon-search"
@click="handleSearch"
></el-button>
</el-input>
</div>
<!-- 列表操作栏 -->
<div class="list-operation-bar" style="margin-bottom: 15px; display: flex; justify-content: space-between;">
<div>
<el-button
type="primary"
icon="el-icon-plus"
@click="handleCreate"
>
新建问卷
</el-button>
<el-button
type="danger"
icon="el-icon-delete"
@click="handleBatchDelete"
:disabled="selectedIds.length === 0"
style="margin-left: 10px;"
>
批量删除
</el-button>
</div>
</div>
<!-- 问卷列表 -->
<el-table
:data="questionnaireList"
border
style="width: 100%;"
v-loading="loading"
@selection-change="handleSelectionChange"
ref="table"
>
<!-- 全选按钮列 -->
<el-table-column
width="80"
align="center"
>
<template slot="header">
<el-button
type="text"
@click="handleSelectAll"
:style="{color: isAllSelected ? '#409EFF' : ''}"
>
{{ isAllSelected ? '取消全选' : '全选' }}
</el-button>
</template>
<template slot-scope="scope">
<el-checkbox
v-model="scope.row.selected"
@change="(val) => handleRowSelect(scope.row, val)"
></el-checkbox>
</template>
</el-table-column>
<el-table-column
prop="id"
label="ID"
width="80"
align="center"
></el-table-column>
<el-table-column
prop="title"
label="问卷名称"
min-width="200"
></el-table-column>
<el-table-column
prop="status"
label="状态"
width="120"
align="center"
>
<template slot-scope="scope">
<el-tag
:type="getStatusType(scope.row.status)"
>
{{ getStatusText(scope.row.status) }}
</el-tag>
</template>
</el-table-column>
<el-table-column
prop="createTime"
label="创建时间"
width="180"
align="center"
></el-table-column>
<el-table-column
prop="effectiveTime"
label="生效时间"
width="220"
align="center"
></el-table-column>
<el-table-column
prop="submitCount"
label="提交量"
width="100"
align="center"
></el-table-column>
<el-table-column
label="操作"
width="350"
align="center"
>
<template slot-scope="scope">
<el-button
size="mini"
@click="handleEdit(scope.row)"
:disabled="scope.row.status === 'closed'"
>
编辑
</el-button>
<!-- 状态下拉单选框 -->
<el-select
size="mini"
v-model="scope.row.status"
@change="(val) => handleStatusChange(scope.row, val)"
style="width: 100px; margin: 0 5px;"
>
<el-option label="草稿" value="draft"></el-option>
<el-option label="发布" value="published"></el-option>
<el-option label="结束" value="closed"></el-option>
</el-select>
<el-button
size="mini"
type="warning"
@click="handleCopy(scope.row)"
>
复制
</el-button>
<el-button
size="mini"
type="danger"
@click="handleDelete(scope.row)"
>
删除
</el-button>
</template>
</el-table-column>
</el-table>
<!-- 分页 -->
<el-pagination
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page="currentPage"
:page-sizes="[10, 20, 50]"
:page-size="pageSize"
:total="total"
layout="total, sizes, prev, pager, next, jumper"
style="margin-top: 20px; text-align: right;"
></el-pagination>
</div>
</template>
<script>
import request from '@/utils/request';
import { getToken } from '@/utils/auth';
import axios from 'axios';
export default {
name: 'WjglIndex',
data() {
return {
questionnaireList: [], // 问卷列表数据
searchKeyword: '', // 搜索关键词
currentPage: 1, // 当前页码
pageSize: 10, // 每页条数
total: 0, // 总条数
loading: false, // 加载状态
statusFilter: '', // 状态筛选
dateRange: [], // 日期范围筛选
selectedIds: [], // 选中的问卷ID数组,用于批量删除
isAllSelected: false, // 是否全选状态
}
},
created() {
// 页面加载时获取问卷列表
this.getList()
},
methods: {
// 根据状态值获取显示文本
getStatusText(status) {
const statusMap = {
'draft': '草稿',
'published': '发布',
'closed': '结束'
}
return statusMap[status] || '未知'
},
// 根据状态值获取标签类型
getStatusType(status) {
const typeMap = {
'draft': 'info', // 草稿 - 蓝色
'published': 'success', // 发布 - 绿色
'closed': 'danger' // 结束 - 红色
}
return typeMap[status] || 'warning'
},
// 日期格式化(
formatDate(date) {
if (!date) return '';
const d = new Date(date);
return `${d.getFullYear()}-${(d.getMonth() + 1).toString().padStart(2, '0')}-${d.getDate().toString().padStart(2, '0')}`;
},
// 获取问卷列表(模拟接口请求)
async getList() {
this.loading = true;
try {
// 1. 构建请求参数
const params = {
pageNum: this.currentPage,
pageSize: this.pageSize,
keyword: this.searchKeyword.trim(), // 搜索关键词
status: this.statusFilter || undefined, // 状态筛选(空值不传递)
// 日期范围转换为yyyy-MM-dd格式(根据后端要求调整)
startTime: this.dateRange[0] ? this.formatDate(this.dateRange[0]) : undefined,
endTime: this.dateRange[1] ? this.formatDate(this.dateRange[1]) : undefined
};
// 2. 调用接口(GET方法,携带token)
const response = await request.get(
'/qiya-wjdc/questionnaires/list',
{
params,
headers: {
'Authorization': `Bearer ${getToken()}` // 传递token
}
}
);
// 3. 解析响应(根据后端实际返回结构调整)
if (response.code === 200) {
const { total, rows } = response;
// 为列表项添加selected属性(用于勾选功能)
this.questionnaireList = rows.map(item => ({
...item,
id: item.questionnaireId,// 统一ID字段
selected: false
}));
this.total = total; // 更新总条数
} else {
this.$message.error(`获取失败:${response.msg || '未知错误'}`);
this.questionnaireList = [];
this.total = 0;
}
// 更新全选状态
this.updateAllSelectedStatus();
} catch (err) {
console.error('获取问卷列表失败:', err);
this.$message.error('网络错误,获取问卷列表失败');
this.questionnaireList = [];
this.total = 0;
} finally {
this.loading = false;
}
},
// 更新全选状态
updateAllSelectedStatus() {
if (this.questionnaireList.length === 0) {
this.isAllSelected = false
return
}
this.isAllSelected = this.questionnaireList.every(item => item.selected)
},
// 全选/取消全选
handleSelectAll() {
const newStatus = !this.isAllSelected
this.questionnaireList.forEach(item => {
item.selected = newStatus
})
// 更新选中的ID列表
this.selectedIds = newStatus
? this.questionnaireList.map(item => item.id)
: []
this.isAllSelected = newStatus
},
// 行选择变化
handleRowSelect(row, isSelected) {
row.selected = isSelected
if (isSelected) {
// 选中时添加ID
if (!this.selectedIds.includes(row.id)) {
this.selectedIds.push(row.id)
}
} else {
// 取消选中时移除ID
this.selectedIds = this.selectedIds.filter(id => id !== row.id)
}
// 更新全选状态
this.updateAllSelectedStatus()
},
// 搜索
handleSearch() {
this.currentPage = 1 // 重置页码
this.getList()
},
// 筛选条件变化
handleFilterChange() {
this.currentPage = 1 // 重置页码
this.getList()
},
// 分页大小变化
handleSizeChange(val) {
this.pageSize = val
this.getList()
},
// 页码变化
handleCurrentChange(val) {
this.currentPage = val
this.getList()
},
// 状态变化处理(下拉单选框变更事件)
async handleStatusChange(row, newStatus) {
console.log('触发状态更新方法');
console.log('当前行数据:', row);
console.log('目标状态:', newStatus);
console.log('row.status:', row.status, '类型:', typeof row.status);
console.log('row.id:', row.id, '类型:', typeof row.id);
//状态未变化则不操作
if(row.status === newStatus) return;
this.loading = true;
try{
const response = await request.put(
`/qiya-wjdc/questionnaires/editStatus/${row.id}/${newStatus}`,
{},
{
headers: {
'Authorization': `Bearer ${getToken()}`,
'Content-Type': 'application/json', // 显式指定
'repeatSubmit': false // 强制跳过防重复检查
}
}
);
if(response.code === 200){
this.$message.success(`状态已更新为${this.getStatusText(newStatus)}`);
this.getList(); // 刷新列表
} else {
this.$message.error(`状态更新失败:${response.msg || '未知错误'}`);
}
} catch(err){
console.error('更新状态失败:', err);
this.$message.error('网络错误,更新状态失败');
} finally {
this.loading = false;
}
},
// 新建问卷
handleCreate() {
this.$router.push('/wjgl/wjsc')
this.$message.info('跳转到新建问卷页面')
},
// 编辑问卷
handleEdit(row) {
this.$router.push(`/wjgl/wjsc/${row.id}?type=edit`)
this.$message.info(`跳转到编辑问卷页面,问卷ID:${row.id}`)
},
// 复制问卷
async handleCopy(row) {
this.loading = true;
try {
// 调用真实复制接口(根据后端实际接口调整)
const response = await request.post(
'/qiya-wjdc/questionnaires/copy',
{ questionnaireId: row.id },
{ headers: { 'Authorization': `Bearer ${getToken()}` } }
);
if (response.code === 200) {
this.$message.success('复制成功');
this.getList();
} else {
this.$message.error(`复制失败:${response.msg || '未知错误'}`);
}
} catch (err) {
console.error('复制失败:', err);
this.$message.error('网络错误,复制失败');
} finally {
this.loading = false;
}
},
// 单个删除问卷
async handleDelete(row) {
this.$confirm('确定要删除该问卷吗?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(async () => {
this.loading = true;
try {
// 调用真实删除接口
const response = await request.post(
'/qiya-wjdc/questionnaires/delete',
{ questionnaireId: row.id },
{ headers: { 'Authorization': `Bearer ${getToken()}` } }
);
if (response.code === 200) {
this.$message.success('删除成功');
this.selectedIds = this.selectedIds.filter(id => id !== row.id);
this.getList();
} else {
this.$message.error(`删除失败:${response.msg || '未知错误'}`);
}
} catch (err) {
console.error('删除失败:', err);
this.$message.error('网络错误,删除失败');
} finally {
this.loading = false;
}
}).catch(() => {
this.$message.info('已取消删除');
});
},
// 处理选择变化(用于保持选中状态同步)
handleSelectionChange(selection) {
this.selectedIds = selection.map(item => item.id)
this.updateAllSelectedStatus()
},
// 批量删除问卷
async handleBatchDelete() {
if (this.selectedIds.length === 0) {
this.$message.warning('请先选择要删除的问卷');
return;
}
this.$confirm(`确定要删除选中的${this.selectedIds.length}个问卷吗?`, '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(async () => {
this.loading = true;
try {
// 调用真实批量删除接口
const response = await request.post(
'/qiya-wjdc/questionnaires/batchDelete',
{ questionnaireIds: this.selectedIds },
{ headers: { 'Authorization': `Bearer ${getToken()}` } }
);
if (response.code === 200) {
this.$message.success(`成功删除${this.selectedIds.length}个问卷`);
this.selectedIds = [];
this.getList();
} else {
this.$message.error(`批量删除失败:${response.msg || '未知错误'}`);
}
} catch (err) {
console.error('批量删除失败:', err);
this.$message.error('网络错误,批量删除失败');
} finally {
this.loading = false;
}
}).catch(() => {
this.$message.info('已取消批量删除');
});
},
}
}
</script>
<style scoped>
.wjgl-container {
padding: 20px;
background-color: #fff;
max-height: calc(100vh - 140px);
overflow-x: auto; /* 防止表格列过多导致横向溢出 */
}
.filter-bar {
display: flex;
align-items: center;
margin-top: 0;
}
.list-operation-bar {
display: flex;
justify-content: space-between;
align-items: center;
}
/* 全选按钮样式优化 */
::v-deep .el-button--text {
padding: 5px 10px;
font-size: 14px;
}
</style>
帮我检查一下,为什么我都能拿到问卷的列表数据,但是修改对应问卷的状态没有能促发handleStatusChange(row, newStatus)这个方法,并且网络上没有该接口请求
最新发布