feat: debug h5

This commit is contained in:
imeepos 2025-09-28 13:30:46 +08:00
parent 95f58aa70e
commit 309665f49a
3 changed files with 29 additions and 8 deletions

View File

@ -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<any>) {
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)
}

View File

@ -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
};
}
}

View File

@ -1,6 +1,7 @@
export abstract class Payment{
export abstract class Payment {
abstract pay(templateCode: string, imageUrl: string): Promise<any>;
abstract pay(templateCode: string, imageUrl: string): Promise<void>;
abstract checkPaymentResult(): Promise<{ paymentId?: string, templateCode?: string }>;
}