diff --git a/activity/invite/index.html b/activity/invite/index.html index e9ed401..e29205f 100644 --- a/activity/invite/index.html +++ b/activity/invite/index.html @@ -313,9 +313,9 @@ })(); - + - + diff --git a/activity/invite/script.js b/activity/invite/script.js index 458e755..ada3c57 100644 --- a/activity/invite/script.js +++ b/activity/invite/script.js @@ -33,12 +33,16 @@ serverTimeMS: 0, pendingInviteCode: '', searchedInviter: null, + boundInviter: null, + boundInviterLoaded: false, + boundInviterLoading: null, inviteRewardStatus: null, rankRows: [], rankLoaded: false, rankLoading: false, searchingInviter: false, bindingInviter: false, + viewingBoundInviter: false, claiming: {}, }; @@ -216,6 +220,7 @@ function canShowBindInviteButton() { var api = window.HyAppAPI || {}; var hasToken = api.getAccessToken && api.getAccessToken(); + if (state.boundInviter) return true; if (hasToken && !state.currentUserTimingLoaded) return false; var current = state.currentUser || {}; var createdAtMS = @@ -306,6 +311,53 @@ renderBindInviteVisibility(); } + function applyBoundInviteReferrer(payload) { + var invite = payload && (payload.invite || payload.Invite); + var inviter = normalizeInviter(payload, ''); + if (!inviter) { + state.boundInviter = null; + renderBindInviteVisibility(); + return; + } + inviter.inviteCode = + textValue( + firstObjectValue(invite, [ + 'invite_code', + 'inviteCode', + 'my_invite_code', + 'myInviteCode', + ]) + ) || inviter.inviteCode; + state.boundInviter = inviter; + state.searchedInviter = inviter; + renderBindInviteVisibility(); + } + + function loadBoundInviteReferrer() { + var api = window.HyAppAPI && window.HyAppAPI.user; + var rootAPI = window.HyAppAPI || {}; + if (state.boundInviterLoading) return state.boundInviterLoading; + if ( + !api || + !api.getInviteReferrer || + !rootAPI.getAccessToken || + !rootAPI.getAccessToken() + ) { + state.boundInviterLoaded = true; + return Promise.resolve(); + } + // 已绑定关系必须每次从服务端读取;本地只缓存展示结果,避免刷新后重新进入搜索绑定流程。 + state.boundInviterLoading = api + .getInviteReferrer() + .then(applyBoundInviteReferrer) + .catch(function () {}) + .then(function () { + state.boundInviterLoaded = true; + state.boundInviterLoading = null; + }); + return state.boundInviterLoading; + } + function loadCurrentUser() { var api = window.HyAppAPI || {}; var userAPI = api.user || api.users; @@ -943,16 +995,18 @@ return ''; } - function openBindModal() { - if (!canShowBindInviteButton()) { - renderBindInviteVisibility(); - return; - } - var api = window.HyAppAPI || {}; - if (!api.getAccessToken || !api.getAccessToken()) { - toast(t('invite.loginRequired', 'Please log in first')); - return; + function showBoundInviterModal() { + if (state.boundInviter) { + state.pendingInviteCode = ''; + state.searchedInviter = state.boundInviter; + state.searchingInviter = false; + state.bindingInviter = false; + showBindResult(state.boundInviter, { bound: true }); + $('#bindInviteModal').hidden = false; } + } + + function showBindSearchModal() { state.pendingInviteCode = ''; state.searchedInviter = null; state.searchingInviter = false; @@ -966,10 +1020,38 @@ }, 0); } + function openBindModal() { + if (!canShowBindInviteButton()) { + renderBindInviteVisibility(); + return; + } + var api = window.HyAppAPI || {}; + if (!api.getAccessToken || !api.getAccessToken()) { + toast(t('invite.loginRequired', 'Please log in first')); + return; + } + if (state.boundInviter) { + showBoundInviterModal(); + return; + } + if (!state.boundInviterLoaded) { + loadBoundInviteReferrer().then(function () { + if (state.boundInviter) { + showBoundInviterModal(); + return; + } + showBindSearchModal(); + }); + return; + } + showBindSearchModal(); + } + function closeBindModal() { $('#bindInviteModal').hidden = true; state.searchingInviter = false; state.bindingInviter = false; + state.viewingBoundInviter = false; renderBindActions(); } @@ -977,10 +1059,11 @@ $('#bindInputCard').hidden = false; $('#bindResultCard').hidden = true; state.searchedInviter = null; + state.viewingBoundInviter = false; renderBindActions(); } - function showBindResult(inviter) { + function showBindResult(inviter, options) { var avatar = $('#inviterAvatar'); var name = $('#inviterName'); var displayID = $('#inviterDisplayId'); @@ -995,6 +1078,7 @@ if (displayID) displayID.textContent = inviter.displayUserID || '--'; $('#bindInputCard').hidden = true; $('#bindResultCard').hidden = false; + state.viewingBoundInviter = Boolean(options && options.bound); renderBindActions(); } @@ -1004,14 +1088,22 @@ var input = $('#bindInviteInput'); var busy = state.searchingInviter || state.bindingInviter; if (confirm) { - confirm.disabled = busy; - confirm.textContent = state.bindingInviter - ? t('invite.binding', 'Binding...') - : state.searchingInviter - ? t('invite.searchingInviter', 'Searching...') - : t('invite.confirm', 'Confirm'); + confirm.hidden = state.viewingBoundInviter; + confirm.disabled = state.viewingBoundInviter || busy; + if (!state.viewingBoundInviter) { + confirm.textContent = state.bindingInviter + ? t('invite.binding', 'Binding...') + : state.searchingInviter + ? t('invite.searchingInviter', 'Searching...') + : t('invite.confirm', 'Confirm'); + } + } + if (cancel) { + cancel.disabled = busy; + cancel.textContent = state.viewingBoundInviter + ? t('invite.close', 'Close') + : t('invite.cancel', 'Cancel'); } - if (cancel) cancel.disabled = busy; if (input) input.disabled = busy; } @@ -1135,6 +1227,10 @@ } function confirmBindInviter() { + if (state.viewingBoundInviter) { + closeBindModal(); + return; + } if (state.bindingInviter) return; var api = window.HyAppAPI && window.HyAppAPI.user; var inviter = state.searchedInviter; @@ -1154,9 +1250,24 @@ state.bindingInviter = false; renderBindActions(); if (invite && (invite.bound || invite.invite_code)) { + var boundInviter = normalizeInviter(response, '') || inviter; + boundInviter.inviteCode = + textValue( + firstObjectValue(invite, [ + 'invite_code', + 'inviteCode', + ]) + ) || + inviter.inviteCode || + state.pendingInviteCode; + state.boundInviter = boundInviter; + state.boundInviterLoaded = true; + state.searchedInviter = boundInviter; + renderBindInviteVisibility(); toast(t('invite.bindSuccess', 'Bound successfully')); closeBindModal(); loadCurrentUser().catch(function () {}); + loadBoundInviteReferrer(); loadInviteActivityReward(); return; } @@ -1218,6 +1329,7 @@ loadCurrentUser().catch(function () { renderInviteShare(); }), + loadBoundInviteReferrer(), loadInviteActivityReward(), ]); } diff --git a/common/api.js b/common/api.js index 31079d9..3fd14ca 100644 --- a/common/api.js +++ b/common/api.js @@ -556,6 +556,11 @@ var default_api = 'https://api.global-interaction.com/'; } ); }, + getInviteReferrer: function () { + return request('/api/v1/users/me/invite-referrer', { + method: 'GET', + }); + }, searchInviteReferrer: function (inviteCode) { return request('/api/v1/users/me/invite-referrer/search', { method: 'GET',