This commit is contained in:
2026-03-15 11:45:11 +08:00
parent 619143323b
commit 668bfeda35
5 changed files with 39 additions and 12 deletions

View File

@@ -1,29 +1,48 @@
import { WalletType } from 'rnwalletman';
import AsyncStorage from '@react-native-async-storage/async-storage';
const DEFAULT_DOMAIN = '192.168.1.198:16000';
const DEFAULT_DOMAIN = 'aa.pfgame.org';
const STORAGE_KEY = 'server_domain';
const HTTPS_KEY = 'server_https';
let _domain = DEFAULT_DOMAIN;
let _useHttps = true;
export async function loadServerDomain(): Promise<string> {
const saved = await AsyncStorage.getItem(STORAGE_KEY);
if (saved) _domain = saved;
const https = await AsyncStorage.getItem(HTTPS_KEY);
if (https !== null) _useHttps = https === 'true';
console.log('loadServerDomain', _domain, 'https:', _useHttps);
return _domain;
}
export async function saveServerDomain(domain: string): Promise<void> {
export async function saveServerDomain(domain: string, useHttps: boolean): Promise<void> {
_domain = domain;
_useHttps = useHttps;
await AsyncStorage.setItem(STORAGE_KEY, domain);
await AsyncStorage.setItem(HTTPS_KEY, String(useHttps));
}
export function getServerDomain(): string {
return _domain;
}
export function getUseHttps(): boolean {
return _useHttps;
}
class Api {
public static get BASE_URL() { return `http://${_domain}`; }
public static get WS_URL() { return `ws://${_domain}/ws`; }
public static get BASE_URL() {
const domain = getServerDomain();
const scheme = getUseHttps() ? 'https' : 'http';
return `${scheme}://${domain}`;
}
public static get WS_URL() {
const domain = getServerDomain();
const scheme = getUseHttps() ? 'wss' : 'ws';
return `${scheme}://${domain}/ws`;
}
private static _instance: Api | null = null;
private userId: number = 0;
@@ -54,6 +73,7 @@ class Api {
}
public async login(username: string, password: string): Promise<number> {
console.log('login', Api.BASE_URL);
const res = await fetch(`${Api.BASE_URL}/login`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },