首页接入统一兜豆账本新人欢迎礼 (v26101006)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-10 05:13:39 +00:00
parent e82a62771e
commit 7f1a7d5aaa
3 changed files with 52 additions and 2 deletions

View File

@@ -3,7 +3,7 @@
(function () {
'use strict';
/* 页面版本号(YYMMDD+序号):显示在「关于兜爸心选」页底部;bump 时同步 head 注释与 ?v= */
var VER = '26101005';
var VER = '26101006';
const $ = (s, c) => (c || document).querySelector(s);
const $$ = (s, c) => Array.from((c || document).querySelectorAll(s));
@@ -5254,6 +5254,40 @@
}).catch(function () {});
}
loadDou();
/* ---------- 新人欢迎礼(2026-10-10 用户定,「首页 / 小程序后台」会话 375797cf 提供账本口径) ----------
进首页就调统一兜豆账本:/doudou-ledger/api/auth/login {app:'box-app', openid, unionid} 换 token →
/me/report {rule:'welcome'}。账本保证每人只发一次:
· granted>0 且 duplicate=false → 弹「新人欢迎礼 +N 兜豆」(N 用返回的 granted,不写死),再按账本余额刷新兑换条;
· duplicate=true → 什么都不显示;接口失败 → 静默,下次进来再调;没有 openid(匿名)→ 不调。
同源走 mp 反代(/doudou-ledger/ 已加进 nginx 统一反代),不跨域。 */
(function welcomeGift() {
const oid = hashParam('openid'), uid = hashParam('unionid');
if (!oid || DEMO) return;
const LEDGER = '/doudou-ledger/api';
fetch(LEDGER + '/auth/login', {
method: 'POST', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ app: 'box-app', openid: oid, unionid: uid || null })
}).then(function (r) { return r.ok ? r.json() : null; }).then(function (j) {
if (!j || !j.token) return null;
return fetch(LEDGER + '/me/report', {
method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: 'Bearer ' + j.token },
body: JSON.stringify({ rule: 'welcome' })
}).then(function (r) { return r.ok ? r.json() : null; });
}).then(function (res) {
if (!res || res.duplicate || !(res.granted > 0)) return;
loadDou(); // 余额以账本为准:领完立刻重拉,不在前端加减
const show = function () {
$('#wgN').textContent = res.granted;
/* 进页面时可能已经有别的弹窗(如完善资料引导)开着 → 不抢,改成一条 toast */
if (openedModal) toast('新人欢迎礼 +' + res.granted + ' 兜豆,已到账');
else openModal('modalWelcome');
};
setTimeout(show, 900);
}).catch(function () {});
})();
$('#wgGo').addEventListener('click', function () { closeModal(); openExchange(); });
document.addEventListener('visibilitychange', function () {
if (document.visibilityState === 'visible') loadDou();
});