diff --git a/config/env.ts b/config/env.ts index da3a21b..e9c8506 100644 --- a/config/env.ts +++ b/config/env.ts @@ -15,7 +15,7 @@ export const ENV_CONFIG = { }, // 测试环境 test: { - API_BASE_URL: 'https://mp-api.s209.websoft.top/api', + API_BASE_URL: 'https://mp-api.websoft.top/api', APP_NAME: '测试环境', DEBUG: 'true', } diff --git a/src/api/shop/shopDealerWithdraw/index.ts b/src/api/shop/shopDealerWithdraw/index.ts index 6968031..70ebfdb 100644 --- a/src/api/shop/shopDealerWithdraw/index.ts +++ b/src/api/shop/shopDealerWithdraw/index.ts @@ -2,6 +2,18 @@ import request from '@/utils/request'; import type { ApiResult, PageResult } from '@/api'; import type { ShopDealerWithdraw, ShopDealerWithdrawParam } from './model'; +// WeChat transfer v3: backend may return `package_info` for MiniProgram to open the +// "confirm receipt" page via `wx.requestMerchantTransfer`. +export type ShopDealerWithdrawCreateResult = + | string + | { + package_info?: string; + packageInfo?: string; + [k: string]: any; + } + | null + | undefined; + /** * 分页查询分销商提现明细表 */ @@ -33,13 +45,14 @@ export async function listShopDealerWithdraw(params?: ShopDealerWithdrawParam) { /** * 添加分销商提现明细表 */ -export async function addShopDealerWithdraw(data: ShopDealerWithdraw) { - const res = await request.post>( +export async function addShopDealerWithdraw(data: ShopDealerWithdraw): Promise { + const res = await request.post>( '/shop/shop-dealer-withdraw', data ); if (res.code === 0) { - return res.message; + // Some backends return `message`, while WeChat transfer flow returns `data.package_info`. + return res.data ?? res.message; } return Promise.reject(new Error(res.message)); } diff --git a/src/dealer/withdraw/index.tsx b/src/dealer/withdraw/index.tsx index 72ebc5f..dcf27e8 100644 --- a/src/dealer/withdraw/index.tsx +++ b/src/dealer/withdraw/index.tsx @@ -25,6 +25,62 @@ interface WithdrawRecordWithDetails extends ShopDealerWithdraw { accountDisplay?: string } +const extractPackageInfo = (result: unknown): string | null => { + if (!result || typeof result !== 'object') return null + const r = result as any + return ( + r.package_info ?? + r.packageInfo ?? + r.package ?? + null + ) +} + +const canRequestMerchantTransferConfirm = (): boolean => { + try { + if (typeof (Taro as any).getEnv === 'function' && (Taro as any).ENV_TYPE) { + const env = (Taro as any).getEnv() + if (env !== (Taro as any).ENV_TYPE.WEAPP) return false + } + + const api = + (globalThis as any).wx?.requestMerchantTransfer || + (Taro as any).requestMerchantTransfer + + return typeof api === 'function' + } catch { + return false + } +} + +const requestMerchantTransferConfirm = (packageInfo: string): Promise => { + if (!canRequestMerchantTransferConfirm()) { + return Promise.reject(new Error('请在微信小程序内完成收款确认')) + } + + // Backend may wrap/format base64 with newlines; WeChat API requires a clean string. + const cleanPackageInfo = String(packageInfo).replace(/\s+/g, '') + + const api = + (globalThis as any).wx?.requestMerchantTransfer || + (Taro as any).requestMerchantTransfer + + if (typeof api !== 'function') { + return Promise.reject(new Error('当前环境不支持商家转账收款确认(缺少 requestMerchantTransfer)')) + } + + return new Promise((resolve, reject) => { + api({ + // WeChat API uses `package`, backend returns `package_info`. + package: cleanPackageInfo, + mchId: '1737910695', + appId: 'wxad831ba00ad6a026', + success: (res: any) => resolve(res), + fail: (err: any) => reject(err) + }) + }) +} + // Some backends may return money fields as number; keep internal usage always as string. const normalizeMoneyString = (money: unknown) => { if (money === null || money === undefined || money === '') return '0.00' @@ -237,12 +293,41 @@ const DealerWithdraw: React.FC = () => { withdrawData.bankName = values.bankName || '银行卡' } - await addShopDealerWithdraw(withdrawData) + // WeChat wallet: backend should return `package_info`, frontend opens the "confirm receipt" page + // for user to click "确认收款". + if (values.accountType === 'wechat') { + if (!canRequestMerchantTransferConfirm()) { + throw new Error('当前环境不支持微信收款确认,请在微信小程序内操作') + } - Taro.showToast({ - title: '提现申请已提交', - icon: 'success' - }) + const createResult = await addShopDealerWithdraw(withdrawData) + const packageInfo = extractPackageInfo(createResult) + if (!packageInfo) { + throw new Error('后台未返回 package_info,无法调起微信收款确认页') + } + + try { + await requestMerchantTransferConfirm(packageInfo) + Taro.showToast({ + title: '已调起收款确认页', + icon: 'success' + }) + } catch (e: any) { + const msg = String(e?.errMsg || e?.message || '') + if (/cancel/i.test(msg)) { + Taro.showToast({title: '已取消收款确认', icon: 'none'}) + } else { + // Keep the original WeChat error for troubleshooting (e.g. "商户号错误"). + throw new Error(msg || '调起收款确认页失败,请稍后重试') + } + } + } else { + await addShopDealerWithdraw(withdrawData) + Taro.showToast({ + title: '提现申请已提交', + icon: 'success' + }) + } // 重置表单 formRef.current?.resetFields() @@ -265,7 +350,7 @@ const DealerWithdraw: React.FC = () => { } } - const quickAmounts = ['100', '300', '500', '1000'] + const quickAmounts = ['1','100', '300', '500', '1000'] const setQuickAmount = (amount: string) => { formRef.current?.setFieldsValue({amount}) @@ -411,7 +496,7 @@ const DealerWithdraw: React.FC = () => { {selectedAccount === 'wechat' && ( - 微信钱包提现将直接转入您的微信零钱 + 提交后将拉起微信收款确认页,需要您点击“确认收款”后才会完成转账 )}