corp-mp/src/pages/landing/index.tsx

215 lines
8.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { Button, Checkbox, CheckboxGroup, Input, Radio, RadioGroup, Text, Textarea, View } from '@tarojs/components'
import Taro from '@tarojs/taro'
import { useState } from 'react'
import './index.scss'
type ServiceType = 'new' | 'update'
type ApplicationForm = {
serviceType: ServiceType
storeName: string
storeAddress: string
contactName: string
contactPhone: string
description: string
}
type FormErrors = Partial<Record<keyof ApplicationForm | 'agreement', string>>
const INITIAL_FORM: ApplicationForm = {
serviceType: 'new',
storeName: '',
storeAddress: '',
contactName: '',
contactPhone: '',
description: '',
}
const PHONE_PATTERN = /^1[3-9]\d{9}$/
const validateForm = (form: ApplicationForm, agreed: boolean) => {
const errors: FormErrors = {}
if (!form.storeName.trim()) errors.storeName = '请输入门店名称'
if (!form.storeAddress.trim()) errors.storeAddress = '请输入门店地址'
if (!form.contactName.trim()) errors.contactName = '请输入联系人姓名'
if (!form.contactPhone.trim()) {
errors.contactPhone = '请输入联系电话'
} else if (!PHONE_PATTERN.test(form.contactPhone.trim())) {
errors.contactPhone = '请输入正确的手机号码'
}
if (!form.description.trim()) errors.description = '请描述需要办理的内容'
if (!agreed) errors.agreement = '请阅读并同意隐私政策和服务说明'
return errors
}
export default function Landing () {
const [form, setForm] = useState<ApplicationForm>(INITIAL_FORM)
const [agreed, setAgreed] = useState(false)
const [errors, setErrors] = useState<FormErrors>({})
const updateField = <K extends keyof ApplicationForm>(field: K, value: ApplicationForm[K]) => {
setForm((current) => ({ ...current, [field]: value }))
setErrors((current) => ({ ...current, [field]: undefined }))
}
const showPolicy = (type: 'privacy' | 'service') => {
const isPrivacy = type === 'privacy'
Taro.showModal({
title: isPrivacy ? '隐私政策' : '服务说明',
content: isPrivacy
? '您填写的信息仅用于本次门店标注服务的联系与确认,我们将妥善保护您的个人信息。'
: '提交申请后,工作人员将在服务时间内联系您核对资料。具体办理结果以实际审核情况为准。',
showCancel: false,
confirmText: '我知道了',
})
}
const handleSubmit = () => {
const validationErrors = validateForm(form, agreed)
const firstError = Object.values(validationErrors)[0]
setErrors(validationErrors)
if (firstError) {
Taro.showToast({ title: firstError, icon: 'none' })
return
}
Taro.showModal({
title: '提交成功',
content: '您的申请已提交,工作人员将在服务时间内联系您确认。',
showCancel: false,
confirmText: '我知道了',
})
}
return (
<View className='landing-page'>
<View className='landing-page__intro'>
<Text className='landing-page__title'>线</Text>
<Text className='landing-page__description'>
</Text>
<Text className='landing-page__notice'></Text>
</View>
<View className='landing-page__form'>
<View className='landing-page__field'>
<Text className='landing-page__label'></Text>
<RadioGroup
className='landing-page__service-types'
onChange={(event) => updateField('serviceType', event.detail.value as ServiceType)}
>
<View className={`landing-page__service-type ${form.serviceType === 'new' ? 'landing-page__service-type--selected' : ''}`}>
<Radio className='landing-page__radio' value='new' checked={form.serviceType === 'new'} color='#07c160'></Radio>
</View>
<View className={`landing-page__service-type ${form.serviceType === 'update' ? 'landing-page__service-type--selected' : ''}`}>
<Radio className='landing-page__radio' value='update' checked={form.serviceType === 'update'} color='#07c160'></Radio>
</View>
</RadioGroup>
</View>
<View className='landing-page__field'>
<Text className='landing-page__label'></Text>
<Input
className={`landing-page__input ${errors.storeName ? 'landing-page__input--error' : ''}`}
value={form.storeName}
maxlength={50}
placeholder='请输入营业执照上的门店名称'
placeholderClass='landing-page__placeholder'
onInput={(event) => updateField('storeName', event.detail.value)}
/>
{errors.storeName && <Text className='landing-page__error'>{errors.storeName}</Text>}
</View>
<View className='landing-page__field'>
<Text className='landing-page__label'></Text>
<Input
className={`landing-page__input ${errors.storeAddress ? 'landing-page__input--error' : ''}`}
value={form.storeAddress}
maxlength={100}
placeholder='请输入完整经营地址'
placeholderClass='landing-page__placeholder'
onInput={(event) => updateField('storeAddress', event.detail.value)}
/>
{errors.storeAddress && <Text className='landing-page__error'>{errors.storeAddress}</Text>}
</View>
<View className='landing-page__field'>
<Text className='landing-page__label'></Text>
<Input
className={`landing-page__input ${errors.contactName ? 'landing-page__input--error' : ''}`}
value={form.contactName}
maxlength={30}
placeholder='请输入联系人姓名'
placeholderClass='landing-page__placeholder'
onInput={(event) => updateField('contactName', event.detail.value)}
/>
{errors.contactName && <Text className='landing-page__error'>{errors.contactName}</Text>}
</View>
<View className='landing-page__field'>
<Text className='landing-page__label'></Text>
<Input
className={`landing-page__input ${errors.contactPhone ? 'landing-page__input--error' : ''}`}
value={form.contactPhone}
type='number'
maxlength={11}
placeholder='请输入手机号码'
placeholderClass='landing-page__placeholder'
onInput={(event) => updateField('contactPhone', event.detail.value)}
/>
{errors.contactPhone && <Text className='landing-page__error'>{errors.contactPhone}</Text>}
</View>
<View className='landing-page__field'>
<Text className='landing-page__label'></Text>
<Textarea
className={`landing-page__textarea ${errors.description ? 'landing-page__input--error' : ''}`}
value={form.description}
maxlength={300}
placeholder='请描述需要办理的内容'
placeholderClass='landing-page__placeholder'
onInput={(event) => updateField('description', event.detail.value)}
/>
{errors.description && <Text className='landing-page__error'>{errors.description}</Text>}
</View>
</View>
<CheckboxGroup
className='landing-page__agreement'
onChange={(event) => {
setAgreed(event.detail.value.includes('agreed'))
setErrors((current) => ({ ...current, agreement: undefined }))
}}
>
<Checkbox className='landing-page__checkbox' value='agreed' checked={agreed} color='#07c160' />
<Text className='landing-page__agreement-text'></Text>
<Text className='landing-page__link' onClick={() => showPolicy('privacy')}></Text>
<Text className='landing-page__agreement-text'></Text>
<Text className='landing-page__link' onClick={() => showPolicy('service')}></Text>
</CheckboxGroup>
{errors.agreement && <Text className='landing-page__agreement-error'>{errors.agreement}</Text>}
<Button
className='landing-page__submit'
hoverClass='landing-page__submit--pressed'
onClick={handleSubmit}
>
</Button>
<View className='landing-page__footer'>
<Text> 9:00-18:00</Text>
<Text>400-800-1234</Text>
<Text></Text>
</View>
</View>
)
}