支付流程重新设计枚举值更新传参方式
This commit is contained in:
@@ -1,26 +1,36 @@
|
||||
import {StaticImageData} from 'next/image'
|
||||
import {useEffect, useState} from 'react'
|
||||
import zod from 'zod'
|
||||
import wechat from '@/components/composites/purchase/_assets/wechat.svg'
|
||||
import alipay from '@/components/composites/purchase/_assets/alipay.svg'
|
||||
|
||||
export const TradeMethodDecoration = {
|
||||
alipay: {
|
||||
text: '支付宝',
|
||||
icon: alipay,
|
||||
},
|
||||
wechat: {
|
||||
text: '微信支付',
|
||||
icon: wechat,
|
||||
},
|
||||
}
|
||||
|
||||
// 支付方法枚举
|
||||
export const PaymentMethod = {
|
||||
export const TradeMethod = {
|
||||
Alipay: 1,
|
||||
WeChat: 2,
|
||||
Wechat: 2,
|
||||
Sft: 3,
|
||||
SftAlipay: 4,
|
||||
SftWeChat: 5,
|
||||
SftWechat: 5,
|
||||
} as const
|
||||
|
||||
// 平台枚举
|
||||
export const Platform = {
|
||||
export const TradePlatform = {
|
||||
Desktop: 1,
|
||||
Mobile: 2,
|
||||
} as const
|
||||
|
||||
// 支付状态枚举
|
||||
export const PaymentStatus = {
|
||||
export const TradeStatus = {
|
||||
Pending: 0,
|
||||
Completed: 1,
|
||||
Cancelled: 2, // 同步修改
|
||||
@@ -28,82 +38,27 @@ export const PaymentStatus = {
|
||||
} as const
|
||||
|
||||
// 定义类型
|
||||
export type PaymentMethod = typeof PaymentMethod[keyof typeof PaymentMethod]
|
||||
export type Platform = typeof Platform[keyof typeof Platform]
|
||||
export type PaymentStatus = typeof PaymentStatus[keyof typeof PaymentStatus]
|
||||
export type TradeMethod = typeof TradeMethod[keyof typeof TradeMethod]
|
||||
export type TradePlatform = typeof TradePlatform[keyof typeof TradePlatform]
|
||||
export type TradeStatus = typeof TradeStatus[keyof typeof TradeStatus]
|
||||
|
||||
// 支付方法配置类型
|
||||
export type PaymentMethodConfig = {
|
||||
name: string
|
||||
icon: StaticImageData | null
|
||||
formValue: string
|
||||
availablePlatforms: Platform[]
|
||||
getActualMethod: (platform: Platform) => PaymentMethod
|
||||
}
|
||||
|
||||
// 支付方法配置
|
||||
export const PAYMENT_METHODS: Record<string, PaymentMethodConfig> = {
|
||||
alipay: {
|
||||
name: '支付宝',
|
||||
icon: alipay,
|
||||
formValue: 'alipay',
|
||||
availablePlatforms: [Platform.Desktop, Platform.Mobile],
|
||||
getActualMethod: platform =>
|
||||
platform === Platform.Desktop ? PaymentMethod.Sft : PaymentMethod.SftAlipay,
|
||||
},
|
||||
wechat: {
|
||||
name: '微信支付',
|
||||
icon: wechat,
|
||||
formValue: 'wechat',
|
||||
availablePlatforms: [Platform.Desktop, Platform.Mobile],
|
||||
getActualMethod: platform =>
|
||||
platform === Platform.Desktop ? PaymentMethod.Sft : PaymentMethod.SftWeChat,
|
||||
},
|
||||
sft: {
|
||||
name: '商福通',
|
||||
icon: null,
|
||||
formValue: 'sft',
|
||||
availablePlatforms: [],
|
||||
getActualMethod: () => PaymentMethod.Sft,
|
||||
},
|
||||
} as const
|
||||
|
||||
// 交易信息类型
|
||||
export interface Trade {
|
||||
inner_no: string
|
||||
method: PaymentMethod
|
||||
pay_url: string
|
||||
status?: PaymentStatus
|
||||
amount?: number
|
||||
created_at?: string
|
||||
expired_at?: string
|
||||
}
|
||||
|
||||
// 支付请求参数类型
|
||||
export type PaymentRequest = {
|
||||
amount: string
|
||||
platform: Platform
|
||||
method: PaymentMethod
|
||||
product_id?: string
|
||||
product_name?: string
|
||||
}
|
||||
|
||||
// 支付结果类型
|
||||
export type PaymentResult = {
|
||||
success: boolean
|
||||
trade_no: string
|
||||
message?: string
|
||||
payment_time?: string
|
||||
value: TradeMethod
|
||||
decoration: {
|
||||
icon: StaticImageData
|
||||
text: string
|
||||
}
|
||||
}
|
||||
|
||||
// 设备检测Hook
|
||||
export const usePlatformType = (): Platform => {
|
||||
export const usePlatformType = (): TradePlatform => {
|
||||
// 在SSR环境下返回默认值
|
||||
const [platform, setPlatform] = useState<Platform>(() => {
|
||||
if (typeof window === 'undefined') return Platform.Desktop
|
||||
const [platform, setPlatform] = useState<TradePlatform>(() => {
|
||||
if (typeof window === 'undefined') return TradePlatform.Desktop
|
||||
return window.matchMedia('(max-width: 768px)').matches
|
||||
? Platform.Mobile
|
||||
: Platform.Desktop
|
||||
? TradePlatform.Mobile
|
||||
: TradePlatform.Desktop
|
||||
})
|
||||
|
||||
useEffect(() => {
|
||||
@@ -112,7 +67,7 @@ export const usePlatformType = (): Platform => {
|
||||
|
||||
const checkPlatform = () => {
|
||||
const isMobile = window.matchMedia('(max-width: 768px)').matches
|
||||
setPlatform(isMobile ? Platform.Mobile : Platform.Desktop)
|
||||
setPlatform(isMobile ? TradePlatform.Mobile : TradePlatform.Desktop)
|
||||
}
|
||||
|
||||
const mediaQuery = window.matchMedia('(max-width: 768px)')
|
||||
@@ -125,21 +80,3 @@ export const usePlatformType = (): Platform => {
|
||||
|
||||
return platform
|
||||
}
|
||||
|
||||
// 支付表单验证schema
|
||||
export const paymentSchema = zod.object({
|
||||
method: zod.enum(['alipay', 'wechat', 'sft', 'sftAlipay', 'sftWeChat']),
|
||||
amount: zod.number().min(1, '充值金额必须大于 0'),
|
||||
})
|
||||
|
||||
export type PaymentSchema = zod.infer<typeof paymentSchema>
|
||||
// 新增函数:根据PaymentMethod获取展示信息
|
||||
export const getPaymentMethodInfo = (method: PaymentMethod) => {
|
||||
const found = Object.values(PAYMENT_METHODS).find(
|
||||
config => config.getActualMethod(Platform.Mobile) === method,
|
||||
)
|
||||
|
||||
return found
|
||||
? {icon: found.icon, name: found.name}
|
||||
: {icon: null, name: '未知支付方式'}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user