166 lines
3.3 KiB
JavaScript
166 lines
3.3 KiB
JavaScript
let options = null
|
|||
|
|
let timer = null
|
||
|
|
let warningShown = false
|
||
|
|
let loggingOut = false
|
||
|
|
let hiddenAt = 0
|
||
|
|
let lastActiveAt = Date.now()
|
||
|
|
let pagePatched = false
|
||
|
|
|
||
|
|
function shouldTrackMethod(name) {
|
||
|
|
return ![
|
||
|
|
'onHide',
|
||
|
|
'onUnload',
|
||
|
|
'onShareAppMessage',
|
||
|
|
'onShareTimeline'
|
||
|
|
].includes(name)
|
||
|
|
}
|
||
|
|
|
||
|
|
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
|
||
|
|
}
|
||
|
|
|
||
|
|
function start() {
|
||
|
|
stop()
|
||
|
|
if (!options) {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
timer = setInterval(check, 15000)
|
||
|
|
}
|
||
|
|
|
||
|
|
function stop() {
|
||
|
|
if (timer) {
|
||
|
|
clearInterval(timer)
|
||
|
|
timer = null
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
function isLoggedIn() {
|
||
|
|
return options && typeof options.isLoggedIn === 'function' ? Boolean(options.isLoggedIn()) : false
|
||
|
|
}
|
||
|
|
|
||
|
|
function markActive() {
|
||
|
|
if (!options || !isLoggedIn() || loggingOut) {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
lastActiveAt = Date.now()
|
||
|
|
warningShown = false
|
||
|
|
}
|
||
|
|
|
||
|
|
function onAppShow() {
|
||
|
|
if (!options) {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if (hiddenAt > 0 && isLoggedIn()) {
|
||
|
|
const hiddenDuration = Date.now() - hiddenAt
|
||
|
|
if (hiddenDuration >= options.timeoutMs) {
|
||
|
|
forceLogout()
|
||
|
|
return
|
||
|
|
}
|
||
|
|
}
|
||
|
|
hiddenAt = 0
|
||
|
|
markActive()
|
||
|
|
start()
|
||
|
|
}
|
||
|
|
|
||
|
|
function onAppHide() {
|
||
|
|
hiddenAt = Date.now()
|
||
|
|
stop()
|
||
|
|
}
|
||
|
|
|
||
|
|
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()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
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
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
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()
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
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
|
||
|
|
}
|