feat: require couple binding and refine miniapp settings
Gate record features and storage entry points with invitation guidance; enforce record access in the backend. Add homepage greetings, right-swipe profile dismissal, and move the smoking switch to settings. Simplify API base configuration and remove the unused legacy tab bar component.
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
// 所有记录入口使用同一绑定检查,个人资料、头像与邀请不受影响。
|
||||
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};
|
||||
+19
-7
@@ -64,6 +64,17 @@ function formatUploadError(data) {
|
||||
return msg;
|
||||
}
|
||||
|
||||
function maybeRedirectBinding(data) {
|
||||
if (!data || Number(data.code) !== 10002) return false;
|
||||
if (!data._bindingPromptShown) {
|
||||
data._bindingPromptShown = true;
|
||||
wx.removeStorageSync('coupleId');
|
||||
dataCache.clear();
|
||||
require('./record-access').requireRecordAccess();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// 存储配额错误码(与后端一致),上传超限时引导用户付费。
|
||||
var STORAGE_QUOTA_EXCEEDED = 10001;
|
||||
function isStorageQuotaError(data) {
|
||||
@@ -170,6 +181,7 @@ function sendRequest(action, data = null, method = 'POST', options = {}) {
|
||||
}
|
||||
if (!options.silent) { try { wx.hideLoading(); } catch(e) {} }
|
||||
if (res.statusCode >= 400 || (res.data && res.data.error)) {
|
||||
maybeRedirectBinding(res.data);
|
||||
reject({ ...fromWire(res.data), _statusCode: res.statusCode });
|
||||
} else {
|
||||
const result = fromWire(res.data);
|
||||
@@ -213,11 +225,11 @@ const api = {
|
||||
);
|
||||
wx.hideLoading();
|
||||
if (data && !data.error && (data.path || data.success)) { dataCache.mutation('uploadAvatar', data); return data; }
|
||||
if (!maybeRedirectStorage(data)) wx.showToast({ title: formatUploadError(data), icon: 'none' });
|
||||
if (!maybeRedirectBinding(data) && !maybeRedirectStorage(data)) wx.showToast({ title: formatUploadError(data), icon: 'none' });
|
||||
throw data || { error: '上传失败' };
|
||||
} catch(e) {
|
||||
wx.hideLoading();
|
||||
if (!maybeRedirectStorage(e)) wx.showToast({ title: formatUploadError(e), icon: 'none' });
|
||||
if (!maybeRedirectBinding(e) && !maybeRedirectStorage(e)) wx.showToast({ title: formatUploadError(e), icon: 'none' });
|
||||
throw e;
|
||||
}
|
||||
},
|
||||
@@ -233,11 +245,11 @@ const api = {
|
||||
);
|
||||
wx.hideLoading();
|
||||
if (data && !data.error && (data.path || data.success)) { dataCache.mutation('uploadAvatar', data); return data; }
|
||||
if (!maybeRedirectStorage(data)) wx.showToast({ title: formatUploadError(data), icon: 'none' });
|
||||
if (!maybeRedirectBinding(data) && !maybeRedirectStorage(data)) wx.showToast({ title: formatUploadError(data), icon: 'none' });
|
||||
throw data || { error: '上传失败' };
|
||||
} catch(e) {
|
||||
wx.hideLoading();
|
||||
if (!maybeRedirectStorage(e)) wx.showToast({ title: formatUploadError(e), icon: 'none' });
|
||||
if (!maybeRedirectBinding(e) && !maybeRedirectStorage(e)) wx.showToast({ title: formatUploadError(e), icon: 'none' });
|
||||
throw e;
|
||||
}
|
||||
},
|
||||
@@ -280,11 +292,11 @@ const api = {
|
||||
});
|
||||
wx.hideLoading();
|
||||
if (data && !data.error && (data.success || data.path || data.uploaded)) { dataCache.mutation('photoUpload', data); return data; }
|
||||
if (!maybeRedirectStorage(data)) wx.showToast({ title: formatUploadError(data), icon: 'none' });
|
||||
if (!maybeRedirectBinding(data) && !maybeRedirectStorage(data)) wx.showToast({ title: formatUploadError(data), icon: 'none' });
|
||||
throw data || { error: '上传失败' };
|
||||
} catch(e) {
|
||||
wx.hideLoading();
|
||||
if (!maybeRedirectStorage(e)) wx.showToast({ title: formatUploadError(e), icon: 'none' });
|
||||
if (!maybeRedirectBinding(e) && !maybeRedirectStorage(e)) wx.showToast({ title: formatUploadError(e), icon: 'none' });
|
||||
throw e;
|
||||
}
|
||||
},
|
||||
@@ -433,7 +445,7 @@ async function uploadFileCompat(options) {
|
||||
success: (res) => {
|
||||
var data = parseUploadResponse(res.data);
|
||||
if (data) {
|
||||
if (maybeRedirectStorage(data)) { reject(data); return; }
|
||||
if (maybeRedirectBinding(data) || maybeRedirectStorage(data)) { reject(data); return; }
|
||||
if (res.statusCode < 400 && !data.error && (data.success || data.path || data.uploaded)) {
|
||||
dataCache.mutation(actionMatch ? decodeURIComponent(actionMatch[1] || actionMatch[2]) : '', data);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user