/** * Purpose: options supports idle-session.js by holding or executing the options page or API role. * Usage: import, call, read, or update this let from the owning page/module while preserving the surrounding loading and permission flow. */ let options = null /** * Purpose: timer supports idle-session.js by holding or executing the timer page or API role. * Usage: import, call, read, or update this let from the owning page/module while preserving the surrounding loading and permission flow. */ let timer = null /** * Purpose: warningShown supports idle-session.js by holding or executing the warning shown page or API role. * Usage: import, call, read, or update this let from the owning page/module while preserving the surrounding loading and permission flow. */ let warningShown = false /** * Purpose: loggingOut supports idle-session.js by holding or executing the logging out page or API role. * Usage: import, call, read, or update this let from the owning page/module while preserving the surrounding loading and permission flow. */ let loggingOut = false /** * Purpose: hiddenAt supports idle-session.js by holding or executing the hidden at page or API role. * Usage: import, call, read, or update this let from the owning page/module while preserving the surrounding loading and permission flow. */ let hiddenAt = 0 /** * Purpose: lastActiveAt supports idle-session.js by holding or executing the last active at page or API role. * Usage: import, call, read, or update this let from the owning page/module while preserving the surrounding loading and permission flow. */ let lastActiveAt = Date.now() /** * Purpose: pagePatched supports idle-session.js by holding or executing the page patched page or API role. * Usage: import, call, read, or update this let from the owning page/module while preserving the surrounding loading and permission flow. */ let pagePatched = false /** * Purpose: shouldTrackMethod supports idle-session.js by holding or executing the should track method page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function shouldTrackMethod(name) { return ![ 'onHide', 'onUnload', 'onShareAppMessage', 'onShareTimeline' ].includes(name) } /** * Purpose: ensurePagePatched supports idle-session.js by holding or executing the ensure page patched page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function ensurePagePatched() { if (pagePatched || typeof Page !== 'function') { return } const originalPage = Page Page = function wrappedPage(definition) { const nextDefinition = {} Object.keys(definition || {}).forEach(key => { const value = definition[key] if (typeof value !== 'function') { nextDefinition[key] = value return } nextDefinition[key] = function wrappedHandler(...args) { if (shouldTrackMethod(key)) { markActive() } return value.apply(this, args) } }) return originalPage(nextDefinition) } pagePatched = true } /** * Purpose: start supports idle-session.js by holding or executing the start page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function start() { stop() if (!options) { return } timer = setInterval(check, 15000) } /** * Purpose: stop supports idle-session.js by holding or executing the stop page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function stop() { if (timer) { clearInterval(timer) timer = null } } /** * Purpose: isLoggedIn supports idle-session.js by holding or executing the is logged in page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function isLoggedIn() { return options && typeof options.isLoggedIn === 'function' ? Boolean(options.isLoggedIn()) : false } /** * Purpose: markActive supports idle-session.js by holding or executing the mark active page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function markActive() { if (!options || !isLoggedIn() || loggingOut) { return } lastActiveAt = Date.now() warningShown = false } /** * Purpose: onAppShow supports idle-session.js by holding or executing the on app show page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function onAppShow() { if (!options) { return } if (hiddenAt > 0 && isLoggedIn()) { const hiddenDuration = Date.now() - hiddenAt if (hiddenDuration >= options.timeoutMs) { forceLogout() return } } hiddenAt = 0 markActive() start() } /** * Purpose: onAppHide supports idle-session.js by holding or executing the on app hide page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function onAppHide() { hiddenAt = Date.now() stop() } /** * Purpose: check supports idle-session.js by holding or executing the check page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function check() { if (!options || !isLoggedIn() || loggingOut) { return } const idleMs = Date.now() - lastActiveAt if (idleMs >= options.timeoutMs) { forceLogout() return } if (idleMs >= options.timeoutMs - options.warningMs && !warningShown) { showWarning() } } /** * Purpose: showWarning supports idle-session.js by holding or executing the show warning page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function showWarning() { if (!options || warningShown || loggingOut) { return } warningShown = true wx.showModal({ title: options.warningTitle || '登录即将过期', content: options.warningContent || '长时间未操作,即将退出登录。', confirmText: options.confirmText || '继续使用', cancelText: options.cancelText || '退出登录', success: result => { warningShown = false if (result.confirm) { markActive() if (typeof options.onContinue === 'function') { options.onContinue() } return } forceLogout() }, fail: () => { warningShown = false } }) } /** * Purpose: forceLogout supports idle-session.js by holding or executing the force logout page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function forceLogout() { if (!options || loggingOut) { return } loggingOut = true Promise.resolve() .then(() => (typeof options.onLogout === 'function' ? options.onLogout() : undefined)) .catch(() => undefined) .finally(() => { loggingOut = false warningShown = false hiddenAt = 0 lastActiveAt = Date.now() }) } /** * Purpose: init supports idle-session.js by holding or executing the init page or API role. * Usage: import, call, read, or update this function from the owning page/module while preserving the surrounding loading and permission flow. */ function init(nextOptions) { options = nextOptions || null lastActiveAt = Date.now() warningShown = false loggingOut = false hiddenAt = 0 ensurePagePatched() start() } module.exports = { init, start, stop, markActive, onAppShow, onAppHide }