身份模型对齐: 新增 /auth/logout 只清授权三字段(保留孕育资料); 退出按钮改调后端; pfSave 落库 PUT /profile + 授权昵称优先 (v26090708)

This commit is contained in:
2026-09-07 23:52:30 +08:00
parent 91db3873ee
commit 712669c651
2 changed files with 48 additions and 27 deletions

View File

@@ -1929,14 +1929,18 @@
const day = Math.min(6, Math.max(0, parseInt(pgDay.value, 10) || 0));
const sexBtn = $('#pgSex .pick.is-active');
/* ---- 妈妈那半 ---- */
/* 等级徽章已按需求去掉,这里直接写整段文本 */
$('#meName').textContent = name;
/* ---- 妈妈那半 ----
身份模型:展示名「授权登录昵称优先」——state.userName 若来自授权登录
(applyRemoteProfile 写入且 _authNameFromAuth 标记),弹窗昵称不覆盖它,
只作本地展示 fallback(state.localName)。 */
if (!state._authNameFromAuth) {
$('#meName').textContent = name;
state.userName = name;
}
if (avBig.classList.contains('has-img')) {
meAvatarImg.src = avPreview.src;
meAvatar.classList.add('has-img');
}
state.userName = name;
/* ⚠️ 这里原来还有一行 state.userBio = bio 和一次 syncBio() 调用。
一句话签名整块已按用户要求删除(输入框 #pfBio、展示行 #meBio、
syncBio()、state.userBio 四处一起删),别只加回其中一处。 */
@@ -1948,6 +1952,17 @@
state.babySex = (sexBtn && sexBtn.dataset.sex) || 'unknown';
state.due = parseDay(pgDue.value);
/* ---- 落库:孕育字段走 PUT /profile(只收 babyName/babySex/dueDate,
显示字段后端明确不收)→ 刷新/重进后资料不丢。失败静默,本地已生效。 ---- */
var _oid = hashParam('openid');
if (_oid) {
fetch('/api/mp/public/mama-box/profile?openid=' + encodeURIComponent(_oid), {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ babyName: babyName, babySex: state.babySex, dueDate: state.due ? toISO(state.due) : '' })
}).catch(function () { /* 断网时保持本地值,页面照常用 */ });
}
/* ⚠️ syncPregnancy() 就是用户说的「同步到孕期卡上边」那一步 ——
它把孕周环、剩余天数、预产期、宝宝多大重刷一遍,顺带 syncBabyWords()
把全站写着小名的地方(首页三个模块名、来信页标题、回信按钮…)一起改。
@@ -3994,8 +4009,13 @@
if (_aw) _aw.classList.add('has-img');
}
var _box = $('#meAuthBox'); if (_box) _box.style.display = _auth ? 'none' : '';
/* 退出按钮只在已登录(有 userName)时显示 —— 未登录"退出"没有意义 */
var _lx = $('#meLogoutBtn'); if (_lx) _lx.style.display = p.userName ? '' : 'none';
/* 授权信息优先:后端返回的 userName/avatar 即「授权登录信息」(含 CRMEB 同步),
标记 _authNameFromAuth —— 「我的资料」弹窗保存时不覆盖它(见 pfSave)。 */
if (p.userName) {
state._authNameFromAuth = true;
}
/* 退出按钮只在已登录(任一授权资料非空)时显示 —— 未登录"退出"没有意义 */
var _lx = $('#meLogoutBtn'); if (_lx) _lx.style.display = _auth ? '' : 'none';
if (!p.userName) { var _ai2 = $('#meAvatarImg'); if (_ai2) _ai2.removeAttribute('src'); }
}
if (pg && pg.code === 200 && pg.result) {
@@ -4158,8 +4178,10 @@
var meAuth = $('#meAuthCard');
if (meAuth) meAuth.addEventListener('click', goAuth);
/* 「退出登录」(个人资料卡右上角图标按钮):清除本机身份(openid/unionid/资料展示)
→ 重新加载页面回到未登录态,便于直接测试授权流程。
/* 「退出登录」(个人资料卡右上角图标按钮):
调后端 /api/mp/public/auth/logout 只清「授权登录信息」三字段(昵称/头像/手机号),
openid/unionid(小程序匿名身份)保留,孕育资料(小名/性别/预产期)保留。
成功后重载页面 → applyRemoteProfile 重新拉到空显示字段 → 回到未登录态、授权入口重现。
⚠️ stopPropagation:me-card 整卡是 data-modal="modalProfile",不阻止冒泡
会顺带打开资料弹窗。 */
var meLogout = $('#meLogoutBtn');
@@ -4167,20 +4189,19 @@
e.preventDefault();
e.stopPropagation();
haptic();
if (!(window.wx && window.wx.miniProgram)) { toast('请在微信小程序中打开'); return; }
try { if (location.hash) location.hash = ''; } catch (e2) {}
// 清除本地读取到的 openid/unionid,回到未登录态
try {
var base = location.href.split('#')[0];
// 保留 query 里的结构与版本戳,仅去掉身份参数
var q = (location.search || '').replace(/([?&])openid=[^&]*(&|$)/g, '$1')
.replace(/([?&])unionid=[^&]*(&|$)/g, '$1').replace(/\?&$/, '?').replace(/&$/, '');
var url = base + (q && q !== '?' ? q : '') ;
// 重新加载当前 H5(去掉身份)→ applyRemoteProfile 因无 openid 跳过,页面回到未登录占位
location.replace(url + '?t=' + Date.now());
} catch (e3) {
toast('已清除身份');
}
var openid = hashParam('openid');
if (!openid) { toast('未获得身份,无法退出'); return; }
fetch('/api/mp/public/auth/logout?openid=' + encodeURIComponent(openid), { method: 'POST' })
.then(function (r) { return r.json(); })
.then(function (res) {
if (res.code === 200) {
toast('已退出登录');
setTimeout(function () { location.replace(location.href.split('#')[0] + '?t=' + Date.now()); }, 600);
} else {
toast(res.message || '退出失败,请重试');
}
})
.catch(function () { toast('退出失败,请检查网络'); });
});
/* 从商城/后台切回小程序首页时,webview 会再次可见 —— 把 H5 重置回「首页」屏。