(async () => { const { utils, EventManager, } = ShopbySkin; var scriptElm = document.createElement('script'); scriptElm.src = '//developers.kakao.com/sdk/js/kakao.js'; document.head.appendChild(scriptElm); const ARR_LOCALHOST = [ 'localhost:3017', 'localhost:3000', ] const REAL_DOMAIN = 'm.energy-scoop.com'; const replaceLocalUrl = (link) => { let resultUrl = link; for ( const localhost of ARR_LOCALHOST ) { resultUrl = resultUrl.replace(localhost, REAL_DOMAIN) } return resultUrl; } document.getElementById('btnCopy').addEventListener('click', function () { const code = document.getElementById('recommendCode').innerText; // 최신 브라우저 if (navigator.clipboard && window.isSecureContext) { navigator.clipboard.writeText(code).then(() => { alert('초대코드가 복사되었습니다'); }); } // 구형 브라우저 / iOS 대응 else { const textarea = document.createElement('textarea'); textarea.value = code; textarea.style.position = 'fixed'; textarea.style.opacity = '0'; document.body.appendChild(textarea); textarea.focus(); textarea.select(); try { document.execCommand('copy'); alert('초대코드가 복사되었습니다'); } catch (err) { alert('복사에 실패했습니다.'); } document.body.removeChild(textarea); } }); const redirectLoginPage = () => { const nextUrl = `${location.origin}/pages/sign-in/sign-in.html`; location.href = `${nextUrl}?from=${encodeURIComponent(location.href)}`; }; const loginModal = () => { ShopbySkin.EventManager.fire('MODAL_ALERT_OPEN', { message: '로그인 후 이용할 수 있습니다.', onClose: () => { redirectLoginPage(); loginModal(); }, }); }; const appModal = () => { ShopbySkin.EventManager.fire('MODAL_ALERT_OPEN', { message: '해당 이벤트는 앱 전용입니다. ', label: '확인', onClose: () => { redirectAppPage('store'); appModal(); }, }); }; const redirectAppPage = (mode) => { const url = 'https://' + window.location.host + '/pages/invite/event-invite.html'; const pn = "com.nhncommerce.myapp.sbt5j52fn8"; if (navigator.userAgent.match(/Android/i)) { if(mode === 'app'){ location.href = pn+"://deeplink?url="+url } if(mode === 'store'){ var clickedAt = +new Date; var timer = setTimeout(function () { if (+new Date - clickedAt < 1800) { if (navigator.userAgent.match(/kakaotalk/i)) { location.href = "kakaotalk://web/openExternal?url="+encodeURIComponent("market://details?id="+pn+"&hl=ko"); setTimeout(function () { location.href = "kakaoweb://inappbrowser/close"; }, 1500); }else{ location.href = "market://details?id=" + pn; } } }, 1500); } window.onblur = function() { if(mode === 'store'){ clearTimeout(timer); } location.href = "kakaoweb://inappbrowser/close"; }; } else if (navigator.userAgent.match(/iPhone|iPad|iPod|mac/i)) { var appstore_url = "https://itunes.apple.com/kr/app/id6756481072?mt=8"; if(mode === 'store'){ var clickedAt = +new Date; var timer = setTimeout(function () { if (+new Date - clickedAt < 2000) { if (navigator.userAgent.match(/kakaotalk/i)) { location.href = "kakaotalk://web/openExternal?url="+encodeURIComponent(appstore_url); setTimeout(function () { location.href = "kakaoweb://closeBrowser"; }, 1500); }else{ location.href = appstore_url; } } }, 1500); } if(mode === 'app'){ location.href = pn+"://deeplink?url="+url; } window.onblur = function() { if(mode === 'store'){ clearTimeout(timer); } if (navigator.userAgent.match(/kakaotalk/i)) { location.href = "kakaoweb://closeBrowser"; } }; }else{ if(mode === 'store'){ window.open("https://play.google.com/store/apps/details?id="+pn+"&hl=ko"); } } }; EventManager.on('PAGE_LOAD_COMPLETED', async () => { if(window.myapp?.helpers?.isMyApp() === false){ appModal(); redirectAppPage('app'); }else{ if (!utils.isSignIn()) { loginModal(); return; }else{ const { data: profile } = await ShopbySkin.actions.queryProfile(); const kaimenApp = new KaimenApp(); let code = ''; /*const result = await Promise.all([ kaimenApp.getRecommendCode({ queryString: { memberNo: profile.memberNo, } }), ]); const data = result.map(({ data }) => data); let code = data[0]['code']; if(code == ''){*/ const result = await Promise.all([ kaimenApp.setRecommendCode({ requestBody: { memberNo: profile.memberNo, memberId: profile.memberId !== null ? profile.memberId : '', email: profile.email !== null ? profile.email : '', } }), ]); const data = result.map(({ data }) => data); code = data[0]['code']; //} document.querySelector("#recommendCode").innerText = code; Kakao.init('11925b34495a8d51b14fa2c00a497526'); //real let link = 'https://' + window.location.host + '/pages/invite/event-share.html?code=' + code; const currentLink = replaceLocalUrl(link); const imageUrl = (document.querySelector("#inviteKakaoImg [slot='banner'] a img") !== null ? document.querySelector("#inviteKakaoImg [slot='banner'] a img")?.src : document.querySelector("#inviteKakaoImg [slot='banner'] img") !== null ? document.querySelector("#inviteKakaoImg [slot='banner'] img")?.src : '').replace("?", ""); Kakao.Share.createDefaultButton({ container: '#kakaotalk-sharing-btn', objectType: 'feed', content: { title: '초대코드 [' + code + ']', description: '지금 초대를 수락하세요!', imageUrl: imageUrl, link: { mobileWebUrl: currentLink, webUrl: currentLink, }, }, }); } } }); })();