317 lines
11 KiB
TypeScript
317 lines
11 KiB
TypeScript
import { config } from '../config';
|
||
|
||
class RemoteConfig {
|
||
data: any;
|
||
url: string;
|
||
timer: number | null;
|
||
updateCallbacks: Function[];
|
||
|
||
constructor() {
|
||
this.data = null;
|
||
this.url = "";
|
||
this.timer = null;
|
||
this.updateCallbacks = [];
|
||
|
||
// 优先使用直接配置的完整 URL (如 Gitee/GitHub Raw URL)
|
||
if (config.remoteConfig.configUrl && config.remoteConfig.configUrl.startsWith('http')) {
|
||
this.url = config.remoteConfig.configUrl;
|
||
console.log('RemoteConfig: Using direct configUrl:', this.url);
|
||
} else {
|
||
console.warn('RemoteConfig: Missing configUrl.');
|
||
}
|
||
}
|
||
|
||
isReady(): boolean {
|
||
return this.data !== null;
|
||
}
|
||
|
||
onUpdate(callback: Function) {
|
||
if (typeof callback === 'function') {
|
||
this.updateCallbacks.push(callback);
|
||
}
|
||
}
|
||
|
||
triggerUpdate() {
|
||
this.updateCallbacks.forEach(cb => {
|
||
try {
|
||
cb();
|
||
} catch (e) {
|
||
console.error('Error in remote config update callback:', e);
|
||
}
|
||
});
|
||
}
|
||
|
||
start() {
|
||
this.fetchConfig();
|
||
// 小程序端通常不需要高频轮询,或者可以在 onShow 中触发
|
||
// const interval = 30000;
|
||
// this.timer = setInterval(() => {
|
||
// this.fetchConfig();
|
||
// }, interval);
|
||
}
|
||
|
||
fetchConfig() {
|
||
if (!this.url) return;
|
||
|
||
console.log('Fetching remote config from:', this.url);
|
||
|
||
wx.request({
|
||
url: this.url,
|
||
data: { t: Date.now() },
|
||
success: (res: any) => {
|
||
if (res.statusCode === 200) {
|
||
this.parseData(res.data);
|
||
} else {
|
||
console.error('Remote config fetch failed with status:', res.statusCode);
|
||
// 获取失败,触发更新以便上层处理错误(此时 data 可能为 null)
|
||
this.triggerUpdate();
|
||
}
|
||
},
|
||
fail: (err) => {
|
||
console.error('Error fetching remote config:', err);
|
||
// 网络错误,触发更新以便上层处理错误
|
||
this.triggerUpdate();
|
||
}
|
||
});
|
||
}
|
||
|
||
|
||
|
||
parseData(data: any) {
|
||
let parsedData = null;
|
||
if (typeof data === 'string') {
|
||
try {
|
||
parsedData = JSON.parse(data.trim());
|
||
} catch (e) {
|
||
console.error('Failed to parse config JSON:', e);
|
||
}
|
||
} else {
|
||
parsedData = data;
|
||
}
|
||
|
||
if (parsedData) {
|
||
this.data = parsedData;
|
||
this.triggerUpdate();
|
||
}
|
||
}
|
||
|
||
getParaValue(paraname: string, agentid: string, gameid: string, channelid: string, marketid: number) {
|
||
let paravalue = null;
|
||
|
||
if (!this.data) {
|
||
return paravalue;
|
||
}
|
||
|
||
// 1. Root level
|
||
if (this.data[paraname]) {
|
||
paravalue = this.data[paraname];
|
||
}
|
||
|
||
// Helper to find item in list and update paravalue
|
||
const findAndCheck = (list: any[], key: string, value: any) => {
|
||
if (!list || !Array.isArray(list)) return null;
|
||
for (const item of list) {
|
||
if (item[key] == value) {
|
||
if (item[paraname]) {
|
||
paravalue = item[paraname];
|
||
}
|
||
return item;
|
||
}
|
||
}
|
||
return null;
|
||
};
|
||
|
||
// 2. Agent level
|
||
const agent = findAndCheck(this.data.agentlist, 'agentid', agentid);
|
||
if (!agent) return paravalue;
|
||
|
||
// 3. Game level
|
||
const game = findAndCheck(agent.gamelist, 'gameid', gameid);
|
||
if (!game) return paravalue;
|
||
|
||
// 4. Channel level
|
||
const channel = findAndCheck(game.channellist, 'channelid', channelid);
|
||
if (!channel) return paravalue;
|
||
|
||
// 5. Market level
|
||
const market = findAndCheck(channel.marketlist, 'marketid', marketid);
|
||
|
||
return paravalue;
|
||
}
|
||
|
||
// 获取游戏列表_下载页面
|
||
getGameListDownHtml(agentid: string, channelid: string) {
|
||
const gamelist: any[] = [];
|
||
if (!this.data || !this.data.agentlist) {
|
||
return gamelist;
|
||
}
|
||
|
||
for (const agent of this.data.agentlist) {
|
||
if (agent.agentid == agentid) {
|
||
if (agent.gamelist) {
|
||
for (const game of agent.gamelist) {
|
||
const o_game: any = {
|
||
name: game.gamename,
|
||
image: game.game_down_image,
|
||
state: game.game_down_state,
|
||
memo: game.game_down_memo
|
||
};
|
||
|
||
if (game.channellist) {
|
||
for (const channel of game.channellist) {
|
||
if (channel.channelid == channelid) {
|
||
const _ios_marketid = channel.ios_defdownload_marketid;
|
||
const _and_marketid = channel.and_defdownload_marketid;
|
||
|
||
if (channel.marketlist) {
|
||
for (const market of channel.marketlist) {
|
||
if (market.marketid == _ios_marketid) {
|
||
o_game.ios_down = market.app_download;
|
||
o_game.ios_size = market.app_size;
|
||
o_game.ios_marketid = _ios_marketid;
|
||
}
|
||
if (market.marketid == _and_marketid) {
|
||
o_game.android_down = market.app_download;
|
||
o_game.android_size = market.app_size;
|
||
}
|
||
}
|
||
}
|
||
if (o_game.ios_down && o_game.android_down) {
|
||
gamelist.push(o_game);
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
return gamelist;
|
||
}
|
||
|
||
// 获取游戏列表_游戏大厅
|
||
getGameListGameHall(agentid: string, channelid: string, marketid: number) {
|
||
const gamelist: any[] = [];
|
||
if (!this.data || !this.data.agentlist) {
|
||
return gamelist;
|
||
}
|
||
|
||
for (const agent of this.data.agentlist) {
|
||
if (agent.agentid == agentid) {
|
||
if (agent.gamelist) {
|
||
for (const game of agent.gamelist) {
|
||
if (game.gameid != "G2hw0ubng0zcoI0r4mx3H2yr4GejidwO") {
|
||
const o_game: any = {
|
||
gameid: game.gameid,
|
||
gamename: game.gamename,
|
||
gamedir: game.game_hall_dir,
|
||
gameimage: game.game_hall_image,
|
||
gameversion: game.game_version,
|
||
gamezip: game.game_zip,
|
||
zipsize: game.game_size
|
||
};
|
||
|
||
if (game.channellist) {
|
||
for (const channel of game.channellist) {
|
||
if (channel.channelid == channelid) {
|
||
if (channel.game_version > 0) {
|
||
o_game.gameversion = channel.game_version;
|
||
}
|
||
if (channel.game_zip) {
|
||
o_game.gamezip = channel.game_zip;
|
||
}
|
||
if (channel.game_size) {
|
||
o_game.zipsize = channel.game_size;
|
||
}
|
||
|
||
if (channel.marketlist) {
|
||
for (const market of channel.marketlist) {
|
||
if (market.marketid == marketid) {
|
||
if (market.game_version > 0) {
|
||
o_game.gameversion = market.game_version;
|
||
}
|
||
if (market.game_zip) {
|
||
o_game.gamezip = market.game_zip;
|
||
}
|
||
if (market.game_size) {
|
||
o_game.zipsize = market.game_size;
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
gamelist.push(o_game);
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
break;
|
||
}
|
||
}
|
||
return gamelist;
|
||
}
|
||
|
||
// 获取代理城市列表
|
||
getAgentList(agentid: string, channelid: string) {
|
||
const agentlist: any[] = [];
|
||
if (!this.data || !this.data.agentlist) {
|
||
return agentlist;
|
||
}
|
||
|
||
for (const agent of this.data.agentlist) {
|
||
if (agent.agentid == agentid || agent.relagentid == agentid) {
|
||
const o_agent = {
|
||
agentid: agent.agentid,
|
||
name: agent.agentname,
|
||
channelid: (agent.gamelist && agent.gamelist[0] && agent.gamelist[0].channellist && agent.gamelist[0].channellist[0]) ? agent.gamelist[0].channellist[0].channelid : ''
|
||
};
|
||
agentlist.push(o_agent);
|
||
}
|
||
}
|
||
return agentlist;
|
||
}
|
||
|
||
// 获取子游戏服务器列表
|
||
getGameServerList(agentid: string) {
|
||
const iplist: string[] = [];
|
||
const paraname = "game_server_http";
|
||
|
||
const doPushToIpList = (ip: string) => {
|
||
if (ip && !iplist.includes(ip)) {
|
||
iplist.push(ip);
|
||
}
|
||
};
|
||
|
||
if (!this.data || !this.data.agentlist) {
|
||
return iplist;
|
||
}
|
||
|
||
const agent = this.data.agentlist.find((a: any) => a.agentid === agentid);
|
||
if (!agent) {
|
||
return iplist;
|
||
}
|
||
|
||
if (this.data[paraname]) {
|
||
doPushToIpList(this.data[paraname]);
|
||
}
|
||
if (agent[paraname]) {
|
||
doPushToIpList(agent[paraname]);
|
||
}
|
||
if (agent.gamelist) {
|
||
for (const game of agent.gamelist) {
|
||
if (game[paraname]) {
|
||
doPushToIpList(game[paraname]);
|
||
}
|
||
}
|
||
}
|
||
return iplist;
|
||
}
|
||
}
|
||
|
||
export const remoteConfig = new RemoteConfig();
|