187 lines
6.0 KiB
JavaScript
187 lines
6.0 KiB
JavaScript
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: '',
|
|
completedTime: '',
|
|
_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,
|
|
completedTime: res.completed_at ? res.completed_at.substring(0, 16).replace('T', ' ') : '',
|
|
});
|
|
|
|
if (!self._visible) return;
|
|
|
|
// 清除之前的轮询
|
|
if (self.data._pollTimer) {
|
|
clearInterval(self.data._pollTimer);
|
|
self.data._pollTimer = null;
|
|
}
|
|
|
|
if (status === 'completed') {
|
|
wx.removeStorageSync('coupleId'); wx.removeStorageSync('petHasPet');
|
|
require('../../utils/data-cache').clear();
|
|
return;
|
|
}
|
|
if ((status === 'pending' || status === 'cooling') && remaining > 0) {
|
|
self.startTimer(remaining);
|
|
self.data._pollTimer = setInterval(function() { self.loadStatus(); }, 10000);
|
|
}
|
|
}).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._deadline = Date.now() + seconds * 1000;
|
|
self.data._remaining = seconds;
|
|
self.data._timer = setInterval(function() {
|
|
self.data._remaining = Math.max(0, Math.ceil((self._deadline - Date.now()) / 1000));
|
|
if (self.data._remaining <= 0) {
|
|
clearInterval(self.data._timer);
|
|
self.loadStatus();
|
|
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: '同意后进入72小时冷静期,期间任一方可以取消。到期自动解除关系并永久删除共同数据,个人抽烟记录保留。',
|
|
cancelText: '再想想',
|
|
confirmText: '确认解绑',
|
|
confirmColor: '#FF4757',
|
|
success: function(res) {
|
|
if (res.confirm) {
|
|
wx.showLoading({ title: '处理中...' });
|
|
api.unbindRespond({ agree: true }).then(function() {
|
|
wx.hideLoading();
|
|
wx.showToast({ title: '已进入72小时冷静期', icon: 'none' });
|
|
self.loadStatus();
|
|
}).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.loadStatus();
|
|
}).catch(function(err) {
|
|
wx.hideLoading();
|
|
wx.showToast({ title: err.error || '操作失败', icon: 'none' });
|
|
});
|
|
}
|
|
}
|
|
});
|
|
},
|
|
|
|
goBack: function() { wx.navigateBack(); },
|
|
goHome: function() { wx.switchTab({ url: '/pages/index/index' }); },
|
|
goToProfile: function() { wx.switchTab({ url: '/pages/profile/profile' }); },
|
|
});
|