safepay.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { Colors } from "@/constants/Colors";
  2. import { useLocalSearchParams, useRouter } from "expo-router";
  3. import { useEffect } from "react";
  4. import {
  5. ActivityIndicator,
  6. AppState,
  7. StyleSheet,
  8. Text,
  9. View,
  10. } from "react-native";
  11. /**
  12. * Alipay Callback Handler
  13. * Handles the "asios://safepay" deep link from Alipay.
  14. * Automatically goes back to invoke the native SDK's callback handling.
  15. */
  16. export default function AlipayResult() {
  17. const router = useRouter();
  18. const params = useLocalSearchParams();
  19. useEffect(() => {
  20. console.log("Alipay callback params:", params);
  21. let hasNavigated = false;
  22. let fallbackTimer: ReturnType<typeof setTimeout>;
  23. const navigateBack = () => {
  24. if (hasNavigated) return;
  25. hasNavigated = true;
  26. if (router.canGoBack()) {
  27. router.back();
  28. } else {
  29. router.replace("/");
  30. }
  31. };
  32. // Listen for AppState changes to know when user actually returns to the app
  33. const subscription = AppState.addEventListener("change", (nextAppState) => {
  34. if (nextAppState === "active") {
  35. // App has returned to foreground. Give a tiny delay for React Native to settle.
  36. setTimeout(navigateBack, 500);
  37. }
  38. });
  39. // Fallback: if AppState doesn't trigger for some reason, navigate after a max delay (e.g., 2000ms)
  40. fallbackTimer = setTimeout(navigateBack, 2000);
  41. return () => {
  42. subscription.remove();
  43. clearTimeout(fallbackTimer);
  44. };
  45. }, []);
  46. return (
  47. <View style={styles.container}>
  48. <ActivityIndicator size="large" color={Colors.neonBlue || "#00F3FF"} />
  49. <Text style={styles.text}>正在返回应用...</Text>
  50. </View>
  51. );
  52. }
  53. const styles = StyleSheet.create({
  54. container: {
  55. flex: 1,
  56. justifyContent: "center",
  57. alignItems: "center",
  58. backgroundColor: Colors.darkBg || "#121212",
  59. },
  60. text: {
  61. marginTop: 20,
  62. color: Colors.textSecondary || "#aaa",
  63. fontSize: 14,
  64. },
  65. });