useGame.tsx 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. if (game_url.indexOf("!doctype") !== -1) {
  47. sessionStorage.setItem("game_url", res.data.game_url);
  48. const url = `${encodeURI(window.location.href)}&return_url=${encodeURI(window.location.href.replace(pathname, ""))}&category_name=${game.provider}`;
  49. router.push(`/game?${url}`);
  50. return;
  51. }
  52. const url = `${encodeURI(res.data.game_url)}&return_url=${encodeURI(window.location.href.replace(pathname, ""))}&category_name=${game.provider}`;
  53. if (new RegExp("https").test(game_url)) {
  54. // 如果是https
  55. router.push(`/game?${url}`);
  56. return;
  57. } else {
  58. window.open(url);
  59. }
  60. } else {
  61. Toast.show(t("code.500"));
  62. }
  63. })
  64. .catch((error) => {
  65. Toast.clear();
  66. });
  67. };
  68. return {
  69. getGameUrl: getGameUrl,
  70. getCoinType: getCoinType,
  71. };
  72. };
  73. export default useGame;