diff --git a/src/app.tsx b/src/app.tsx index e7b300a..088394c 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -1,5 +1,5 @@ import { PropsWithChildren } from 'react' -import { useLaunch } from '@tarojs/taro' +import Taro, { useLaunch, navigateTo } from '@tarojs/taro' import { Provider } from 'react-redux' import configStore from './store' @@ -18,9 +18,19 @@ function App({ children }: PropsWithChildren) { if (!isLoggedIn) { // 可以根据需要决定是否自动跳转登录 await authorize.login() + return; + } + // 检查是否是支付跳转页面 + const paymentResult = await payment.checkPaymentResult() + if (paymentResult && paymentResult.paymentId && paymentResult.templateCode) { + console.log({ paymentResult }) + Taro.navigateTo({ + url: `/pages/result/index?payment_id=${paymentResult.paymentId}`, + }); + } else { + const result = await payment.pay(`character_figurine_v1`, `http://gips2.baidu.com/it/u=1674525583,3037683813&fm=3028&app=3028&f=JPEG&fmt=auto?w=1024&h=1024`) + console.log({ result }) } - const result = await payment.pay(`character_figurine_v1`, ``) - console.log({ result }) } catch (error) { console.error('登录检查失败:', error) } diff --git a/src/platforms/h5/payment.ts b/src/platforms/h5/payment.ts index b910836..d40a7bd 100644 --- a/src/platforms/h5/payment.ts +++ b/src/platforms/h5/payment.ts @@ -3,17 +3,27 @@ import { Payment } from "../types/payment"; export class H5Payment extends Payment { async pay(templateCode: string, imageUrl: string) { const sdk = useServerSdk() - const { hostname, protocol, port } = window.location; + const { hostname, protocol, port, hash } = window.location; let baseUrl = `${protocol}//${hostname}` if (hostname === 'localhost') { baseUrl = `${protocol}//${hostname}:${port}` } - const cbUrl = `${baseUrl}` - const response = await sdk.payTemplateCode(templateCode, imageUrl, cbUrl) + const response = await sdk.payTemplateCode(templateCode, imageUrl, `${baseUrl}#/${hash}`) if (response.url) { window.location.href = response.url; return; } throw new Error(`payment error: ${response}`) } + + async checkPaymentResult(): Promise<{ paymentId?: string; templateCode?: string }> { + const urlParams = new URLSearchParams(window.location.search); + const paymentId = urlParams.get('paymentId') || undefined; + const templateCode = urlParams.get('templateCode') || undefined; + + return { + paymentId, + templateCode + }; + } } \ No newline at end of file diff --git a/src/platforms/types/payment.ts b/src/platforms/types/payment.ts index 0acbd24..ed42bb7 100644 --- a/src/platforms/types/payment.ts +++ b/src/platforms/types/payment.ts @@ -1,6 +1,7 @@ -export abstract class Payment{ +export abstract class Payment { - abstract pay(templateCode: string, imageUrl: string): Promise; + abstract pay(templateCode: string, imageUrl: string): Promise; + abstract checkPaymentResult(): Promise<{ paymentId?: string, templateCode?: string }>; } \ No newline at end of file