From 138f28793fd312fdcd6ca687f9b25e506e749fc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E5=BF=A0=E6=9E=97?= <170083662@qq.com> Date: Mon, 16 Mar 2026 22:12:16 +0800 Subject: [PATCH] =?UTF-8?q?feat(credit):=20=E9=87=8D=E6=9E=84=E5=B0=8F?= =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E7=AB=AF=E5=AE=A2=E6=88=B7=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 优化代码结构,使用函数组件和Hooks替代类组件 - 实现表单验证和错误处理机制 - 添加加载状态和提交防抖保护 - 增加城市选择和文件上传功能 - 实现推荐状态切换和长按删除功能 - 统一错误提示和用户反馈机制 - 优化页面标题和导航体验 --- src/api/credit/creditMpCustomer/index.ts | 13 +- src/credit/creditMpCustomer/add.tsx | 202 ++++++++++++++--------- src/credit/creditMpCustomer/index.tsx | 173 +++++++++++++------ src/credit/order/add.tsx | 94 ++++++++--- 4 files changed, 314 insertions(+), 168 deletions(-) diff --git a/src/api/credit/creditMpCustomer/index.ts b/src/api/credit/creditMpCustomer/index.ts index d3d8edb..70691db 100644 --- a/src/api/credit/creditMpCustomer/index.ts +++ b/src/api/credit/creditMpCustomer/index.ts @@ -1,4 +1,5 @@ import request from '@/utils/request'; +import { withCreditUserScope } from '@/api/credit/utils/data-scope'; import type { ApiResult, PageResult } from '@/api/index'; import type { CreditMpCustomer, CreditMpCustomerParam } from './model'; @@ -8,7 +9,7 @@ import type { CreditMpCustomer, CreditMpCustomerParam } from './model'; export async function pageCreditMpCustomer(params: CreditMpCustomerParam) { const res = await request.get>>( '/credit/credit-mp-customer/page', - params + withCreditUserScope(params) ); if (res.code === 0) { return res.data; @@ -22,7 +23,7 @@ export async function pageCreditMpCustomer(params: CreditMpCustomerParam) { export async function listCreditMpCustomer(params?: CreditMpCustomerParam) { const res = await request.get>( '/credit/credit-mp-customer', - params + withCreditUserScope(params) ); if (res.code === 0 && res.data) { return res.data; @@ -62,6 +63,9 @@ export async function updateCreditMpCustomer(data: CreditMpCustomer) { * 删除小程序端客户 */ export async function removeCreditMpCustomer(id?: number) { + if (id == null) { + return Promise.reject(new Error('缺少id')); + } const res = await request.del>( '/credit/credit-mp-customer/' + id ); @@ -75,11 +79,10 @@ export async function removeCreditMpCustomer(id?: number) { * 批量删除小程序端客户 */ export async function removeBatchCreditMpCustomer(data: (number | undefined)[]) { + const ids = (data || []).filter((v): v is number => typeof v === 'number'); const res = await request.del>( '/credit/credit-mp-customer/batch', - { - data - } + ids ); if (res.code === 0) { return res.message; diff --git a/src/credit/creditMpCustomer/add.tsx b/src/credit/creditMpCustomer/add.tsx index 51cdd3d..6ac0d13 100644 --- a/src/credit/creditMpCustomer/add.tsx +++ b/src/credit/creditMpCustomer/add.tsx @@ -1,98 +1,136 @@ -import {useEffect, useState, useRef} from "react"; -import {useRouter} from '@tarojs/taro' -import {Button, Loading, CellGroup, Input, TextArea, Form} from '@nutui/nutui-react-taro' -import Taro from '@tarojs/taro' -import {View} from '@tarojs/components' -import {CreditMpCustomer} from "@/api/credit/creditMpCustomer/model"; -import {getCreditMpCustomer, listCreditMpCustomer, updateCreditMpCustomer, addCreditMpCustomer} from "@/api/credit/creditMpCustomer"; +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 AddCreditMpCustomer = () => { - const {params} = useRouter(); - const [loading, setLoading] = useState(true) - const [FormData, setFormData] = useState({}) const formRef = useRef(null) - - const reload = async () => { - if (params.id) { - const data = await getCreditMpCustomer(Number(params.id)) - setFormData(data) - } else { - setFormData({}) - } - } - - // 提交表单 - const submitSucceed = async (values: any) => { - try { - if (params.id) { - // 编辑模式 - await updateCreditMpCustomer({ - ...values, - id: Number(params.id) - }) - } else { - // 新增模式 - await addCreditMpCustomer(values) - } - - Taro.showToast({ - title: `操作成功`, - icon: 'success' - }) - - setTimeout(() => { - return Taro.navigateBack() - }, 1000) - } catch (error) { - Taro.showToast({ - title: `操作失败`, - icon: 'error' - }); - } - } - - const submitFailed = (error: any) => { - console.log(error, 'err...') - } + const [loading, setLoading] = useState(true) + const [initialValues, setInitialValues] = useState>({}) + const [submitting, setSubmitting] = useState(false) useEffect(() => { - reload().then(() => { - setLoading(false) - }) - }, []); + 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 加载中 } return ( - <> +
submitSucceed(values)} - onFinishFailed={(errors) => submitFailed(errors)} + onFinish={onFinish} footer={ -
- -
+ } > - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + +