diff --git a/app/Http/Controllers/Business/Pages.php b/app/Http/Controllers/Business/Pages.php index 7b2bcdb5..f9505b87 100644 --- a/app/Http/Controllers/Business/Pages.php +++ b/app/Http/Controllers/Business/Pages.php @@ -957,54 +957,44 @@ class Pages extends Controller $repository = app(JdWalletBillRepository::class); $cache = $this->upstreamCaches->forSource('jiansun'); $scopePrefix = 'jd-wallet-bills.' . $ledger; + $errors = []; + $remember = function (string $scope, array $parameters, callable $resolver, mixed $fallback) use ($cache, &$errors): mixed { + try { + return $cache->remember($scope, $parameters, $resolver); + } catch (ServiceUnavailableHttpException $exception) { + $errors[] = $exception->getMessage(); + } catch (Throwable $exception) { + report($exception); + $errors[] = '京东账单数据暂时无法读取,请稍后重试。'; + } - try { - $result = $cache->remember( - $scopePrefix . '.list', - ['filters' => $filters, 'page' => $page, 'limit' => $pageSize], - fn () => $ledger === 'consumption' - ? $repository->paginateConsumption($filters['store_name'], $filters['from'], $filters['to'], $page, $pageSize) - : $repository->paginateRecharge($filters['store_name'], $filters['from'], $filters['to'], $page, $pageSize), - ); - $summary = $cache->remember( - $scopePrefix . '.summary', - ['filters' => $filters], - fn () => $ledger === 'consumption' - ? $repository->summaryConsumption($filters['store_name'], $filters['from'], $filters['to']) - : $repository->summaryRecharge($filters['store_name'], $filters['from'], $filters['to']), - ); - $shopOptions = $cache->remember( - 'jd-wallet-bills.options', - [], - fn () => $repository->shopOptions(), - ); - } catch (ServiceUnavailableHttpException $exception) { - return [ - 'view' => 'banking.marketing_jd_wallet_bills.partials.embedded-table', - 'ledger' => $ledger, - 'bills' => new LengthAwarePaginator([], 0, $pageSize, $page, [ - 'path' => $request->url(), 'query' => $request->query(), - ]), - 'filters' => $filters, - 'summary' => ['amount_total' => 0, 'balance_total' => 0, 'count' => 0], - 'shopOptions' => [], - 'error' => $exception->getMessage(), - ]; - } catch (Throwable $exception) { - report($exception); + return $fallback; + }; - return [ - 'view' => 'banking.marketing_jd_wallet_bills.partials.embedded-table', - 'ledger' => $ledger, - 'bills' => new LengthAwarePaginator([], 0, $pageSize, $page, [ - 'path' => $request->url(), 'query' => $request->query(), - ]), - 'filters' => $filters, - 'summary' => ['amount_total' => 0, 'balance_total' => 0, 'count' => 0], - 'shopOptions' => [], - 'error' => '京东账单数据暂时无法读取,请稍后重试。', - ]; - } + // 三项分别读取,确保生产冷启动时一次请求就能登记列表、汇总和选项, + // 同时保留已经命中的共享快照,不因另一项暂缺而清空整页。 + $result = $remember( + $scopePrefix . '.list', + ['filters' => $filters, 'page' => $page, 'limit' => $pageSize], + fn () => $ledger === 'consumption' + ? $repository->paginateConsumption($filters['store_name'], $filters['from'], $filters['to'], $page, $pageSize) + : $repository->paginateRecharge($filters['store_name'], $filters['from'], $filters['to'], $page, $pageSize), + ['rows' => [], 'total' => 0], + ); + $summary = $remember( + $scopePrefix . '.summary', + ['filters' => $filters], + fn () => $ledger === 'consumption' + ? $repository->summaryConsumption($filters['store_name'], $filters['from'], $filters['to']) + : $repository->summaryRecharge($filters['store_name'], $filters['from'], $filters['to']), + ['amount_total' => 0, 'balance_total' => 0, 'count' => 0], + ); + $shopOptions = $remember( + 'jd-wallet-bills.options', + [], + fn () => $repository->shopOptions(), + [], + ); return [ 'view' => 'banking.marketing_jd_wallet_bills.partials.embedded-table', @@ -1019,7 +1009,9 @@ class Pages extends Controller 'filters' => $filters, 'summary' => $summary, 'shopOptions' => $shopOptions, - 'error' => null, + 'error' => $errors === [] + ? null + : implode(' ', array_values(array_unique(array_filter($errors)))), ]; } diff --git a/modules/EcommerceFinance/Repositories/JdWalletBillRepository.php b/modules/EcommerceFinance/Repositories/JdWalletBillRepository.php index bef1757c..77d9f126 100644 --- a/modules/EcommerceFinance/Repositories/JdWalletBillRepository.php +++ b/modules/EcommerceFinance/Repositories/JdWalletBillRepository.php @@ -144,8 +144,8 @@ class JdWalletBillRepository $bindings[] = $fromDate . ' 00:00:00'; } if ($toDate !== null && preg_match('/^\d{4}-\d{2}-\d{2}$/', $toDate) === 1) { - $conditions[] = $dateField . ' <= ?'; - $bindings[] = $toDate . ' 23:59:59'; + $conditions[] = $dateField . ' < DATEADD(day, 1, CAST(? AS date))'; + $bindings[] = $toDate; } return ['sql' => $conditions === [] ? '' : ' WHERE ' . implode(' AND ', $conditions), 'bindings' => $bindings]; diff --git a/modules/EcommerceFinance/Services/JianSunSnapshotRefresher.php b/modules/EcommerceFinance/Services/JianSunSnapshotRefresher.php index 1c09a17f..3a9f8a28 100644 --- a/modules/EcommerceFinance/Services/JianSunSnapshotRefresher.php +++ b/modules/EcommerceFinance/Services/JianSunSnapshotRefresher.php @@ -33,7 +33,7 @@ class JianSunSnapshotRefresher implements UpstreamSnapshotRefresher private readonly TmallTaobaoSpreadBillRepository $taobaoAlipay, private readonly SupplierPayableRepository $supplier, private readonly SupplierPayableReportRepository $supplierPayableReports, - private readonly ?JdWalletBillRepository $jdWallet = null, + private readonly JdWalletBillRepository $jdWallet, ) { } @@ -114,31 +114,31 @@ class JianSunSnapshotRefresher implements UpstreamSnapshotRefresher ), 'pinduoduo-bills.options' => $this->pinduoduo->storeOptions(), - 'jd-wallet-bills.recharge.list' => $this->jdWallet?->paginateRecharge( + 'jd-wallet-bills.recharge.list' => $this->jdWallet->paginateRecharge( $storeName, $fromDate, $toDate, $page, $limit, ), - 'jd-wallet-bills.recharge.summary' => $this->jdWallet?->summaryRecharge( + 'jd-wallet-bills.recharge.summary' => $this->jdWallet->summaryRecharge( $storeName, $fromDate, $toDate, ), - 'jd-wallet-bills.consumption.list' => $this->jdWallet?->paginateConsumption( + 'jd-wallet-bills.consumption.list' => $this->jdWallet->paginateConsumption( $storeName, $fromDate, $toDate, $page, $limit, ), - 'jd-wallet-bills.consumption.summary' => $this->jdWallet?->summaryConsumption( + 'jd-wallet-bills.consumption.summary' => $this->jdWallet->summaryConsumption( $storeName, $fromDate, $toDate, ), - 'jd-wallet-bills.options' => $this->jdWallet?->shopOptions(), + 'jd-wallet-bills.options' => $this->jdWallet->shopOptions(), 'taobao-alipay-bills.list' => $this->taobaoAlipay->paginate( $storeName, diff --git a/resources/assets/js/views/business/marketing-account-bills.js b/resources/assets/js/views/business/marketing-account-bills.js new file mode 100644 index 00000000..8a301b5f --- /dev/null +++ b/resources/assets/js/views/business/marketing-account-bills.js @@ -0,0 +1,206 @@ +/** + * 营销账户账单页面的服务端局部导航。 + * + * 链接始终保留完整 href 作为渐进增强回退;普通左键操作只替换当前 + * 工作区并同步 History,避免平台标签、京东账单类型、筛选和分页触发整页刷新。 + */ +(function () { + const workspaceSelector = '[data-marketing-account-bills-workspace]'; + const pagePathMarker = '/workspace/billing-transactions/marketing-account-bills'; + let activeRequest = null; + + function currentWorkspace() { + return document.querySelector(workspaceSelector); + } + + function isSupportedUrl(url) { + const target = new URL(url, window.location.origin); + + return target.origin === window.location.origin + && target.pathname.indexOf(pagePathMarker) !== -1; + } + + function updateHeader(nextDocument) { + const currentHeader = document.querySelector('[data-page-title-first]'); + const nextHeader = nextDocument.querySelector('[data-page-title-first]'); + + if (currentHeader && nextHeader) { + currentHeader.innerHTML = nextHeader.innerHTML; + } + + if (nextDocument.title) { + document.title = nextDocument.title; + } + } + + function setLoading(workspace, loading) { + const status = workspace.querySelector('[data-marketing-account-bills-status]'); + + if (loading) { + workspace.setAttribute('aria-busy', 'true'); + if (status) { + status.textContent = '正在加载账单数据'; + } + } else { + workspace.removeAttribute('aria-busy'); + } + } + + function showError(workspace, url) { + const error = workspace.querySelector('[data-marketing-account-bills-navigation-error]'); + const status = workspace.querySelector('[data-marketing-account-bills-status]'); + + setLoading(workspace, false); + + if (status) { + status.textContent = '账单内容加载失败'; + } + + if (error) { + const retry = error.querySelector('[data-marketing-account-bills-retry]'); + error.hidden = false; + if (retry) { + retry.href = url; + } + } + } + + function initialiseInsertedContent(workspace) { + if (window.Alpine && typeof window.Alpine.initTree === 'function') { + window.Alpine.initTree(workspace); + } + } + + async function navigate(url, options) { + const settings = Object.assign({ pushState: true, focus: null }, options || {}); + const workspace = currentWorkspace(); + + if (!workspace || !isSupportedUrl(url)) { + return false; + } + + if (activeRequest) { + activeRequest.abort(); + } + + const request = new AbortController(); + activeRequest = request; + setLoading(workspace, true); + + try { + const response = await window.fetch(url, { + credentials: 'same-origin', + headers: { + Accept: 'text/html', + 'X-Requested-With': 'XMLHttpRequest', + }, + signal: request.signal, + }); + + if (!response.ok) { + throw new Error(`HTTP ${response.status}`); + } + + const html = await response.text(); + const nextDocument = new DOMParser().parseFromString(html, 'text/html'); + const nextWorkspace = nextDocument.querySelector(workspaceSelector); + + if (!nextWorkspace) { + throw new Error('目标页面缺少营销账单工作区'); + } + + const importedWorkspace = document.importNode(nextWorkspace, true); + workspace.replaceWith(importedWorkspace); + initialiseInsertedContent(importedWorkspace); + updateHeader(nextDocument); + + const target = new URL(url, window.location.origin); + const relativeUrl = `${target.pathname}${target.search}${target.hash}`; + + if (settings.pushState && relativeUrl !== `${window.location.pathname}${window.location.search}${window.location.hash}`) { + window.history.pushState({ marketingAccountBills: true }, '', relativeUrl); + } + + if (settings.focus) { + const focusTarget = importedWorkspace.querySelector(settings.focus); + if (focusTarget) { + window.requestAnimationFrame(() => focusTarget.focus({ preventScroll: true })); + } + } + + return true; + } catch (error) { + if (error.name !== 'AbortError') { + showError(workspace, url); + } + + return false; + } finally { + if (activeRequest === request) { + activeRequest = null; + } + } + } + + document.addEventListener('click', (event) => { + const link = event.target.closest( + `${workspaceSelector} .business-tab-link, ` + + `${workspaceSelector} [data-jd-wallet-navigation], ` + + `${workspaceSelector} .jd-wallet-pagination a` + ); + + if (!link + || link.hasAttribute('data-marketing-account-bills-full-page') + || event.button !== 0 + || event.metaKey + || event.ctrlKey + || event.shiftKey + || event.altKey + || link.target === '_blank' + ) { + return; + } + + if (!isSupportedUrl(link.href)) { + return; + } + + event.preventDefault(); + event.stopPropagation(); + + const focusSelector = link.classList.contains('marketing-bills-subtab') + ? '.marketing-bills-subtab.is-active' + : '.business-tab-link.is-active'; + + navigate(link.href, { focus: focusSelector }); + }, true); + + document.addEventListener('submit', (event) => { + const form = event.target.closest(`${workspaceSelector} [data-jd-wallet-filter]`); + + if (!form) { + return; + } + + event.preventDefault(); + const target = new URL(form.action, window.location.origin); + + new FormData(form).forEach((value, key) => { + const normalized = String(value).trim(); + if (normalized === '') { + target.searchParams.delete(key); + } else { + target.searchParams.set(key, normalized); + } + }); + + target.searchParams.delete('page'); + navigate(target.href, { focus: '#jd-filter-store' }); + }, true); + + window.addEventListener('popstate', () => { + if (currentWorkspace() && isSupportedUrl(window.location.href)) { + navigate(window.location.href, { pushState: false }); + } + }); +}()); diff --git a/resources/views/banking/marketing_jd_wallet_bills/partials/embedded-table.blade.php b/resources/views/banking/marketing_jd_wallet_bills/partials/embedded-table.blade.php index 6bd0ec20..39825cc3 100644 --- a/resources/views/banking/marketing_jd_wallet_bills/partials/embedded-table.blade.php +++ b/resources/views/banking/marketing_jd_wallet_bills/partials/embedded-table.blade.php @@ -3,6 +3,15 @@ .marketing-bills-subtabs { display: flex; gap: 8px; margin: 0 0 16px; padding-bottom: 16px; border-bottom: 1px solid var(--vip-border, #D8D2C7); } .marketing-bills-subtab { display: inline-flex; min-height: 34px; align-items: center; gap: 6px; border: 1px solid var(--vip-border, #D8D2C7); border-radius: 8px; background: #fff; padding: 0 12px; color: #595959; font-size: 0.8125rem; font-weight: 500; } .marketing-bills-subtab:hover, .marketing-bills-subtab.is-active { border-color: var(--vip-content-accent-dark, #856632); background: var(--vip-content-accent-soft, #F7F0DE); color: var(--vip-content-accent-dark, #856632); } + .marketing-bills-subtab:focus-visible, .jd-wallet-button:focus-visible { outline: none; box-shadow: 0 0 0 3px rgba(133, 102, 50, 0.2); } + [data-marketing-account-bills-workspace][aria-busy="true"] { cursor: progress; opacity: 0.72; } + .jd-wallet-summary-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; } + .jd-wallet-summary-card { border: 1px solid var(--vip-border, #D8D2C7); border-radius: 8px; background: #fff; padding: 12px 16px; } + .jd-wallet-summary-label { color: #595959; font-size: 0.75rem; font-weight: 500; } + .jd-wallet-summary-value { margin-top: 4px; color: #171717; font-size: 0.875rem; font-weight: 700; font-variant-numeric: tabular-nums; } + .jd-wallet-filter { margin-bottom: 16px; border: 1px solid var(--vip-border, #D8D2C7); border-radius: 8px; background: var(--vip-content-bg, #F8F6F1); padding: 12px 16px; } + .jd-wallet-error { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; border: 1px solid #B80000; border-radius: 8px; background: #FFF1F1; padding: 12px 16px; color: #B80000; font-size: 0.8125rem; } + .jd-wallet-empty { padding: 40px 20px; color: #595959; text-align: center; } .jd-wallet-table-wrap { overflow-x: auto; border: 1px solid var(--vip-border, #D8D2C7); border-radius: 8px; } .jd-wallet-table { min-width: 100%; white-space: nowrap; font-size: 0.78125rem; } .jd-wallet-table th { position: sticky; top: 0; padding: 0.65rem 0.75rem; border-bottom: 1px solid var(--vip-border, #D8D2C7); background: var(--vip-content-accent-soft, #F7F0DE); color: #171717; font-size: 0.75rem; font-weight: 600; text-align: left; } @@ -28,6 +37,7 @@ .jd-wallet-pagination .pagination-xs { display: flex; width: 100%; justify-content: center; margin-top: 0; } .jd-wallet-pagination .pagination-xs > nav, .jd-wallet-pagination .pagination-xs > nav > ul { margin-right: auto; margin-left: auto; } + @media (min-width: 640px) { .jd-wallet-summary-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); } } @php @@ -41,38 +51,45 @@ $currentUrl = request()->url(); @endphp -