xiaoju-survey/web/src/render/pages/IndexPage.vue

160 lines
4.1 KiB
Vue
Raw Normal View History

2024-05-09 12:34:24 +00:00
<template>
<div class="index">
2024-05-21 13:31:55 +00:00
<ProgressBar />
<div class="wrapper" ref="boxRef">
<HeaderContent :bannerConf="bannerConf" :readonly="true" />
2024-05-09 12:34:24 +00:00
<div class="content">
<MainTitle :bannerConf="bannerConf" :readonly="true"></MainTitle>
<MainRenderer ref="mainRef"></MainRenderer>
<SubmitButton
:validate="validate"
:submitConf="submitConf"
:readonly="true"
:renderData="renderData"
@submit="handleSubmit"
></SubmitButton>
<LogoIcon :logo-conf="logoConf" :readonly="true" />
2024-05-09 12:34:24 +00:00
</div>
</div>
</div>
</template>
<script setup lang="ts">
import { computed, ref } from 'vue'
import { useStore } from 'vuex'
// @ts-ignore
import communalLoader from '@materials/communals/communalLoader.js'
2024-05-09 12:34:24 +00:00
import MainRenderer from '../components/MainRenderer.vue'
import AlertDialog from '../components/AlertDialog.vue'
import ConfirmDialog from '../components/ConfirmDialog.vue'
2024-05-21 13:31:55 +00:00
import ProgressBar from '../components/ProgressBar.vue'
2024-05-09 12:34:24 +00:00
import { submitForm } from '../api/survey'
import encrypt from '../utils/encrypt'
import useCommandComponent from '../hooks/useCommandComponent'
interface Props {
2024-05-15 12:34:56 +00:00
questionInfo?: any
isMobile?: boolean
}
withDefaults(defineProps<Props>(), {
questionInfo: {},
2024-05-15 12:34:56 +00:00
isMobile: false
})
const HeaderContent = communalLoader.loadComponent('HeaderContent')
const MainTitle = communalLoader.loadComponent('MainTitle')
const SubmitButton = communalLoader.loadComponent('SubmitButton')
const LogoIcon = communalLoader.loadComponent('LogoIcon')
const mainRef = ref<any>()
const boxRef = ref<HTMLElement>()
const alert = useCommandComponent(AlertDialog)
const confirm = useCommandComponent(ConfirmDialog)
const store = useStore()
const bannerConf = computed(() => store.state?.bannerConf || {})
const renderData = computed(() => store.getters.renderData)
const submitConf = computed(() => store.state?.submitConf || {})
const logoConf = computed(() => store.state?.bottomConf || {})
const surveyPath = computed(() => store.state?.surveyPath || '')
const validate = (cbk: (v: boolean) => void) => {
const index = 0
mainRef.value.$refs.formGroup[index].validate(cbk)
}
const normalizationRequestBody = () => {
const enterTime = store.state.enterTime
const encryptInfo = store.state.encryptInfo
2024-05-29 14:04:44 +00:00
const formValues = store.state.formValues
const result: any = {
surveyPath: surveyPath.value,
2024-05-29 14:04:44 +00:00
data: JSON.stringify(formValues),
difTime: Date.now() - enterTime,
clientTime: Date.now()
}
if (encryptInfo?.encryptType) {
result.encryptType = encryptInfo?.encryptType
result.data = encrypt[result.encryptType as 'rsa']({
data: result.data,
secretKey: encryptInfo?.data?.secretKey
})
if (encryptInfo?.data?.sessionId) {
result.sessionId = encryptInfo.data.sessionId
2024-05-09 12:34:24 +00:00
}
} else {
result.data = JSON.stringify(result.data)
}
return result
}
const submitSurver = async () => {
if (surveyPath.value.length > 8) {
store.commit('setRouter', 'successPage')
return
}
try {
const params = normalizationRequestBody()
const res: any = await submitForm(params)
if (res.code === 200) {
store.commit('setRouter', 'successPage')
} else {
alert({
title: res.errmsg || '提交失败'
})
2024-05-09 12:34:24 +00:00
}
} catch (error) {
console.log(error)
}
}
2024-05-09 12:34:24 +00:00
const handleSubmit = () => {
const confirmAgain = store.state.submitConf.confirmAgain
const { again_text, is_again } = confirmAgain
if (is_again) {
confirm({
title: again_text,
onConfirm: async () => {
try {
submitSurver()
2024-05-15 12:34:56 +00:00
} catch (error) {
console.log(error)
} finally {
confirm.close()
2024-05-09 12:34:24 +00:00
}
}
})
} else {
submitSurver()
2024-05-09 12:34:24 +00:00
}
}
</script>
<style scoped lang="scss">
.index {
min-height: 100%;
2024-05-09 12:34:24 +00:00
.wrapper {
min-height: 100%;
background-color: var(--primary-background-color);
display: flex;
flex-direction: column;
2024-05-09 12:34:24 +00:00
.content {
flex: 1;
margin: 0 0.3rem;
background: rgba(255, 255, 255, var(--opacity));
border-radius: 8px 8px 0 0;
height: 100%;
}
}
}
</style>