| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415 |
- 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,
- } 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 {
- IForgetPassReq,
- INoticeData,
- IProtectInfo,
- IRoutesResponse,
- IUserData,
- IWithdrawChannel,
- } from "./data";
- const APP_OTP_INTERVAL = 60;
- 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 _landscapeGames: string[];
- private _withdrawChannel: IWithdrawChannel;
- private _serverKick: boolean;
- private _protectInfo: IProtectInfo = null;
- private _lastOtpTs = 0;
- private _noticesList: INoticeData[] = [];
- private _currencyKind: CurrencyType;
- 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 landscapeGames() { return this._landscapeGames; }
- public get withdrawChannel() { return this._withdrawChannel; }
- 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 };
- public get otpCD() {
- const times = Math.floor((Date.now() - this._lastOtpTs) / 1000);
- if (times > APP_OTP_INTERVAL) return 0;
- return APP_OTP_INTERVAL - times;
- }
- 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 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, act }) {
- const params = {
- type: APP_LOGIN_TYPE.PHONE,
- country: 1,
- phone: account,
- code: code,
- password,
- repassword: password,
- act,
- }
- const res = await Http.call<IUserData>(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<IUserData>(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<IUserData>(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<IUserData>(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) {
- // 新注册的
- }
- }
- 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 cd = this.otpCD;
- if (cd > 0) return false;
- const params = {
- Phone: account,
- country: 1,
- }
- const res = await Http.call(APP_SEND_CODE, params);
- if (success(res)) {
- this._lastOtpTs = Date.now();
- return true;
- }
- return false;
- }
- async reqProtectCheck() {
- const res = await Http.call<IProtectInfo[]>(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<ApiEnvelope> {
- 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<IUserData>(APP_GAME_INFO);
- if (success(res)) {
- this.parseLogin(res.data);
- return true;
- }
- return false;
- }
- 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() {
- return this._noticesList.shift();
- }
- }
- export const mainStore = new MainStore();
|