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={ -
- -
+ } > - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + +