MainStore.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. import {
  2. APP_LOGIN,
  3. APP_LOGIN_3RD,
  4. APP_REGISTER,
  5. APP_FAVO_ADD,
  6. APP_FAVO_GET,
  7. APP_FAVO_REMOVE,
  8. APP_GAME_CARD_INFO,
  9. APP_PROTECT_CHECK,
  10. APP_PROTECT_GET,
  11. APP_ROUTES,
  12. success,
  13. APP_SEND_CODE,
  14. } from "../../api";
  15. import { AppKernel } from "../../app/AppKernel";
  16. import { Login3rdType, APP_LOGIN_TYPE } from "../../define/Define";
  17. import { LOCAL_SIGN } from "../../constants/Localkey";
  18. import { ApiEnvelope, Http } from "../../request";
  19. import { getEnterPlatform, localStorageSet } from "../../utils/Helper";
  20. import { IGameInfo } from "../pageSlice/data";
  21. import {
  22. IFavoriteRequest,
  23. IProtectInfo,
  24. IRoutesResponse,
  25. IUserData,
  26. IWithdrawChannel,
  27. } from "./data";
  28. class MainStore {
  29. private _firstLaunch = true;
  30. private _hallServerUrl: string;
  31. private _isGuest: boolean;
  32. private _userInfo: IUserData;
  33. private _isLogin: boolean;
  34. private _upgradeBonus: number;
  35. private _registerBonus: number;
  36. private _recommendGame: string;
  37. private _serviceLink: string;
  38. private _landscapeGames: string[];
  39. private _withdrawChannel: IWithdrawChannel;
  40. private _serverKick: boolean;
  41. private _favorites: number[] = [];
  42. private _favoriteGames: IGameInfo[] = [];
  43. private _protectInfo: IProtectInfo = null;
  44. private _gameCardInfo = {};
  45. public gameID = 0;
  46. public gameLink = "";
  47. public get firstLaunch() { return this._firstLaunch; }
  48. public get hallServerUrl() { return this._hallServerUrl; }
  49. public get isGuest() { return this._isGuest; }
  50. public get userInfo() { return this._userInfo; }
  51. public get userID() { return this._userInfo?.UserID ?? 0; }
  52. public get isLogin() { return this._isLogin; }
  53. public get isBindPhone() {
  54. if (!this.userInfo) return false;
  55. const phone = this.userInfo.Phone || '';
  56. return phone.startsWith(this.userInfo.RegisterLocation)
  57. }
  58. public get upgradeBonus() { return this._upgradeBonus; }
  59. public get registerBonus() { return this._registerBonus; }
  60. public get recommendGame() { return this._recommendGame; }
  61. public get serviceLink() { return this._serviceLink; }
  62. public get landscapeGames() { return this._landscapeGames; }
  63. public get withdrawChannel() { return this._withdrawChannel; }
  64. public get serverKick() { return this._serverKick; }
  65. public get favorites() { return this._favorites; }
  66. public get favoriteGames() { return this._favoriteGames; }
  67. public get protectInfo() { return this._protectInfo; }
  68. async reqGetRoutes() {
  69. const platform = AppKernel.platform;
  70. const params: any = {
  71. ad_id: platform.getUrlParam("ad_id"),
  72. adset_id: platform.getUrlParam("adset_id"),
  73. campaign_id: platform.getUrlParam("campaign_id"),
  74. pixel: platform.getUrlParam("pixel"),
  75. };
  76. if (window.navigator && window.navigator.userAgent) {
  77. params.ua = window.navigator.userAgent;
  78. }
  79. // let ia = getInAppBrowser();
  80. // if (ia != null && ia !== "") {
  81. // params.params.ia = ia;
  82. // }
  83. /// TODO: 所有的启动参数
  84. // let urlvars = global.getUrlvars();
  85. // if (urlvars != null && urlvars !== "") {
  86. // params.params.urlvars = urlvars;
  87. // }
  88. // let pwa = checkIsStandalone() || isTWA();
  89. // if (pwa) {
  90. // params.params.pwa = 1;
  91. // }
  92. if (typeof document !== "undefined") {
  93. params.cookie = document.cookie || "";
  94. }
  95. params.platform = getEnterPlatform();
  96. const res: IRoutesResponse = await Http.call(APP_ROUTES, params);
  97. if (!res || res.code !== 0) {
  98. return false;
  99. }
  100. this._firstLaunch = false;
  101. let hallServerUrl = '';
  102. let guest = 0;
  103. let upgradeBonus = 0;
  104. let registerBonus = 0;
  105. let recommendGame = '';
  106. let serviceLink = "";
  107. let landscapeGames = [];
  108. let withdrawChannel: IWithdrawChannel = null;
  109. const conf = res.conf;
  110. if (conf) {
  111. if (!!conf.hall) hallServerUrl = conf.hall;
  112. // if (!!conf.DOLLAR) global.DOLLAR = conf.DOLLAR;
  113. // if (!!conf.currency) global.currency = conf.currency;
  114. // global.pf = conf.pf;
  115. // console.log(`>>>>>>>> global.pf: ${JSON.stringify(global.pf)}`)
  116. // //平台配置
  117. // if (!!conf.pf && !!conf.pf.id) {
  118. // let { type, id } = conf.pf;
  119. // initTrackAll(type, id, isLogin ? userInfo.UserID : null);
  120. // }
  121. // //promoteInstall
  122. // if (!!conf.promoteInstall) promoteInstall = conf.promoteInstall;
  123. //guest
  124. if (!!conf.guest) guest = conf.guest;
  125. // 绑定手机奖励
  126. if (!!conf.upgradeBonus) upgradeBonus = conf.upgradeBonus;
  127. // 初始注册奖励
  128. if (!!conf.registerBonus) registerBonus = conf.registerBonus;
  129. ///领取注册奖励后进入的游戏
  130. if (!!conf.recommendGame) recommendGame = conf.recommendGame;
  131. //客服地址
  132. if (!!conf.serviceLink) serviceLink = conf.serviceLink;
  133. // //弹出转盘
  134. // if (!!conf.popWheel) popWheel = conf.popWheel;
  135. // //弹出绑定电话奖励,并显示浮标
  136. // if (!!conf.popBindPhone) popBindPhone = hasPhone ? 0 : conf.popBindPhone;
  137. // //显示首冲浮标
  138. // if (!!conf.firstBonus) firstBonus = conf.firstBonus;
  139. // //显示救济浮标
  140. // if (!!conf.openRelief) openRelief = conf.openRelief;
  141. // //sport 配置
  142. if (!!conf.bb) {
  143. // bbConf = conf.bb;
  144. // if (userInfo != null) userInfo.bb = bbConf;
  145. }
  146. // if (!!conf.loginOpen) loginOpen = conf.loginOpen;
  147. // if (!!conf.registerOpen) registerOpen = conf.registerOpen;
  148. // if (!!conf.popFirst) popFirst = conf.popFirst;
  149. // if (!!conf.showInstall) showInstall = conf.showInstall;
  150. // //download 配置
  151. // if (!!conf.download) download = conf.download;
  152. // if (!!conf.vipConfig) vipConfig = conf.vipConfig;
  153. if (!!conf.LandscapeGames) landscapeGames = conf.LandscapeGames;
  154. // // adjust相关配置
  155. // if (!!conf.AdjustToken && !!conf.AdjustConfig) global.tracker.initAdjust(conf.AdjustToken, conf.AdjustConfig);
  156. // if (!!conf.popPwaBonus) popPwaBonus = conf.popPwaBonus;
  157. // if (!!conf.outLimit) outLimit = conf.outLimit;
  158. if (!!conf.withdrawChannel) withdrawChannel = conf.withdrawChannelNew;
  159. // if (!!conf.freeChannel) freeChannel = conf.freeChannel;
  160. // if (!!conf.slotsPartner) slotsPartner = conf.slotsPartner;
  161. // if (!!conf.sharePop) sharePop = conf.sharePop;
  162. // googleRedirectH5 = parseGoogleRedirectH5Conf(conf);
  163. // googleRedirectH5Bonus = parseGoogleRedirectH5BonusConf(conf);
  164. // if (conf.jumpapk === 1 || conf.jumpapk === "1" || conf.jumpapk === 2 || conf.jumpapk === "2" || conf.jumpapk === 3 || conf.jumpapk === "3") {
  165. // googleRedirectJumpApk = Number(conf.jumpapk);
  166. // localStorageSet(LOCAL_GOOGLE_REDIRECT_JUMPAPK, conf.jumpapk);
  167. // } else {
  168. // localStorageSet(LOCAL_GOOGLE_REDIRECT_JUMPAPK, "");
  169. // }
  170. // if (conf.jumplater != null) jumplater = Number(conf.jumplater) || 0;
  171. // if (conf.weight1234 != null) fbGuideWeight = conf.weight1234;
  172. // // 注册后自动进游戏
  173. // if (conf.isAutoEnterGame != null) isAutoEnterGame = conf.isAutoEnterGame;
  174. // // 强制引导开关
  175. // if (conf.jumpios != null) jumpToH5 = conf.jumpios;
  176. // // 内置客服JS
  177. // if (!!conf.servicejs) SsqHelp.setServicejs(conf.servicejs);
  178. }
  179. const user = res.user;
  180. this.parseLogin(user);
  181. this._hallServerUrl = hallServerUrl;
  182. this._isGuest = guest === 1;
  183. this._upgradeBonus = upgradeBonus;
  184. this._registerBonus = registerBonus;
  185. this._recommendGame = recommendGame;
  186. this._serviceLink = serviceLink;
  187. this._landscapeGames = landscapeGames;
  188. this._withdrawChannel = withdrawChannel;
  189. return true;
  190. }
  191. async reqRegister({ account, code, password }) {
  192. const params = {
  193. type: APP_LOGIN_TYPE.PHONE,
  194. country: 1,
  195. phone: account,
  196. code: code,
  197. password,
  198. repassword: password,
  199. }
  200. const res = await Http.call<IUserData>(APP_REGISTER, params);
  201. if (success(res)) {
  202. this.parseLogin(res.data);
  203. return true;
  204. }
  205. return false;
  206. }
  207. async reqLogin(arg) {
  208. const loginType = arg.type;
  209. // const lang = global.i18n?.getCurrLang?.() || "";
  210. const params: any = {
  211. type: loginType,
  212. password: arg?.password,
  213. platform: getEnterPlatform(),
  214. // lang
  215. }
  216. switch (loginType) {
  217. case APP_LOGIN_TYPE.ID:
  218. params.account = arg.account;
  219. break;
  220. case APP_LOGIN_TYPE.PHONE:
  221. params.country = arg.country;
  222. params.account = arg.phone;
  223. break;
  224. case APP_LOGIN_TYPE.MAIL:
  225. params.account = arg.email;
  226. break;
  227. case APP_LOGIN_TYPE.GUEST:
  228. delete params.password;
  229. break;
  230. }
  231. const res = await Http.call<IUserData>(APP_LOGIN, params)
  232. if (success(res)) {
  233. this.parseLogin(res.data);
  234. return true;
  235. }
  236. return false;
  237. }
  238. async reqLogin3rd(type: Login3rdType, token: string) {
  239. const params: any = {
  240. provider: type,
  241. token: token
  242. }
  243. const res = await Http.call<IUserData>(APP_LOGIN_3RD, params);
  244. if (success(res)) {
  245. this.parseLogin(res.data);
  246. return true;
  247. }
  248. return false;
  249. }
  250. private parseLogin(data: IUserData) {
  251. if (!data || typeof data !== 'object') return;
  252. this.gameID = 0;
  253. this._isLogin = true;
  254. this._userInfo = data;
  255. this._serverKick = false;
  256. if (!!data.recommendGame) this._recommendGame = data.recommendGame;
  257. if (!!data.sign) localStorageSet(LOCAL_SIGN, data.sign);
  258. if (data.reg === 1) {
  259. // 新注册的
  260. }
  261. }
  262. clearGame() {
  263. this.gameID = 0;
  264. this.gameLink = "";
  265. }
  266. setServerKick(value: boolean) {
  267. this._serverKick = value;
  268. }
  269. updateHallBalance(balance: number) {
  270. if (!this._userInfo || !Number.isFinite(balance)) return;
  271. if ((this._userInfo.vip || 0) > 0) {
  272. this._userInfo.Score = balance;
  273. } else {
  274. this._userInfo.InsureScore = balance;
  275. }
  276. }
  277. async reqSendCode(account: string) {
  278. const params = {
  279. Phone: account,
  280. country: 1,
  281. }
  282. const res = await Http.call(APP_SEND_CODE, params);
  283. return success(res);
  284. }
  285. async reqFavoAdd(id: number) {
  286. const res = await Http.call(APP_FAVO_ADD, { id });
  287. if (success(res)) this.addFavorite(id);
  288. return res;
  289. }
  290. async reqFavoRemove(id: number) {
  291. const res = await Http.call(APP_FAVO_REMOVE, { id });
  292. if (success(res)) this.removeFavorite(id);
  293. return res;
  294. }
  295. async reqUpdateFavoInfo(arg: IFavoriteRequest) {
  296. return arg.like === 1
  297. ? this.reqFavoAdd(arg.id)
  298. : this.reqFavoRemove(arg.id);
  299. }
  300. async reqFavoGet() {
  301. const res = await Http.call<IGameInfo[]>(APP_FAVO_GET);
  302. if (!success(res)) return res;
  303. this._favoriteGames = Array.isArray(res.data) ? res.data : [];
  304. this._favorites = this._favoriteGames
  305. .map(item => Number(item?.id))
  306. .filter(id => Number.isFinite(id));
  307. return res;
  308. }
  309. async reqProtectCheck() {
  310. const res = await Http.call<IProtectInfo[]>(APP_PROTECT_CHECK);
  311. if (success(res) && Array.isArray(res.data) && res.data[0]) {
  312. const info = { ...res.data[0] };
  313. info.TotalReward = Number(info.total_recharge || 0) - Number(info.can_receive_num || 0);
  314. this._protectInfo = info;
  315. }
  316. return res;
  317. }
  318. async reqProtectGet(): Promise<ApiEnvelope> {
  319. const res = await Http.call(APP_PROTECT_GET);
  320. if (success(res) && this._protectInfo) {
  321. this._protectInfo.CurProtectTimes = Number(this._protectInfo.CurProtectTimes || 0) + 1;
  322. }
  323. return res;
  324. }
  325. private addFavorite(id: number) {
  326. const favoriteId = Number(id);
  327. if (!Number.isFinite(favoriteId) || this._favorites.includes(favoriteId)) return;
  328. this._favorites = [...this._favorites, favoriteId];
  329. }
  330. private removeFavorite(id: number) {
  331. const favoriteId = Number(id);
  332. this._favorites = this._favorites.filter(item => item !== favoriteId);
  333. this._favoriteGames = this._favoriteGames.filter(item => Number(item?.id) !== favoriteId);
  334. }
  335. async getGameCardInfo(gameId: number) {
  336. if (this._gameCardInfo[gameId]) {
  337. return this._gameCardInfo[gameId];
  338. }
  339. const payload = await Http.call<IGameInfo>(APP_GAME_CARD_INFO, { id: gameId })
  340. if (!success(payload) || !payload.data) {
  341. return null;
  342. }
  343. const gameInfo = payload.data;
  344. const id = Number(gameInfo.id);
  345. const level = gameInfo.level_info ? 1 : 0;
  346. const cardInfo = { ...gameInfo, level };
  347. this._gameCardInfo[id] = cardInfo;
  348. return cardInfo;
  349. }
  350. }
  351. export const mainStore = new MainStore();