diff --git a/assets/css/admin/coupons.css b/assets/css/admin/coupons.css new file mode 100644 index 00000000..5f529b50 --- /dev/null +++ b/assets/css/admin/coupons.css @@ -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; } +} diff --git a/assets/js/admin-coupons.js b/assets/js/admin-coupons.js new file mode 100644 index 00000000..e11b9013 --- /dev/null +++ b/assets/js/admin-coupons.js @@ -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(); + }); +})(); diff --git a/src/Admin/Native/PayCoupons.php b/src/Admin/Native/PayCoupons.php index 4a423758..4231297a 100644 --- a/src/Admin/Native/PayCoupons.php +++ b/src/Admin/Native/PayCoupons.php @@ -9,8 +9,12 @@ defined('ABSPATH') || exit; class PayCoupons { - public const CAPABILITY = 'manage_options'; - public const PAGE_SLUG = 'aksrt_coupons'; + use PayCouponsForms; + + public const CAPABILITY = 'manage_options'; + public const PAGE_SLUG = 'aksrt_coupons'; + public const CREATE_DIALOG = 'aksrt-coupon-create'; + public const BATCH_DIALOG = 'aksrt-coupon-batch'; private static $instance = null; @@ -25,6 +29,7 @@ class PayCoupons { if (!is_admin()) { return; } + add_action('admin_enqueue_scripts', [$this, 'enqueue_assets']); add_action('admin_post_aksrt_pay_create_coupon', [$this, 'handle_create_coupon']); add_action('admin_post_aksrt_pay_batch_coupons', [$this, 'handle_batch_coupons']); add_action('admin_post_aksrt_pay_update_coupon', [$this, 'handle_update_coupon']); @@ -32,18 +37,34 @@ class PayCoupons { add_action('admin_post_aksrt_pay_enable_coupon', [$this, 'handle_enable_coupon']); } + public function enqueue_assets(): void { + $page = isset($_GET['page']) ? sanitize_key((string) wp_unslash($_GET['page'])) : ''; + if ($page !== self::PAGE_SLUG) { + return; + } + $css = '/assets/css/admin/coupons.css'; + $js = '/assets/js/admin-coupons.js'; + wp_enqueue_style('aksrt-admin-coupons', AKSRT_URI . $css, [NativeUi::STYLE_HANDLE], (string) filemtime(AKSRT_DIR . $css)); + wp_enqueue_script('aksrt-admin-coupons', AKSRT_URI . $js, [], (string) filemtime(AKSRT_DIR . $js), true); + } + public function render_page(): void { $this->ensure_cap(); - NativeUi::page_open(); + $ready = class_exists('\AKSRT\Payment\Coupon'); + NativeUi::page_open('aksrt-coupons-page'); NativeUi::header( __('优惠码', 'aksrt'), __('创建、批量生成与管理结算优惠码。配置项仍在 AKSRT 主题设置 SPA。', 'aksrt'), - 'dashicons-tickets-alt' + 'dashicons-tickets-alt', + $ready ? [ + ['label' => __('新建优惠码', 'aksrt'), 'dialog' => self::CREATE_DIALOG, 'primary' => true, 'icon' => 'dashicons-plus-alt2'], + ['label' => __('批量生成', 'aksrt'), 'dialog' => self::BATCH_DIALOG, 'icon' => 'dashicons-randomize'], + ] : [] ); $this->render_flash(); - if (!class_exists('\AKSRT\Payment\Coupon')) { + if (!$ready) { echo '
' . esc_html__('优惠码模块未初始化。', 'aksrt') . '
' . esc_html__('优惠码不存在。', 'aksrt') . '
' . esc_html(sprintf( + delete_transient($key); + $text = implode("\n", array_map('strval', $codes)); + $copy = ''; + NativeUi::card_open( + sprintf( /* translators: %d count */ - __('成功生成 %d 个优惠码。', 'aksrt'), - $generated - )) . '
' . esc_html__('离开或刷新本页后不再显示,请及时复制保存。', 'aksrt') . '
'; + echo ''; + NativeUi::card_close(); } private function render_list(\AKSRT\Payment\Coupon $svc): void { @@ -265,7 +171,7 @@ class PayCoupons { echo '' . esc_html((string) ($row['code'] ?? '')) . '