const { getLayout } = require('../../utils/layout'); // pages/unbind/unbind.js - 情侣解绑确认页 const { api } = require('../../utils/request'); Page({ onResize: function() { this.setData(getLayout()); }, data: { loading: true, loadError: false, status: 'none', isRequester: false, requesterName: '', remaining: 0, timerText: '', createdTime: '', respondedTime: '', _timer: null, }, onLoad: function() { this.setData(getLayout()); }, onShow: function() { this._visible = true; // 每次页面显示时重新加载状态(对方可能已经操作了) this.loadStatus(); }, onHide: function() { this._visible = false; this.stopTimers(); }, onUnload: function() { this._visible = false; this.stopTimers(); }, stopTimers: function() { if (this.data._timer) clearInterval(this.data._timer); if (this.data._pollTimer) clearInterval(this.data._pollTimer); this.data._timer = null; this.data._pollTimer = null; }, loadStatus: function() { if (this._statusLoading) return; this._statusLoading = true; var self = this; self.stopTimers(); self.setData({ loading: true, loadError:false }); api.unbindStatus().then(function(res) { var status = res.status || 'none'; var remaining = res.remaining_seconds || 0; var timerText = self.formatRemaining(remaining); var createdTime = res.created_at ? res.created_at.substring(0, 16).replace('T', ' ') : ''; var respondedTime = res.responded_at ? res.responded_at.substring(0, 16).replace('T', ' ') : ''; self.setData({ loading: false, status: status, isRequester: !!res.is_requester, requesterName: res.requester_name || '对方', remaining: remaining, timerText: timerText, createdTime: createdTime, respondedTime: respondedTime, }); if (!self._visible) return; // 清除之前的轮询 if (self.data._pollTimer) { clearInterval(self.data._pollTimer); self.data._pollTimer = null; } // 如果状态为 agreed(双方已同意解绑),清除缓存后跳转到登录页 if (status === 'agreed') { wx.removeStorageSync('coupleId'); wx.removeStorageSync('petHasPet'); wx.showToast({ title: '情侣关系已解除', icon: 'none' }); setTimeout(function() { wx.reLaunch({ url: '/pages/index/index' }); }, 2000); return; } // 启动倒计时 if (status === 'pending' && remaining > 0) { self.startTimer(remaining); // 如果是发起方,启动轮询检查对方是否已同意 if (self.data.isRequester) { self.data._pollTimer = setInterval(function() { api.unbindStatus().then(function(pollRes) { if (pollRes.status === 'agreed') { clearInterval(self.data._pollTimer); self.data._pollTimer = null; wx.removeStorageSync('coupleId'); wx.removeStorageSync('petHasPet'); wx.showToast({ title: '对方已同意解绑,正在跳转...', icon: 'none' }); setTimeout(function() { wx.reLaunch({ url: '/pages/index/index' }); }, 2000); } }).catch(function() {}); }, 5000); } } }).catch(function(err) { self.setData({ loading: false, loadError:true }); }).finally(function() { self._statusLoading = false; }); }, startTimer: function(seconds) { var self = this; if (self.data._timer) clearInterval(self.data._timer); self.data._remaining = seconds; self.data._timer = setInterval(function() { self.data._remaining--; if (self.data._remaining <= 0) { clearInterval(self.data._timer); self.setData({ status: 'expired', timerText: '已过期' }); return; } self.setData({ timerText: self.formatRemaining(self.data._remaining) }); }, 1000); }, formatRemaining: function(seconds) { if (seconds <= 0) return '已过期'; var h = Math.floor(seconds / 3600); var m = Math.floor((seconds % 3600) / 60); var s = seconds % 60; return h + '时 ' + m + '分 ' + s + '秒'; }, // 同意解绑 agreeUnbind: function() { var self = this; wx.showModal({ title: '💔 确认解绑', content: '确定同意解除情侣绑定吗?\n\n确认后所有情侣记录(记忆日、日记、相册、歌曲、打卡)将被永久删除,且不可恢复!\n\n仅保留双方的个人资料。', cancelText: '再想想', confirmText: '确认解绑', confirmColor: '#FF4757', success: function(res) { if (res.confirm) { wx.showLoading({ title: '处理中...' }); api.unbindRespond({ agree: true }).then(function() { wx.hideLoading(); wx.showToast({ title: '已解除绑定', icon: 'success' }); wx.removeStorageSync('coupleId'); wx.removeStorageSync('petHasPet'); self.setData({ status: 'agreed' }); setTimeout(function() { wx.reLaunch({ url: '/pages/index/index' }); }, 1500); }).catch(function(err) { wx.hideLoading(); wx.showToast({ title: err.error || '操作失败', icon: 'none' }); }); } } }); }, // 拒绝解绑 rejectUnbind: function() { var self = this; wx.showModal({ title: '拒绝解绑', content: '确定拒绝对方的解绑请求吗?', cancelText: '取消', confirmText: '拒绝解绑', success: function(res) { if (res.confirm) { wx.showLoading({ title: '处理中...' }); api.unbindRespond({ agree: false }).then(function() { wx.hideLoading(); wx.showToast({ title: '已拒绝解绑', icon: 'success' }); self.setData({ status: 'rejected' }); }).catch(function(err) { wx.hideLoading(); wx.showToast({ title: err.error || '操作失败', icon: 'none' }); }); } } }); }, // 撤销解绑请求 cancelUnbind: function() { var self = this; wx.showModal({ title: '撤销请求', content: '确定撤销解绑请求吗?', cancelText: '取消', confirmText: '撤销', success: function(res) { if (res.confirm) { wx.showLoading({ title: '处理中...' }); api.unbindCancel().then(function() { wx.hideLoading(); wx.showToast({ title: '已撤销', icon: 'success' }); self.setData({ status: 'cancelled' }); }).catch(function(err) { wx.hideLoading(); wx.showToast({ title: err.error || '操作失败', icon: 'none' }); }); } } }); }, goBack: function() { wx.navigateBack(); }, goHome: function() { wx.reLaunch({ url: '/pages/index/index' }); }, goToProfile: function() { wx.reLaunch({ url: '/pages/profile/profile' }); }, });