import { useEffect, useMemo, useRef, useState } from 'react' import Taro, { useRouter } from '@tarojs/taro' import { View } from '@tarojs/components' import { Button, CellGroup, Form, Input, Loading, TextArea } from '@nutui/nutui-react-taro' import type { CreditMpCustomer } from '@/api/credit/creditMpCustomer/model' import { addCreditMpCustomer, getCreditMpCustomer, updateCreditMpCustomer } from '@/api/credit/creditMpCustomer' export default function CreditMpCustomerAddPage() { const { params } = useRouter() const id = useMemo(() => { const n = Number(params?.id) return Number.isFinite(n) && n > 0 ? n : undefined }, [params?.id]) const formRef = useRef(null) const [loading, setLoading] = useState(true) const [initialValues, setInitialValues] = useState>({}) const [submitting, setSubmitting] = useState(false) useEffect(() => { Taro.setNavigationBarTitle({ title: id ? '编辑小程序端客户' : '新增小程序端客户' }) }, [id]) useEffect(() => { const run = async () => { setLoading(true) try { if (id) { const data = await getCreditMpCustomer(id) setInitialValues(data || {}) } else { setInitialValues({}) } } catch (e) { console.error('加载失败:', e) Taro.showToast({ title: (e as any)?.message || '加载失败', icon: 'none' }) } finally { setLoading(false) } } run() }, [id]) const onFinish = async (values: any) => { if (submitting) return setSubmitting(true) try { if (id) { await updateCreditMpCustomer({ ...values, id }) } else { await addCreditMpCustomer(values) } Taro.showToast({ title: '操作成功', icon: 'success' }) setTimeout(() => Taro.navigateBack(), 800) } catch (e) { console.error('保存失败:', e) Taro.showToast({ title: (e as any)?.message || '操作失败', icon: 'none' }) } finally { setSubmitting(false) } } if (loading) { return 加载中 } return (
{id ? '更新' : '保存'} } >