index.jsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import styles from './styles.module.scss'
  2. import {useEffect, useRef, useState} from "react";
  3. import {useThemeProvider} from "@contexts/themeContext";
  4. import {useDispatch, useSelector} from "react-redux";
  5. import {
  6. getBBConf,
  7. getUserBBToken,
  8. getUserID,
  9. getUserInfo, getUserLang, getUserNick,
  10. getUserNickAndLang,
  11. getUserThemeAndLang
  12. } from "@features/mainSlice";
  13. import {APP_THEME} from "@constants/app";
  14. import {useLocation, useNavigate} from "react-router-dom";
  15. import {ROUTE_PATH} from "@constants/routes";
  16. import {reqGameInfo} from "@features/api";
  17. import {getUrlParam} from "@utils/helpers";
  18. const i18n=global.i18n;
  19. const objInfo={
  20. bt:null,
  21. timer:null
  22. }
  23. const checkStartPath=(pathname)=>{
  24. let path=pathname.startsWith(ROUTE_PATH.RACING)?"/horse-racing-55":"/";
  25. let bt_path=getUrlParam("bt-path");
  26. if (bt_path!=null&&bt_path!==""){
  27. path=bt_path;
  28. }
  29. return path;
  30. }
  31. const WSport=({resetSport})=>{
  32. const {isMobile}=useThemeProvider();
  33. const bbConf=useSelector(getBBConf);
  34. const {theme}=useThemeProvider();
  35. const user=useSelector(getUserInfo);
  36. const userID=useSelector(getUserID);
  37. const language=i18n.language;
  38. const navigate=useNavigate();
  39. const dispatch=useDispatch();
  40. const {pathname,hash} = useLocation();
  41. // const tempSPath=pathname.startsWith(ROUTE_PATH.RACING)?"/horse-racing-55":"/";
  42. const tempSPath=checkStartPath(pathname);
  43. const [sportPath,setSportPath]=useState(tempSPath);
  44. const btToken=useSelector(getUserBBToken);
  45. const userLang=useSelector(getUserLang);
  46. const userNick=useSelector(getUserNick);
  47. const refreshToken=async ()=>{
  48. return new Promise((resolve, reject) => {
  49. dispatch(reqGameInfo()).then(({payload})=>{
  50. let data=payload.data;
  51. if (data!=null){
  52. let bb=data.bb;
  53. if (bb!=null){
  54. resolve(bb.token);
  55. }
  56. else reject('');
  57. }
  58. else reject('');
  59. });
  60. });
  61. }
  62. const initBetby=()=>{
  63. if (bbConf==null)return null;
  64. let brand_id=bbConf.id;
  65. let bbtheme=bbConf.theme;
  66. let themeIndex=0;
  67. if (theme===APP_THEME.LIGHT)themeIndex=1;
  68. let themeName=bbtheme[themeIndex];
  69. // let token=null;
  70. // if (bbConf.token!=null)token=bbConf.token;
  71. // if (user!=null&&user.bb!=null&&user.bb.token!=null)token=user.bb.token;
  72. // token=null;
  73. let token=btToken;
  74. let betSlipOffsetBottom=isMobile()?70:0;
  75. let stickyTop=isMobile()?55:60;
  76. var bt = new window.BTRenderer().initialize({
  77. brand_id: brand_id,
  78. token: token,
  79. onTokenExpired: ()=>refreshToken(),
  80. themeName: themeName,
  81. // themeName: "bc-game-dark-tile",
  82. lang: language,
  83. target: document.getElementById('betby'),
  84. betSlipOffsetBottom: betSlipOffsetBottom,
  85. betSlipOffsetRight: 0,
  86. betSlipOffsetTop: 130,
  87. betslipZIndex: 100,
  88. stickyTop: stickyTop,
  89. url:sportPath,
  90. onRouteChange: function() {},
  91. onLogin: function() {
  92. navigate(ROUTE_PATH.SIGN_IN);
  93. },
  94. onRegister: function() {
  95. navigate(ROUTE_PATH.SIGN_UP);
  96. },
  97. onRecharge:function (){
  98. navigate(ROUTE_PATH.PAY);
  99. },
  100. onSessionRefresh: function() {
  101. // resetBT();
  102. if (resetSport)resetSport();
  103. },
  104. onBetSlipStateChange: function() {}
  105. })
  106. return bt;
  107. }
  108. const clearBT=()=>{
  109. if (objInfo.bt){
  110. objInfo.bt.kill();
  111. objInfo.bt=null;
  112. }
  113. }
  114. const clearTimer=()=>{
  115. if (objInfo.timer!=null){
  116. clearTimeout(objInfo.timer);
  117. objInfo.timer=null;
  118. }
  119. }
  120. useEffect(()=>{
  121. if (pathname.startsWith(ROUTE_PATH.SPORTS)){
  122. let sub=pathname.replace(ROUTE_PATH.SPORTS,"");
  123. if (sub==="")sub="/";
  124. setSportPath(sub);
  125. if (objInfo.bt){
  126. objInfo.bt.updateOptions({
  127. url:sub
  128. })
  129. }
  130. }
  131. else if (pathname.startsWith(ROUTE_PATH.RACING))
  132. {
  133. let sub=pathname.replace(ROUTE_PATH.RACING,"");
  134. if (sub==="")sub="/horse-racing-55";
  135. setSportPath(sub);
  136. if (objInfo.bt){
  137. objInfo.bt.updateOptions({
  138. url:sub
  139. })
  140. }
  141. }
  142. },[pathname])
  143. useEffect(()=>{
  144. if (hash===""){
  145. clearTimer();
  146. objInfo.timer=setTimeout(()=>{
  147. if (objInfo.bt){
  148. objInfo.bt.updateOptions({
  149. url:sportPath
  150. })
  151. }
  152. },20);
  153. }
  154. },[hash])
  155. useEffect(()=>{
  156. if (objInfo.bt){
  157. if (bbConf==null)return null;
  158. let bbtheme=bbConf.theme;
  159. let themeIndex=0;
  160. if (theme===APP_THEME.LIGHT)themeIndex=1;
  161. let themeName=bbtheme[themeIndex];
  162. objInfo.bt.updateOptions({
  163. themeName:themeName
  164. })
  165. }
  166. },[theme])
  167. // useEffect(()=>{
  168. // if (objInfo.bt){
  169. // objInfo.bt.updateOptions({
  170. // lang: language,
  171. // })
  172. // }
  173. // },[language])
  174. const resetBT=()=>{
  175. clearBT();
  176. objInfo.bt=initBetby();
  177. }
  178. useEffect(()=>{
  179. resetBT();
  180. return ()=>{
  181. clearBT();
  182. clearTimer();
  183. }
  184. },[userID,userLang,userNick])
  185. //,theme,language
  186. return (
  187. <div id="betby" className={`${styles.container} ${isMobile()?styles.container_m:''}`}></div>
  188. )
  189. }
  190. export default WSport;