feat: 初始化居家上门服务系统完整项目代码
- Spring Boot 后端服务 (hss-home-service) - delivery-miniapp 配送小程序 - website 官网 (Nuxt) - docs 架构设计文档 - Docker 容器化部署配置 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
101
hss-home-service/website/composables/useLeadForm.ts
Normal file
101
hss-home-service/website/composables/useLeadForm.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import { ref } from 'vue'
|
||||
|
||||
type LeadType = 'demo' | 'download' | 'contact'
|
||||
|
||||
type ApiResponse<T = unknown> = {
|
||||
code: number | string
|
||||
message: string
|
||||
data?: T
|
||||
requestId?: string
|
||||
timestamp?: string | number
|
||||
}
|
||||
|
||||
export type LeadForm = {
|
||||
name: string
|
||||
orgName: string
|
||||
phone: string
|
||||
city?: string
|
||||
position?: string
|
||||
focusArea?: string
|
||||
contact?: string
|
||||
message?: string
|
||||
type: LeadType
|
||||
extra?: Record<string, string>
|
||||
}
|
||||
|
||||
function isSuccess(code: number | string): boolean {
|
||||
return code === 200 || code === '200' || code === 'SUCCESS'
|
||||
}
|
||||
|
||||
function validatePhone(phone: string): boolean {
|
||||
return /^1[3-9]\d{9}$/.test(phone)
|
||||
}
|
||||
|
||||
export function useLeadForm(type: LeadType) {
|
||||
const config = useRuntimeConfig()
|
||||
|
||||
const loading = ref(false)
|
||||
const success = ref(false)
|
||||
const error = ref('')
|
||||
|
||||
const form = ref<LeadForm>({
|
||||
name: '',
|
||||
orgName: '',
|
||||
phone: '',
|
||||
type
|
||||
})
|
||||
|
||||
async function submit() {
|
||||
error.value = ''
|
||||
|
||||
if (!form.value.name.trim()) {
|
||||
error.value = '请填写姓名'
|
||||
return
|
||||
}
|
||||
if (!form.value.orgName.trim()) {
|
||||
error.value = '请填写单位名称'
|
||||
return
|
||||
}
|
||||
if (!validatePhone(form.value.phone)) {
|
||||
error.value = '请填写正确的手机号'
|
||||
return
|
||||
}
|
||||
|
||||
loading.value = true
|
||||
|
||||
try {
|
||||
if (config.public.useMockLead) {
|
||||
await new Promise(resolve => setTimeout(resolve, 600))
|
||||
success.value = true
|
||||
loading.value = false
|
||||
return
|
||||
}
|
||||
|
||||
const res = await $fetch<ApiResponse>(`${config.public.apiPrefix}/leads`, {
|
||||
method: 'POST',
|
||||
body: {
|
||||
...form.value,
|
||||
source: 'official_website',
|
||||
submittedAt: new Date().toISOString()
|
||||
}
|
||||
})
|
||||
|
||||
if (isSuccess(res.code)) {
|
||||
success.value = true
|
||||
} else {
|
||||
error.value = res.message || '提交失败,请稍后重试'
|
||||
}
|
||||
} catch (e: any) {
|
||||
error.value = e?.data?.message || e?.message || '网络异常,请稍后重试'
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function reset() {
|
||||
success.value = false
|
||||
error.value = ''
|
||||
}
|
||||
|
||||
return { form, loading, success, error, submit, reset }
|
||||
}
|
||||
Reference in New Issue
Block a user