Files
crawler-plugin/admin-frontend-vue/src/pages/shop/ShopKeysPage.vue
T

641 lines
21 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">
import BatchDeleteActions from '@/components/BatchDeleteActions.vue'
import BatchDeleteCheckbox from '@/components/BatchDeleteCheckbox.vue'
import { useRowSelection } from '@/utils/use-row-selection'
import { createLatestRequestGate } from '@/utils/latest-request'
import { formatDateTime } from '@/utils/datetime'
/** 店铺密钥管理页 · 像素复刻旧版 admin.html panel-shop-keys(自绘:面板头+新增店铺密钥、序号列、令牌掩码、白名单状态药丸+悬停详情、旧式分页)。
* script 逻辑沿用现有 Vue 实现(编辑留空令牌=沿用);弹窗保留现有组件。 */
import { computed, onMounted, reactive, ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { useCompanyTabStore } from '@/stores/company-tab'
import { fetchShopKeyList, submitCreateShopKey, updateShopKey, deleteShopKey, checkShopKeyWhitelist } from './shop-key-api.ts'
import { emptyShopKeyForm, normalizeZiniaoToken, validateProxyUrl } from './shop-key-form-model.ts'
import type { IpWhitelistStatus, ShopKeyItem } from './shop-dto.ts'
import { whitelistStatusMeta, whitelistStatusOptions } from './shop-key-model.ts'
import OldPagination from '@/components/OldPagination.vue'
import { showAdminFeedback } from '@/components/admin-feedback-ui'
import { openAdminConfirm } from '@/components/admin-confirm'
import LoadFailureBar from '@/components/LoadFailureBar.vue'
const companyTab = useCompanyTabStore()
const loading = ref(false)
const loadFailure = ref('')
const rows = ref<ShopKeyItem[]>([])
const total = ref(0)
const page = ref(1)
const pageSize = ref(10)
const jumpPage = ref('')
const totalPages = computed(() => Math.max(1, Math.ceil(total.value / pageSize.value)))
/** 筛选:三个条件全部交后端做。曾经的"备注名/紫鸟账号"是前端 computed,只在**当前这一页的
* 10 行**里过滤且 total 纹丝不动 —— 第 11 行起就筛不掉,翻页后结果还会自己变回去。 */
const filter = reactive({ keyword: '', token: '', whitelistStatus: null as IpWhitelistStatus | null })
const statusOptions = whitelistStatusOptions()
const anyFilterActive = computed(
() => Boolean(filter.keyword.trim() || filter.token.trim() || filter.whitelistStatus),
)
const dialogVisible = ref(false)
const editingId = ref<number | null>(null)
const saving = ref(false)
/** 正在检测白名单的行 id,用于按钮 loading 态。 */
const checkingId = ref<number | null>(null)
const form = reactive(emptyShopKeyForm())
function maskToken(token: string): string {
const value = token || ''
if (value.length <= 8) return '••••••••'
return `${value.slice(0, 4)}••••••••${value.slice(-4)}`
}
/** 编辑时保留原令牌的内存引用:令牌不落页面明文展示,留空提交则沿用原令牌。 */
const originalToken = ref('')
/** 白名单状态药丸悬停详情:检测时间 + 检测消息(对齐 admin)。 */
function whitelistTooltip(row: ShopKeyItem): string {
const parts: string[] = []
if (row.ipWhitelistCheckedAt) parts.push(`检测时间:${formatDateTime(row.ipWhitelistCheckedAt)}`)
if (row.ipWhitelistMessage) parts.push(row.ipWhitelistMessage)
if (row.ipWhitelistFailCount > 0) parts.push(`连续失败 ${row.ipWhitelistFailCount} 次`)
if (parts.length === 0) parts.push(`白名单状态:${whitelistStatusMeta(row.ipWhitelistStatus).label}`)
return parts.join('\n')
}
const batchListGate = createLatestRequestGate()
async function load() {
const token = batchListGate.begin()
loading.value = true
loadFailure.value = ''
try {
const result = await fetchShopKeyList({
page: page.value,
pageSize: pageSize.value,
keyword: filter.keyword.trim() || undefined,
token: filter.token.trim() || undefined,
whitelistStatus: filter.whitelistStatus || undefined,
companyId: companyTab.requestCompanyId,
})
if (!batchListGate.isCurrent(token)) return
rows.value = result.items
total.value = result.total
page.value = result.page
} catch (error) {
if (!batchListGate.isCurrent(token)) return
loadFailure.value = error instanceof Error ? error.message : '密钥列表加载失败'
showAdminFeedback(loadFailure.value, 'error')
} finally {
if (batchListGate.isCurrent(token)) loading.value = false
}
}
function openCreate() {
editingId.value = null
Object.assign(form, emptyShopKeyForm())
dialogVisible.value = true
}
function openEdit(row: ShopKeyItem) {
editingId.value = row.id
originalToken.value = row.ziniaoToken
Object.assign(form, emptyShopKeyForm())
form.remarkName = row.remarkName
form.ziniaoAccountName = row.ziniaoAccountName
form.proxyUrl = row.proxyUrl || ''
dialogVisible.value = true
}
/** 手动检测白名单:绕过缓存真实请求一次紫鸟,并重置自动重试次数。 */
async function checkWhitelist(row: ShopKeyItem) {
checkingId.value = row.id
try {
const updated = await checkShopKeyWhitelist(row.id)
if (updated.ipWhitelistStatus === 'ALLOWED') {
ElMessage.success('白名单检测通过')
} else if (updated.ipWhitelistStatus === 'BLOCKED') {
showAdminFeedback(`白名单未放行:${updated.ipWhitelistMessage || '请求被拒绝'}`, 'error')
} else {
ElMessage.warning(updated.ipWhitelistMessage || '检测未通过')
}
load()
} catch (error) {
showAdminFeedback(error instanceof Error ? error.message : '检测失败', 'error')
} finally {
checkingId.value = null
}
}
async function submit() {
const token = normalizeZiniaoToken(form.ziniaoToken)
const creating = editingId.value == null
if (creating && !token) {
ElMessage.warning('请完整填写紫鸟账号名称、紫鸟令牌')
return
}
if (!(form.ziniaoAccountName || '').trim()) {
ElMessage.warning('请完整填写紫鸟账号名称、紫鸟令牌')
return
}
const proxyError = validateProxyUrl(form.proxyUrl)
if (proxyError) {
ElMessage.warning(proxyError)
return
}
// 编辑留空令牌 = 沿用原令牌(令牌不明文展示)。
form.ziniaoToken = creating ? token : token || originalToken.value
saving.value = true
try {
if (creating) {
await submitCreateShopKey({ ...form }, companyTab.requestCompanyId)
} else {
await updateShopKey(editingId.value as number, { ...form })
}
ElMessage.success(creating ? '已新增' : '已保存')
dialogVisible.value = false
load()
} catch (error) {
showAdminFeedback(error instanceof Error ? error.message : '保存失败', 'error')
} finally {
saving.value = false
}
}
const batch = useRowSelection({
pageRows: () => rows.value,
rowId: (row) => row.id,
rowLabel: (row) => row.ziniaoAccountName,
deleteOne: (id) => deleteShopKey(id),
refresh: load,
blocked: () => loading.value || !!loadFailure.value,
scope: () => companyTab.activeId,
})
async function remove(row: ShopKeyItem) {
if (batch.busy) return
// 对齐旧版:原生 confirm。
if (!(await openAdminConfirm({ message: `确定删除店铺密钥“${row.ziniaoAccountName}”吗?`, danger: true }))) return
try {
await deleteShopKey(row.id)
batch.removeFromSelection(row.id)
ElMessage.success('已删除')
load()
} catch (error) {
showAdminFeedback(error instanceof Error ? error.message : '删除失败', 'error')
}
}
function changePage(next: number) {
if (next < 1 || next > totalPages.value) return
page.value = next
load()
}
function changeSize(size: number) {
pageSize.value = size
page.value = 1
load()
}
function goJump() {
const n = Number.parseInt(jumpPage.value, 10)
if (Number.isNaN(n)) {
ElMessage.warning('请输入页码')
return
}
changePage(Math.min(Math.max(n, 1), totalPages.value))
}
function apply() {
page.value = 1
void load()
}
// 切公司 = 清掉全部筛选 + 回第 1 页 + 重取数,与其余 9 页同一条约定
// (护栏 company-tab-wiring 的 WATCH_BODY_PINS 逐字钉着这一行)。
// 筛选改到服务端做之后这条更要守:留着上一家填的词去筛新一家,首屏就是一张
// 「暂无匹配密钥」而框里还显示着那个词——tab 看着像坏了,请求/后端/权限三处都没错。
watch(() => companyTab.activeId, () => { filter.keyword = ''; filter.token = ''; filter.whitelistStatus = null; page.value = 1; void load() })
onMounted(load)
</script>
<template>
<div class="keys-view">
<section class="panel-box">
<div class="keys-head">
<h3>店铺密钥列表</h3>
<button class="btn" type="button" @click="openCreate">新增店铺密钥</button>
</div>
<div class="form-row keys-filter-row">
<div class="form-group" style="min-width: 220px">
<label>备注名 / 紫鸟账号</label>
<input v-model="filter.keyword" type="text" placeholder="模糊搜索备注名或紫鸟账号" @keyup.enter="apply" />
</div>
<div class="form-group" style="min-width: 220px">
<label>紫鸟令牌</label>
<input v-model="filter.token" type="text" placeholder="粘贴令牌或其片段定位这一把" @keyup.enter="apply" />
</div>
<div class="form-group" style="min-width: 160px">
<label>白名单状态</label>
<el-select v-model="filter.whitelistStatus" class="admin-filter" clearable placeholder="全部状态">
<el-option v-for="option in statusOptions" :key="option.value" :label="option.label" :value="option.value" />
</el-select>
</div>
<button class="btn" type="button" @click="apply">查询</button>
</div>
<LoadFailureBar v-if="loadFailure" :message="loadFailure" @retry="load" />
<BatchDeleteActions :batch="batch" />
<div class="shop-key-table-scroll">
<table>
<thead>
<!-- 刚性列(序号/两个时间/操作)给准 px,文本列收到"能 ellipsis 但不白占"的宽度:
原来 1538px 在 ~1280 的内容区里放不下,"操作"整列要横向滚动才看得见。 -->
<tr>
<th class="batch-delete-cell" scope="col">
<BatchDeleteCheckbox label="全选当前页可删除记录" :checked="batch.pageAll" :partial="batch.pageSome"
:disabled="batch.disabled || batch.pageEmpty" @change="batch.togglePage" />
</th>
<th style="width: 52px">序号</th>
<th style="width: 110px">备注名</th>
<th style="width: 140px">紫鸟账号名称</th>
<th style="width: 160px">紫鸟令牌</th>
<th style="width: 90px">代理</th>
<th style="width: 110px">白名单状态 <span class="table-help" aria-hidden="true">?</span></th>
<th style="width: 160px">创建时间</th>
<th style="width: 160px">修改时间</th>
<th style="width: 245px">操作</th>
</tr>
</thead>
<tbody>
<template v-if="rows.length">
<tr v-for="(row, index) in rows" :key="row.id" :class="{ 'batch-selected': batch.isSelected(row) }">
<td class="batch-delete-cell">
<BatchDeleteCheckbox :label="batch.reason(row) || `选择 ${batch.label(row)}`" :checked="batch.isSelected(row)"
:disabled="batch.disabled || !!batch.reason(row)" @change="batch.toggle(row)" />
</td>
<td>{{ (page - 1) * pageSize + index + 1 }}</td>
<td>{{ row.remarkName || '—' }}</td>
<td>{{ row.ziniaoAccountName }}</td>
<td>
<span class="mono-mask" :title="row.ziniaoToken">{{ maskToken(row.ziniaoToken) }}</span>
</td>
<td>
<span class="mono-mask" :title="row.proxyUrl || '直连'">{{ row.proxyUrl || '直连' }}</span>
</td>
<td>
<span
class="wh-pill"
:class="{
'is-allowed': whitelistStatusMeta(row.ipWhitelistStatus).tag === 'success',
'is-blocked': whitelistStatusMeta(row.ipWhitelistStatus).tag === 'danger',
'is-unknown': whitelistStatusMeta(row.ipWhitelistStatus).tag === 'info',
}"
:title="whitelistTooltip(row)"
>
{{ whitelistStatusMeta(row.ipWhitelistStatus).label }}
</span>
</td>
<td>{{ formatDateTime(row.createdAt) }}</td>
<td>{{ formatDateTime(row.updatedAt) }}</td>
<td class="ops-cell">
<button class="btn btn-sm" type="button" @click="openEdit(row)">编辑</button>
<button
class="btn btn-sm btn-ghost"
type="button"
:disabled="checkingId === row.id"
@click="checkWhitelist(row)"
>
{{ checkingId === row.id ? '检测中…' : '检测白名单' }}
</button>
<button class="btn btn-sm btn-danger" type="button" :disabled="batch.busy" @click="remove(row)">删除</button>
</td>
</tr>
</template>
<tr v-else-if="loading">
<td colspan="10" class="empty-tip">加载中...</td>
</tr>
<tr v-else>
<td colspan="10" class="empty-tip">{{ anyFilterActive ? '暂无匹配密钥' : '暂无店铺密钥' }}</td>
</tr>
</tbody>
</table>
</div>
<OldPagination :total="total" :page="page" :page-size="pageSize" @change="changePage" @size-change="changeSize" />
</section>
<el-dialog v-model="dialogVisible" :title="editingId == null ? '新增密钥' : '编辑密钥'" width="560px">
<el-form label-width="110px">
<el-form-item label="备注名">
<el-input v-model="form.remarkName" placeholder="可选" />
</el-form-item>
<el-form-item label="紫鸟账号" required>
<el-input v-model="form.ziniaoAccountName" placeholder="紫鸟浏览器账号名称" />
</el-form-item>
<el-form-item label="紫鸟令牌" required>
<el-input
v-model="form.ziniaoToken"
type="textarea"
:rows="3"
:placeholder="editingId == null ? '粘贴紫鸟令牌(将自动去除 Bearer 前缀)' : '留空则沿用原令牌;如需更换请粘贴新令牌(将自动去除 Bearer 前缀)'"
/>
</el-form-item>
<el-form-item label="代理地址">
<el-input
v-model="form.proxyUrl"
placeholder="留空直连;形如 http://host:port 或 http://user:pass@host:port"
/>
<div class="form-hint">
仅当该账号的服务器出口 IP 不在紫鸟白名单时填写:代理 IP 需已由紫鸟公司加白,留空则直连。
</div>
</el-form-item>
</el-form>
<template #footer>
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary" :loading="saving" @click="submit">保存</el-button>
</template>
</el-dialog>
</div>
</template>
<style scoped>
/* 像素复刻旧版 admin.html panel-shop-keys(蓝白末层)。 */
.keys-view {
font-family: inherit;
color: #24384d;
}
.panel-box {
width: 100%;
min-width: 0;
padding: 20px 22px 24px;
border: 1px solid #d8e3ee;
border-radius: 14px;
background: linear-gradient(145deg, #ffffff, #f9fbfd);
box-shadow: 0 1px 2px rgba(39, 67, 94, 0.04), 0 14px 30px -24px rgba(39, 67, 94, 0.28);
}
h3 {
margin: 0;
font-size: 15px;
font-weight: 650;
color: #24384d;
letter-spacing: 0.2px;
}
.keys-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 16px;
}
.keys-filter-row {
margin: 0 0 16px;
}
.form-row {
display: flex;
flex-wrap: wrap;
align-items: flex-end;
gap: 14px 18px;
}
.form-group {
display: flex;
flex-direction: column;
gap: 7px;
margin-bottom: 0;
}
.form-group label {
color: #5b6f83;
font-size: 12.5px;
font-weight: 600;
}
.form-group input,
.form-group select {
min-width: 0;
min-height: 42px;
padding: 8px 12px;
border: 1px solid #cbd9e6;
border-radius: 9px;
background: #f8fbfd;
color: #24384d;
font-size: 13.5px;
font-family: inherit;
color-scheme: light;
outline: none;
transition: border-color 160ms ease, box-shadow 160ms ease, background 160ms ease;
}
.form-group input:hover,
.form-group select:hover {
border-color: #9fb7cd;
}
.form-group input:focus,
.form-group select:focus {
background: #ffffff;
border-color: #5f85ad;
box-shadow: 0 0 0 3px rgba(95, 133, 173, 0.16);
}
.btn {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 8px;
min-height: 42px;
padding: 9px 18px;
border: 1px solid #4f78a5;
border-radius: 9px;
background: linear-gradient(135deg, #5f85ad, #4f78a5);
color: #ffffff;
font-family: inherit;
font-size: 13.5px;
cursor: pointer;
box-shadow: 0 8px 18px -14px rgba(79, 120, 165, 0.72);
transition: background 160ms ease, box-shadow 160ms ease, transform 120ms ease;
}
.btn:hover:not(:disabled) {
background: linear-gradient(135deg, #7094ba, #5d83ac);
box-shadow: 0 11px 22px -14px rgba(79, 120, 165, 0.8);
}
.btn:disabled {
cursor: not-allowed;
opacity: 0.55;
}
.btn-danger {
background: linear-gradient(135deg, #c06d77, #b35f6a);
border-color: #b35f6a;
}
.btn-danger:hover:not(:disabled) {
background: linear-gradient(135deg, #cb7c84, #b96570);
}
.btn-ghost {
background: #ffffff;
border-color: #c7d7e5;
color: #4f78a5;
box-shadow: none;
}
.btn-ghost:hover:not(:disabled) {
background: #edf5fb;
border-color: #95b1cb;
color: #2f5d8b;
}
.form-hint {
margin-top: 4px;
color: #8293a5;
font-size: 12px;
line-height: 1.5;
}
.btn-sm {
min-height: 36px;
padding: 7px 12px;
}
.shop-key-table-scroll {
width: 100%;
min-width: 0;
overflow-x: auto;
border: 1px solid #dbe5ee;
border-radius: 10px;
background: #ffffff;
}
.shop-key-table-scroll table {
width: 100%;
border-collapse: collapse;
table-layout: fixed;
min-width: 1200px;
}
.shop-key-table-scroll th,
.shop-key-table-scroll td {
padding: 10px 12px;
text-align: left;
font-size: 13.5px;
line-height: 1.5;
border-bottom: 1px solid #e0e8ef;
vertical-align: middle;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.shop-key-table-scroll th {
background: #edf4fa;
color: #4e6479;
border-bottom-color: #d5e1eb;
font-size: 12.5px;
font-weight: 600;
letter-spacing: 0.4px;
}
.shop-key-table-scroll tbody tr:hover td {
background: #f1f7fb;
}
.shop-key-table-scroll tbody tr:last-child td {
border-bottom: 0;
}
.table-help {
display: inline-flex;
align-items: center;
justify-content: center;
width: 14px;
height: 14px;
margin-left: 3px;
border-radius: 50%;
background: #c7d7e5;
color: #fff;
font-size: 10px;
line-height: 1;
}
.mono-mask {
font-family: Consolas, "Cascadia Mono", monospace;
font-size: 12.5px;
letter-spacing: 0.5px;
}
.wh-pill {
display: inline-flex;
align-items: center;
padding: 2px 10px;
border-radius: 999px;
border: 1px solid;
font-size: 12px;
line-height: 1.7;
}
.wh-pill.is-allowed {
color: #3d7158;
background: #e8f2eb;
border-color: #bdd7c5;
}
.wh-pill.is-blocked {
color: #91474f;
background: #f8ebeb;
border-color: #e4c2c5;
}
.wh-pill.is-unknown,
.wh-pill.is-pending,
.wh-pill.is-notchecked {
color: #5b6f83;
background: #f1f6fa;
border-color: #d6e2ec;
}
.ops-cell {
display: flex;
align-items: center;
gap: 8px;
white-space: nowrap;
/* 兜底:单元格是 overflow:hidden + table-layout:fixed,按钮总宽一旦超过列宽,
最后一颗会整颗被裁掉(看不见也点不到,2026-10-08 的"删除按钮没了"就是这么来的)。
允许换行等于把"静默消失"换成"多占一行"。 */
flex-wrap: wrap;
}
.empty-tip {
padding: 44px 24px;
text-align: center;
color: #8293a5;
font-size: 13.5px;
}
.pagination {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 10px;
margin-top: 18px;
color: #5b6f83;
font-size: 13px;
}
.pagination button {
min-height: 30px;
padding: 4px 12px;
border: 1px solid #c7d7e5;
border-radius: 8px;
background: #ffffff;
color: #5b6f83;
font-family: inherit;
font-size: 13px;
cursor: pointer;
}
.pagination button:hover:not(:disabled) {
background: #edf5fb;
border-color: #95b1cb;
color: #2f5d8b;
}
.pagination button:disabled {
background: #eef3f7;
color: #9baaba;
cursor: not-allowed;
}
.page-total {
margin-right: 4px;
}
.page-jump {
display: inline-flex;
align-items: center;
gap: 6px;
}
.page-jump input {
width: 56px;
min-height: 30px;
padding: 4px 8px;
border: 1px solid #cbd9e6;
border-radius: 8px;
background: #f8fbfd;
color: #24384d;
font-size: 13px;
font-family: inherit;
}
</style>