// 所有记录入口使用同一绑定检查,个人资料、头像与邀请不受影响。 let showing = false; function hasRecordAccess() { const id = wx.getStorageSync('coupleId'); return !!id && String(id) !== '0'; } function invitePartner() { getApp().globalData.openProfileBind = true; wx.switchTab({url:'/pages/profile/profile'}); } function requireRecordAccess(onCancel) { if (hasRecordAccess()) return true; if (showing) return false; showing = true; wx.showModal({ title:'邀请另一半,开启共同记录', content:'绑定情侣后,就可以一起写日记、收藏照片、记录纪念日和使用其他记录功能。邀请TA加入,开始你们的生活手账吧。', confirmText:'立即邀请', cancelText:'暂时不了', confirmColor:'#B74768', success(result) { if (result.confirm) invitePartner(); else if (onCancel) onCancel(); }, fail() { if (onCancel) onCancel(); }, complete() { showing = false; } }); return false; } function guardRecordEntries(definition, names) { names.forEach(name => { const original = definition[name]; if (original) definition[name] = function(...args) { if (!requireRecordAccess()) return; return original.apply(this, args); }; }); return definition; } // 分享链接、历史页面栈等直接进入二级页面时同样拦截,不加载数据。 function withRecordAccess(definition) { function enter(page, original, args) { const allowed = hasRecordAccess(); page.setData({recordAccessBlocked:!allowed}); if (!allowed) { requireRecordAccess(() => wx.switchTab({url:'/pages/index/index'})); return; } return original && original.apply(page, args); } const load = definition.onLoad, show = definition.onShow, change = definition.onDataChanged; definition.onLoad = function(...args) { this.setData({recordAccessBlocked:true}); const app = typeof getApp === 'function' && getApp(); this._recordEntryReady = Promise.resolve(app && app.ensureAuth ? app.ensureAuth() : null) .then(() => enter(this, load, args)).catch(error => { console.warn('检查记录权限失败', error); wx.showToast({title:'登录状态获取失败,请重新进入',icon:'none'}); }); return this._recordEntryReady; }; definition.onShow = function(...args) { return Promise.resolve(this._recordEntryReady).then(() => enter(this, show, args)); }; definition.onDataChanged = function(...args) { return enter(this, change, args); }; return definition; } module.exports = {hasRecordAccess, requireRecordAccess, invitePartner, guardRecordEntries, withRecordAccess};