MainStore.ts 15 KB

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