问卷导出

master
lion 1 week ago
parent 45c3911066
commit 3d94640740

@ -80,6 +80,7 @@
><el-button @click="loadStats">重试</el-button></el-empty>
</div>
<div v-else v-loading="responsesLoading">
<div style="margin-bottom: 16px"><el-button type="primary" icon="el-icon-download" :loading="exporting" :disabled="exporting" @click="exportResponses">导出全部答卷及附件</el-button><span style="margin-left: 12px; color: #909399">导出当前问卷全部答卷,不限当前页</span></div>
<el-table
:data="responses"
border
@ -165,7 +166,8 @@
<small>{{ item.created_at }}</small>
<SurveyAttachments
v-if="item.files && item.files.length"
:files="item.files" :response-id="item.id"
:files="item.files"
:response-id="item.id"
/>
<p v-else>{{ item.text }}</p>
<el-button
@ -188,6 +190,9 @@
</el-dialog>
</template>
<script>
import axios from 'axios'
import { getToken } from '@/utils/auth'
import { saveAs } from 'file-saver'
import {
index,
show,
@ -212,6 +217,7 @@ export default {
summary: null,
loading: false,
responsesLoading: false,
exporting: false,
responses: [],
page: 1,
total: 0,
@ -251,6 +257,22 @@ export default {
}
},
methods: {
async exportResponses() {
if (this.exporting || !this.surveyData) return
this.exporting = true
try {
const result = await axios.get('/api/admin/course-content-evaluation-form/index', {
baseURL: process.env.VUE_APP_BASE_API,
params: { export_bundle: 1, course_content_evaluation_id: this.surveyData.id },
headers: { Authorization: 'Bearer ' + getToken() }, responseType: 'blob', timeout: 300000
})
if ((result.headers['content-type'] || '').includes('application/json')) {
const error = JSON.parse(await result.data.text())
throw new Error(error.errmsg || '导出失败,请重试')
}
saveAs(result.data, '问卷答卷_' + this.surveyData.id + '.zip')
} catch (e) { this.$message.error('导出失败,请检查登录状态或稍后重试') } finally { this.exporting = false }
},
close() {
this.$emit('update:visible', false)
},

@ -485,4 +485,14 @@ describe('Survey editing and results', () => {
settings.destroy()
editor.destroy()
})
it('exports all survey responses without the current page filter', async() => {
const blob = new Blob(['zip'])
const get = jest.spyOn(axios, 'get').mockResolvedValue({ headers: { 'content-type': 'application/zip' }, data: blob })
const state = { exporting: false, surveyData: { id: 10 }, page: 3, $message: { error: jest.fn() } }
await Results.methods.exportResponses.call(state)
expect(get).toHaveBeenCalledWith('/api/admin/course-content-evaluation-form/index', expect.objectContaining({ params: { export_bundle: 1, course_content_evaluation_id: 10 } }))
expect(saveAs).toHaveBeenCalledWith(blob, '问卷答卷_10.zip')
expect(state.exporting).toBe(false)
get.mockRestore()
})
})

Loading…
Cancel
Save