MainStore.ts 13 KB

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