加入服务器的配置设置,可以设置服务器的url+port

This commit is contained in:
2026-03-14 14:10:29 +08:00
parent 0709c07bec
commit 83ce361404
5 changed files with 94 additions and 8 deletions

66
App.tsx
View File

@@ -35,7 +35,7 @@ import {
PaytmBusinessOTPBind,
} from './components/WalletBindComponents';
import Api from './services/api';
import Api, { loadServerDomain, saveServerDomain, getServerDomain } from './services/api';
import { AppProps, WalletmanAppState } from './types';
import { styles } from './styles';
import WebView from "react-native-webview";
@@ -62,6 +62,9 @@ export default class App extends Component<AppProps, WalletmanAppState> {
showMobikwikPersonalBind: false,
showFreechargePersonalBind: false,
proxyStatus: 'idle',
showServerSettings: false,
settingsHost: '',
settingsPort: '',
};
this.deviceId = DeviceInfo.getUniqueIdSync();
@@ -69,6 +72,7 @@ export default class App extends Component<AppProps, WalletmanAppState> {
}
async componentDidMount() {
await loadServerDomain();
await this.setupPermissions();
this.onProxyMessageSub = onProxyMessage((msg) => {
@@ -141,7 +145,7 @@ export default class App extends Component<AppProps, WalletmanAppState> {
wsUrl: Api.WS_URL,
clientId: this.clientId || '',
userId: userId,
// debug: true,
debug: true,
heartbeatInterval: 10000,
reconnectInterval: 5000,
reconnectMaxAttempts: Infinity,
@@ -630,6 +634,60 @@ export default class App extends Component<AppProps, WalletmanAppState> {
return null;
}
openServerSettings = () => {
const domain = getServerDomain();
const colonIdx = domain.lastIndexOf(':');
const host = colonIdx > 0 ? domain.substring(0, colonIdx) : domain;
const port = colonIdx > 0 ? domain.substring(colonIdx + 1) : '';
this.setState({ showServerSettings: true, settingsHost: host, settingsPort: port });
};
saveDomain = async () => {
const { settingsHost, settingsPort } = this.state;
const domain = settingsPort ? `${settingsHost}:${settingsPort}` : settingsHost;
await saveServerDomain(domain);
this.setState({ showServerSettings: false });
Alert.alert('已保存', '重启 App 后生效');
};
renderServerSettingsModal() {
const { showServerSettings, settingsHost, settingsPort } = this.state;
return (
<Modal visible={showServerSettings} transparent animationType="fade">
<View style={{ flex: 1, backgroundColor: 'rgba(0,0,0,0.5)', justifyContent: 'center', alignItems: 'center' }}>
<View style={{ backgroundColor: '#fff', borderRadius: 10, padding: 20, width: '85%' }}>
<Text style={{ fontSize: 16, fontWeight: 'bold', marginBottom: 16 }}></Text>
<Text style={{ fontSize: 13, color: '#666', marginBottom: 4 }}>Host</Text>
<TextInput
style={{ borderWidth: 1, borderColor: '#ddd', borderRadius: 6, paddingHorizontal: 10, paddingVertical: 8, marginBottom: 12, fontSize: 14 }}
value={settingsHost}
onChangeText={t => this.setState({ settingsHost: t })}
placeholder="192.168.1.198"
autoCapitalize="none"
keyboardType="default"
/>
<Text style={{ fontSize: 13, color: '#666', marginBottom: 4 }}>Port</Text>
<TextInput
style={{ borderWidth: 1, borderColor: '#ddd', borderRadius: 6, paddingHorizontal: 10, paddingVertical: 8, marginBottom: 20, fontSize: 14 }}
value={settingsPort}
onChangeText={t => this.setState({ settingsPort: t })}
placeholder="16000"
keyboardType="number-pad"
/>
<View style={{ flexDirection: 'row', justifyContent: 'flex-end' }}>
<TouchableOpacity onPress={() => this.setState({ showServerSettings: false })} style={{ paddingHorizontal: 16, paddingVertical: 8, marginRight: 10 }}>
<Text style={{ color: '#666' }}></Text>
</TouchableOpacity>
<TouchableOpacity onPress={this.saveDomain} style={{ backgroundColor: '#3498db', paddingHorizontal: 16, paddingVertical: 8, borderRadius: 6 }}>
<Text style={{ color: '#fff', fontWeight: 'bold' }}></Text>
</TouchableOpacity>
</View>
</View>
</View>
</Modal>
);
}
render() {
return (
<View style={styles.container}>
@@ -653,6 +711,10 @@ export default class App extends Component<AppProps, WalletmanAppState> {
);
})()}
{this.renderBindModal()}
{this.renderServerSettingsModal()}
<TouchableOpacity onPress={this.openServerSettings} style={{ alignSelf: 'flex-end', paddingHorizontal: 12, paddingVertical: 4 }}>
<Text style={{ fontSize: 12, color: '#3498db' }}> </Text>
</TouchableOpacity>
<View style={[styles.bindButton, { backgroundColor: "transparent", flexDirection: "row", justifyContent: "space-between", alignItems: "center" }]}>
<TouchableOpacity style={[styles.bindButton, { marginBottom: 0, width: '45%', backgroundColor: "#888888" }]} onPress={() => {
this.setState({ showPaytmPersonalBind: true, paytmPersonalBindType: 'otpMode' });

View File

@@ -11,6 +11,7 @@
"postinstall": "patch-package"
},
"dependencies": {
"@react-native-async-storage/async-storage": "^1.23.1",
"@react-native-cookies/cookies": "^6.2.1",
"@react-native-ml-kit/barcode-scanning": "^2.0.0",
"buffer": "^6.0.3",

View File

@@ -1,12 +1,30 @@
import { WalletType } from 'rnwalletman';
import AsyncStorage from '@react-native-async-storage/async-storage';
const DOMAIN = '192.168.1.117:16000';
const BASE_URL = `http://${DOMAIN}`;
const WS_URL = `ws://${DOMAIN}/ws`;
const DEFAULT_DOMAIN = '192.168.1.198:16000';
const STORAGE_KEY = 'server_domain';
let _domain = DEFAULT_DOMAIN;
export async function loadServerDomain(): Promise<string> {
const saved = await AsyncStorage.getItem(STORAGE_KEY);
if (saved) _domain = saved;
return _domain;
}
export async function saveServerDomain(domain: string): Promise<void> {
_domain = domain;
await AsyncStorage.setItem(STORAGE_KEY, domain);
}
export function getServerDomain(): string {
return _domain;
}
class Api {
public static readonly BASE_URL = BASE_URL;
public static readonly WS_URL = WS_URL;
public static get BASE_URL() { return `http://${_domain}`; }
public static get WS_URL() { return `ws://${_domain}/ws`; }
private static _instance: Api | null = null;
private userId: number = 0;

View File

@@ -26,4 +26,9 @@ export interface WalletmanAppState {
/* Proxy 状态 */
proxyStatus: 'idle' | 'connecting' | 'connected' | 'disconnected' | 'error';
proxyError?: string;
/* Server 设置 */
showServerSettings: boolean;
settingsHost: string;
settingsPort: string;
}