MainStore.ts 13 KB

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