package com.bb.front; import java.nio.charset.Charset; import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import jakarta.servlet.http.HttpServletRequest; import jakarta.validation.Valid; import org.codehaus.jettison.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestHeader; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.UriComponentsBuilder; import com.bb.exception.ApiException; import com.bb.jwt.JwtClame; import com.bb.jwt.JwtManager; import com.bb.model.ApiResponse; import com.bb.model.ApiVendorCompInfo; import com.bb.model.Banner; import com.bb.model.BoardListSearch; import com.bb.model.CashSearch; import com.bb.model.Member; import com.bb.model.Site; import com.bb.model.SiteSearch; import com.bb.service.BoardService; import com.bb.service.CashService; import com.bb.service.CommonService; import com.bb.service.UserService; import com.bb.util.IPKit; import io.jsonwebtoken.ExpiredJwtException; import io.swagger.v3.oas.annotations.security.SecurityRequirement; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @RestController @Slf4j @RequestMapping("/api") @RequiredArgsConstructor @SecurityRequirement(name = "Authorization") public class ApiFrontController { private final UserService userService; private final CashService cashService; private final BoardService boardService; private final CommonService commonService; @Autowired RestTemplate restTemplate; private final JwtManager jwtManager; @GetMapping("/health") public String health( HttpServletRequest request,ModelMap model) throws Exception { return "alive"; } @ResponseBody @PostMapping(value="/main") public ApiResponse main(@RequestHeader String token, HttpServletRequest request) throws Exception { ApiResponse apiResponse = new ApiResponse(); try { Site site = getSiteByCheck(request); if(site==null) { throw new ApiException("1000", "accessDinied"); } JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfo(token); apiResponse.success(); } catch(ApiException e) { log.error(e.toString()); apiResponse = e.getApiResponse(); } catch (Exception e) { log.error(e.toString()); e.printStackTrace(); apiResponse.fail(); } return apiResponse; } @ResponseBody @PostMapping(value="/getToken") public ApiResponse getToken(@RequestHeader String token, HttpServletRequest request) throws Exception { ApiResponse apiResponse = new ApiResponse(); try { Site site = getSiteByCheck(request); if(site==null) { throw new ApiException("1000", "accessDinied"); } JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfo(token); JwtClame jwtinfo = new JwtClame(); jwtinfo.setSiteIdx(site.getSiteIdx()); jwtinfo.setSiteId(site.getSiteId()); jwtinfo.setMemId(tokenInfo.getMid()); jwtinfo.setMemIdx(tokenInfo.getMidx()); jwtinfo.setPartnerLevel(tokenInfo.getPartnerLevel()); int tokenValidationSec = commonService.getSiteLogoutTimeLimit(site.getSiteId()); log.info("#-getToken::tokenValidationSec: " + tokenValidationSec); String tok = jwtManager.generateToken(jwtinfo, tokenValidationSec); apiResponse.put("token", tok); //로그인 로그 , try { Map logParam = new HashMap(); logParam.put("siteId", site.getSiteId()); logParam.put("memId", tokenInfo.getMid()); logParam.put("logType", "TOKEN"); logParam.put("logoutYn", ""); logParam.put("logName", "로그인토큰"); logParam.put("logDesc", "토큰갱신"); logParam.put("regId", ""); logParam.put("regIp", IPKit.getIpAddressByRequest(request)); logParam.put("token", tok); commonService.insertlog(logParam); logParam.put("actionType", 2); commonService.updateAcctionLog(logParam); } catch(Exception e){System.out.print(e.toString());} apiResponse.success(); } catch(ExpiredJwtException je) { log.error("#-getToken::"+je.getMessage()); } catch(ApiException e) { log.error("#-getToken::"+e.toString()); apiResponse = e.getApiResponse(); } catch (Exception e) { log.error("#-getToken::"+e.toString()); e.printStackTrace(); apiResponse.fail(); } return apiResponse; } @SecurityRequirement(name = "Authorization") @ResponseBody @PostMapping(value="/main/popupList") public ApiResponse popupList(@RequestHeader String token, HttpServletRequest request) throws Exception { ApiResponse apiResponse = new ApiResponse(); try { if(token==null) { throw new ApiException("1000", "accessDinied"); } JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfo(token); SiteSearch paramSite = new SiteSearch(); paramSite.setSiteId(tokenInfo.getSid()); List popupList = commonService.getLoginPopupList(paramSite); apiResponse.put("list", popupList); apiResponse.success(); } catch(ApiException e) { log.error(e.toString()); apiResponse = e.getApiResponse(); } catch (Exception e) { log.error(e.toString()); e.printStackTrace(); apiResponse.fail(); } return apiResponse; } @ResponseBody @PostMapping(value="/main/cashList") public ApiResponse cashList(HttpServletRequest request) throws Exception { ApiResponse apiResponse = new ApiResponse(); try { Site site = getSiteByCheck(request); if(site==null) { throw new ApiException("1000", "accessDinied"); } CashSearch inParam = new CashSearch(); inParam.setSiteId(site.getSiteId()); inParam.setCashType("1"); List> cashInList = cashService.getMainCashList(inParam); CashSearch outParam = new CashSearch(); outParam.setSiteId(site.getSiteId()); outParam.setCashType("-1"); List> cashOutList = cashService.getMainCashList(outParam); apiResponse.put("cashInList", cashInList); apiResponse.put("cashOutList", cashOutList); apiResponse.success(); } catch(ApiException e) { log.error(e.toString()); apiResponse = e.getApiResponse(); } catch (Exception e) { log.error(e.toString()); e.printStackTrace(); apiResponse.fail(); } return apiResponse; } @ResponseBody @PostMapping(value={"/main/nt/list"}) public ApiResponse blist(HttpServletRequest request) throws Exception { ApiResponse apiResponse = new ApiResponse(); try { Site site = getSiteByCheck(request); if(site==null) { throw new ApiException("1000", "accessDinied"); } BoardListSearch search = new BoardListSearch(); search.setSiteId(site.getSiteId()); List> boardList = boardService.getMainBoardList(search); apiResponse.put("boardList", boardList); apiResponse.success(); } catch(ApiException e) { log.error(e.toString()); apiResponse = e.getApiResponse(); } catch (Exception e) { log.error(e.toString()); e.printStackTrace(); apiResponse.fail(); } return apiResponse; } @ResponseBody @PostMapping(value={"/main/board/list"}) public ApiResponse blist2(HttpServletRequest request, @Valid @RequestBody BoardListSearch search) throws Exception { ApiResponse apiResponse = new ApiResponse(); try { Site site = getSiteByCheck(request); if(site==null) { throw new ApiException("1000", "accessDinied"); } search.setSiteId(site.getSiteId()); log.info("#-MAIN::blist2::"+site.getSiteId()+"::: BoardListSearch : " + search); List> boardList = boardService.getMainBoardList2(search); apiResponse.put("boardList", boardList); apiResponse.success(); } catch(ApiException e) { log.error(e.toString()); apiResponse = e.getApiResponse(); } catch (Exception e) { log.error(e.toString()); e.printStackTrace(); apiResponse.fail(); } return apiResponse; } @ResponseBody @PostMapping(value={"/isPlayGame"}) public ApiResponse isPlayGame(HttpServletRequest request, @RequestHeader String token) throws Exception { ApiResponse apiResponse = new ApiResponse(); try { if(token==null) { throw new ApiException("1000", "accessDinied"); } JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfo(token); Member search = new Member(); search.setMemId(tokenInfo.getMid()); search.setSiteId(tokenInfo.getSid()); search.setIntervalTime(-120); HashMap lastGameInfo = userService.getLastGameInfo(search); if(lastGameInfo != null && lastGameInfo.get("cashType").toString().equals("BET")) { // Playing Game apiResponse.put("isPlayGame", "Y"); } else { // Not Playing Game apiResponse.put("isPlayGame", "N"); } apiResponse.success(); } catch(ApiException e) { log.error(e.toString()); apiResponse = e.getApiResponse(); } catch (Exception e) { log.error(e.toString()); e.printStackTrace(); apiResponse.fail(); } return apiResponse; } @ResponseBody @GetMapping(value={"/coin"}) public ApiResponse coin(HttpServletRequest request, @RequestHeader String token, @RequestParam String symbol) throws Exception { ApiResponse apiResponse = new ApiResponse(); try { if(token==null) { throw new ApiException("1000", "accessDinied"); } JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfo(token); final String LOG_PREFIX = "#-TRIPLE::COIN::"+tokenInfo.getSid()+"::"+tokenInfo.getMid()+":::"; log.info(LOG_PREFIX+ "Request symbol : " + symbol); HashMap param = new HashMap(); param.put("siteId", tokenInfo.getSid()); param.put("apiVendorCode", "triple"); ApiVendorCompInfo vendorCompInfo = userService.getApiVendorCompInfo(param); // 헤더에 토큰 세팅 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON)); headers.set("User-Agent", "PostmanRuntime/7.28.4"); headers.set("Authorization", vendorCompInfo.getClientSecretKey()); headers.setAcceptCharset(Arrays.asList(Charset.forName("UTF-8"))); UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(vendorCompInfo.getApiUrl() + "/getToken"); HttpEntity> entity = new HttpEntity<>(null, headers); ResponseEntity tokenRes = restTemplate.postForEntity(builder.toUriString(), entity, String.class); log.info(LOG_PREFIX+ "Token Response : " + tokenRes.getBody()); JSONObject tokenObj = new JSONObject(tokenRes.getBody()); String apiToken = tokenObj.getJSONObject("data").getString("token"); headers.set("token", apiToken); builder = UriComponentsBuilder.fromHttpUrl(vendorCompInfo.getApiUrl() + "/coin?symbol=" + symbol + "¤cy=usd"); entity = new HttpEntity<>(null, headers); ResponseEntity coinRes = restTemplate.postForEntity(builder.toUriString(), entity, String.class); log.info(LOG_PREFIX+ "Coin Response : " + coinRes.getBody()); JSONObject resultObj = new JSONObject(coinRes.getBody()); JSONObject dataObj = resultObj.getJSONObject("data"); JSONObject infoObj = dataObj.getJSONObject("info"); String currencyUsd = infoObj.getString("currentPrice"); builder = UriComponentsBuilder.fromHttpUrl(vendorCompInfo.getApiUrl() + "/coin?symbol=" + symbol + "¤cy=krw"); entity = new HttpEntity<>(null, headers); coinRes = restTemplate.postForEntity(builder.toUriString(), entity, String.class); log.info(LOG_PREFIX+ "Coin Response : " + coinRes.getBody()); resultObj = new JSONObject(coinRes.getBody()); dataObj = resultObj.getJSONObject("data"); infoObj = dataObj.getJSONObject("info"); String currencyKrw = infoObj.getString("currentPrice"); HashMap dataMap = new HashMap<>(); dataMap.put("currencyUsd", currencyUsd); dataMap.put("currencyKrw", currencyKrw); apiResponse.put("info", dataMap); apiResponse.success(); } catch(ApiException e) { JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfo(token); final String LOG_PREFIX = "#-TRIPLE::COIN::"+tokenInfo.getSid()+"::"+tokenInfo.getMid()+":::"; log.error(e.toString()); apiResponse = e.getApiResponse(); } catch (Exception e) { JwtManager.TokenInfo tokenInfo = jwtManager.getTokenInfo(token); final String LOG_PREFIX = "#-TRIPLE::COIN::"+tokenInfo.getSid()+"::"+tokenInfo.getMid()+":::"; log.error(e.toString()); e.printStackTrace(); apiResponse.fail(); } return apiResponse; } private Site getSiteByCheck(HttpServletRequest request) { Site site = userService.getSiteInfoByKey(request.getHeader("Authorization").toString()); return site; } }