Files
sake/build/resources/main/static/mobile/js/bnft/bnftMain/BnftMain.js

344 lines
12 KiB
JavaScript

/**
* 혜택 메인 페이지 관련 자바스크립트
*/
// 전역 변수로 디바이스 권한 정보 저장
var globalDevicePermission = {
phacAgreAt: null // 신체활동접근 권한 여부
};
$(document).ready(function() {
// 배너 슬라이드 초기화
initBannerSlide();
// 리스트 애니메이션 효과
initListAnimation();
// 상단 이동 버튼 이벤트
initTopButton();
// 혜택 메뉴 클릭 이벤트 초기화
initBenefitMenuClick();
// 모달 초기화
initModal();
// 마케팅동의 팝업 표시
mbrMarktClauseAgreePopup();
var needDeviceIdCheck = $('#needDeviceIdCheck').val(); // deviceId가 없었는지 플래그
// 1차 요청 결과 확인
if (needDeviceIdCheck || !bnftMainData || bnftMainData.error) {
console.log('deviceId가 없거나 데이터 조회 실패, 2차 요청 시작');
// window.app.get_device_info()로 deviceId 조회
var json_str = window.app.get_device_info();
var result = JSON.parse(json_str);
var deviceId = result.deviceId; // 디바이스 고유ID(IME)
if (deviceId) {
// 2차 AJAX 요청
$.ajax({
url: '/sopmal/bnft/devicePermission',
method: 'POST',
contentType: 'application/json',
data: JSON.stringify({
deviceId: deviceId
}),
success: function(response) {
console.log('2차 요청 성공:', response);
if (response.statusCode === 0 && response.resBody && response.resBody.result) {
var devicePermissionData = JSON.parse(response.resBody.result);
console.log('디바이스 권한 정보:', devicePermissionData);
// 전역 변수에 권한 정보 저장
globalDevicePermission.phacAgreAt = devicePermissionData.phacAgreAt;
// 디바이스 권한 정보를 기반으로 혜택 데이터 재조회 또는 처리
if (devicePermissionData.phacAgreAt === 'Y') {
console.log('신체활동접근 권한이 허용되었습니다.');
} else {
console.log('신체활동접근 권한이 거부되었습니다.');
}
} else {
console.error('응답 형식이 올바르지 않습니다:', response);
showErrorMessage('디바이스 권한 정보 조회에 실패했습니다.');
}
},
error: function(xhr, status, error) {
console.error('2차 요청 실패:', error);
showErrorMessage('혜택 정보 조회 중 오류가 발생했습니다.');
}
});
} else {
console.error('deviceId 조회 실패');
showErrorMessage('디바이스 정보를 확인할 수 없습니다.');
}
} else {
console.log('1차 요청으로 데이터 조회 완료');
// 1차 요청이 성공한 경우 기존 데이터 사용
renderBnftMainData(bnftMainData);
}
// URL 파라미터 읽어서 표시
const urlParams = new URLSearchParams(window.location.search);
const strInnb = urlParams.get('strInnb');
const sopmalCd = urlParams.get('sopmalCd');
if (strInnb || sopmalCd) {
$('#qr-info-display').show();
$('#store-id-value').text(strInnb || '-');
$('#sopmal-cd-value').text(sopmalCd || '-');
}
});
/**
* 배너 슬라이드 초기화 함수
*/
function initBannerSlide() {
// 배너가 있을 경우만 슬라이드 초기화
if ($('.benefit_main_bannerWrap').length > 0) {
const $slides = $('.benefit_bn');
const slideCount = $slides.length;
// 슬라이드가 1개 이상인 경우에만 실행
if (slideCount > 0) {
let currentIndex = 0;
let interval;
// 페이지네이션 버튼 생성
createPagination();
// 첫 번째 슬라이드 표시
showSlide(currentIndex);
// 자동 슬라이드 시작
startSliding();
// 슬라이드 영역에 마우스 진입/이탈 시 자동 슬라이드 제어
$('.benefit_main_bannerWrap').hover(stopSliding, startSliding);
// 페이지네이션 버튼 클릭 이벤트
$('.pagination').on('click', 'button', function() {
currentIndex = $(this).data('index');
showSlide(currentIndex);
});
// 슬라이드 영역 클릭 시 다음 슬라이드로 이동
$('.benefit_main_banner').on('click', function() {
nextSlide();
});
/**
* 특정 인덱스의 슬라이드 표시
*/
function showSlide(index) {
$slides.hide();
$slides.eq(index).fadeIn();
$('.pagination button').removeClass('active');
$('.pagination button').eq(index).addClass('active');
}
/**
* 페이지네이션 버튼 생성
*/
function createPagination() {
const $pagination = $('.pagination');
$pagination.empty();
for (let i = 0; i < slideCount; i++) {
$pagination.append(`<button data-index="${i}"></button>`);
}
}
/**
* 다음 슬라이드 표시
*/
function nextSlide() {
currentIndex = (currentIndex + 1) % slideCount;
showSlide(currentIndex);
}
/**
* 이전 슬라이드 표시
*/
function previousSlide() {
currentIndex = (currentIndex - 1 + slideCount) % slideCount;
showSlide(currentIndex);
}
/**
* 자동 슬라이드 시작
*/
function startSliding() {
stopSliding(); // 기존 인터벌 제거
interval = setInterval(nextSlide, 3000); // 3초마다 슬라이드 전환
}
/**
* 자동 슬라이드 중지
*/
function stopSliding() {
if (interval) {
clearInterval(interval);
}
}
}
}
}
/**
* 리스트 애니메이션 효과 초기화
*/
function initListAnimation() {
$('.item_li li').each(function(index) {
$(this).delay(index * 50).queue(function(next) {
$(this).css({
opacity: 1,
transform: 'translateY(0)'
});
next();
});
});
}
/**
* 상단 이동 버튼 이벤트 초기화
*/
function initTopButton() {
$('.quick-menu__to-top button').on('click', function() {
$('html, body').animate({
scrollTop: 0
}, 300);
});
}
/**
* 모달 초기화 함수
*/
function initModal() {
MicroModal.init({
disableScroll: true,
awaitCloseAnimation: true
});
}
function mbrMarktClauseAgreePopup() {
console.log('마케팅동의 팝업 표시');
const marktAgreeAt = $('#mbrMarktAgreeAt').val();
console.log('marktAgreeAt:', marktAgreeAt);
// 마케팅동의 체크 (메인 페이지와 동일한 방식)
if (marktAgreeAt === 'N') {
console.log('마케팅동의 필요');
// 마케팅동의 팝업 표시
const stplatDvCd = '05';
$.ajax({
type: 'post',
url: "/sopmal/mbr/selectMbrMarktAgreePopup",
data: {stplatDvCd: stplatDvCd},
success: function (data) {
$('#mbrMarktClauseAgreeDiv').html(data);
openModal('mbrMarktClauseAgreePopup');
}
});
e.preventDefault();
return;
}
}
/**
* 혜택 메뉴 클릭 이벤트 초기화
*/
function initBenefitMenuClick() {
$('.bnft-menu-item').on('click', function(e) {
const avalAt = $(this).data('aval-at');
const bnfUrl = $(this).data('bnf-url');
const bnfDvCd = String($(this).data('bnf-dv-cd'));
const isParticipated = $(this).find('.check_done').length > 0;
const ntkChlAt = $(this).data('ntk-chl-at');
const ntkBnfInnb = $(this).data('ntk-bnf-innb');
const cpslProgrsAt = $(this).data('cpsl-progrs-at');
const cpslBnfInnb = $(this).data('cpsl-bnf-innb');
const intrstAvalAt = $(this).data('intrst-aval-at');
// 디버깅 로그 추가
console.log('=== 혜택 메뉴 클릭 디버깅 ===');
console.log('avalAt:', avalAt, '(타입:', typeof avalAt, ')');
console.log('intrstAvalAt:', intrstAvalAt, '(타입:', typeof intrstAvalAt, ')');
console.log('bnfDvCd:', bnfDvCd);
console.log('bnfUrl:', bnfUrl);
console.log('isParticipated:', isParticipated);
console.log('globalDevicePermission.phacAgreAt:', globalDevicePermission.phacAgreAt);
console.log('=============================');
// 만보기(020) 메뉴 클릭 시 권한 체크
if (bnfDvCd === '020' && (avalAt === 'N' || globalDevicePermission.phacAgreAt === 'N')) {
console.log('만보기 신체활동 권한 미허용으로 차단됨');
MicroModal.show('modal-stepcnt-permission');
e.preventDefault();
return;
}
// 관심건강분야 설정(120) 메뉴에서만 관심분야 설정 여부 체크
if (bnfDvCd === '120' && intrstAvalAt === 'N') {
console.log('관심분야 미설정으로 차단됨');
MicroModal.show('modal-bnft-unavailable');
e.preventDefault();
return;
}
// 행운복권(030)이고 참여완료 상태인 경우
if (bnfDvCd === '030' && isParticipated) {
// 행운복권 참여완료 모달 표시
MicroModal.show('modal-lucky-participated');
e.preventDefault();
}
// OX 퀴즈(130)이고 참여완료 상태인 경우
else if (bnfDvCd === '130' && isParticipated) {
// OX 퀴즈 참여완료 모달 표시
MicroModal.show('modal-oxquiz-participated');
e.preventDefault();
}
// 캡슐만들기(050)인 경우 진행 중인 게임 여부에 따라 분기
else if (bnfDvCd === '050' && avalAt === 'Y') {
if (cpslProgrsAt === 'Y' && cpslBnfInnb) {
// 진행 중인 게임이 있는 경우 - createGame으로 bnfInnb와 함께 이동
window.location.href = '/sopmal/bnft/createGame?bnfInnb=' + cpslBnfInnb;
} else {
// 진행 중인 게임이 없는 경우 - selectCapsule로 이동
window.location.href = '/sopmal/bnft/selectCapsule';
}
e.preventDefault();
}
// 섭취챌린지(100)인 경우 참여 여부에 따라 분기
else if (bnfDvCd === '100' && avalAt === 'Y') {
if (ntkChlAt === 'Y' && ntkBnfInnb) {
// 참여 중인 섭취챌린지가 있는 경우
window.location.href = '/sopmal/bnft/intakchlDailyRecordMain?bnfInnb=' + ntkBnfInnb;
} else {
// 참여 중인 섭취챌린지가 없는 경우
window.location.href = bnfUrl;
}
e.preventDefault();
}
else if (avalAt === 'Y') {
console.log('정상 이동:', bnfUrl);
window.location.href = bnfUrl;
} else {
console.log('avalAt 값이 Y가 아니어서 차단됨. 실제값:', avalAt);
MicroModal.show('modal-bnft-unavailable');
e.preventDefault();
}
});
}