wechatFollowReminder.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // 仅内存去重:工作台重建不会再弹,冷启动或重新登录可以再次检查。
  2. const sessionMemory = { handledSession: '' }
  3. export function createWechatFollowReminder({
  4. getSessionKey, isActive, loadStatus, showPrompt, openGuide, memory = sessionMemory
  5. }) {
  6. let revision = 0
  7. let pending = false
  8. const cancel = () => {
  9. revision++
  10. pending = false
  11. }
  12. const check = async () => {
  13. const session = getSessionKey()
  14. if (!session || !isActive() || pending || memory.handledSession === session) return
  15. const currentRevision = ++revision
  16. const current = () => revision === currentRevision && isActive() && getSessionKey() === session
  17. pending = true
  18. let presenting = false
  19. try {
  20. const status = await loadStatus()
  21. if (!current()) return
  22. memory.handledSession = session
  23. if (!status.enabled || (status.bound && status.subscribed)) return
  24. presenting = true
  25. const confirmed = await showPrompt({
  26. title: status.bound ? '关注服务号' : '开启服务号通知',
  27. content: status.bound
  28. ? '您绑定的微信尚未关注“山峡化工”服务号,暂时无法接收业务通知。是否前往关注?'
  29. : '关注“山峡化工”服务号并绑定门户账号,即可接收审批、发放及证件提醒。如已关注,请完成账号绑定。',
  30. confirmText: status.bound ? '去关注' : '去开启',
  31. cancelText: '稍后再说',
  32. confirmColor: '#0a9f6d'
  33. })
  34. if (confirmed && current()) openGuide()
  35. } catch {
  36. // 状态不可用时不打断登录;弹窗自身失败允许下次进入工作台重试。
  37. if (presenting && current() && memory.handledSession === session) {
  38. memory.handledSession = ''
  39. }
  40. } finally {
  41. if (revision === currentRevision) pending = false
  42. }
  43. }
  44. return { check, cancel }
  45. }