MainStore.ts 12 KB

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