Files
shanxia2.0/miniprogram/utils/wechatFollowReminder.js
T

50 lines
1.9 KiB
JavaScript

// 仅内存去重:工作台重建不会再弹,冷启动或重新登录可以再次检查。
const sessionMemory = { handledSession: '' }
export function createWechatFollowReminder({
getSessionKey, isActive, loadStatus, showPrompt, openGuide, memory = sessionMemory
}) {
let revision = 0
let pending = false
const cancel = () => {
revision++
pending = false
}
const check = async () => {
const session = getSessionKey()
if (!session || !isActive() || pending || memory.handledSession === session) return
const currentRevision = ++revision
const current = () => revision === currentRevision && isActive() && getSessionKey() === session
pending = true
let presenting = false
try {
const status = await loadStatus()
if (!current()) return
memory.handledSession = session
if (!status.enabled || (status.bound && status.subscribed)) return
presenting = true
const confirmed = await showPrompt({
title: status.bound ? '关注服务号' : '开启服务号通知',
content: status.bound
? '您绑定的微信尚未关注“山峡化工”服务号,暂时无法接收业务通知。是否前往关注?'
: '关注“山峡化工”服务号并绑定门户账号,即可接收审批、发放及证件提醒。如已关注,请完成账号绑定。',
confirmText: status.bound ? '去关注' : '去开启',
cancelText: '稍后再说',
confirmColor: '#0a9f6d'
})
if (confirmed && current()) openGuide()
} catch {
// 状态不可用时不打断登录;弹窗自身失败允许下次进入工作台重试。
if (presenting && current() && memory.handledSession === session) {
memory.handledSession = ''
}
} finally {
if (revision === currentRevision) pending = false
}
}
return { check, cancel }
}