MainStore.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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. APP_BIND_SMS,
  14. APP_CHANGE_BIND_PHONE,
  15. APP_BIND_3RD,
  16. APP_UNBIND_3RD,
  17. APP_UNBIND_3RD_CONFIRM,
  18. } from "../../api";
  19. import { success } from '@scripts/api/ApiHelper';
  20. import { AppKernel } from "../../app/AppKernel";
  21. import { Login3rdType, APP_LOGIN_TYPE, COUNTRY_CODE, CurrencyType, CURRENCY_TYPE } from "../../define/Define";
  22. import { LOCAL_CURRENCY_MODE, LOCAL_SIGN } from "../../constants/Localkey";
  23. import { ApiEnvelope, Http } from "../../request";
  24. import { getEnterPlatform, localStorageGet, localStorageSet } from "../../utils/Helper";
  25. import {
  26. IForgetPassReq,
  27. INoticeData,
  28. IProtectInfo,
  29. IRoutesResponse,
  30. IUserData,
  31. IWithdrawChannel,
  32. } from "./data";
  33. import { IGoogleLoginRes } from "@scripts/platform/Google/GoogleImpl";
  34. const APP_OTP_INTERVAL = 60;
  35. export enum OtpType {
  36. Main,
  37. ChangeOld,
  38. Unbind3rd,
  39. }
  40. class MainStore {
  41. private _hallServerUrl: string;
  42. private _isGuest: boolean;
  43. private _userInfo: IUserData;
  44. private _isLogin: boolean;
  45. private _upgradeBonus: number;
  46. private _registerBonus: number;
  47. private _recommendGame: string;
  48. private _serviceLink: string;
  49. private _landscapeGames: string[];
  50. private _withdrawChannel: IWithdrawChannel;
  51. private _serverKick: boolean;
  52. private _protectInfo: IProtectInfo = null;
  53. private _noticesList: INoticeData[] = [];
  54. private _currencyKind: CurrencyType;
  55. private _otpTsMap: Record<OtpType, number> = {
  56. [OtpType.Main]: 0,
  57. [OtpType.ChangeOld]: 0,
  58. [OtpType.Unbind3rd]: 0,
  59. };
  60. private calcOtpCD(lastTs: number): number {
  61. const times = Math.floor((Date.now() - lastTs) / 1000);
  62. if (times > APP_OTP_INTERVAL) return 0;
  63. return APP_OTP_INTERVAL - times;
  64. }
  65. public setOtpTs(type: OtpType): void {
  66. this._otpTsMap[type] = Date.now();
  67. }
  68. public getOtpCD(type: OtpType): number {
  69. return this.calcOtpCD(this._otpTsMap[type]);
  70. }
  71. public set currencyKind(value: CurrencyType) {
  72. this._currencyKind = value;
  73. localStorageSet(LOCAL_CURRENCY_MODE, value);
  74. }
  75. public get currencyKind() { return this._currencyKind };
  76. public get hallServerUrl() { return this._hallServerUrl; }
  77. public get isGuest() { return this._isGuest; }
  78. public get userInfo() { return this._userInfo; }
  79. public get userID() { return this._userInfo?.UserID ?? 0; }
  80. public get isLogin() { return this._isLogin; }
  81. public get isBindPhone() {
  82. if (!this.userInfo) return false;
  83. const phone = this.userInfo.Phone || '';
  84. return phone.startsWith(this.userInfo.RegisterLocation)
  85. }
  86. public get upgradeBonus() { return this._upgradeBonus; }
  87. public get registerBonus() { return this._registerBonus; }
  88. public get recommendGame() { return this._recommendGame; }
  89. public get serviceLink() { return this._serviceLink; }
  90. public get landscapeGames() { return this._landscapeGames; }
  91. public get withdrawChannel() { return this._withdrawChannel; }
  92. public get serverKick() { return this._serverKick; }
  93. public get protectInfo() { return this._protectInfo; }
  94. public get scScore() { return this.userInfo?.Score || 0 };
  95. public get gcScore() { return this.userInfo?.InsureScore || 0 };
  96. constructor() {
  97. this._currencyKind = localStorageGet(LOCAL_CURRENCY_MODE, CURRENCY_TYPE.GC) as CurrencyType;
  98. }
  99. async reqGetRoutes() {
  100. const platform = AppKernel.platform;
  101. const params: any = {
  102. ad_id: platform.getUrlParam("ad_id"),
  103. adset_id: platform.getUrlParam("adset_id"),
  104. campaign_id: platform.getUrlParam("campaign_id"),
  105. pixel: platform.getUrlParam("pixel"),
  106. };
  107. if (window.navigator && window.navigator.userAgent) {
  108. params.ua = window.navigator.userAgent;
  109. }
  110. // let ia = getInAppBrowser();
  111. // if (ia != null && ia !== "") {
  112. // params.params.ia = ia;
  113. // }
  114. /// TODO: 所有的启动参数
  115. // let urlvars = global.getUrlvars();
  116. // if (urlvars != null && urlvars !== "") {
  117. // params.params.urlvars = urlvars;
  118. // }
  119. // let pwa = checkIsStandalone() || isTWA();
  120. // if (pwa) {
  121. // params.params.pwa = 1;
  122. // }
  123. if (typeof document !== "undefined") {
  124. params.cookie = document.cookie || "";
  125. }
  126. params.platform = getEnterPlatform();
  127. const res: IRoutesResponse = await Http.call(APP_ROUTES, params);
  128. if (!res || res.code !== 0) {
  129. return false;
  130. }
  131. let hallServerUrl = '';
  132. let guest = 0;
  133. let upgradeBonus = 0;
  134. let registerBonus = 0;
  135. let recommendGame = '';
  136. let serviceLink = "";
  137. let landscapeGames = [];
  138. let withdrawChannel: IWithdrawChannel = null;
  139. const conf = res.conf;
  140. if (conf) {
  141. if (!!conf.hall) hallServerUrl = conf.hall;
  142. // if (!!conf.DOLLAR) global.DOLLAR = conf.DOLLAR;
  143. // if (!!conf.currency) global.currency = conf.currency;
  144. // global.pf = conf.pf;
  145. // console.log(`>>>>>>>> global.pf: ${JSON.stringify(global.pf)}`)
  146. // //平台配置
  147. // if (!!conf.pf && !!conf.pf.id) {
  148. // let { type, id } = conf.pf;
  149. // initTrackAll(type, id, isLogin ? userInfo.UserID : null);
  150. // }
  151. // //promoteInstall
  152. // if (!!conf.promoteInstall) promoteInstall = conf.promoteInstall;
  153. //guest
  154. if (!!conf.guest) guest = conf.guest;
  155. // 绑定手机奖励
  156. if (!!conf.upgradeBonus) upgradeBonus = conf.upgradeBonus;
  157. // 初始注册奖励
  158. if (!!conf.registerBonus) registerBonus = conf.registerBonus;
  159. ///领取注册奖励后进入的游戏
  160. if (!!conf.recommendGame) recommendGame = conf.recommendGame;
  161. //客服地址
  162. if (!!conf.serviceLink) serviceLink = conf.serviceLink;
  163. // //弹出转盘
  164. // if (!!conf.popWheel) popWheel = conf.popWheel;
  165. // //弹出绑定电话奖励,并显示浮标
  166. // if (!!conf.popBindPhone) popBindPhone = hasPhone ? 0 : conf.popBindPhone;
  167. // //显示首冲浮标
  168. // if (!!conf.firstBonus) firstBonus = conf.firstBonus;
  169. // //显示救济浮标
  170. // if (!!conf.openRelief) openRelief = conf.openRelief;
  171. // //sport 配置
  172. if (!!conf.bb) {
  173. // bbConf = conf.bb;
  174. // if (userInfo != null) userInfo.bb = bbConf;
  175. }
  176. // if (!!conf.loginOpen) loginOpen = conf.loginOpen;
  177. // if (!!conf.registerOpen) registerOpen = conf.registerOpen;
  178. // if (!!conf.popFirst) popFirst = conf.popFirst;
  179. // if (!!conf.showInstall) showInstall = conf.showInstall;
  180. // //download 配置
  181. // if (!!conf.download) download = conf.download;
  182. // if (!!conf.vipConfig) vipConfig = conf.vipConfig;
  183. if (!!conf.LandscapeGames) landscapeGames = conf.LandscapeGames;
  184. // // adjust相关配置
  185. // if (!!conf.AdjustToken && !!conf.AdjustConfig) global.tracker.initAdjust(conf.AdjustToken, conf.AdjustConfig);
  186. // if (!!conf.popPwaBonus) popPwaBonus = conf.popPwaBonus;
  187. // if (!!conf.outLimit) outLimit = conf.outLimit;
  188. if (!!conf.withdrawChannel) withdrawChannel = conf.withdrawChannelNew;
  189. // if (!!conf.freeChannel) freeChannel = conf.freeChannel;
  190. // if (!!conf.slotsPartner) slotsPartner = conf.slotsPartner;
  191. // if (!!conf.sharePop) sharePop = conf.sharePop;
  192. // googleRedirectH5 = parseGoogleRedirectH5Conf(conf);
  193. // googleRedirectH5Bonus = parseGoogleRedirectH5BonusConf(conf);
  194. // if (conf.jumpapk === 1 || conf.jumpapk === "1" || conf.jumpapk === 2 || conf.jumpapk === "2" || conf.jumpapk === 3 || conf.jumpapk === "3") {
  195. // googleRedirectJumpApk = Number(conf.jumpapk);
  196. // localStorageSet(LOCAL_GOOGLE_REDIRECT_JUMPAPK, conf.jumpapk);
  197. // } else {
  198. // localStorageSet(LOCAL_GOOGLE_REDIRECT_JUMPAPK, "");
  199. // }
  200. // if (conf.jumplater != null) jumplater = Number(conf.jumplater) || 0;
  201. // if (conf.weight1234 != null) fbGuideWeight = conf.weight1234;
  202. // // 注册后自动进游戏
  203. // if (conf.isAutoEnterGame != null) isAutoEnterGame = conf.isAutoEnterGame;
  204. // // 强制引导开关
  205. // if (conf.jumpios != null) jumpToH5 = conf.jumpios;
  206. // // 内置客服JS
  207. // if (!!conf.servicejs) SsqHelp.setServicejs(conf.servicejs);
  208. }
  209. const user = res.user;
  210. this.parseLogin(user);
  211. this._hallServerUrl = hallServerUrl;
  212. this._isGuest = guest === 1;
  213. this._upgradeBonus = upgradeBonus;
  214. this._registerBonus = registerBonus;
  215. this._recommendGame = recommendGame;
  216. this._serviceLink = serviceLink;
  217. this._landscapeGames = landscapeGames;
  218. this._withdrawChannel = withdrawChannel;
  219. return true;
  220. }
  221. async reqRegister({ account, code, password, act }) {
  222. const params = {
  223. type: APP_LOGIN_TYPE.PHONE,
  224. country: 1,
  225. phone: account,
  226. code: code,
  227. password,
  228. repassword: password,
  229. act,
  230. }
  231. const res = await Http.call<IUserData>(APP_REGISTER, params);
  232. if (success(res)) {
  233. this.parseLogin(res.data);
  234. }
  235. return res;
  236. }
  237. async reqLogin(arg) {
  238. const loginType = arg.type;
  239. // const lang = global.i18n?.getCurrLang?.() || "";
  240. const params: any = {
  241. type: loginType,
  242. password: arg?.password,
  243. platform: getEnterPlatform(),
  244. // lang
  245. }
  246. switch (loginType) {
  247. case APP_LOGIN_TYPE.ID:
  248. params.account = arg.account;
  249. break;
  250. case APP_LOGIN_TYPE.PHONE:
  251. params.country = arg.country;
  252. params.account = arg.phone;
  253. break;
  254. case APP_LOGIN_TYPE.MAIL:
  255. params.account = arg.email;
  256. break;
  257. case APP_LOGIN_TYPE.GUEST:
  258. delete params.password;
  259. break;
  260. }
  261. const res = await Http.call<IUserData>(APP_LOGIN, params)
  262. if (success(res)) {
  263. this.parseLogin(res.data);
  264. }
  265. return res;
  266. }
  267. async reqLogin3rd(type: Login3rdType, token: string) {
  268. const params: any = {
  269. provider: type,
  270. token: token
  271. }
  272. const res = await Http.call<IUserData>(APP_LOGIN_3RD, params);
  273. if (success(res)) {
  274. this.parseLogin(res.data);
  275. }
  276. return res;
  277. }
  278. async reqForgetPass(args: IForgetPassReq) {
  279. args.renewpassword = args.newpassword;
  280. if (!args.country) args.country = COUNTRY_CODE;
  281. const res = await Http.call<IUserData>(APP_FORGET_PASS, args);
  282. if (success(res)) {
  283. this.parseLogin(res.data);
  284. return true;
  285. }
  286. return false;
  287. }
  288. private parseLogin(data: IUserData) {
  289. if (!data || typeof data !== 'object') return;
  290. this._isLogin = true;
  291. this._userInfo = data;
  292. this._serverKick = false;
  293. if (!!data.recommendGame) this._recommendGame = data.recommendGame;
  294. if (!!data.sign) localStorageSet(LOCAL_SIGN, data.sign);
  295. if (data.reg === 1) {
  296. // 新注册的
  297. }
  298. }
  299. setServerKick(value: boolean) {
  300. this._serverKick = value;
  301. }
  302. updateHallBalance(balance: number) {
  303. if (!this._userInfo || !Number.isFinite(balance)) return;
  304. if ((this._userInfo.vip || 0) > 0) {
  305. this._userInfo.Score = balance;
  306. } else {
  307. this._userInfo.InsureScore = balance;
  308. }
  309. }
  310. async reqSendCode(account: string, isChangeOld: boolean = false) {
  311. const Type = isChangeOld ? 3 : 2 //换绑发旧手机为3
  312. const cd = isChangeOld
  313. ? this.getOtpCD(OtpType.ChangeOld)
  314. : this.getOtpCD(OtpType.Main);
  315. if (cd > 0) return false;
  316. const params = {
  317. Phone: account,
  318. country: 1,
  319. Type
  320. }
  321. const res = await Http.call(APP_SEND_CODE, params);
  322. if (success(res)) {
  323. if (isChangeOld) {
  324. this.setOtpTs(OtpType.ChangeOld);
  325. } else {
  326. this.setOtpTs(OtpType.Main);
  327. }
  328. return true;
  329. }
  330. return false;
  331. }
  332. async reqProtectCheck() {
  333. const res = await Http.call<IProtectInfo[]>(APP_PROTECT_CHECK);
  334. if (success(res) && Array.isArray(res.data) && res.data[0]) {
  335. const info = { ...res.data[0] };
  336. info.TotalReward = Number(info.total_recharge || 0) - Number(info.can_receive_num || 0);
  337. this._protectInfo = info;
  338. }
  339. return res;
  340. }
  341. async reqProtectGet(): Promise<ApiEnvelope> {
  342. const res = await Http.call(APP_PROTECT_GET);
  343. if (success(res) && this._protectInfo) {
  344. this._protectInfo.CurProtectTimes = Number(this._protectInfo.CurProtectTimes || 0) + 1;
  345. }
  346. return res;
  347. }
  348. async reqUserInfo() {
  349. const res = await Http.call<IUserData>(APP_GAME_INFO);
  350. if (success(res)) {
  351. this.parseLogin(res.data);
  352. return true;
  353. }
  354. return false;
  355. }
  356. async reqChangeUserInfo(payload: any) {
  357. const params = {
  358. ...payload
  359. };
  360. const res = await Http.call<IUserData>(APP_CHANGE_INFO, params);
  361. if (success(res)) {
  362. this._userInfo = res.data;
  363. AppKernel.events.emit('user:update');
  364. return true;
  365. }
  366. return false;
  367. }
  368. updateUserInfo(data: IUserData) {
  369. if (!data || typeof data !== 'object') return;
  370. this.parseLogin(data);
  371. }
  372. async reqLogout() {
  373. const res = await Http.call(APP_LOGOUT);
  374. if (success(res)) {
  375. return true;
  376. }
  377. return false;
  378. }
  379. onUserLogout() {
  380. delete this._userInfo;
  381. this._isLogin = false;
  382. this._noticesList = [];
  383. console.log('mainStore onLogout!!!');
  384. }
  385. getNoticeData() {
  386. return this._noticesList.shift();
  387. }
  388. async reqBindPhoneConfirm({ account, code, password }) {
  389. const params = {
  390. phone: account,
  391. code,
  392. password,
  393. country: 1
  394. }
  395. const res = await Http.call<IUserData>(APP_BIND_SMS, params);
  396. if (success(res)) {
  397. mainStore.updateUserInfo(res.data);
  398. AppKernel.events.emit('user:update');
  399. return true;
  400. }
  401. return false;
  402. }
  403. async reqChangePhoneConfirm({ oldCode, newPhone, newCode, password }) {
  404. const params = {
  405. oldCode,
  406. newPhone,
  407. newCode,
  408. password,
  409. country: 1
  410. }
  411. const res = await Http.call<IUserData>(APP_CHANGE_BIND_PHONE, params);
  412. if (success(res)) {
  413. mainStore.updateUserInfo(res.data);
  414. AppKernel.events.emit('user:update');
  415. return true;
  416. }
  417. return false;
  418. }
  419. async reqBind3rd(type: Login3rdType, token: string) {
  420. const params: any = {
  421. provider: type,
  422. token: token
  423. }
  424. const res = await Http.call(APP_BIND_3RD, params);
  425. if (success(res)) {
  426. await this.reqUserInfo();
  427. AppKernel.events.emit('user:update');
  428. }
  429. return res;
  430. }
  431. async reqBindFacebook() {
  432. const res = await AppKernel.fb.login();
  433. console.log("facebook bind res: ", res);
  434. if (res.accessToken) {
  435. return await this.reqBind3rd('facebook', res.accessToken);
  436. }
  437. return false;
  438. }
  439. async reqBindGoogle(res?: IGoogleLoginRes) {
  440. if (!res) {
  441. /// h5 通过dom按钮触发登录
  442. res = await AppKernel.google.login();
  443. }
  444. if (res && res.accessToken) {
  445. return await this.reqBind3rd('google', res.accessToken);
  446. }
  447. return true;
  448. }
  449. async reqUnBind3rdCode({ verify_type }) {
  450. const params = {
  451. verify_type
  452. }
  453. const res = await Http.call(APP_UNBIND_3RD, params);
  454. if (success(res)) {
  455. this.setOtpTs(OtpType.Unbind3rd);
  456. return true;
  457. }
  458. return false;
  459. }
  460. async reqUnBind3rdConfirm({ verify_type, code }) {
  461. const params = {
  462. verify_type,
  463. code
  464. }
  465. const res = await Http.call(APP_UNBIND_3RD_CONFIRM, params);
  466. if (success(res)) {
  467. await this.reqUserInfo();
  468. AppKernel.events.emit('user:update');
  469. return true;
  470. }
  471. return false;
  472. }
  473. }
  474. export const mainStore = new MainStore();