ConsultationRequestForm.tsx 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import React, { useState } from 'react';
  2. import { useForm } from 'react-hook-form';
  3. import { zodResolver } from '@hookform/resolvers/zod';
  4. import { z } from 'zod';
  5. import { Button } from '@/client/components/ui/button';
  6. import { Input } from '@/client/components/ui/input';
  7. import { Textarea } from '@/client/components/ui/textarea';
  8. import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/client/components/ui/card';
  9. import { Label } from '@/client/components/ui/label';
  10. import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/client/components/ui/select';
  11. import { toast } from 'react-toastify';
  12. import { publicConsultationRequestClient } from '@/client/api';
  13. import type { InferRequestType } from 'hono/client';
  14. // 表单验证Schema
  15. const ConsultationRequestFormSchema = z.object({
  16. customerName: z.string().min(2, '姓名至少2个字符').max(255),
  17. companyName: z.string().max(255).optional(),
  18. phone: z.string().regex(/^1[3-9]\d{9}$/, '请输入正确的手机号'),
  19. email: z.string().email('请输入正确的邮箱地址').optional(),
  20. projectType: z.string().min(1, '请选择项目类型'),
  21. projectDescription: z.string().min(10, '项目描述至少10个字符').max(2000),
  22. budgetRange: z.string().max(100).optional(),
  23. timeline: z.string().max(100).optional(),
  24. isGuest: z.boolean().default(true)
  25. });
  26. type ConsultationRequestFormData = z.infer<typeof ConsultationRequestFormSchema>;
  27. // 项目类型选项
  28. const projectTypes = [
  29. { value: '企业ERP系统', label: '企业ERP系统' },
  30. { value: '智慧政务平台', label: '智慧政务平台' },
  31. { value: '医疗信息化系统', label: '医疗信息化系统' },
  32. { value: '教育信息化平台', label: '教育信息化平台' },
  33. { value: '电商平台', label: '电商平台' },
  34. { value: '移动应用开发', label: '移动应用开发' },
  35. { value: '大数据分析平台', label: '大数据分析平台' },
  36. { value: '人工智能应用', label: '人工智能应用' },
  37. { value: '物联网系统', label: '物联网系统' },
  38. { value: '其他', label: '其他' }
  39. ];
  40. // 预算范围选项
  41. const budgetRanges = [
  42. { value: '10万以下', label: '10万以下' },
  43. { value: '10-50万', label: '10-50万' },
  44. { value: '50-100万', label: '50-100万' },
  45. { value: '100-500万', label: '100-500万' },
  46. { value: '500万以上', label: '500万以上' }
  47. ];
  48. // 时间要求选项
  49. const timelines = [
  50. { value: '1-3个月', label: '1-3个月' },
  51. { value: '3-6个月', label: '3-6个月' },
  52. { value: '6-12个月', label: '6-12个月' },
  53. { value: '12个月以上', label: '12个月以上' }
  54. ];
  55. interface ConsultationRequestFormProps {
  56. onSuccess?: () => void;
  57. onCancel?: () => void;
  58. className?: string;
  59. }
  60. export default function ConsultationRequestForm({
  61. onSuccess,
  62. onCancel,
  63. className = ''
  64. }: ConsultationRequestFormProps) {
  65. const [isSubmitting, setIsSubmitting] = useState(false);
  66. const {
  67. register,
  68. handleSubmit,
  69. formState: { errors },
  70. setValue,
  71. watch
  72. } = useForm<ConsultationRequestFormData>({
  73. resolver: zodResolver(ConsultationRequestFormSchema),
  74. defaultValues: {
  75. isGuest: true
  76. }
  77. });
  78. const onSubmit = async (data: ConsultationRequestFormData) => {
  79. setIsSubmitting(true);
  80. try {
  81. const response = await publicConsultationRequestClient.$post({
  82. json: data
  83. });
  84. if (response.status === 200) {
  85. const result = await response.json();
  86. toast.success(result.message || '客户需求提交成功!');
  87. onSuccess?.();
  88. } else {
  89. const error = await response.json();
  90. toast.error(error.message || '提交失败,请稍后重试');
  91. }
  92. } catch (error) {
  93. console.error('提交客户需求失败:', error);
  94. toast.error('网络错误,请检查网络连接后重试');
  95. } finally {
  96. setIsSubmitting(false);
  97. }
  98. };
  99. return (
  100. <Card className={`w-full max-w-2xl mx-auto ${className}`}>
  101. <CardHeader>
  102. <CardTitle className="text-2xl font-bold">项目咨询需求</CardTitle>
  103. <CardDescription>
  104. 请填写您的项目需求信息,我们将尽快与您联系并提供专业的咨询服务
  105. </CardDescription>
  106. </CardHeader>
  107. <CardContent>
  108. <form onSubmit={handleSubmit(onSubmit)} className="space-y-6">
  109. {/* 基本信息 */}
  110. <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
  111. <div className="space-y-2">
  112. <Label htmlFor="customerName">客户姓名 *</Label>
  113. <Input
  114. id="customerName"
  115. placeholder="请输入您的姓名"
  116. {...register('customerName')}
  117. className={errors.customerName ? 'border-red-500' : ''}
  118. />
  119. {errors.customerName && (
  120. <p className="text-sm text-red-500">{errors.customerName.message}</p>
  121. )}
  122. </div>
  123. <div className="space-y-2">
  124. <Label htmlFor="companyName">公司名称</Label>
  125. <Input
  126. id="companyName"
  127. placeholder="请输入公司名称(选填)"
  128. {...register('companyName')}
  129. />
  130. </div>
  131. <div className="space-y-2">
  132. <Label htmlFor="phone">手机号 *</Label>
  133. <Input
  134. id="phone"
  135. placeholder="请输入手机号"
  136. {...register('phone')}
  137. className={errors.phone ? 'border-red-500' : ''}
  138. />
  139. {errors.phone && (
  140. <p className="text-sm text-red-500">{errors.phone.message}</p>
  141. )}
  142. </div>
  143. <div className="space-y-2">
  144. <Label htmlFor="email">邮箱地址</Label>
  145. <Input
  146. id="email"
  147. type="email"
  148. placeholder="请输入邮箱地址(选填)"
  149. {...register('email')}
  150. className={errors.email ? 'border-red-500' : ''}
  151. />
  152. {errors.email && (
  153. <p className="text-sm text-red-500">{errors.email.message}</p>
  154. )}
  155. </div>
  156. </div>
  157. {/* 项目信息 */}
  158. <div className="space-y-4">
  159. <div className="space-y-2">
  160. <Label htmlFor="projectType">项目类型 *</Label>
  161. <Select onValueChange={(value) => setValue('projectType', value)}>
  162. <SelectTrigger className={errors.projectType ? 'border-red-500' : ''}>
  163. <SelectValue placeholder="请选择项目类型" />
  164. </SelectTrigger>
  165. <SelectContent>
  166. {projectTypes.map((type) => (
  167. <SelectItem key={type.value} value={type.value}>
  168. {type.label}
  169. </SelectItem>
  170. ))}
  171. </SelectContent>
  172. </Select>
  173. {errors.projectType && (
  174. <p className="text-sm text-red-500">{errors.projectType.message}</p>
  175. )}
  176. </div>
  177. <div className="space-y-2">
  178. <Label htmlFor="projectDescription">项目描述 *</Label>
  179. <Textarea
  180. id="projectDescription"
  181. placeholder="请详细描述您的项目需求、目标和期望效果..."
  182. rows={4}
  183. {...register('projectDescription')}
  184. className={errors.projectDescription ? 'border-red-500' : ''}
  185. />
  186. {errors.projectDescription && (
  187. <p className="text-sm text-red-500">{errors.projectDescription.message}</p>
  188. )}
  189. <p className="text-sm text-gray-500">
  190. 已输入 {watch('projectDescription')?.length || 0} / 2000 字符
  191. </p>
  192. </div>
  193. <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
  194. <div className="space-y-2">
  195. <Label htmlFor="budgetRange">预算范围</Label>
  196. <Select onValueChange={(value) => setValue('budgetRange', value)}>
  197. <SelectTrigger>
  198. <SelectValue placeholder="请选择预算范围(选填)" />
  199. </SelectTrigger>
  200. <SelectContent>
  201. {budgetRanges.map((range) => (
  202. <SelectItem key={range.value} value={range.value}>
  203. {range.label}
  204. </SelectItem>
  205. ))}
  206. </SelectContent>
  207. </Select>
  208. </div>
  209. <div className="space-y-2">
  210. <Label htmlFor="timeline">时间要求</Label>
  211. <Select onValueChange={(value) => setValue('timeline', value)}>
  212. <SelectTrigger>
  213. <SelectValue placeholder="请选择时间要求(选填)" />
  214. </SelectTrigger>
  215. <SelectContent>
  216. {timelines.map((timeline) => (
  217. <SelectItem key={timeline.value} value={timeline.value}>
  218. {timeline.label}
  219. </SelectItem>
  220. ))}
  221. </SelectContent>
  222. </Select>
  223. </div>
  224. </div>
  225. </div>
  226. {/* 操作按钮 */}
  227. <div className="flex gap-4 pt-4">
  228. <Button
  229. type="submit"
  230. className="flex-1 bg-blue-600 hover:bg-blue-700"
  231. disabled={isSubmitting}
  232. >
  233. {isSubmitting ? '提交中...' : '提交咨询需求'}
  234. </Button>
  235. {onCancel && (
  236. <Button
  237. type="button"
  238. variant="outline"
  239. onClick={onCancel}
  240. disabled={isSubmitting}
  241. >
  242. 取消
  243. </Button>
  244. )}
  245. </div>
  246. <p className="text-sm text-gray-500 text-center">
  247. 提交即表示您同意我们的服务条款和隐私政策
  248. </p>
  249. </form>
  250. </CardContent>
  251. </Card>
  252. );
  253. }