import { APP_LOGIN, APP_LOGIN_3RD, APP_REGISTER, APP_PROTECT_CHECK, APP_PROTECT_GET, APP_ROUTES, APP_SEND_CODE, APP_GAME_INFO, APP_FORGET_PASS, APP_LOGOUT, APP_CHANGE_INFO, APP_BIND_SMS, APP_CHANGE_BIND_PHONE, APP_BIND_3RD, APP_UNBIND_3RD, APP_UNBIND_3RD_CONFIRM, APP_BIG_WIN, } from "../../api"; import { success } from '@scripts/api/ApiHelper'; import { AppKernel } from "../../app/AppKernel"; import { Login3rdType, APP_LOGIN_TYPE, COUNTRY_CODE, CurrencyType, CURRENCY_TYPE } from "../../define/Define"; import { LOCAL_CURRENCY_MODE, LOCAL_SIGN } from "../../constants/Localkey"; import { ApiEnvelope, Http } from "../../request"; import { getEnterPlatform, localStorageGet, localStorageSet } from "../../utils/Helper"; import { IBigWinItem, IBigWinRes, IForgetPassReq, IProtectInfo, IRoutesResponse, IUserData, IWithdrawChannel, } from "./data"; import { IGoogleLoginRes } from "@scripts/platform/Google/GoogleImpl"; import { trackEvent } from "@scripts/tracking/TrackEvents"; import { AppTracks } from "@scripts/app/AppTrack"; const APP_OTP_INTERVAL = 60; export enum OtpType { Main, ChangeOld, Unbind3rd, } class MainStore { 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 _withdrawChannel: IWithdrawChannel; private _withdrawFeeRate: number; private _serverKick: boolean; private _protectInfo: IProtectInfo = null; private _noticesList: IBigWinItem[] = []; private _currencyKind: CurrencyType; private _lastNoticeIdx = 0; private _otpTsMap: Record = { [OtpType.Main]: 0, [OtpType.ChangeOld]: 0, [OtpType.Unbind3rd]: 0, }; private calcOtpCD(lastTs: number): number { const times = Math.floor((Date.now() - lastTs) / 1000); if (times > APP_OTP_INTERVAL) return 0; return APP_OTP_INTERVAL - times; } public setOtpTs(type: OtpType): void { this._otpTsMap[type] = Date.now(); } public getOtpCD(type: OtpType): number { return this.calcOtpCD(this._otpTsMap[type]); } public set currencyKind(value: CurrencyType) { this._currencyKind = value; localStorageSet(LOCAL_CURRENCY_MODE, value); } public get currencyKind() { return this._currencyKind }; 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 withdrawChannel() { return this._withdrawChannel; } public get withdrawFeeRate() { return this._withdrawFeeRate; } public get serverKick() { return this._serverKick; } public get protectInfo() { return this._protectInfo; } public get scScore() { return this.userInfo?.Score || 0 }; public get gcScore() { return this.userInfo?.InsureScore || 0 }; constructor() { this._currencyKind = localStorageGet(LOCAL_CURRENCY_MODE, CURRENCY_TYPE.GC) as CurrencyType; } 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; } let hallServerUrl = ''; let guest = 0; let upgradeBonus = 0; let registerBonus = 0; let recommendGame = ''; let serviceLink = ""; let WithdrawFeeRate = 5; let withdrawChannel: IWithdrawChannel = null; const conf = res.conf; if (conf) { if (!!conf.hall) hallServerUrl = conf.hall; 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.withdrawChannelNew) withdrawChannel = conf.withdrawChannelNew; if (!!conf.WithdrawFeeRate) WithdrawFeeRate = conf.WithdrawFeeRate; } 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._withdrawChannel = withdrawChannel; this._withdrawFeeRate = WithdrawFeeRate; return true; } async reqRegister({ account, code, password, act }) { const params = { type: APP_LOGIN_TYPE.PHONE, country: 1, phone: account, code: code, password, repassword: password, act, } const res = await Http.call(APP_REGISTER, params); if (success(res)) { this.parseLogin(res.data); } return res; } 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 res; } 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 res; } async reqForgetPass(args: IForgetPassReq) { args.renewpassword = args.newpassword; if (!args.country) args.country = COUNTRY_CODE; const res = await Http.call(APP_FORGET_PASS, args); if (success(res)) { this.parseLogin(res.data); return true; } return false; } private parseLogin(data: IUserData) { if (!data || typeof data !== 'object') return; 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) { // 新注册的 trackEvent(AppTracks.Registered, { external_id: data.UserID, event_id: "reg_" + data.UserID }) } } 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, isChangeOld: boolean = false) { const Type = isChangeOld ? 3 : 2 //换绑发旧手机为3 const cd = isChangeOld ? this.getOtpCD(OtpType.ChangeOld) : this.getOtpCD(OtpType.Main); if (cd > 0) return false; const params = { Phone: account, country: 1, Type } const res = await Http.call(APP_SEND_CODE, params); if (success(res)) { if (isChangeOld) { this.setOtpTs(OtpType.ChangeOld); } else { this.setOtpTs(OtpType.Main); } return true; } return false; } 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; } async reqUserInfo() { const res = await Http.call(APP_GAME_INFO); if (success(res)) { this.parseLogin(res.data); AppKernel.events.emit('user:update'); return true; } return false; } async reqChangeUserInfo(payload: any) { const params = { ...payload }; const res = await Http.call(APP_CHANGE_INFO, params); if (success(res)) { this._userInfo = res.data; AppKernel.events.emit('user:update'); return true; } return false; } updateUserInfo(data: IUserData) { if (!data || typeof data !== 'object') return; this.parseLogin(data); } async reqLogout() { const res = await Http.call(APP_LOGOUT); if (success(res)) { return true; } return false; } onUserLogout() { delete this._userInfo; this._isLogin = false; this._noticesList = []; console.log('mainStore onLogout!!!'); } getNoticeData() { const data = this._noticesList.shift(); if (!!data) this._lastNoticeIdx = data.id; return data; } async reqBindPhoneConfirm({ account, code, password }) { const params = { phone: account, code, password, country: 1 } const res = await Http.call(APP_BIND_SMS, params); if (success(res)) { mainStore.updateUserInfo(res.data); AppKernel.events.emit('user:update'); return true; } return false; } async reqChangePhoneConfirm({ oldCode, newPhone, newCode, password }) { const params = { oldCode, newPhone, newCode, password, country: 1 } const res = await Http.call(APP_CHANGE_BIND_PHONE, params); if (success(res)) { mainStore.updateUserInfo(res.data); AppKernel.events.emit('user:update'); return true; } return false; } async reqBind3rd(type: Login3rdType, token: string) { const params: any = { provider: type, token: token } const res = await Http.call(APP_BIND_3RD, params); if (success(res)) { await this.reqUserInfo(); AppKernel.events.emit('user:update'); } return res; } async reqBindFacebook() { const res = await AppKernel.fb.login(); console.log("facebook bind res: ", res); if (res.accessToken) { return await this.reqBind3rd('facebook', res.accessToken); } return false; } async reqBindGoogle(res?: IGoogleLoginRes) { if (!res) { /// h5 通过dom按钮触发登录 res = await AppKernel.google.login(); } if (res && res.accessToken) { return await this.reqBind3rd('google', res.accessToken); } return true; } async reqUnBind3rdCode({ verify_type }) { const params = { verify_type } const res = await Http.call(APP_UNBIND_3RD, params); if (success(res)) { this.setOtpTs(OtpType.Unbind3rd); return true; } return false; } async reqUnBind3rdConfirm({ verify_type, code }) { const params = { verify_type, code } const res = await Http.call(APP_UNBIND_3RD_CONFIRM, params); if (success(res)) { await this.reqUserInfo(); AppKernel.events.emit('user:update'); return true; } return false; } async reqBigWinNotices() { const params = { currency: CURRENCY_TYPE.SC, lastId: this._lastNoticeIdx, }; const res = await Http.call(APP_BIG_WIN, params); if (success(res)) { let list = res.data.items || []; list = list.sort((a, b) => a.id - b.id) this._noticesList = [...this._noticesList, ...list]; return this._noticesList.length > 0; } return false; } } export const mainStore = new MainStore();