useGame.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. console.log('category_name',game)
  42. Toast.clear();
  43. if (res.data && res.data.game_url) {
  44. eventStartTrial();
  45. const game_url = res.data.game_url;
  46. // 获取游戏的品牌
  47. const categoryName:any = game?.category_name;
  48. console.log('category_name222',game,categoryName.toLowerCase())
  49. // 然后是文档对象
  50. if (game_url.indexOf("!doctype") !== -1) {
  51. sessionStorage.setItem("game_url", res.data.game_url);
  52. const url = `${encodeURI(window.location.href)}&return_url=${encodeURI(window.location.href.replace(pathname, ""))}&category_name=${game.provider}`;
  53. router.push(`/game?${url}`);
  54. return;
  55. }
  56. const url = `${encodeURI(res.data.game_url)}&return_url=${encodeURI(window.location.href.replace(pathname, ""))}&category_name=${game.provider}`;
  57. // 如果是pp游戏,则当前窗口打开新的地址,不需要加return_url
  58. if(categoryName.toLowerCase() === 'pp'){
  59. const ppUrl = `${encodeURI(res.data.game_url)}&category_name=${game.provider}`;
  60. console.log('ppUrl',ppUrl)
  61. return
  62. window.open(ppUrl,'_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. });
  79. };
  80. return {
  81. getGameUrl: getGameUrl,
  82. getCoinType: getCoinType,
  83. };
  84. };
  85. export default useGame;