useGame.tsx 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import { GameListRep, GameRequest, getGameDetailApi } from "@/api/home";
  2. import { useEventPoint } from "@/hooks/useEventPoint";
  3. import { defaultLocale, usePathname, useRouter } from "@/i18n/routing";
  4. import { getCookies } from "@/utils/Cookies";
  5. import { Toast } from "antd-mobile";
  6. import { useTranslations } from "next-intl";
  7. // 地址白名单
  8. const whiteUrls = ["192.168.0.84", "localhost", "192.168.0.46"];
  9. const useGame = () => {
  10. const router = useRouter();
  11. const t = useTranslations();
  12. const { eventStartTrial } = useEventPoint();
  13. const pathname = usePathname();
  14. const getCoinType = (game: GameListRep, mode: number) => {
  15. const type = game.coin_types!;
  16. // 本金加彩金
  17. if (mode) return mode;
  18. if (type & 1 || type & 2) {
  19. return 1;
  20. }
  21. // 免费币
  22. if (type & 4) {
  23. return 2;
  24. }
  25. // 重币
  26. if (type & 8) {
  27. return 3;
  28. }
  29. return;
  30. };
  31. const getGameUrl = (game: GameListRep, params: GameRequest) => {
  32. Toast.show({
  33. icon: "loading",
  34. duration: 0,
  35. maskStyle: { zIndex: 99999, background: "rgba(0,0,0,0.5)" },
  36. });
  37. // const brand = brandList.find((item) => item.gid === game.game_id)?.brand ?? "";
  38. const lang = getCookies("language") || defaultLocale;
  39. getGameDetailApi({ ...params, language: lang === "br" ? "pt" : lang })
  40. .then((res) => {
  41. Toast.clear();
  42. if (res.data && res.data.game_url) {
  43. eventStartTrial();
  44. const game_url = res.data.game_url;
  45. // 获取游戏的品牌
  46. const gameId = game?.game_id || "";
  47. // 然后是文档对象
  48. if (game_url.indexOf("!doctype") !== -1) {
  49. sessionStorage.setItem("game_url", res.data.game_url);
  50. const url = `${encodeURI(window.location.href)}&return_url=${encodeURI(window.location.href.replace(pathname, ""))}&category_name=${game.provider}`;
  51. router.push(`/game?${url}`);
  52. return;
  53. }
  54. const url = `${encodeURI(res.data.game_url)}&return_url=${encodeURI(window.location.href.replace(pathname, ""))}&category_name=${game.provider}`;
  55. // 如果是pp游戏,则当前窗口打开新的地址,不需要加return_url
  56. // gid 如果开头是1的6位数的数字,则认为是pp游戏
  57. let isPP = /^1\d{5}$/.test(gameId);
  58. // 如果是play游戏,则当前窗口打开新的地址
  59. const isPlay = /^5\d{5}$/.test(gameId);
  60. if (isPP || isPlay) {
  61. // const ppUrl = `${encodeURI(res.data.game_url)}&category_name=${game.provider}`;
  62. window.open(url, "_self");
  63. return;
  64. }
  65. if (new RegExp("https").test(game_url)) {
  66. // 如果是https
  67. router.push(`/game?${url}`);
  68. return;
  69. } else {
  70. window.open(url);
  71. }
  72. } else {
  73. Toast.show(t("code.500"));
  74. }
  75. })
  76. .catch((error) => {
  77. Toast.clear();
  78. Toast.show(t(`code.${error.data?.code || 500}`));
  79. });
  80. };
  81. return {
  82. getGameUrl: getGameUrl,
  83. getCoinType: getCoinType,
  84. };
  85. };
  86. export default useGame;