Files
crawler-plugin/admin-frontend-vue/src/pages/tasks/BrandGatingPanel.vue
T
huangzd1997 b963561043 fix(cr2-frontend): 代理归属回退、换号继承确认与请求竞态收口
- CR-04:全局代理加 proxy_uid 归属,跨账号不再静默回退/补报
- CR-12:取消静默继承,设置页显式确认;同 OS 作用域、locked 代理排除、无效快照清除、uid+epoch 双校验、登出 capture 超时
- CR-26/27:关键词分页初始值与档位一致;两页面请求序号闸丢弃旧响应

vue-tsc 两前端 rc=0;定向 35 + 8 + 2 例全绿。
2026-10-26 11:33:37 +08:00

757 lines
20 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
/**
* 品牌Gating库管理页(独立菜单 admin_brand_gating,V174 恢复)。
* 数据源 /api/admin/brand-gating(biz_brand_gating,全平台共享,不随公司 Tab 收窄)。
*/
import { computed, onMounted, reactive, ref } from 'vue'
import { formatDateTime } from '@/utils/datetime'
import { actionableErrorText, type AdminFeedbackKind } from '@/components/admin-feedback'
import { showAdminFeedback } from '@/components/admin-feedback-ui'
import { openAdminConfirm } from '@/components/admin-confirm'
import OldPagination from '@/components/OldPagination.vue'
import LoadFailureBar from '@/components/LoadFailureBar.vue'
import { createLatestRequestGate } from '@/utils/latest-request'
import {
GATING_COUNTRIES,
GATING_SOURCES,
GATING_TYPES,
createBrandGatingFilter,
createBrandGatingForm,
validateBrandGatingForm,
type BrandGatingFilter,
type BrandGatingForm,
type BrandGatingRow,
type BrandGatingStats,
} from './brand-gating-model.ts'
import {
deleteBrandGating,
fetchBrandGatingPage,
fetchBrandGatingStats,
importBrandGatingText,
submitBrandGating,
updateBrandGatingStatus,
} from './brand-gating-api.ts'
const filter = reactive<BrandGatingFilter>(createBrandGatingFilter())
const page = ref(1)
const pageSize = ref(10)
const rows = ref<BrandGatingRow[]>([])
const total = ref(0)
const pageLoading = ref(false)
const loadError = ref('')
const loaded = ref(false)
const stats = ref<BrandGatingStats | null>(null)
const statsLoading = ref(false)
const statsError = ref('')
const form = reactive<BrandGatingForm>(createBrandGatingForm())
const addBusy = ref(false)
const batchText = ref('')
const importBusy = ref(false)
const busyRowId = ref(0)
// 快速改筛选/翻页时先发的请求可能后返回;列表与统计各一个序号闸(CR-27)
const pageGate = createLatestRequestGate()
const statsGate = createLatestRequestGate()
const countryOptions = GATING_COUNTRIES.filter((country) => country !== '全部')
const typeOptions = GATING_TYPES
const statsCards = computed(() => {
const current = stats.value
const byType = new Map((current?.byType ?? []).map((item) => [item.name, item.count]))
return [
{ label: 'Gating总记录', value: current?.total ?? 0, tone: '' },
{ label: '生效中', value: current?.active ?? 0, tone: 'ok' },
{ label: '已解除', value: current?.disabled ?? 0, tone: 'muted' },
{ label: '去重品牌数', value: current?.brands ?? 0, tone: '' },
{ label: '品牌gating', value: byType.get('品牌gating') ?? 0, tone: 'primary' },
{ label: '类目gating', value: byType.get('类目gating') ?? 0, tone: 'warn' },
{ label: '账户不符合', value: byType.get('账户不符合') ?? 0, tone: 'danger' },
]
})
function feedback(message: string, kind: AdminFeedbackKind): void {
showAdminFeedback(message, kind)
}
async function loadStats(): Promise<void> {
const token = statsGate.begin()
statsLoading.value = true
statsError.value = ''
try {
const result = await fetchBrandGatingStats()
if (!statsGate.isCurrent(token)) return
stats.value = result
} catch (error) {
if (!statsGate.isCurrent(token)) return
statsError.value = actionableErrorText(error)
} finally {
if (statsGate.isCurrent(token)) statsLoading.value = false
}
}
async function loadPage(nextPage = page.value): Promise<void> {
const token = pageGate.begin()
pageLoading.value = true
loadError.value = ''
try {
const result = await fetchBrandGatingPage(filter, nextPage, pageSize.value)
if (!pageGate.isCurrent(token)) return
rows.value = result.rows
total.value = result.total
page.value = nextPage
loaded.value = true
} catch (error) {
if (!pageGate.isCurrent(token)) return
loadError.value = actionableErrorText(error)
} finally {
if (pageGate.isCurrent(token)) pageLoading.value = false
}
}
function query(): void {
loadPage(1)
}
function resetFilter(): void {
Object.assign(filter, createBrandGatingFilter())
loadPage(1)
}
function changePageSize(size: number): void {
pageSize.value = size
loadPage(1)
}
async function addOne(): Promise<void> {
const error = validateBrandGatingForm(form)
if (error) {
feedback(error, 'warning')
return
}
addBusy.value = true
try {
const added = await submitBrandGating(form)
if (added) {
feedback('新增成功', 'success')
form.brand = ''
form.asin = ''
form.remark = ''
} else {
feedback('该品牌 + 国家已存在,未重复添加', 'warning')
}
await Promise.all([loadPage(added ? 1 : page.value), loadStats()])
} catch (error) {
feedback(actionableErrorText(error), 'error')
} finally {
addBusy.value = false
}
}
async function batchAdd(): Promise<void> {
const text = batchText.value.trim()
if (!text) {
feedback('请粘贴批量数据', 'warning')
return
}
importBusy.value = true
try {
const result = await importBrandGatingText(text)
feedback(`批量导入完成:新增 ${result.added} 条,跳过重复 ${result.skipped} 条`, 'success')
batchText.value = ''
await Promise.all([loadPage(1), loadStats()])
} catch (error) {
feedback(actionableErrorText(error), 'error')
} finally {
importBusy.value = false
}
}
async function toggleStatus(row: BrandGatingRow): Promise<void> {
const enabling = row.status !== 1
if (!enabling) {
const confirmed = await openAdminConfirm({ danger: false,
title: '解除Gating',
message: `确认解除「${row.brand}」的Gating记录?解除后排查会跳过该品牌。`,
confirmText: '确认解除',
})
if (!confirmed) return
}
busyRowId.value = row.id
try {
await updateBrandGatingStatus(row.id, enabling ? 1 : 0)
feedback(enabling ? '已恢复生效' : '已解除,排查不再拦截', 'success')
await Promise.all([loadPage(), loadStats()])
} catch (error) {
feedback(actionableErrorText(error), 'error')
} finally {
busyRowId.value = 0
}
}
async function removeRow(row: BrandGatingRow): Promise<void> {
const confirmed = await openAdminConfirm({ danger: true,
targetName: row.brand,
message: `确认删除 ID=${row.id} 的「${row.brand}」Gating记录?此操作可能影响已有数据。`,
})
if (!confirmed) return
busyRowId.value = row.id
try {
await deleteBrandGating(row.id)
feedback('已删除', 'success')
const nextPage = rows.value.length === 1 && page.value > 1 ? page.value - 1 : page.value
await Promise.all([loadPage(nextPage), loadStats()])
} catch (error) {
feedback(actionableErrorText(error), 'error')
} finally {
busyRowId.value = 0
}
}
function typeClass(type: string): string {
if (type.includes('账户')) return 'g-tag-acc'
if (type.includes('类目')) return 'g-tag-cat'
return 'g-tag-brand'
}
function sourceText(source: string): string {
if (source === 'AUTO') return '自动同步'
if (source === 'MANUAL') return '手动'
return source || '—'
}
onMounted(() => {
loadStats()
loadPage(1)
})
</script>
<template>
<div class="gating-panel">
<div v-if="statsError" class="g-stats-error">
<span>统计加载失败:{{ statsError }}</span>
<button type="button" @click="loadStats">重试</button>
</div>
<section class="g-cards" v-loading="statsLoading">
<div v-for="card in statsCards" :key="card.label" class="g-card" :class="card.tone">
<div class="g-n">{{ card.value.toLocaleString() }}</div>
<div class="g-t">{{ card.label }}</div>
</div>
</section>
<div class="g-forms">
<section class="g-panel">
<div class="g-panel-head">新增 Gating 品牌</div>
<div class="g-panel-body">
<div class="g-fields">
<label class="g-field">
<span>品牌 *</span>
<input v-model="form.brand" placeholder="品牌(必填)" @keyup.enter="addOne" />
</label>
<label class="g-field">
<span>国家</span>
<select v-model="form.country">
<option v-for="country in GATING_COUNTRIES" :key="country" :value="country">{{ country }}</option>
</select>
</label>
<label class="g-field">
<span>类型</span>
<select v-model="form.gatingType">
<option v-for="type in typeOptions" :key="type" :value="type">{{ type }}</option>
</select>
</label>
<label class="g-field">
<span>ASIN</span>
<input v-model="form.asin" placeholder="ASIN(可选)" @keyup.enter="addOne" />
</label>
<label class="g-field g-field-wide">
<span>备注</span>
<input v-model="form.remark" placeholder="备注(可选)" @keyup.enter="addOne" />
</label>
</div>
<div class="g-form-actions">
<button class="g-btn g-btn-primary" type="button" :disabled="addBusy" @click="addOne">
{{ addBusy ? '新增中…' : '新增' }}
</button>
</div>
</div>
</section>
<section class="g-panel">
<div class="g-panel-head">批量导入 Gating 品牌</div>
<div class="g-panel-body">
<textarea
v-model="batchText"
class="g-textarea"
placeholder="每行一个,格式:品牌,国家,类型,ASIN,备注(后四列可省略)&#10;示例:&#10;tory burch,英国,品牌gating&#10;jellycat,全部,品牌gating&#10;crocs,德国,类目gating"
></textarea>
<div class="g-form-actions">
<button class="g-btn g-btn-violet" type="button" :disabled="importBusy" @click="batchAdd">
{{ importBusy ? '导入中…' : '批量导入' }}
</button>
</div>
</div>
</section>
</div>
<section class="g-panel">
<div class="g-panel-head">Gating 品牌列表</div>
<div class="g-panel-body">
<div class="g-filters">
<input v-model="filter.keyword" placeholder="搜索品牌 / ASIN" @keyup.enter="query" />
<select v-model="filter.country">
<option value="">全部国家</option>
<option v-for="country in countryOptions" :key="country" :value="country">{{ country }}</option>
</select>
<select v-model="filter.gatingType">
<option value="">全部类型</option>
<option v-for="type in typeOptions" :key="type" :value="type">{{ type }}</option>
</select>
<select v-model="filter.status">
<option value="">全部状态</option>
<option value="1">生效</option>
<option value="0">已解除</option>
</select>
<select v-model="filter.source">
<option value="">全部来源</option>
<option v-for="source in GATING_SOURCES" :key="source" :value="source">{{ sourceText(source) }}</option>
</select>
<button class="g-btn g-btn-primary" type="button" @click="query">查询</button>
<button class="g-btn g-btn-ghost" type="button" @click="resetFilter">重置</button>
</div>
<LoadFailureBar v-if="loadError" :message="loadError" @retry="loadPage()" />
<div class="g-table-wrap" v-loading="pageLoading">
<table v-if="rows.length" class="g-table">
<thead>
<tr>
<th style="width: 64px">ID</th>
<th>品牌</th>
<th>ASIN</th>
<th style="width: 82px">国家</th>
<th style="width: 112px">类型</th>
<th style="width: 82px">状态</th>
<th style="width: 100px">来源</th>
<th>店铺</th>
<th>备注</th>
<th style="width: 156px">创建时间</th>
<th style="width: 118px">操作</th>
</tr>
</thead>
<tbody>
<tr v-for="row in rows" :key="row.id">
<td class="g-dim">{{ row.id }}</td>
<td class="g-strong">{{ row.brand }}</td>
<td class="g-mono">{{ row.asin || '—' }}</td>
<td>{{ row.country || '—' }}</td>
<td><span class="g-tag" :class="typeClass(row.gatingType)">{{ row.gatingType || '—' }}</span></td>
<td>
<span class="g-tag" :class="row.status === 1 ? 'g-tag-on' : 'g-tag-off'">
{{ row.status === 1 ? '生效' : '已解除' }}
</span>
</td>
<td :class="row.source === 'AUTO' ? 'g-source-auto' : 'g-source-manual'">{{ sourceText(row.source) }}</td>
<td>{{ row.shopName || '—' }}</td>
<td class="g-remark" :title="row.remark">{{ row.remark || '—' }}</td>
<td class="g-time">{{ formatDateTime(row.createdAt) }}</td>
<td>
<div class="g-ops">
<button
class="g-btn g-btn-sm"
:class="row.status === 1 ? 'g-btn-ghost' : 'g-btn-green'"
type="button"
:disabled="busyRowId === row.id"
@click="toggleStatus(row)"
>
{{ row.status === 1 ? '解除' : '恢复' }}
</button>
<button
class="g-btn g-btn-sm g-btn-danger"
type="button"
:disabled="busyRowId === row.id"
@click="removeRow(row)"
>
删除
</button>
</div>
</td>
</tr>
</tbody>
</table>
<div v-else-if="!pageLoading && loaded" class="g-empty">暂无数据,请先在上方新增或使用前端排查软件同步</div>
</div>
<OldPagination
v-if="total"
:total="total"
:page="page"
:page-size="pageSize"
@change="loadPage"
@size-change="changePageSize"
/>
</div>
</section>
</div>
</template>
<style scoped>
.gating-panel {
--g-border: #e5e9f0;
--g-text: #1c2230;
--g-text2: #5a6474;
--g-text3: #98a1b1;
--g-primary: #2e5be6;
--g-danger: #d63a4a;
--g-green: #1f9d62;
--g-amber: #d9912b;
--g-violet: #7c5cd6;
--g-surface: #ffffff;
--g-mono: "Cascadia Mono", "SF Mono", Consolas, "Courier New", monospace;
--g-shadow: 0 1px 2px rgba(20, 30, 60, 0.05), 0 4px 18px rgba(20, 30, 60, 0.06);
display: flex;
flex-direction: column;
gap: 14px;
}
.g-stats-error {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
padding: 9px 14px;
border-radius: 9px;
border: 1px solid #f3c6cc;
background: #fdebed;
color: var(--g-danger);
font-size: 12px;
}
.g-stats-error button {
border: none;
background: none;
color: var(--g-danger);
font-family: inherit;
font-size: 12px;
font-weight: 700;
cursor: pointer;
}
.g-cards {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(132px, 1fr));
gap: 10px;
}
.g-card {
background: var(--g-surface);
border: 1px solid var(--g-border);
border-top: 3px solid #9ca3af;
border-radius: 10px;
box-shadow: var(--g-shadow);
padding: 12px 14px;
}
.g-card.ok { border-top-color: var(--g-green); }
.g-card.muted { border-top-color: #b8c0ce; }
.g-card.primary { border-top-color: var(--g-primary); }
.g-card.warn { border-top-color: var(--g-amber); }
.g-card.danger { border-top-color: var(--g-danger); }
.g-card .g-n {
font-size: 24px;
font-weight: 700;
color: var(--g-text);
font-variant-numeric: tabular-nums;
line-height: 1.2;
}
.g-card .g-t {
margin-top: 3px;
font-size: 11.5px;
color: var(--g-text3);
}
.g-forms {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(340px, 1fr));
gap: 14px;
}
.g-panel {
background: var(--g-surface);
border: 1px solid var(--g-border);
border-radius: 10px;
box-shadow: var(--g-shadow);
overflow: hidden;
}
.g-panel-head {
padding: 11px 15px;
background: #fafbfd;
border-bottom: 1px solid #e9edf3;
font-size: 13px;
font-weight: 700;
color: var(--g-text);
}
.g-panel-head::before {
content: "";
display: inline-block;
width: 7px;
height: 7px;
border-radius: 2px;
background: #2dd4bf;
margin-right: 8px;
vertical-align: 1px;
}
.g-panel-body {
padding: 14px 15px;
}
.g-fields {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 10px 12px;
}
.g-field {
display: flex;
flex-direction: column;
gap: 5px;
min-width: 0;
}
.g-field > span {
font-size: 12px;
color: var(--g-text2);
}
.g-field-wide {
grid-column: span 2;
}
.g-field input,
.g-field select,
.g-filters input,
.g-filters select {
width: 100%;
height: 34px;
padding: 0 10px;
border: 1px solid #d7dce5;
border-radius: 7px;
background: #fbfcfe;
color: var(--g-text);
font-family: inherit;
font-size: 13px;
outline: none;
}
.g-field input:focus,
.g-field select:focus,
.g-filters input:focus,
.g-filters select:focus {
border-color: var(--g-primary);
box-shadow: 0 0 0 3px rgba(46, 91, 230, 0.12);
background: #fff;
}
.g-form-actions {
display: flex;
justify-content: flex-end;
margin-top: 12px;
}
.g-textarea {
width: 100%;
height: 126px;
padding: 9px 10px;
border: 1px solid #d7dce5;
border-radius: 7px;
background: #fbfcfe;
color: var(--g-text);
font-family: inherit;
font-size: 12.5px;
line-height: 1.6;
resize: vertical;
outline: none;
}
.g-textarea:focus {
border-color: var(--g-primary);
box-shadow: 0 0 0 3px rgba(46, 91, 230, 0.12);
background: #fff;
}
.g-filters {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
margin-bottom: 12px;
}
.g-filters input {
width: 190px;
}
.g-filters select {
width: 132px;
}
.g-btn {
height: 34px;
padding: 0 15px;
border: 1px solid transparent;
border-radius: 7px;
font-family: inherit;
font-size: 13px;
font-weight: 600;
cursor: pointer;
transition: background 0.15s, border-color 0.15s, color 0.15s;
}
.g-btn:disabled {
opacity: 0.55;
cursor: not-allowed;
}
.g-btn-sm {
height: 28px;
padding: 0 10px;
font-size: 12px;
border-radius: 6px;
}
.g-btn-primary {
background: var(--g-primary);
border-color: var(--g-primary);
color: #fff;
}
.g-btn-primary:hover:not(:disabled) {
background: #1e4bd8;
border-color: #1e4bd8;
}
.g-btn-violet {
background: var(--g-violet);
border-color: var(--g-violet);
color: #fff;
}
.g-btn-violet:hover:not(:disabled) {
background: #6b4bc4;
border-color: #6b4bc4;
}
.g-btn-green {
background: #e7f7ef;
border-color: #bce7d2;
color: var(--g-green);
}
.g-btn-green:hover:not(:disabled) {
background: #d9f2e5;
}
.g-btn-danger {
background: #fdebed;
border-color: #f3c6cc;
color: var(--g-danger);
}
.g-btn-danger:hover:not(:disabled) {
background: #fbdde1;
}
.g-btn-ghost {
background: #fff;
border-color: #d7dce5;
color: var(--g-text2);
}
.g-btn-ghost:hover:not(:disabled) {
border-color: var(--g-primary);
color: var(--g-primary);
}
.g-table-wrap {
overflow-x: auto;
border: 1px solid var(--g-border);
border-radius: 9px;
min-height: 90px;
}
.g-table {
width: 100%;
border-collapse: separate;
border-spacing: 0;
font-size: 12.5px;
}
.g-table th {
position: sticky;
top: 0;
z-index: 1;
padding: 10px;
background: #f7f9fc;
border-bottom: 1px solid var(--g-border);
color: var(--g-text2);
font-size: 12px;
font-weight: 600;
text-align: left;
white-space: nowrap;
}
.g-table td {
padding: 9px 10px;
border-bottom: 1px solid #eff2f7;
color: var(--g-text);
white-space: nowrap;
}
.g-table tbody tr:hover td {
background: #f7faff;
}
.g-table tbody tr:last-child td {
border-bottom: none;
}
.g-strong {
font-weight: 600;
}
.g-dim {
color: var(--g-text3);
}
.g-mono {
font-family: var(--g-mono);
}
.g-time {
color: var(--g-text2);
font-size: 11.5px;
}
.g-remark {
max-width: 190px;
overflow: hidden;
text-overflow: ellipsis;
color: var(--g-text2);
}
.g-source-auto {
color: var(--g-violet);
}
.g-source-manual {
color: #0d9488;
}
.g-tag {
display: inline-block;
padding: 2px 8px;
border-radius: 6px;
font-size: 11px;
font-weight: 600;
white-space: nowrap;
}
.g-tag-brand {
background: #eff6ff;
color: #2563eb;
}
.g-tag-cat {
background: #fef3c7;
color: #b45309;
}
.g-tag-acc {
background: #fee2e2;
color: #dc2626;
}
.g-tag-on {
background: #d1fae5;
color: #10b981;
}
.g-tag-off {
background: #e5e7eb;
color: #9ca3af;
}
.g-ops {
display: flex;
flex-wrap: wrap;
gap: 6px;
}
.g-empty {
padding: 42px 16px;
text-align: center;
color: var(--g-text3);
font-size: 13px;
}
</style>