import { APP_LOGIN, APP_LOGIN_3RD, APP_REGISTER, APP_FAVO_ADD, APP_FAVO_GET, APP_FAVO_REMOVE, APP_GAME_CARD_INFO, APP_PROTECT_CHECK, APP_PROTECT_GET, APP_ROUTES, success, APP_SEND_CODE, } from "../../api"; import { AppKernel } from "../../app/AppKernel"; import { Login3rdType, APP_LOGIN_TYPE } from "../../define/Define"; import { LOCAL_SIGN } from "../../constants/Localkey"; import { ApiEnvelope, Http } from "../../request"; import { getEnterPlatform, localStorageSet } from "../../utils/Helper"; import { IGameInfo } from "../pageSlice/data"; import { IFavoriteRequest, IProtectInfo, IRoutesResponse, IUserData, IWithdrawChannel, } from "./data"; class MainStore { private _firstLaunch = true; private _hallServerUrl: string; private _isGuest: boolean; private _userInfo: IUserData; private _isLogin: boolean; private _upgradeBonus: number; private _registerBonus: number; private _recommendGame: string; private _serviceLink: string; private _landscapeGames: string[]; private _withdrawChannel: IWithdrawChannel; private _serverKick: boolean; private _favorites: number[] = []; private _favoriteGames: IGameInfo[] = []; private _protectInfo: IProtectInfo = null; private _gameCardInfo = {}; public gameID = 0; public gameLink = ""; public get firstLaunch() { return this._firstLaunch; } public get hallServerUrl() { return this._hallServerUrl; } public get isGuest() { return this._isGuest; } public get userInfo() { return this._userInfo; } public get userID() { return this._userInfo?.UserID ?? 0; } public get isLogin() { return this._isLogin; } public get isBindPhone() { if (!this.userInfo) return false; const phone = this.userInfo.Phone || ''; return phone.startsWith(this.userInfo.RegisterLocation) } public get upgradeBonus() { return this._upgradeBonus; } public get registerBonus() { return this._registerBonus; } public get recommendGame() { return this._recommendGame; } public get serviceLink() { return this._serviceLink; } public get landscapeGames() { return this._landscapeGames; } public get withdrawChannel() { return this._withdrawChannel; } public get serverKick() { return this._serverKick; } public get favorites() { return this._favorites; } public get favoriteGames() { return this._favoriteGames; } public get protectInfo() { return this._protectInfo; } async reqGetRoutes() { const platform = AppKernel.platform; const params: any = { ad_id: platform.getUrlParam("ad_id"), adset_id: platform.getUrlParam("adset_id"), campaign_id: platform.getUrlParam("campaign_id"), pixel: platform.getUrlParam("pixel"), }; if (window.navigator && window.navigator.userAgent) { params.ua = window.navigator.userAgent; } // let ia = getInAppBrowser(); // if (ia != null && ia !== "") { // params.params.ia = ia; // } /// TODO: 所有的启动参数 // let urlvars = global.getUrlvars(); // if (urlvars != null && urlvars !== "") { // params.params.urlvars = urlvars; // } // let pwa = checkIsStandalone() || isTWA(); // if (pwa) { // params.params.pwa = 1; // } if (typeof document !== "undefined") { params.cookie = document.cookie || ""; } params.platform = getEnterPlatform(); const res: IRoutesResponse = await Http.call(APP_ROUTES, params); if (!res || res.code !== 0) { return false; } this._firstLaunch = false; let hallServerUrl = ''; let guest = 0; let upgradeBonus = 0; let registerBonus = 0; let recommendGame = ''; let serviceLink = ""; let landscapeGames = []; let withdrawChannel: IWithdrawChannel = null; const conf = res.conf; if (conf) { if (!!conf.hall) hallServerUrl = conf.hall; // if (!!conf.DOLLAR) global.DOLLAR = conf.DOLLAR; // if (!!conf.currency) global.currency = conf.currency; // global.pf = conf.pf; // console.log(`>>>>>>>> global.pf: ${JSON.stringify(global.pf)}`) // //平台配置 // if (!!conf.pf && !!conf.pf.id) { // let { type, id } = conf.pf; // initTrackAll(type, id, isLogin ? userInfo.UserID : null); // } // //promoteInstall // if (!!conf.promoteInstall) promoteInstall = conf.promoteInstall; //guest if (!!conf.guest) guest = conf.guest; // 绑定手机奖励 if (!!conf.upgradeBonus) upgradeBonus = conf.upgradeBonus; // 初始注册奖励 if (!!conf.registerBonus) registerBonus = conf.registerBonus; ///领取注册奖励后进入的游戏 if (!!conf.recommendGame) recommendGame = conf.recommendGame; //客服地址 if (!!conf.serviceLink) serviceLink = conf.serviceLink; // //弹出转盘 // if (!!conf.popWheel) popWheel = conf.popWheel; // //弹出绑定电话奖励,并显示浮标 // if (!!conf.popBindPhone) popBindPhone = hasPhone ? 0 : conf.popBindPhone; // //显示首冲浮标 // if (!!conf.firstBonus) firstBonus = conf.firstBonus; // //显示救济浮标 // if (!!conf.openRelief) openRelief = conf.openRelief; // //sport 配置 if (!!conf.bb) { // bbConf = conf.bb; // if (userInfo != null) userInfo.bb = bbConf; } // if (!!conf.loginOpen) loginOpen = conf.loginOpen; // if (!!conf.registerOpen) registerOpen = conf.registerOpen; // if (!!conf.popFirst) popFirst = conf.popFirst; // if (!!conf.showInstall) showInstall = conf.showInstall; // //download 配置 // if (!!conf.download) download = conf.download; // if (!!conf.vipConfig) vipConfig = conf.vipConfig; if (!!conf.LandscapeGames) landscapeGames = conf.LandscapeGames; // // adjust相关配置 // if (!!conf.AdjustToken && !!conf.AdjustConfig) global.tracker.initAdjust(conf.AdjustToken, conf.AdjustConfig); // if (!!conf.popPwaBonus) popPwaBonus = conf.popPwaBonus; // if (!!conf.outLimit) outLimit = conf.outLimit; if (!!conf.withdrawChannel) withdrawChannel = conf.withdrawChannelNew; // if (!!conf.freeChannel) freeChannel = conf.freeChannel; // if (!!conf.slotsPartner) slotsPartner = conf.slotsPartner; // if (!!conf.sharePop) sharePop = conf.sharePop; // googleRedirectH5 = parseGoogleRedirectH5Conf(conf); // googleRedirectH5Bonus = parseGoogleRedirectH5BonusConf(conf); // if (conf.jumpapk === 1 || conf.jumpapk === "1" || conf.jumpapk === 2 || conf.jumpapk === "2" || conf.jumpapk === 3 || conf.jumpapk === "3") { // googleRedirectJumpApk = Number(conf.jumpapk); // localStorageSet(LOCAL_GOOGLE_REDIRECT_JUMPAPK, conf.jumpapk); // } else { // localStorageSet(LOCAL_GOOGLE_REDIRECT_JUMPAPK, ""); // } // if (conf.jumplater != null) jumplater = Number(conf.jumplater) || 0; // if (conf.weight1234 != null) fbGuideWeight = conf.weight1234; // // 注册后自动进游戏 // if (conf.isAutoEnterGame != null) isAutoEnterGame = conf.isAutoEnterGame; // // 强制引导开关 // if (conf.jumpios != null) jumpToH5 = conf.jumpios; // // 内置客服JS // if (!!conf.servicejs) SsqHelp.setServicejs(conf.servicejs); } const user = res.user; this.parseLogin(user); this._hallServerUrl = hallServerUrl; this._isGuest = guest === 1; this._upgradeBonus = upgradeBonus; this._registerBonus = registerBonus; this._recommendGame = recommendGame; this._serviceLink = serviceLink; this._landscapeGames = landscapeGames; this._withdrawChannel = withdrawChannel; return true; } async reqRegister({ account, code, password }) { const params = { type: APP_LOGIN_TYPE.PHONE, country: 1, phone: account, code: code, password, repassword: password, } const res = await Http.call(APP_REGISTER, params); if (success(res)) { this.parseLogin(res.data); return true; } return false; } async reqLogin(arg) { const loginType = arg.type; // const lang = global.i18n?.getCurrLang?.() || ""; const params: any = { type: loginType, password: arg?.password, platform: getEnterPlatform(), // lang } switch (loginType) { case APP_LOGIN_TYPE.ID: params.account = arg.account; break; case APP_LOGIN_TYPE.PHONE: params.country = arg.country; params.account = arg.phone; break; case APP_LOGIN_TYPE.MAIL: params.account = arg.email; break; case APP_LOGIN_TYPE.GUEST: delete params.password; break; } const res = await Http.call(APP_LOGIN, params) if (success(res)) { this.parseLogin(res.data); return true; } return false; } async reqLogin3rd(type: Login3rdType, token: string) { const params: any = { provider: type, token: token } const res = await Http.call(APP_LOGIN_3RD, params); if (success(res)) { this.parseLogin(res.data); return true; } return false; } private parseLogin(data: IUserData) { if (!data || typeof data !== 'object') return; this.gameID = 0; this._isLogin = true; this._userInfo = data; this._serverKick = false; if (!!data.recommendGame) this._recommendGame = data.recommendGame; if (!!data.sign) localStorageSet(LOCAL_SIGN, data.sign); if (data.reg === 1) { // 新注册的 } } clearGame() { this.gameID = 0; this.gameLink = ""; } setServerKick(value: boolean) { this._serverKick = value; } updateHallBalance(balance: number) { if (!this._userInfo || !Number.isFinite(balance)) return; if ((this._userInfo.vip || 0) > 0) { this._userInfo.Score = balance; } else { this._userInfo.InsureScore = balance; } } async reqSendCode(account: string) { const params = { Phone: account, country: 1, } const res = await Http.call(APP_SEND_CODE, params); return success(res); } async reqFavoAdd(id: number) { const res = await Http.call(APP_FAVO_ADD, { id }); if (success(res)) this.addFavorite(id); return res; } async reqFavoRemove(id: number) { const res = await Http.call(APP_FAVO_REMOVE, { id }); if (success(res)) this.removeFavorite(id); return res; } async reqUpdateFavoInfo(arg: IFavoriteRequest) { return arg.like === 1 ? this.reqFavoAdd(arg.id) : this.reqFavoRemove(arg.id); } async reqFavoGet() { const res = await Http.call(APP_FAVO_GET); if (!success(res)) return res; this._favoriteGames = Array.isArray(res.data) ? res.data : []; this._favorites = this._favoriteGames .map(item => Number(item?.id)) .filter(id => Number.isFinite(id)); return res; } async reqProtectCheck() { const res = await Http.call(APP_PROTECT_CHECK); if (success(res) && Array.isArray(res.data) && res.data[0]) { const info = { ...res.data[0] }; info.TotalReward = Number(info.total_recharge || 0) - Number(info.can_receive_num || 0); this._protectInfo = info; } return res; } async reqProtectGet(): Promise { const res = await Http.call(APP_PROTECT_GET); if (success(res) && this._protectInfo) { this._protectInfo.CurProtectTimes = Number(this._protectInfo.CurProtectTimes || 0) + 1; } return res; } private addFavorite(id: number) { const favoriteId = Number(id); if (!Number.isFinite(favoriteId) || this._favorites.includes(favoriteId)) return; this._favorites = [...this._favorites, favoriteId]; } private removeFavorite(id: number) { const favoriteId = Number(id); this._favorites = this._favorites.filter(item => item !== favoriteId); this._favoriteGames = this._favoriteGames.filter(item => Number(item?.id) !== favoriteId); } async getGameCardInfo(gameId: number) { if (this._gameCardInfo[gameId]) { return this._gameCardInfo[gameId]; } const payload = await Http.call(APP_GAME_CARD_INFO, { id: gameId }) if (!success(payload) || !payload.data) { return null; } const gameInfo = payload.data; const id = Number(gameInfo.id); const level = gameInfo.level_info ? 1 : 0; const cardInfo = { ...gameInfo, level }; this._gameCardInfo[id] = cardInfo; return cardInfo; } } export const mainStore = new MainStore();