refactor: 重构 render/pages 目录下三个文件, 使用 Vue3 组合式 API 写法 (#113)

This commit is contained in:
alwayrun 2024-05-15 20:32:57 +08:00 committed by GitHub
parent c67d8dd134
commit 5c57d5f5c3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 121 additions and 142 deletions

View File

@ -8,29 +8,23 @@
</div> </div>
</div> </div>
</template> </template>
<script> <script setup lang="ts">
export default { import { computed } from 'vue'
name: 'errorPage', import { useStore } from 'vuex'
data() {
return { const store = useStore()
imageMap: {
overTime: '/imgs/icons/overtime.webp' const errorImageUrl = computed(() => {
const errorType = store.state?.errorInfo?.errorType
const imageMap = {
overTime: '/imgs/icons/overtime.webp',
default: '/imgs/icons/error.webp',
} }
}
}, return imageMap[errorType as 'overTime'] || imageMap.default
computed: { })
errorImageUrl() {
return this.imageMap[this.errorType] || this.imageMap.default const errorMsg = computed(() => store.state?.errorInfo?.errorMsg)
},
errorType() {
return this.$store.state?.errorInfo?.errorType
},
errorMsg() {
return this.$store.state?.errorInfo?.errorMsg
}
},
mounted() {}
}
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
.result-page-wrap { .result-page-wrap {

View File

@ -1,19 +1,21 @@
<template> <template>
<div class="index"> <div class="index">
<progressBar /> <progressBar />
<div class="wrapper" ref="box"> <div class="wrapper" ref="boxRef">
<HeaderSetter></HeaderSetter> <HeaderSetter></HeaderSetter>
<div class="content"> <div class="content">
<MainTitle></MainTitle> <MainTitle></MainTitle>
<MainRenderer ref="main"></MainRenderer> <MainRenderer ref="mainRef"></MainRenderer>
<submit :validate="validate" :renderData="renderData" @submit="onSubmit"></submit> <submit :validate="validate" :renderData="renderData" @submit="handleSubmit"></submit>
<LogoIcon /> <LogoIcon />
</div> </div>
</div> </div>
</div> </div>
</template> </template>
<script setup lang="ts">
import { computed, ref } from 'vue'
import { useStore } from 'vuex'
<script>
import HeaderSetter from '../components/HeaderSetter.vue' import HeaderSetter from '../components/HeaderSetter.vue'
import MainTitle from '../components/MainTitle.vue' import MainTitle from '../components/MainTitle.vue'
import submit from '../components/SubmitSetter.vue' import submit from '../components/SubmitSetter.vue'
@ -28,115 +30,101 @@ import encrypt from '../utils/encrypt'
import useCommandComponent from '../hooks/useCommandComponent' import useCommandComponent from '../hooks/useCommandComponent'
export default { interface Props {
name: 'indexPage', questionInfo?: any,
props: { isMobile?: boolean,
questionInfo: { }
type: Object,
default: () => ({}) withDefaults(defineProps<Props>(), {
}, questionInfo: {},
isMobile: { isMobile: false,
type: Boolean, })
default: false
} const mainRef = ref<any>()
}, const boxRef = ref<HTMLElement>()
components: {
HeaderSetter, const alert = useCommandComponent(AlertDialog)
MainTitle, const confirm = useCommandComponent(ConfirmDialog)
submit,
MainRenderer, const store = useStore()
progressBar,
LogoIcon const renderData = computed(() => store.getters.renderData)
},
computed: { const validate = (cbk: (v: boolean) => void) => {
formModel() {
return this.$store.getters.formModel
},
confirmAgain() {
return this.$store.state.submitConf.confirmAgain
},
surveyPath() {
return this.$store.state.surveyPath
},
renderData() {
return this.$store.getters.renderData
},
encryptInfo() {
return this.$store.state.encryptInfo
}
},
created() {
this.alert = useCommandComponent(AlertDialog)
this.confirm = useCommandComponent(ConfirmDialog)
},
methods: {
validate(cbk) {
const index = 0 const index = 0
this.$refs.main.$refs.formGroup[index].validate(cbk) mainRef.value.$refs.formGroup[index].validate(cbk)
}, }
onSubmit() {
const { again_text, is_again } = this.confirmAgain const normalizationRequestBody = () => {
if (is_again) { const enterTime = store.state.enterTime
this.confirm({ const encryptInfo = store.state.encryptInfo
title: again_text, const formModel = store.getters.formModel
onConfirm: async () => { const surveyPath = store.state.surveyPath
try {
await this.submitForm() const result: any = {
} catch (error) { surveyPath,
console.error(error) data: JSON.stringify(formModel),
} finally { difTime: Date.now() - enterTime,
this.confirm.close()
}
}
})
} else {
this.submitForm()
}
},
getSubmitData() {
const result = {
surveyPath: this.surveyPath,
data: JSON.stringify(this.formModel),
difTime: Date.now() - this.$store.state.enterTime,
clientTime: Date.now() clientTime: Date.now()
} }
if (this.encryptInfo?.encryptType) {
result.encryptType = this.encryptInfo?.encryptType if (encryptInfo?.encryptType) {
result.data = encrypt[result.encryptType]({ result.encryptType = encryptInfo?.encryptType
result.data = encrypt[result.encryptType as 'rsa']({
data: result.data, data: result.data,
secretKey: this.encryptInfo?.data?.secretKey secretKey: encryptInfo?.data?.secretKey
}) })
if (this.encryptInfo?.data?.sessionId) { if (encryptInfo?.data?.sessionId) {
result.sessionId = this.encryptInfo.data.sessionId result.sessionId = encryptInfo.data.sessionId
} }
} else { } else {
result.data = JSON.stringify(result.data) result.data = JSON.stringify(result.data)
} }
return result return result
}, }
async submitForm() {
const submitSurver = async () => {
try { try {
const submitData = this.getSubmitData() const params = normalizationRequestBody()
const res = await submitForm(submitData) console.log(params)
const res: any = await submitForm(params)
if (res.code === 200) { if (res.code === 200) {
this.$store.commit('setRouter', 'successPage') store.commit('setRouter', 'successPage')
} else { } else {
this.alert({ alert({
title: res.errmsg || '提交失败' title: res.errmsg || '提交失败'
}) })
} }
} catch (error) { } catch (error) {
console.log(error) console.log(error)
} }
}
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()
} catch(error) {
console.log(error)
} finally {
confirm.close()
} }
} }
})
} else {
submitSurver()
}
} }
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">
.index { .index {
// padding-bottom: 0.8rem;
min-height: 100%; min-height: 100%;
.wrapper { .wrapper {
min-height: 100%; min-height: 100%;

View File

@ -9,20 +9,17 @@
</div> </div>
</div> </div>
</template> </template>
<script> <script setup lang="ts">
import { computed } from 'vue'
import { useStore } from 'vuex'
import LogoIcon from '../components/LogoIcon.vue' import LogoIcon from '../components/LogoIcon.vue'
export default {
name: 'resultPage', const store = useStore()
components: { LogoIcon },
computed: { const successMsg = computed(() => {
submitConf() { const msgContent = store.state?.submitConf?.msgContent || {}
return this.$store?.state?.submitConf || {} return msgContent?.msg_200 || '提交成功'
}, })
successMsg() {
return this.submitConf?.msgContent?.msg_200 || '提交成功'
}
}
}
</script> </script>
<style lang="scss" scoped> <style lang="scss" scoped>
@import '@/render/styles/variable.scss'; @import '@/render/styles/variable.scss';