From 2f4241951d273ea7000e985f53fc56eb93f8bc2b Mon Sep 17 00:00:00 2001 From: TQCasey <494294315@qq.com> Date: Fri, 6 Feb 2026 17:48:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=A3=E7=90=86=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.tsx | 59 +++++++++---------- android/app/src/main/AndroidManifest.xml | 3 + libs/rnwalletman | 2 +- package.json | 5 +- ...eact-native-background-actions+4.0.1.patch | 15 +++++ services/api.ts | 7 ++- 6 files changed, 58 insertions(+), 33 deletions(-) create mode 100644 patches/react-native-background-actions+4.0.1.patch diff --git a/App.tsx b/App.tsx index 6bb10de..a931ee4 100644 --- a/App.tsx +++ b/App.tsx @@ -29,7 +29,7 @@ import { PhonePePersonalBind, SmsMessage, NotificationMessage, - proxyManager, + proxyBackgroundService, } from "rnwalletman"; import BarcodeScanning from '@react-native-ml-kit/barcode-scanning'; import RNFS from 'react-native-fs'; @@ -71,18 +71,21 @@ export default class App extends Component { } async componentDidMount() { - // 先登录获取 userId - try { - const userId = await Api.instance.login('test123', '123456'); - console.log('[登录成功] userId:', userId); - } catch (error) { - console.error('[登录失败]', error); - Alert.alert('登录失败', String(error)); - return; - } - await this.setupPermissions(); - await this.initProxyClient(); + + // Login with auto-retry every 3s until success (setTimeout, non-blocking) + const doLogin = () => { + Api.instance.login('test123', '123456') + .then(async (userId) => { + console.log('[Login] userId:', userId); + await this.startProxyClient(); + }) + .catch((error) => { + console.log('[Login] retry in 3s:', error); + setTimeout(doLogin, 3000); + }); + }; + doLogin(); this.appStateSubscription = AppState.addEventListener('change', this.handleAppStateChange); } @@ -94,11 +97,9 @@ export default class App extends Component { handleAppStateChange = (nextAppState: AppStateStatus) => { if (nextAppState === 'background' || nextAppState === 'inactive') { - console.log('[AppState] 应用进入后台,关闭代理'); - this.stopProxyClient(); + console.log('[AppState] 应用进入后台,代理服务继续运行'); } else if (nextAppState === 'active') { - console.log('[AppState] 应用回到前台,重连代理'); - this.initProxyClient(); + console.log('[AppState] 应用回到前台'); } } @@ -121,32 +122,30 @@ export default class App extends Component { }); } - async initProxyClient() { + async startProxyClient() { try { this.clientId = DeviceInfo.getUniqueIdSync(); const userId = Api.instance.getUserId(); - console.log('[Proxy] 初始化客户端:', this.clientId, 'userId:', userId); + const { clientId } = this; + console.log('[Proxy] 初始化后台服务:', clientId, 'userId:', userId); - await proxyManager.start({ - wsUrl: 'ws://192.168.1.117:16001/ws', + await proxyBackgroundService.start({ + wsUrl: Api.WS_URL, clientId: this.clientId || '', userId: userId, - debug: true, + // debug: true, heartbeatInterval: 10000, reconnectInterval: 5000, - reconnectMaxAttempts: 3, + reconnectMaxAttempts: Infinity, onConnected: () => { - console.log('[Proxy] 客户端已连接'); + console.log('[Proxy] 后台服务已连接'); }, onDisconnected: () => { - console.log('[Proxy] 客户端已断开'); + console.log('[Proxy] 后台服务已断开'); }, onError: (error: string) => { console.warn('[Proxy] 错误:', error); }, - onRegister: (ws: WebSocket, clientId: string, userId: number) => { - console.log('[Proxy] 客户端已注册:', clientId, userId); - }, }); } catch (error) { console.error('[Proxy] 初始化失败:', error); @@ -155,10 +154,10 @@ export default class App extends Component { stopProxyClient() { try { - proxyManager.stop(); - console.log('[Proxy] 客户端已断开'); + proxyBackgroundService.stop(); + console.log('[Proxy] 后台服务已停止'); } catch (error) { - console.error('[Proxy] 断开失败:', error); + console.error('[Proxy] 停止失败:', error); } } diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 7b072b0..5cebfaa 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -1,6 +1,9 @@ + + + = Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { +- contentIntent = PendingIntent.getActivity(context,0, notificationIntent, PendingIntent.FLAG_MUTABLE | PendingIntent.FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT); ++ if (Build.VERSION.SDK_INT >= 34) { // Android 14 (UPSIDE_DOWN_CAKE) ++ contentIntent = PendingIntent.getActivity(context,0, notificationIntent, PendingIntent.FLAG_MUTABLE | 0x01000000); // FLAG_ALLOW_UNSAFE_IMPLICIT_INTENT + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { + contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_MUTABLE); + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { diff --git a/services/api.ts b/services/api.ts index 6344962..6178f67 100644 --- a/services/api.ts +++ b/services/api.ts @@ -1,7 +1,12 @@ import { WalletType } from 'rnwalletman'; +const DOMAIN = '192.168.1.168:16000'; +const BASE_URL = `http://${DOMAIN}`; +const WS_URL = `ws://${DOMAIN}/ws`; + class Api { - public static readonly BASE_URL = 'http://192.168.1.117:16000'; + public static readonly BASE_URL = BASE_URL; + public static readonly WS_URL = WS_URL; private static _instance: Api | null = null; private userId: number = 0;