Compare commits
2 Commits
e95db8e321
...
a468dc7dd3
| Author | SHA1 | Date |
|---|---|---|
|
|
a468dc7dd3 | |
|
|
309665f49a |
18
src/app.tsx
18
src/app.tsx
|
|
@ -1,5 +1,5 @@
|
||||||
import { PropsWithChildren } from 'react'
|
import { PropsWithChildren } from 'react'
|
||||||
import { useLaunch } from '@tarojs/taro'
|
import Taro, { useLaunch, navigateTo } from '@tarojs/taro'
|
||||||
import { Provider } from 'react-redux'
|
import { Provider } from 'react-redux'
|
||||||
import configStore from './store'
|
import configStore from './store'
|
||||||
|
|
||||||
|
|
@ -21,16 +21,26 @@ function App({ children }: PropsWithChildren<any>) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const authorize = createPlatformFactory().createAuthorize()
|
const authorize = createPlatformFactory().createAuthorize()
|
||||||
// const payment = createPlatformFactory().createPayment()
|
const payment = createPlatformFactory().createPayment()
|
||||||
try {
|
try {
|
||||||
// 检查登录状态,包括OAuth 2.0回调处理
|
// 检查登录状态,包括OAuth 2.0回调处理
|
||||||
const isLoggedIn = await authorize.checkLogin()
|
const isLoggedIn = await authorize.checkLogin()
|
||||||
if (!isLoggedIn) {
|
if (!isLoggedIn) {
|
||||||
// 可以根据需要决定是否自动跳转登录
|
// 可以根据需要决定是否自动跳转登录
|
||||||
await authorize.login()
|
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) {
|
} catch (error) {
|
||||||
console.error('登录检查失败:', error)
|
console.error('登录检查失败:', error)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,17 +3,27 @@ import { Payment } from "../types/payment";
|
||||||
export class H5Payment extends Payment {
|
export class H5Payment extends Payment {
|
||||||
async pay(templateCode: string, imageUrl: string) {
|
async pay(templateCode: string, imageUrl: string) {
|
||||||
const sdk = useServerSdk()
|
const sdk = useServerSdk()
|
||||||
const { hostname, protocol, port } = window.location;
|
const { hostname, protocol, port, hash } = window.location;
|
||||||
let baseUrl = `${protocol}//${hostname}`
|
let baseUrl = `${protocol}//${hostname}`
|
||||||
if (hostname === 'localhost') {
|
if (hostname === 'localhost') {
|
||||||
baseUrl = `${protocol}//${hostname}:${port}`
|
baseUrl = `${protocol}//${hostname}:${port}`
|
||||||
}
|
}
|
||||||
const cbUrl = `${baseUrl}`
|
const response = await sdk.payTemplateCode(templateCode, imageUrl, `${baseUrl}#/${hash}`)
|
||||||
const response = await sdk.payTemplateCode(templateCode, imageUrl, cbUrl)
|
|
||||||
if (response.url) {
|
if (response.url) {
|
||||||
window.location.href = response.url;
|
window.location.href = response.url;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
throw new Error(`payment error: ${response}`)
|
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
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,5 +2,6 @@
|
||||||
|
|
||||||
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 }>;
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue