MainStore.ts 15 KB

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