feat(user-center): Zibll-like assets charge, transfer, and dual cards
Complete wallet/points assets surface: sidebar mini dual cards, dual asset row, balance charge packages REST + modal, transfer search/pay, points-to-balance exchange, and payment success credit for recharge orders.
This commit is contained in:
+269
-8
@@ -515,19 +515,280 @@
|
||||
});
|
||||
}
|
||||
|
||||
if (document.readyState === 'loading') {
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
initSignin();
|
||||
initInvite();
|
||||
initVipShop();
|
||||
initVipPointsExchange();
|
||||
initCardPass();
|
||||
// ===== 余额充值 / 转账 / 积分兑余额(Zibll 资产中心) =====
|
||||
function openModal(el) {
|
||||
if (!el) return;
|
||||
el.hidden = false;
|
||||
el.classList.add('is-open');
|
||||
document.body.classList.add('uc-modal-open');
|
||||
}
|
||||
function closeModal(el) {
|
||||
if (!el) return;
|
||||
el.hidden = true;
|
||||
el.classList.remove('is-open');
|
||||
if (!document.querySelector('.uc-modal.is-open')) {
|
||||
document.body.classList.remove('uc-modal-open');
|
||||
}
|
||||
}
|
||||
function pollPaidOrder(orderNumber) {
|
||||
if (!orderNumber) return;
|
||||
var tries = 0;
|
||||
var maxTries = 40;
|
||||
var timer = setInterval(function () {
|
||||
tries += 1;
|
||||
api('/pay/content/order/' + encodeURIComponent(orderNumber)).then(function (r) {
|
||||
if (r.ok && r.json && r.json.paid) {
|
||||
clearInterval(timer);
|
||||
window.location.reload();
|
||||
}
|
||||
}).catch(function () {});
|
||||
if (tries >= maxTries) clearInterval(timer);
|
||||
}, 3000);
|
||||
}
|
||||
|
||||
function initBalanceCharge() {
|
||||
var modal = document.querySelector('[data-aksrt-charge-modal]');
|
||||
if (!modal) return;
|
||||
var form = modal.querySelector('[data-aksrt-charge-form]');
|
||||
var tip = modal.querySelector('[data-aksrt-charge-tip]');
|
||||
var busy = false;
|
||||
function setTip(text, isError) {
|
||||
if (!tip) return;
|
||||
tip.textContent = text || '';
|
||||
tip.classList.toggle('is-error', !!isError);
|
||||
}
|
||||
document.querySelectorAll('[data-aksrt-charge-open]').forEach(function (btn) {
|
||||
btn.addEventListener('click', function () { openModal(modal); });
|
||||
});
|
||||
} else {
|
||||
modal.querySelectorAll('[data-aksrt-charge-close]').forEach(function (btn) {
|
||||
btn.addEventListener('click', function () { closeModal(modal); });
|
||||
});
|
||||
modal.querySelectorAll('.uc-charge-package input').forEach(function (inp) {
|
||||
inp.addEventListener('change', function () {
|
||||
modal.querySelectorAll('.uc-charge-package').forEach(function (el) {
|
||||
el.classList.toggle('is-active', el.contains(inp) && inp.checked);
|
||||
});
|
||||
});
|
||||
});
|
||||
if (!form) return;
|
||||
form.addEventListener('submit', function (e) {
|
||||
e.preventDefault();
|
||||
if (busy) return;
|
||||
var pkgInput = form.querySelector('input[name="balance_package"]:checked') || form.querySelector('input[name="balance_package"]');
|
||||
var pkg = pkgInput ? pkgInput.value : 'custom';
|
||||
var customEl = form.querySelector('input[name="custom_price"]');
|
||||
var custom = customEl ? parseFloat(customEl.value || '0') : 0;
|
||||
if (custom > 0) {
|
||||
pkg = 'custom';
|
||||
}
|
||||
if ((!pkg || pkg === 'custom') && !(custom > 0) && !pkgInput) {
|
||||
setTip('请选择或输入充值金额', true);
|
||||
return;
|
||||
}
|
||||
if (pkg === 'custom' && !(custom > 0)) {
|
||||
setTip('请输入有效充值金额', true);
|
||||
return;
|
||||
}
|
||||
var methodEl = form.querySelector('input[name="payment_method"]:checked');
|
||||
var method = methodEl ? methodEl.value : 'alipay';
|
||||
busy = true;
|
||||
setTip('正在创建订单…');
|
||||
api('/pay/balance/charge', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({
|
||||
package: pkg,
|
||||
custom_price: custom,
|
||||
payment_method: method
|
||||
})
|
||||
}).then(function (r) {
|
||||
busy = false;
|
||||
if (!r.ok || (r.json && r.json.code && !r.json.success)) {
|
||||
setTip((r.json && r.json.message) || '充值失败', true);
|
||||
return;
|
||||
}
|
||||
var data = (r.json && r.json.data) ? r.json.data : r.json;
|
||||
if (data && data.redirect && (data.payment_url || data.qr_content)) {
|
||||
if (data.payment_url) window.open(data.payment_url, '_blank');
|
||||
setTip(data.message || '请完成支付,支付成功后将自动刷新。');
|
||||
if (data.order_number) pollPaidOrder(data.order_number);
|
||||
return;
|
||||
}
|
||||
setTip((data && data.message) || '充值成功,正在刷新…');
|
||||
window.location.reload();
|
||||
}).catch(function () {
|
||||
busy = false;
|
||||
setTip('网络错误,请重试', true);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function initTransfer() {
|
||||
var modal = document.querySelector('[data-aksrt-transfer-modal]');
|
||||
if (!modal) return;
|
||||
var form = modal.querySelector('[data-aksrt-transfer-form]');
|
||||
var tip = modal.querySelector('[data-aksrt-transfer-tip]');
|
||||
var results = modal.querySelector('[data-aksrt-transfer-results]');
|
||||
var selected = modal.querySelector('[data-aksrt-transfer-selected]');
|
||||
var recipientInput = modal.querySelector('[data-aksrt-transfer-recipient]');
|
||||
var typeInput = modal.querySelector('[data-aksrt-transfer-type]');
|
||||
var title = modal.querySelector('[data-aksrt-transfer-title]');
|
||||
var searchTimer = null;
|
||||
var busy = false;
|
||||
function setTip(text, isError) {
|
||||
if (!tip) return;
|
||||
tip.textContent = text || '';
|
||||
tip.classList.toggle('is-error', !!isError);
|
||||
}
|
||||
document.querySelectorAll('[data-aksrt-transfer-open]').forEach(function (btn) {
|
||||
btn.addEventListener('click', function () {
|
||||
var type = btn.getAttribute('data-aksrt-transfer-open') || 'balance';
|
||||
if (typeInput) typeInput.value = type;
|
||||
if (title) title.textContent = type === 'points' ? '积分转账' : '余额转账';
|
||||
if (results) results.innerHTML = '';
|
||||
if (selected) selected.textContent = '';
|
||||
if (recipientInput) recipientInput.value = '';
|
||||
setTip('');
|
||||
openModal(modal);
|
||||
});
|
||||
});
|
||||
modal.querySelectorAll('[data-aksrt-transfer-close]').forEach(function (btn) {
|
||||
btn.addEventListener('click', function () { closeModal(modal); });
|
||||
});
|
||||
var searchEl = modal.querySelector('[data-aksrt-transfer-search]');
|
||||
if (searchEl) {
|
||||
searchEl.addEventListener('input', function () {
|
||||
var q = String(searchEl.value || '').trim();
|
||||
if (searchTimer) clearTimeout(searchTimer);
|
||||
if (q.length < 1) {
|
||||
if (results) results.innerHTML = '';
|
||||
return;
|
||||
}
|
||||
searchTimer = setTimeout(function () {
|
||||
api('/pay/transfer/search?s=' + encodeURIComponent(q)).then(function (r) {
|
||||
if (!results) return;
|
||||
results.innerHTML = '';
|
||||
if (!r.ok) return;
|
||||
var data = (r.json && r.json.data) ? r.json.data : r.json;
|
||||
var users = (data && data.users) || [];
|
||||
if (!users.length) {
|
||||
results.innerHTML = '<div class="muted-2-color em09">未找到相关用户</div>';
|
||||
return;
|
||||
}
|
||||
users.forEach(function (u) {
|
||||
var item = document.createElement('button');
|
||||
item.type = 'button';
|
||||
item.className = 'uc-transfer-user';
|
||||
item.innerHTML = '<img src="' + (u.avatar || '') + '" alt="" class="uc-transfer-user__avatar"><span>' + (u.display_name || u.user_login) + '</span><span class="muted-2-color em09">@' + (u.user_login || '') + '</span>';
|
||||
item.addEventListener('click', function () {
|
||||
if (recipientInput) recipientInput.value = String(u.id);
|
||||
if (selected) selected.textContent = '已选择:' + (u.display_name || u.user_login);
|
||||
results.innerHTML = '';
|
||||
});
|
||||
results.appendChild(item);
|
||||
});
|
||||
});
|
||||
}, 280);
|
||||
});
|
||||
}
|
||||
if (!form) return;
|
||||
form.addEventListener('submit', function (e) {
|
||||
e.preventDefault();
|
||||
if (busy) return;
|
||||
var type = typeInput ? typeInput.value : 'balance';
|
||||
var recipient = recipientInput ? parseInt(recipientInput.value || '0', 10) : 0;
|
||||
var amountEl = form.querySelector('[data-aksrt-transfer-amount]');
|
||||
var amount = amountEl ? amountEl.value : 0;
|
||||
if (!recipient) {
|
||||
setTip('请选择收款用户', true);
|
||||
return;
|
||||
}
|
||||
if (!(parseFloat(amount) > 0)) {
|
||||
setTip('请输入有效金额', true);
|
||||
return;
|
||||
}
|
||||
busy = true;
|
||||
setTip('正在转账…');
|
||||
api('/pay/transfer', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ type: type, recipient: recipient, amount: amount })
|
||||
}).then(function (r) {
|
||||
busy = false;
|
||||
if (!r.ok || (r.json && r.json.code && !r.json.success)) {
|
||||
setTip((r.json && r.json.message) || '转账失败', true);
|
||||
return;
|
||||
}
|
||||
var data = (r.json && r.json.data) ? r.json.data : r.json;
|
||||
setTip((data && data.message) || '转账成功,正在刷新…');
|
||||
window.location.reload();
|
||||
}).catch(function () {
|
||||
busy = false;
|
||||
setTip('网络错误,请重试', true);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function initExchange() {
|
||||
var modal = document.querySelector('[data-aksrt-exchange-modal]');
|
||||
if (!modal) return;
|
||||
var form = modal.querySelector('[data-aksrt-exchange-form]');
|
||||
var tip = modal.querySelector('[data-aksrt-exchange-tip]');
|
||||
var busy = false;
|
||||
function setTip(text, isError) {
|
||||
if (!tip) return;
|
||||
tip.textContent = text || '';
|
||||
tip.classList.toggle('is-error', !!isError);
|
||||
}
|
||||
document.querySelectorAll('[data-aksrt-exchange-open]').forEach(function (btn) {
|
||||
btn.addEventListener('click', function () { openModal(modal); });
|
||||
});
|
||||
modal.querySelectorAll('[data-aksrt-exchange-close]').forEach(function (btn) {
|
||||
btn.addEventListener('click', function () { closeModal(modal); });
|
||||
});
|
||||
if (!form) return;
|
||||
form.addEventListener('submit', function (e) {
|
||||
e.preventDefault();
|
||||
if (busy) return;
|
||||
var pointsEl = form.querySelector('[data-aksrt-exchange-points]');
|
||||
var points = pointsEl ? parseInt(pointsEl.value || '0', 10) : 0;
|
||||
if (!(points > 0)) {
|
||||
setTip('请输入兑换积分数量', true);
|
||||
return;
|
||||
}
|
||||
busy = true;
|
||||
setTip('正在兑换…');
|
||||
api('/pay/balance/exchange', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ points: points })
|
||||
}).then(function (r) {
|
||||
busy = false;
|
||||
if (!r.ok || (r.json && r.json.code && !r.json.success)) {
|
||||
setTip((r.json && r.json.message) || '兑换失败', true);
|
||||
return;
|
||||
}
|
||||
var data = (r.json && r.json.data) ? r.json.data : r.json;
|
||||
setTip((data && data.message) || '兑换成功,正在刷新…');
|
||||
window.location.reload();
|
||||
}).catch(function () {
|
||||
busy = false;
|
||||
setTip('网络错误,请重试', true);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function bootAssets() {
|
||||
initSignin();
|
||||
initInvite();
|
||||
initVipShop();
|
||||
initVipPointsExchange();
|
||||
initCardPass();
|
||||
initBalanceCharge();
|
||||
initTransfer();
|
||||
initExchange();
|
||||
}
|
||||
|
||||
if (document.readyState === 'loading') {
|
||||
document.addEventListener('DOMContentLoaded', bootAssets);
|
||||
} else {
|
||||
bootAssets();
|
||||
}
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user