MainStore.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 _withdrawChannel: IWithdrawChannel;
  50. private _withdrawFeeRate: number;
  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 withdrawChannel() { return this._withdrawChannel; }
  91. public get withdrawFeeRate() { return this._withdrawFeeRate; }
  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 WithdrawFeeRate = 5;
  138. let withdrawChannel: IWithdrawChannel = null;
  139. const conf = res.conf;
  140. if (conf) {
  141. if (!!conf.hall) hallServerUrl = conf.hall;
  142. if (!!conf.guest) guest = conf.guest;
  143. // 绑定手机奖励
  144. if (!!conf.upgradeBonus) upgradeBonus = conf.upgradeBonus;
  145. // 初始注册奖励
  146. if (!!conf.registerBonus) registerBonus = conf.registerBonus;
  147. ///领取注册奖励后进入的游戏
  148. if (!!conf.recommendGame) recommendGame = conf.recommendGame;
  149. //客服地址
  150. if (!!conf.serviceLink) serviceLink = conf.serviceLink;
  151. if (!!conf.withdrawChannelNew) withdrawChannel = conf.withdrawChannelNew;
  152. if (!!conf.WithdrawFeeRate) WithdrawFeeRate = conf.WithdrawFeeRate;
  153. }
  154. const user = res.user;
  155. this.parseLogin(user);
  156. this._hallServerUrl = hallServerUrl;
  157. this._isGuest = guest === 1;
  158. this._upgradeBonus = upgradeBonus;
  159. this._registerBonus = registerBonus;
  160. this._recommendGame = recommendGame;
  161. this._serviceLink = serviceLink;
  162. this._withdrawChannel = withdrawChannel;
  163. this._withdrawFeeRate = WithdrawFeeRate;
  164. return true;
  165. }
  166. async reqRegister({ account, code, password, act }) {
  167. const params = {
  168. type: APP_LOGIN_TYPE.PHONE,
  169. country: 1,
  170. phone: account,
  171. code: code,
  172. password,
  173. repassword: password,
  174. act,
  175. }
  176. const res = await Http.call<IUserData>(APP_REGISTER, params);
  177. if (success(res)) {
  178. this.parseLogin(res.data);
  179. }
  180. return res;
  181. }
  182. async reqLogin(arg) {
  183. const loginType = arg.type;
  184. // const lang = global.i18n?.getCurrLang?.() || "";
  185. const params: any = {
  186. type: loginType,
  187. password: arg?.password,
  188. platform: getEnterPlatform(),
  189. // lang
  190. }
  191. switch (loginType) {
  192. case APP_LOGIN_TYPE.ID:
  193. params.account = arg.account;
  194. break;
  195. case APP_LOGIN_TYPE.PHONE:
  196. params.country = arg.country;
  197. params.account = arg.phone;
  198. break;
  199. case APP_LOGIN_TYPE.MAIL:
  200. params.account = arg.email;
  201. break;
  202. case APP_LOGIN_TYPE.GUEST:
  203. delete params.password;
  204. break;
  205. }
  206. const res = await Http.call<IUserData>(APP_LOGIN, params)
  207. if (success(res)) {
  208. this.parseLogin(res.data);
  209. }
  210. return res;
  211. }
  212. async reqLogin3rd(type: Login3rdType, token: string) {
  213. const params: any = {
  214. provider: type,
  215. token: token
  216. }
  217. const res = await Http.call<IUserData>(APP_LOGIN_3RD, params);
  218. if (success(res)) {
  219. this.parseLogin(res.data);
  220. }
  221. return res;
  222. }
  223. async reqForgetPass(args: IForgetPassReq) {
  224. args.renewpassword = args.newpassword;
  225. if (!args.country) args.country = COUNTRY_CODE;
  226. const res = await Http.call<IUserData>(APP_FORGET_PASS, args);
  227. if (success(res)) {
  228. this.parseLogin(res.data);
  229. return true;
  230. }
  231. return false;
  232. }
  233. private parseLogin(data: IUserData) {
  234. if (!data || typeof data !== 'object') return;
  235. this._isLogin = true;
  236. this._userInfo = data;
  237. this._serverKick = false;
  238. if (!!data.recommendGame) this._recommendGame = data.recommendGame;
  239. if (!!data.sign) localStorageSet(LOCAL_SIGN, data.sign);
  240. if (data.reg === 1) {
  241. // 新注册的
  242. }
  243. }
  244. setServerKick(value: boolean) {
  245. this._serverKick = value;
  246. }
  247. updateHallBalance(balance: number) {
  248. if (!this._userInfo || !Number.isFinite(balance)) return;
  249. if ((this._userInfo.vip || 0) > 0) {
  250. this._userInfo.Score = balance;
  251. } else {
  252. this._userInfo.InsureScore = balance;
  253. }
  254. }
  255. async reqSendCode(account: string, isChangeOld: boolean = false) {
  256. const Type = isChangeOld ? 3 : 2 //换绑发旧手机为3
  257. const cd = isChangeOld
  258. ? this.getOtpCD(OtpType.ChangeOld)
  259. : this.getOtpCD(OtpType.Main);
  260. if (cd > 0) return false;
  261. const params = {
  262. Phone: account,
  263. country: 1,
  264. Type
  265. }
  266. const res = await Http.call(APP_SEND_CODE, params);
  267. if (success(res)) {
  268. if (isChangeOld) {
  269. this.setOtpTs(OtpType.ChangeOld);
  270. } else {
  271. this.setOtpTs(OtpType.Main);
  272. }
  273. return true;
  274. }
  275. return false;
  276. }
  277. async reqProtectCheck() {
  278. const res = await Http.call<IProtectInfo[]>(APP_PROTECT_CHECK);
  279. if (success(res) && Array.isArray(res.data) && res.data[0]) {
  280. const info = { ...res.data[0] };
  281. info.TotalReward = Number(info.total_recharge || 0) - Number(info.can_receive_num || 0);
  282. this._protectInfo = info;
  283. }
  284. return res;
  285. }
  286. async reqProtectGet(): Promise<ApiEnvelope> {
  287. const res = await Http.call(APP_PROTECT_GET);
  288. if (success(res) && this._protectInfo) {
  289. this._protectInfo.CurProtectTimes = Number(this._protectInfo.CurProtectTimes || 0) + 1;
  290. }
  291. return res;
  292. }
  293. async reqUserInfo() {
  294. const res = await Http.call<IUserData>(APP_GAME_INFO);
  295. if (success(res)) {
  296. this.parseLogin(res.data);
  297. AppKernel.events.emit('user:update');
  298. return true;
  299. }
  300. return false;
  301. }
  302. async reqChangeUserInfo(payload: any) {
  303. const params = {
  304. ...payload
  305. };
  306. const res = await Http.call<IUserData>(APP_CHANGE_INFO, params);
  307. if (success(res)) {
  308. this._userInfo = res.data;
  309. AppKernel.events.emit('user:update');
  310. return true;
  311. }
  312. return false;
  313. }
  314. updateUserInfo(data: IUserData) {
  315. if (!data || typeof data !== 'object') return;
  316. this.parseLogin(data);
  317. }
  318. async reqLogout() {
  319. const res = await Http.call(APP_LOGOUT);
  320. if (success(res)) {
  321. return true;
  322. }
  323. return false;
  324. }
  325. onUserLogout() {
  326. delete this._userInfo;
  327. this._isLogin = false;
  328. this._noticesList = [];
  329. console.log('mainStore onLogout!!!');
  330. }
  331. getNoticeData() {
  332. return this._noticesList.shift();
  333. }
  334. async reqBindPhoneConfirm({ account, code, password }) {
  335. const params = {
  336. phone: account,
  337. code,
  338. password,
  339. country: 1
  340. }
  341. const res = await Http.call<IUserData>(APP_BIND_SMS, params);
  342. if (success(res)) {
  343. mainStore.updateUserInfo(res.data);
  344. AppKernel.events.emit('user:update');
  345. return true;
  346. }
  347. return false;
  348. }
  349. async reqChangePhoneConfirm({ oldCode, newPhone, newCode, password }) {
  350. const params = {
  351. oldCode,
  352. newPhone,
  353. newCode,
  354. password,
  355. country: 1
  356. }
  357. const res = await Http.call<IUserData>(APP_CHANGE_BIND_PHONE, params);
  358. if (success(res)) {
  359. mainStore.updateUserInfo(res.data);
  360. AppKernel.events.emit('user:update');
  361. return true;
  362. }
  363. return false;
  364. }
  365. async reqBind3rd(type: Login3rdType, token: string) {
  366. const params: any = {
  367. provider: type,
  368. token: token
  369. }
  370. const res = await Http.call(APP_BIND_3RD, params);
  371. if (success(res)) {
  372. await this.reqUserInfo();
  373. AppKernel.events.emit('user:update');
  374. }
  375. return res;
  376. }
  377. async reqBindFacebook() {
  378. const res = await AppKernel.fb.login();
  379. console.log("facebook bind res: ", res);
  380. if (res.accessToken) {
  381. return await this.reqBind3rd('facebook', res.accessToken);
  382. }
  383. return false;
  384. }
  385. async reqBindGoogle(res?: IGoogleLoginRes) {
  386. if (!res) {
  387. /// h5 通过dom按钮触发登录
  388. res = await AppKernel.google.login();
  389. }
  390. if (res && res.accessToken) {
  391. return await this.reqBind3rd('google', res.accessToken);
  392. }
  393. return true;
  394. }
  395. async reqUnBind3rdCode({ verify_type }) {
  396. const params = {
  397. verify_type
  398. }
  399. const res = await Http.call(APP_UNBIND_3RD, params);
  400. if (success(res)) {
  401. this.setOtpTs(OtpType.Unbind3rd);
  402. return true;
  403. }
  404. return false;
  405. }
  406. async reqUnBind3rdConfirm({ verify_type, code }) {
  407. const params = {
  408. verify_type,
  409. code
  410. }
  411. const res = await Http.call(APP_UNBIND_3RD_CONFIRM, params);
  412. if (success(res)) {
  413. await this.reqUserInfo();
  414. AppKernel.events.emit('user:update');
  415. return true;
  416. }
  417. return false;
  418. }
  419. }
  420. export const mainStore = new MainStore();