qs_xinchun2026_h5/components/AICoupletForm.vue

261 lines
4.9 KiB
Vue

<template>
<div class="form-modal" v-if="visible" @touchmove="handleTouchMove">
<div class="modal-overlay"></div>
<div class="modal-content">
<div class="modal-body">
<div class="couplet-form">
<div class="form-row">
<div class="input-group">
<label class="label">上联首字:</label>
<input
v-model="topKeyword"
type="text"
maxlength="1"
class="underline-input"
@input="validateCharacter('top')"
/>
</div>
<div class="input-group">
<label class="label">下联首字:</label>
<input
v-model="bottomKeyword"
type="text"
maxlength="1"
class="underline-input"
@input="validateCharacter('bottom')"
/>
</div>
</div>
<div class="form-actions">
<img
src="/static/images/btn_submit.png"
class="submit-btn-image"
@click="generateCouplet"
/>
</div>
</div>
</div>
</div>
<div class="close-button-area">
<img
src="/static/images/btn_close.png"
class="close-button-image"
@click="$emit('close')"
/>
</div>
</div>
</template>
<script setup>
import { ref, computed } from 'vue'
const props = defineProps({
visible: {
type: Boolean,
default: false
}
})
const emit = defineEmits(['close', 'generate'])
const topKeyword = ref('')
const bottomKeyword = ref('')
// 校验是否为汉字
const isChineseCharacter = (char) => {
return /^[\u4e00-\u9fa5]$/.test(char)
}
// 校验输入字符
const validateCharacter = (type) => {
if (type === 'top' && topKeyword.value) {
if (!isChineseCharacter(topKeyword.value)) {
topKeyword.value = ''
uni.showToast({
title: '请输入汉字',
icon: 'none',
duration: 2000
})
}
}
if (type === 'bottom' && bottomKeyword.value) {
if (!isChineseCharacter(bottomKeyword.value)) {
bottomKeyword.value = ''
uni.showToast({
title: '请输入汉字',
icon: 'none',
duration: 2000
})
}
}
}
// 表单是否有效
const isFormValid = computed(() => {
return topKeyword.value && bottomKeyword.value
})
// 生成春联
const generateCouplet = () => {
if (!isFormValid.value) {
uni.showToast({
title: '请输入完整的上下联首字',
icon: 'none',
duration: 2000
})
return
}
emit('generate', topKeyword.value + bottomKeyword.value)
topKeyword.value = ''
bottomKeyword.value = ''
}
// 阻止 iOS 滚动穿透
const handleTouchMove = (e) => {
e.preventDefault()
}
</script>
<style scoped>
.form-modal {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
z-index: 999;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.modal-overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.7);
}
.modal-content {
position: relative;
z-index: 99;
width: 701rpx;
height: 444rpx;
background-image: url('/static/info/couplet_info_box.png');
background-size: 100% 100%;
background-repeat: no-repeat;
display: flex;
flex-direction: column;
}
.modal-body {
flex: 1;
display: flex;
align-items: center;
justify-content: center;
padding: 0 55rpx;
}
.couplet-form {
width: 100%;
margin-top: 150rpx;
text-align: center;
}
.form-title {
font-size: 48rpx;
color: #fff;
margin-bottom: 80rpx;
font-weight: bold;
text-shadow: 2rpx 2rpx 4rpx rgba(0, 0, 0, 0.3);
}
.form-row {
display: flex;
margin-bottom: 60rpx;
align-items: center;
justify-content: space-between;
}
.input-group {
display: flex;
flex-direction: row;
align-items: center;
}
.label {
color: white;
font-size: 32rpx;
text-shadow: 1rpx 1rpx 2rpx rgba(0, 0, 0, 0.3);
}
.underline-input {
width: 80rpx;
height: 40rpx;
line-height: 40rpx;
background-color: transparent;
border: none;
border-bottom: 2rpx solid white;
color: white;
font-size: 32rpx;
text-align: center;
outline: none;
padding: 0;
}
.underline-input:focus {
border-bottom: 3rpx solid #fff;
}
.form-actions {
display: flex;
justify-content: center;
}
.submit-btn-image {
width: 306rpx;
height: 97rpx;
cursor: pointer;
transition: all 0.3s;
}
.submit-btn-image:hover {
transform: scale(1.05);
}
.submit-btn-image.disabled {
opacity: 0.5;
cursor: not-allowed;
}
.submit-btn-image.disabled:hover {
transform: none;
}
.close-button-area {
display: flex;
justify-content: center;
align-items: center;
padding: 30rpx 0;
z-index: 99;
}
.close-button-image {
width: 61rpx;
height: 60rpx;
cursor: pointer;
transition: all 0.3s;
}
.close-button-image:hover {
transform: scale(1.1);
}
</style>