MainStore.ts 13 KB

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