feat(admin): create and batch-generate coupons in dialogs

- Move "新建优惠码" / "批量生成" into header-launched <dialog>s
- Extract forms into PayCouponsForms trait with shared rule fields,
  hints, validation and currency/percent unit switching
- Generated codes card gets a copy-all button; list codes copy on click
- Drop old inline forms, render_generated_dump and the unread
  aksrt_coupon_generated redirect arg

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
kate522andClaude Fable 5 committed 2026-10-31 19:03:49 +08:00
1 parent 92a3c2b8c7
commit 4f6e17ba5e
4 files changed
+459 -158

No files matched your search

+54
View File
@@ -0,0 +1,54 @@
/* 优惠码(aksrt_coupons):页头按钮 + 新建/批量生成弹窗。复用 native-ui.css 的 --an-* 令牌,规则限定在本页类名下。 */
.aksrt-coupons-page [hidden],
.aksrt-coupon-dialog [hidden] { display: none !important; }
.aksrt-coupons-page .aksrt-native-header__actions .button,
.aksrt-coupon-generated .aksrt-native-card__actions .button { display: inline-flex; align-items: center; gap: 5px; }
.aksrt-coupons-page .aksrt-native-header__actions .dashicons { margin: 0; font-size: 17px; line-height: 20px; }
.aksrt-coupon-generated .aksrt-native-card__actions .dashicons { margin: 0; font-size: 14px; width: 14px; height: 14px; }
/* ---------- 列表 ---------- */
.aksrt-coupons-page .wp-list-table { min-width: 1000px; }
.aksrt-coupons-page .wp-list-table td { vertical-align: middle; overflow-wrap: anywhere; }
.aksrt-coupon-code { display: inline-flex; align-items: center; gap: 5px; max-width: 100%; padding: 4px 7px; color: var(--an-text); background: var(--an-bg-soft); border: 1px solid transparent; border-radius: 5px; text-align: left; font-size: 13px; overflow-wrap: anywhere; cursor: pointer; }
.aksrt-coupon-code .dashicons { flex: none; width: 14px; height: 14px; color: var(--an-muted); font-size: 14px; }
.aksrt-coupon-code:hover { color: var(--an-primary); background: var(--an-primary-soft); border-color: var(--an-primary); }
.aksrt-coupon-code:focus-visible { outline: 2px solid var(--an-primary); outline-offset: 2px; }
.aksrt-coupon-generated textarea { margin-top: 8px; resize: vertical; }
/* ---------- 弹窗 ---------- */
html.aksrt-coupons-modal-open { overflow: hidden; }
.aksrt-coupon-dialog { width: min(720px, calc(100vw - 32px)); max-width: none; max-height: calc(100vh - 48px); max-height: calc(100dvh - 48px); margin: auto; padding: 0; overflow: hidden; color: var(--an-text-2); background: var(--an-card); border: 0; border-radius: var(--an-radius); box-shadow: 0 16px 64px rgba(15, 23, 42, .2); }
.aksrt-coupon-dialog::backdrop { background: rgba(15, 23, 42, .45); }
.aksrt-coupon-dialog__frame { display: flex; flex-direction: column; max-height: inherit; margin: 0; }
.aksrt-coupon-dialog__head { display: flex; flex: none; align-items: flex-start; justify-content: space-between; gap: 16px; padding: 20px 24px 18px; border-bottom: 1px solid var(--an-border-soft); }
.aksrt-coupon-dialog .aksrt-coupon-dialog__head h2 { margin: 0; padding: 0; color: var(--an-text); font-size: 18px; font-weight: 600; line-height: 1.5; }
.aksrt-coupon-dialog__head p { margin: 5px 0 0; font-size: 13px; line-height: 1.6; }
.aksrt-coupon-dialog__close { display: inline-flex; flex: none; align-items: center; justify-content: center; width: 32px; height: 32px; padding: 0; color: var(--an-text-2); background: transparent; border: 0; border-radius: var(--an-radius-sm); cursor: pointer; }
.aksrt-coupon-dialog__close:hover { color: var(--an-text); background: var(--an-bg-soft); }
.aksrt-coupon-dialog__body { min-height: 0; padding: 20px 24px 24px; overflow-y: auto; overscroll-behavior: contain; }
.aksrt-coupon-dialog__foot { display: flex; flex: none; justify-content: flex-end; gap: 8px; padding: 14px 24px; background: var(--an-card); border-top: 1px solid var(--an-border-soft); }
.aksrt-coupon-dialog__foot .button { min-width: 110px; }
.aksrt-coupon-dialog .button:disabled { opacity: .65; cursor: not-allowed; }
.aksrt-coupon-dialog :is(a, button, input, select, textarea):focus-visible { outline: 2px solid var(--an-primary); outline-offset: 2px; }
.aksrt-coupon-dialog .aksrt-native-form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.aksrt-coupon-dialog .aksrt-coupon-batch-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.aksrt-coupon-dialog .aksrt-coupon-section { margin: 0 0 12px; color: var(--an-text); font-size: 14px; font-weight: 600; line-height: 1.5; }
.aksrt-coupon-dialog .aksrt-coupon-section ~ .aksrt-coupon-section { margin-top: 22px; padding-top: 18px; border-top: 1px solid var(--an-border-soft); }
.aksrt-coupon-inline { display: flex; align-items: center; gap: 6px; }
.aksrt-coupon-inline input { flex: 1 1 auto; min-width: 0; }
.aksrt-coupon-unit { flex: none; min-width: 1.2em; color: var(--an-muted); }
/* ---------- 复制提示 ---------- */
.aksrt-coupon-toast { position: fixed; right: 24px; bottom: 24px; z-index: 100000; max-width: min(360px, calc(100vw - 48px)); padding: 10px 14px; color: #fff; background: rgba(15, 23, 42, .9); border-radius: var(--an-radius-sm); font-size: 13px; opacity: 0; transform: translateY(8px); transition: opacity .2s, transform .2s; pointer-events: none; }
.aksrt-coupon-toast.is-visible { opacity: 1; transform: none; }
@media (max-width: 600px) {
.aksrt-coupon-dialog { width: calc(100vw - 24px); max-height: calc(100dvh - 24px); }
.aksrt-coupon-dialog__head,
.aksrt-coupon-dialog__body,
.aksrt-coupon-dialog__foot { padding-left: 16px; padding-right: 16px; }
.aksrt-coupon-dialog .aksrt-native-form-grid,
.aksrt-coupon-dialog .aksrt-coupon-batch-grid { grid-template-columns: 1fr; }
.aksrt-coupon-dialog__foot .button { flex: 1; min-width: 0; }
}
+145
View File
@@ -0,0 +1,145 @@
/** 优惠码:新建/批量生成弹窗、类型与范围联动、防重复提交、点击复制。仅本页入队。 */
(function () {
'use strict';
var root = document.querySelector('.aksrt-coupons-page');
if (!root) return;
var toast = root.querySelector('[data-aksrt-coupon-toast]');
var toastTimer;
var forms = Array.from(root.querySelectorAll('[data-aksrt-coupon-form]'));
function notify(key) {
if (!toast) return;
clearTimeout(toastTimer);
toast.textContent = toast.getAttribute('data-' + key);
toast.classList.add('is-visible');
toastTimer = setTimeout(function () { toast.classList.remove('is-visible'); }, 3000);
}
/** 类型切换面值的步长/上限与单位;「全部商品」时范围 ID 禁用,否则必填。 */
function syncForm(form) {
var type = form.elements.type;
var value = form.elements.value;
var unit = form.querySelector('[data-aksrt-coupon-unit]');
if (type && value) {
var percent = type.value === 'percent';
value.step = percent ? '1' : '0.01';
if (percent) value.max = '100'; else value.removeAttribute('max');
if (unit) unit.textContent = unit.getAttribute(percent ? 'data-percent' : 'data-fixed');
}
var scope = form.elements.scope_type;
var ids = form.elements.scope_ids;
if (scope && ids) {
var all = scope.value === '';
ids.disabled = all;
ids.required = !all;
}
}
function lockScroll() {
document.documentElement.classList.toggle('aksrt-coupons-modal-open', !!root.querySelector('dialog[open]'));
}
root.querySelectorAll('.aksrt-coupon-dialog').forEach(function (dialog) {
if (typeof dialog.showModal !== 'function') return;
var opener = null;
var startedOutside = false;
var form = dialog.querySelector('form');
function outside(event) {
var rect = dialog.getBoundingClientRect();
return event.clientX < rect.left || event.clientX > rect.right || event.clientY < rect.top || event.clientY > rect.bottom;
}
function requestClose() {
if (!form.dataset.submitting) dialog.close();
}
root.querySelectorAll('[data-aksrt-dialog-open="' + dialog.id + '"]').forEach(function (button) {
button.addEventListener('click', function () {
opener = button;
syncForm(form);
dialog.showModal();
lockScroll();
});
});
dialog.querySelectorAll('[data-aksrt-dialog-close]').forEach(function (button) {
button.addEventListener('click', requestClose);
});
dialog.addEventListener('pointerdown', function (event) {
startedOutside = event.target === dialog && outside(event);
});
dialog.addEventListener('click', function (event) {
if (startedOutside && event.target === dialog && outside(event)) requestClose();
startedOutside = false;
});
dialog.addEventListener('cancel', function (event) {
if (form.dataset.submitting) event.preventDefault();
});
dialog.addEventListener('close', function () {
lockScroll();
if (opener && opener.isConnected) opener.focus();
opener = null;
});
});
forms.forEach(function (form) {
var submit = form.querySelector('[data-aksrt-coupon-submit]');
if (submit) submit.setAttribute('data-idle-label', submit.textContent);
form.addEventListener('change', function () { syncForm(form); });
form.addEventListener('submit', function (event) {
if (form.dataset.submitting) {
event.preventDefault();
return;
}
form.dataset.submitting = '1';
if (submit) {
submit.textContent = form.getAttribute('data-busy-label');
// 延后禁用:同步禁用 submit 按钮会让部分浏览器丢掉这次提交。
setTimeout(function () { submit.disabled = true; }, 0);
}
});
syncForm(form);
});
root.addEventListener('click', async function (event) {
var button = event.target.closest('[data-clipboard-text]');
if (!button) return;
var value = button.getAttribute('data-clipboard-text');
try {
if (navigator.clipboard && window.isSecureContext) {
await navigator.clipboard.writeText(value);
} else {
var input = document.createElement('textarea');
input.value = value;
input.className = 'screen-reader-text';
root.appendChild(input);
input.select();
var copied = false;
try {
copied = document.execCommand('copy');
} finally {
input.remove();
button.focus();
}
if (!copied) throw new Error('copy');
}
notify('copied');
} catch (error) {
notify('copy-failed');
}
});
// bfcache 返回时恢复按钮,避免表单停在提交中状态。
window.addEventListener('pageshow', function () {
forms.forEach(function (form) {
var submit = form.querySelector('[data-aksrt-coupon-submit]');
delete form.dataset.submitting;
if (submit) {
submit.disabled = false;
submit.textContent = submit.getAttribute('data-idle-label');
}
syncForm(form);
});
lockScroll();
});
})();