110 lines
2.8 KiB
TypeScript
110 lines
2.8 KiB
TypeScript
import { getSurveysPage, deleteSurveys, saveTemplates } from '@/api/home';
|
|
import { ref } from 'vue';
|
|
import { showDialog, showConfirmDialog, showFailToast, showToast } from 'vant';
|
|
import { getSurveysDetail } from '@/api/design';
|
|
|
|
const searchValue = ref('');
|
|
const survey = ref<SurveyItem[]>([]);
|
|
const total = ref(0);
|
|
const loading = ref(false);
|
|
const requestLoading = ref(false);
|
|
const finished = ref(false);
|
|
const currentSurvey = ref<SurveyItem>();
|
|
|
|
async function fetchSingleSurvey(sn: string) {
|
|
const res = await getSurveysDetail(sn);
|
|
// const res = await getSetting({sn})
|
|
// console.log(res);
|
|
if (res.data.code === 0) {
|
|
currentSurvey.value = res.data.data;
|
|
}
|
|
}
|
|
|
|
async function fetchSurveys(form: any) {
|
|
requestLoading.value = true;
|
|
const params = {
|
|
page: form.page,
|
|
per_page: form.pageSize,
|
|
group_id: 0,
|
|
// project_name: searchValue.value
|
|
key_word: searchValue.value
|
|
};
|
|
const res = await getSurveysPage(params);
|
|
if (res.data.code === 0) {
|
|
survey.value = survey.value.concat(res.data.data);
|
|
total.value = res.data.meta.total;
|
|
survey.value.forEach((item) => {
|
|
const sceneName = JSON.parse(JSON.stringify(item.scene_name));
|
|
|
|
const nameList = sceneName ? sceneName.split('-') : [];
|
|
if (nameList.length > 0) {
|
|
item.scene_name = nameList[1] ? nameList[1] : nameList[0];
|
|
}
|
|
|
|
const timeList = item.created_at.split(' ');
|
|
if (nameList.length) {
|
|
item.created_at = timeList[0];
|
|
}
|
|
});
|
|
loading.value = false;
|
|
// 数据全部加载完成
|
|
if (survey.value.length >= total.value) {
|
|
finished.value = true;
|
|
}
|
|
|
|
} else {
|
|
// Toast()
|
|
}
|
|
requestLoading.value = false;
|
|
}
|
|
|
|
function deleteItem(item: SurveyItem, form: any) {
|
|
showDialog({
|
|
title: `确认删除问卷 "${item.project_name}" ?`,
|
|
showCancelButton: true,
|
|
confirmButtonColor: '#03B03C'
|
|
})
|
|
.then(async () => {
|
|
const res = await deleteSurveys(item.sn);
|
|
if (res.data.message) {
|
|
showToast(res.data.message);
|
|
} else {
|
|
showToast('删除成功!');
|
|
}
|
|
form.page = 1;
|
|
survey.value = [];
|
|
await fetchSurveys(form);
|
|
})
|
|
.catch(() => {
|
|
// on cancel
|
|
});
|
|
}
|
|
|
|
// 保存为模板
|
|
async function saveTemplate(item: SurveyItem) {
|
|
const data = JSON.parse(JSON.stringify(item));
|
|
const res = await saveTemplates(item.sn, data);
|
|
if (res.data.code === 200 || res.data.code === 201) {
|
|
showConfirmDialog({
|
|
message: '模板保存成功,请前往更多模板页面查看',
|
|
showCancelButton: false
|
|
});
|
|
} else {
|
|
showFailToast(res.data);
|
|
}
|
|
}
|
|
|
|
export {
|
|
fetchSurveys,
|
|
loading,
|
|
finished,
|
|
survey,
|
|
total,
|
|
searchValue,
|
|
deleteItem,
|
|
saveTemplate,
|
|
currentSurvey,
|
|
requestLoading,
|
|
fetchSingleSurvey
|
|
};
|