diff --git a/profile.html b/profile.html index e396fe0..3261b03 100644 --- a/profile.html +++ b/profile.html @@ -78,7 +78,7 @@ >帮助与服务帮助、反馈、推广 退出登录返回登录页退出登录返回首页
@@ -355,7 +355,7 @@ - 确认退出 + 确认退出
diff --git a/public/js/profile-common.js b/public/js/profile-common.js index d0e59fd..b93570e 100644 --- a/public/js/profile-common.js +++ b/public/js/profile-common.js @@ -55,9 +55,23 @@ }); } + function performLogout(targetUrl) { + var api = window.GenealogyApi && window.GenealogyApi.defaultClient; + var redirect = function () { + window.location.href = targetUrl || 'index.html'; + }; + + if (!api || !api.logout) { + redirect(); + return; + } + + api.logout().catch(function () {}).then(redirect); + } + function bindLogout() { $(document).on('click', '[data-logout-open]', function (event) { - var targetUrl = $(this).attr('data-logout-url') || 'login.html'; + var targetUrl = $(this).attr('data-logout-url') || 'index.html'; event.preventDefault(); if (!layerApi) { @@ -70,11 +84,17 @@ content: '确认退出当前账号吗?退出后需要重新登录才能继续管理家谱。', confirmText: '确认退出', onConfirm: function () { - window.location.href = targetUrl; + performLogout(targetUrl); } }); }); + $(document).on('click', '[data-logout-confirm]', function (event) { + event.preventDefault(); + closeLegacyLogout(); + performLogout($(this).attr('href') || 'index.html'); + }); + $(document).on('click', '[data-logout-close]', function () { closeLegacyLogout(); }); diff --git a/public/js/profile-pages.js b/public/js/profile-pages.js index bb7420a..2bf86f3 100644 --- a/public/js/profile-pages.js +++ b/public/js/profile-pages.js @@ -92,6 +92,28 @@ return root.GenealogyApi && root.GenealogyApi.defaultClient; } + function shouldRedirectToHome(api, error) { + var status = error && (error.status || error.code); + + return !api || !api.getToken || !api.getToken() || Number(status) === 401; + } + + function redirectToHome() { + if (!root.location) return; + if (typeof root.location.replace === 'function') { + root.location.replace('index.html'); + return; + } + root.location.href = 'index.html'; + } + + function redirectUnauthorized(api, error) { + if (!shouldRedirectToHome(api, error)) return false; + if (api && api.clearToken) api.clearToken(); + redirectToHome(); + return true; + } + function query(selector, rootNode) { if (!documentRef) return null; return (rootNode || documentRef).querySelector(selector); @@ -167,12 +189,14 @@ var profile; if (!api || !documentRef || !documentRef.querySelector('[data-profile-page]')) return; + if (redirectUnauthorized(api)) return; try { profile = await api.currentProfile(); renderProfile(buildProfileView(profile)); fillProfileForm(profile); } catch (error) { + if (redirectUnauthorized(api, error)) return; showMessage(error.message || '个人资料加载失败'); } } @@ -181,6 +205,7 @@ var api = getApi(); if (!api || !form) return; + if (redirectUnauthorized(api)) return; try { setSubmitting(form, true); @@ -190,6 +215,7 @@ showMessage('个人资料已保存'); await loadProfile(); } catch (error) { + if (redirectUnauthorized(api, error)) return; setStatus(form, '保存失败'); showMessage(error.message || '个人资料保存失败'); } finally { @@ -218,6 +244,7 @@ getAvatarText: getAvatarText, buildProfileView: buildProfileView, buildProfileUpdateBody: buildProfileUpdateBody, + shouldRedirectToHome: shouldRedirectToHome, init: init }; }); diff --git a/tests/profile-logout-structure.test.js b/tests/profile-logout-structure.test.js new file mode 100644 index 0000000..32747b0 --- /dev/null +++ b/tests/profile-logout-structure.test.js @@ -0,0 +1,15 @@ +const assert = require('assert'); +const fs = require('fs'); +const path = require('path'); + +const profileHtml = fs.readFileSync(path.join(__dirname, '..', 'profile.html'), 'utf8'); +const profileCommon = fs.readFileSync(path.join(__dirname, '..', 'public', 'js', 'profile-common.js'), 'utf8'); + +assert.match(profileHtml, /data-logout-confirm/); +assert.match(profileHtml, //); +assert.match(profileHtml, /退出登录<\/span>返回首页<\/b>/); +assert.match(profileCommon, /function performLogout\(targetUrl\)/); +assert.match(profileCommon, /api\.logout\(\)/); +assert.match(profileCommon, /targetUrl \|\| 'index\.html'/); + +console.log('profile logout structure tests passed'); diff --git a/tests/profile-pages.test.js b/tests/profile-pages.test.js index da50013..3b5c09e 100644 --- a/tests/profile-pages.test.js +++ b/tests/profile-pages.test.js @@ -61,6 +61,22 @@ function run() { districtCode: undefined }); + assert.strictEqual(ProfilePages.shouldRedirectToHome({ + getToken: () => '' + }), true); + assert.strictEqual(ProfilePages.shouldRedirectToHome({ + getToken: () => 'token-x' + }), false); + assert.strictEqual(ProfilePages.shouldRedirectToHome({ + getToken: () => 'token-x' + }, { status: 401 }), true); + assert.strictEqual(ProfilePages.shouldRedirectToHome({ + getToken: () => 'token-x' + }, { code: 401 }), true); + assert.strictEqual(ProfilePages.shouldRedirectToHome({ + getToken: () => 'token-x' + }, { status: 500 }), false); + console.log('profile-pages tests passed'); }