Compare commits
10 Commits
5928072c6a
...
1d4b4decf3
| Author | SHA1 | Date | |
|---|---|---|---|
| 1d4b4decf3 | |||
| b963561043 | |||
| b5e4c45add | |||
| 688910f738 | |||
| 8323245567 | |||
| 9b2469ec33 | |||
| 23c1402a75 | |||
| e265c188f7 | |||
| fcb6b5f545 | |||
| 583dad92ec |
@@ -41,6 +41,8 @@ export const COMPANY_SCOPED_PAGES: readonly CompanyScopedPage[] = [
|
||||
// 而是「本公司账号名下的店铺」:后端由 DataReadScope 把公司 → 成员账号 → biz_shop_manage.created_by_id 逐级推导,
|
||||
// 前端只负责把 activeId 当 company_id 送下去。
|
||||
{ menuKey: 'admin_shop_data_duplicate_check', apiFiles: ['src/pages/tasks/duplicate-console-api.ts'], pageFiles: ['src/pages/tasks/DuplicateCheckPage.vue'] },
|
||||
// 关键词使用记录(2026-10-25):只读留痕按行 company_id 收窄;超管切 tab 重取,非超管后端强制本公司。
|
||||
{ menuKey: 'admin_keyword_usage', apiFiles: ['src/pages/tasks/keyword-usage-api.ts'], pageFiles: ['src/pages/tasks/KeywordUsagePage.vue'] },
|
||||
{ menuKey: 'admin_shop_data_crawl_tasks', apiFiles: ['src/pages/tasks/shop-data-api.ts'], pageFiles: ['src/pages/tasks/ShopDataTasksPage.vue'] },
|
||||
// 批次 4(控制方 2026-10-05 点名「菜单权限和用户管理也要区分公司 tab 的数据」)。
|
||||
// 这一页的后端早就认 company_id(AdminUserService 只在**站点超管**身上用它筛,公司超管恒锁本公司),
|
||||
|
||||
@@ -72,6 +72,14 @@ export const OPERATION_GUIDES: Record<string, OperationGuideData> = {
|
||||
text: '店铺数据任务按状态和时间筛选。批量操作前请核对已选任务。',
|
||||
steps: ['筛选任务', '检查状态', '执行批量操作'],
|
||||
},
|
||||
admin_brand_gating: {
|
||||
text: '维护不符合规则的品牌(品牌Gating / 类目Gating)。新增后可用下方筛选快速回查;云端 Gating 库与前端排查软件同步同一份数据,命中后会自动拦截。',
|
||||
steps: ['新增或批量导入', '筛选回查', '按需解除或删除'],
|
||||
},
|
||||
admin_keyword_usage: {
|
||||
text: '本页记录所有关键词操作,只读、不可删除、永久留痕可追溯;与桌面端互通:桌面复制、添加、删除关键词会自动写入本页,两端同源。',
|
||||
steps: ['选择公司与筛选条件', '切换使用/删除记录', '核对操作人与时间'],
|
||||
},
|
||||
// admin_shop_data_duplicate_check(店铺撞款监控)为例外页:不展示全局提示条,不参与对齐。
|
||||
admin_image_video_tasks: {
|
||||
text: '可先使用筛选缩小任务范围,再查看任务状态、结果和权限范围。',
|
||||
|
||||
@@ -32,7 +32,7 @@ export interface WorkbenchClientUploadTarget {
|
||||
fileName: string
|
||||
}
|
||||
|
||||
/** 第一步:向后端申请直传 PUT 预签名地址(登录会话签发,3 分钟有效)。 */
|
||||
/** 第一步:向后端申请直传 PUT 预签名地址(登录会话签发,默认 1 小时有效)。 */
|
||||
export async function requestWorkbenchClientPresign(
|
||||
fileName: string,
|
||||
version: string,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
<script setup lang="ts">
|
||||
/**
|
||||
* 品牌Gating库管理面板:以第三个页签嵌在「店铺数据重复检查」页里。
|
||||
* 品牌Gating库管理页(独立菜单 admin_brand_gating,V174 恢复)。
|
||||
* 数据源 /api/admin/brand-gating(biz_brand_gating,全平台共享,不随公司 Tab 收窄)。
|
||||
*/
|
||||
import { computed, onMounted, reactive, ref } from 'vue'
|
||||
@@ -10,6 +10,7 @@ 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,
|
||||
@@ -49,7 +50,10 @@ const addBusy = ref(false)
|
||||
const batchText = ref('')
|
||||
const importBusy = ref(false)
|
||||
const busyRowId = ref(0)
|
||||
const tipVisible = ref(true)
|
||||
|
||||
// 快速改筛选/翻页时先发的请求可能后返回;列表与统计各一个序号闸(CR-27)
|
||||
const pageGate = createLatestRequestGate()
|
||||
const statsGate = createLatestRequestGate()
|
||||
|
||||
const countryOptions = GATING_COUNTRIES.filter((country) => country !== '全部')
|
||||
const typeOptions = GATING_TYPES
|
||||
@@ -73,30 +77,37 @@ function feedback(message: string, kind: AdminFeedbackKind): void {
|
||||
}
|
||||
|
||||
async function loadStats(): Promise<void> {
|
||||
const token = statsGate.begin()
|
||||
statsLoading.value = true
|
||||
statsError.value = ''
|
||||
try {
|
||||
stats.value = await fetchBrandGatingStats()
|
||||
const result = await fetchBrandGatingStats()
|
||||
if (!statsGate.isCurrent(token)) return
|
||||
stats.value = result
|
||||
} catch (error) {
|
||||
if (!statsGate.isCurrent(token)) return
|
||||
statsError.value = actionableErrorText(error)
|
||||
} finally {
|
||||
statsLoading.value = false
|
||||
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 {
|
||||
pageLoading.value = false
|
||||
if (pageGate.isCurrent(token)) pageLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -219,15 +230,6 @@ onMounted(() => {
|
||||
|
||||
<template>
|
||||
<div class="gating-panel">
|
||||
<div v-if="tipVisible" class="g-tip">
|
||||
<span class="g-tip-title">当前页面操作提示</span>
|
||||
<span>
|
||||
维护不符合规则的品牌(品牌Gating / 类目Gating)。新增后可用下方筛选快速回查;云端 Gating 库与前端排查软件同步同一份数据,命中后会自动拦截。
|
||||
<b>Gating 库为全平台共享数据,不随上方公司页签切换。</b>
|
||||
</span>
|
||||
<button class="g-tip-x" type="button" @click="tipVisible = false">收起 ✕</button>
|
||||
</div>
|
||||
|
||||
<div v-if="statsError" class="g-stats-error">
|
||||
<span>统计加载失败:{{ statsError }}</span>
|
||||
<button type="button" @click="loadStats">重试</button>
|
||||
@@ -415,39 +417,6 @@ onMounted(() => {
|
||||
gap: 14px;
|
||||
}
|
||||
|
||||
.g-tip {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
gap: 10px;
|
||||
background: #f5f9ff;
|
||||
border: 1px solid #cfe0fb;
|
||||
border-left: 4px solid var(--g-primary);
|
||||
border-radius: 10px;
|
||||
padding: 10px 14px;
|
||||
font-size: 12px;
|
||||
color: #1e40af;
|
||||
line-height: 1.7;
|
||||
}
|
||||
.g-tip-title {
|
||||
flex: none;
|
||||
font-weight: 700;
|
||||
color: #1d4ed8;
|
||||
}
|
||||
.g-tip b {
|
||||
color: #1d4ed8;
|
||||
}
|
||||
.g-tip-x {
|
||||
flex: none;
|
||||
margin-left: auto;
|
||||
border: none;
|
||||
background: none;
|
||||
color: #6b9ef0;
|
||||
font-family: inherit;
|
||||
font-size: 12px;
|
||||
font-weight: 700;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.g-stats-error {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
|
||||
@@ -13,9 +13,8 @@ import { aggregateDrawerRows, drawerSummaryOf, dupWithinGroup, indexShopGroups,
|
||||
import type { DuplicateConsole, LedgerRow } from './duplicate-console-model.ts'
|
||||
import { ASIN_COUNTRY_CODES, asinCountryLabel } from '../asin/asin-country.ts'
|
||||
import { orderedPair } from '@/utils/datetime'
|
||||
import BrandGatingPanel from './BrandGatingPanel.vue'
|
||||
|
||||
type Tab = 'dup' | 'ledger' | 'gating'
|
||||
type Tab = 'dup' | 'ledger'
|
||||
type Scope = 'global' | 'group'
|
||||
|
||||
const consoleData = ref<DuplicateConsole | null>(null)
|
||||
@@ -432,18 +431,18 @@ onMounted(() => {
|
||||
<div class="dup-head">
|
||||
<div>
|
||||
<h2>店铺数据撞款监控</h2>
|
||||
<p>按 ASIN×店铺×站点分析同一 ASIN 在多家自营店铺/分组内的重复上架;分组归属来自店铺管理,只读展示。品牌Gating库在同一页的第三个页签维护。</p>
|
||||
<p>按 ASIN×店铺×站点分析同一 ASIN 在多家自营店铺/分组内的重复上架;分组归属来自店铺管理,只读展示。</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div v-if="loadError && activeTab !== 'gating'" class="load-banner">
|
||||
<div v-if="loadError" class="load-banner">
|
||||
<span>{{ loadError }}</span>
|
||||
<button class="btn btn-ghost btn-sm" type="button" @click="loadConsole">重试</button>
|
||||
<button class="btn btn-ghost btn-sm" type="button" @click="loadError = ''">知道了</button>
|
||||
</div>
|
||||
|
||||
<!-- 筛选条件(撞款/台账专用,Gating 页签隐藏) -->
|
||||
<section v-if="activeTab !== 'gating'" class="card filter">
|
||||
<!-- 筛选条件(撞款/台账专用) -->
|
||||
<section class="card filter">
|
||||
<div class="filter-title">筛选条件</div>
|
||||
<div class="filter-row">
|
||||
<div class="f-group">
|
||||
@@ -493,9 +492,9 @@ onMounted(() => {
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<div v-loading="loading && activeTab !== 'gating'" class="dup-body-wrap">
|
||||
<div v-loading="loading" class="dup-body-wrap">
|
||||
<!-- KPI 统计 -->
|
||||
<section v-if="consoleData && activeTab !== 'gating'" class="kpis">
|
||||
<section v-if="consoleData" class="kpis">
|
||||
<div v-for="kpi in kpis" :key="kpi.label" class="kpi" :class="kpi.tone">
|
||||
<div class="k-label"><span class="k-dot"></span>{{ kpi.label }}</div>
|
||||
<div class="k-value">{{ kpi.value }}</div>
|
||||
@@ -504,7 +503,7 @@ onMounted(() => {
|
||||
</section>
|
||||
|
||||
<!-- 店铺上架分布 -->
|
||||
<section v-if="consoleData && activeTab !== 'gating'" class="card store-strip">
|
||||
<section v-if="consoleData" class="card store-strip">
|
||||
<div class="strip-head">
|
||||
<span class="strip-title">店铺上架分布</span>
|
||||
<span class="strip-note">各店铺在本批站点中的上架记录量(含重复扫描)</span>
|
||||
@@ -527,9 +526,8 @@ onMounted(() => {
|
||||
<div class="tabs">
|
||||
<button class="tab" :class="{ active: activeTab === 'dup' }" type="button" @click="switchTab('dup')">撞款监控 <span class="t-count">{{ totalDupCount }}</span></button>
|
||||
<button class="tab" :class="{ active: activeTab === 'ledger' }" type="button" @click="switchTab('ledger')">全部ASIN台账 <span class="t-count">{{ ledgerTotal }}</span></button>
|
||||
<button class="tab" :class="{ active: activeTab === 'gating' }" type="button" @click="switchTab('gating')">品牌Gating库</button>
|
||||
</div>
|
||||
<div v-if="activeTab !== 'gating'" class="actions">
|
||||
<div class="actions">
|
||||
<span v-if="overview" class="updated">数据更新时间 {{ overview.scannedAt || '—' }}</span>
|
||||
<button v-if="isSuper" class="btn btn-ghost btn-sm" type="button" @click="groupInfoVisible = true">
|
||||
<svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.2" stroke-linecap="round" stroke-linejoin="round"><path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2" /><circle cx="9" cy="7" r="4" /><path d="M23 21v-2a4 4 0 0 0-3-3.87" /><path d="M16 3.13a4 4 0 0 1 0 7.75" /></svg>
|
||||
@@ -546,13 +544,8 @@ onMounted(() => {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- ============ 品牌Gating库(全平台共享数据) ============ -->
|
||||
<template v-if="activeTab === 'gating'">
|
||||
<BrandGatingPanel />
|
||||
</template>
|
||||
|
||||
<!-- ============ 撞款监控 ============ -->
|
||||
<template v-else-if="consoleData && activeTab === 'dup'">
|
||||
<template v-if="consoleData && activeTab === 'dup'">
|
||||
<div v-if="isSuper" class="scope-bar">
|
||||
<div class="seg">
|
||||
<button class="seg-btn" :class="{ active: scope === 'global' }" type="button" @click="switchScope('global')">全店撞款</button>
|
||||
@@ -668,7 +661,7 @@ onMounted(() => {
|
||||
<OldPagination v-if="ledgerTotal" :total="ledgerTotal" :page="ledgerPage" :page-size="ledgerPageSize" @change="goPage" @size-change="changeLedgerSize" />
|
||||
</template>
|
||||
|
||||
<div v-if="!consoleData && activeTab !== 'gating' && !loadError" class="card empty-state">
|
||||
<div v-if="!consoleData && !loadError" class="card empty-state">
|
||||
<div class="e-title">暂无扫描结果</div>
|
||||
请点击右上角「重新分析」触发撞款扫描
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,545 @@
|
||||
<script setup lang="ts">
|
||||
/**
|
||||
* 关键词使用记录(独立菜单 admin_keyword_usage):只读展示 keyword_usage / keyword_add_log /
|
||||
* keyword_delete_log,按公司 tab 取数。口径对齐独立小网页后台 v1.2.6g。
|
||||
*/
|
||||
import { computed, onMounted, reactive, ref, watch } from 'vue'
|
||||
import { formatDateTime } from '@/utils/datetime'
|
||||
import { useAdminSessionStore } from '@/stores/admin-session'
|
||||
import { useCompanyTabStore } from '@/stores/company-tab'
|
||||
import { actionableErrorText } from '@/components/admin-feedback'
|
||||
import OldPagination from '@/components/OldPagination.vue'
|
||||
import LoadFailureBar from '@/components/LoadFailureBar.vue'
|
||||
import { createLatestRequestGate } from '@/utils/latest-request'
|
||||
import {
|
||||
KEYWORD_USAGE_PAGE_SIZE,
|
||||
KEYWORD_USAGE_SCENES,
|
||||
createKeywordUsageFilter,
|
||||
keywordUsageCountsText,
|
||||
keywordUsageSceneLabel,
|
||||
type KeywordUsageCounts,
|
||||
type KeywordUsageFilter,
|
||||
type KeywordUsageOverview,
|
||||
type KeywordUsageRow,
|
||||
} from './keyword-usage-model.ts'
|
||||
import {
|
||||
fetchKeywordUsage,
|
||||
fetchKeywordUsageDeletes,
|
||||
fetchKeywordUsageOverview,
|
||||
} from './keyword-usage-api.ts'
|
||||
|
||||
const companyTab = useCompanyTabStore()
|
||||
const session = useAdminSessionStore()
|
||||
const me = computed(() => session.user?.username || '')
|
||||
|
||||
const filter = reactive<KeywordUsageFilter>(createKeywordUsageFilter())
|
||||
const activeTab = ref<'usage' | 'deletes'>('usage')
|
||||
const loadError = ref('')
|
||||
|
||||
const overview = ref<KeywordUsageOverview | null>(null)
|
||||
const overviewLoading = ref(false)
|
||||
|
||||
const usageRows = ref<KeywordUsageRow[]>([])
|
||||
const usageTotal = ref(0)
|
||||
const usagePage = ref(1)
|
||||
const usagePageSize = ref(KEYWORD_USAGE_PAGE_SIZE)
|
||||
const usageCounts = ref<KeywordUsageCounts>({ copy: 0, copyExisting: 0, copyDeleted: 0, add: 0 })
|
||||
const usageLoading = ref(false)
|
||||
|
||||
const deleteRows = ref<KeywordUsageRow[]>([])
|
||||
const deleteTotal = ref(0)
|
||||
const deletePage = ref(1)
|
||||
const deletePageSize = ref(KEYWORD_USAGE_PAGE_SIZE)
|
||||
const deleteLoading = ref(false)
|
||||
|
||||
// 切公司/快速改筛选时,先发的请求可能后返回;每个加载口径各一个序号闸(CR-27)
|
||||
const overviewGate = createLatestRequestGate()
|
||||
const usageGate = createLatestRequestGate()
|
||||
const deleteGate = createLatestRequestGate()
|
||||
|
||||
const statsCards = computed(() => {
|
||||
const stats = overview.value?.stats
|
||||
return [
|
||||
{ label: '复制关键词', value: stats?.used ?? 0, tone: 'primary' },
|
||||
{ label: '现存词', value: stats?.exist ?? 0, tone: 'ok' },
|
||||
{ label: '已删除词留痕', value: stats?.deletedKw ?? 0, tone: 'danger' },
|
||||
{ label: '添加关键词', value: stats?.added ?? 0, tone: 'info' },
|
||||
{ label: '使用记录合计', value: stats?.totalUse ?? 0, tone: '' },
|
||||
{ label: '删除记录', value: stats?.deletes ?? 0, tone: 'violet' },
|
||||
]
|
||||
})
|
||||
const userOptions = computed(() => overview.value?.users ?? [])
|
||||
const countryOptions = computed(() => overview.value?.countries ?? [])
|
||||
const usageSummary = computed(() => keywordUsageCountsText(usageCounts.value, usageTotal.value))
|
||||
|
||||
function queryParams() {
|
||||
return {
|
||||
companyId: companyTab.requestCompanyId,
|
||||
user: filter.user,
|
||||
country: filter.country,
|
||||
keyword: filter.keyword.trim(),
|
||||
scene: filter.scene,
|
||||
}
|
||||
}
|
||||
|
||||
async function loadOverview(): Promise<void> {
|
||||
const token = overviewGate.begin()
|
||||
overviewLoading.value = true
|
||||
try {
|
||||
const result = await fetchKeywordUsageOverview({ companyId: companyTab.requestCompanyId })
|
||||
if (!overviewGate.isCurrent(token)) return
|
||||
overview.value = result
|
||||
} catch (error) {
|
||||
if (!overviewGate.isCurrent(token)) return
|
||||
loadError.value = actionableErrorText(error)
|
||||
} finally {
|
||||
if (overviewGate.isCurrent(token)) overviewLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function loadUsage(page = usagePage.value): Promise<void> {
|
||||
const token = usageGate.begin()
|
||||
usageLoading.value = true
|
||||
try {
|
||||
const result = await fetchKeywordUsage({ ...queryParams(), page, pageSize: usagePageSize.value })
|
||||
if (!usageGate.isCurrent(token)) return
|
||||
usageRows.value = result.rows
|
||||
usageTotal.value = result.total
|
||||
usageCounts.value = result.counts
|
||||
usagePage.value = page
|
||||
} catch (error) {
|
||||
if (!usageGate.isCurrent(token)) return
|
||||
loadError.value = actionableErrorText(error)
|
||||
} finally {
|
||||
if (usageGate.isCurrent(token)) usageLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function loadDeletes(page = deletePage.value): Promise<void> {
|
||||
const token = deleteGate.begin()
|
||||
deleteLoading.value = true
|
||||
try {
|
||||
const result = await fetchKeywordUsageDeletes({ ...queryParams(), page, pageSize: deletePageSize.value })
|
||||
if (!deleteGate.isCurrent(token)) return
|
||||
deleteRows.value = result.rows
|
||||
deleteTotal.value = result.total
|
||||
deletePage.value = page
|
||||
} catch (error) {
|
||||
if (!deleteGate.isCurrent(token)) return
|
||||
loadError.value = actionableErrorText(error)
|
||||
} finally {
|
||||
if (deleteGate.isCurrent(token)) deleteLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function reload(): Promise<void> {
|
||||
loadError.value = ''
|
||||
await Promise.all([loadOverview(), loadUsage(1), loadDeletes(1)])
|
||||
}
|
||||
|
||||
function query(): void {
|
||||
loadError.value = ''
|
||||
void Promise.all([loadUsage(1), loadDeletes(1)])
|
||||
}
|
||||
|
||||
function reset(): void {
|
||||
Object.assign(filter, createKeywordUsageFilter())
|
||||
query()
|
||||
}
|
||||
|
||||
function changeUsagePageSize(size: number): void {
|
||||
usagePageSize.value = size
|
||||
void loadUsage(1)
|
||||
}
|
||||
|
||||
function changeDeletePageSize(size: number): void {
|
||||
deletePageSize.value = size
|
||||
void loadDeletes(1)
|
||||
}
|
||||
|
||||
watch(() => companyTab.activeId, () => {
|
||||
filter.user = ''
|
||||
filter.country = ''
|
||||
usagePage.value = 1
|
||||
deletePage.value = 1
|
||||
void reload()
|
||||
})
|
||||
|
||||
onMounted(() => {
|
||||
void reload()
|
||||
})
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="kw-page">
|
||||
<LoadFailureBar v-if="loadError" :message="loadError" @retry="reload" />
|
||||
|
||||
<section class="kw-cards" v-loading="overviewLoading">
|
||||
<div v-for="card in statsCards" :key="card.label" class="kw-card" :class="card.tone">
|
||||
<div class="kw-n">{{ card.value.toLocaleString() }}</div>
|
||||
<div class="kw-t">{{ card.label }}</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section class="kw-panel">
|
||||
<div class="kw-panel-head">筛选条件</div>
|
||||
<div class="kw-panel-body">
|
||||
<div class="kw-filters">
|
||||
<label class="kw-field">
|
||||
<span>使用者</span>
|
||||
<select v-model="filter.user">
|
||||
<option value="">全部使用者</option>
|
||||
<option v-for="user in userOptions" :key="user" :value="user">{{ user }}</option>
|
||||
</select>
|
||||
</label>
|
||||
<label class="kw-field">
|
||||
<span>国家</span>
|
||||
<select v-model="filter.country">
|
||||
<option value="">全部国家</option>
|
||||
<option v-for="country in countryOptions" :key="country" :value="country">{{ country }}</option>
|
||||
</select>
|
||||
</label>
|
||||
<label class="kw-field kw-field-wide">
|
||||
<span>关键词</span>
|
||||
<input v-model="filter.keyword" placeholder="搜索关键词" @keyup.enter="query" />
|
||||
</label>
|
||||
<label class="kw-field">
|
||||
<span>场景</span>
|
||||
<select v-model="filter.scene">
|
||||
<option value="">全部场景</option>
|
||||
<option v-for="scene in KEYWORD_USAGE_SCENES" :key="scene" :value="scene">{{ scene }}</option>
|
||||
</select>
|
||||
</label>
|
||||
<div class="kw-actions">
|
||||
<button class="kw-btn kw-btn-primary" type="button" @click="query">查询</button>
|
||||
<button class="kw-btn kw-btn-ghost" type="button" @click="reset">重置</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<div class="kw-tabs">
|
||||
<button class="kw-tab" :class="{ active: activeTab === 'usage' }" type="button" @click="activeTab = 'usage'">使用记录</button>
|
||||
<button class="kw-tab" :class="{ active: activeTab === 'deletes' }" type="button" @click="activeTab = 'deletes'">删除记录</button>
|
||||
</div>
|
||||
|
||||
<section v-show="activeTab === 'usage'" class="kw-panel">
|
||||
<div class="kw-panel-head">
|
||||
使用记录
|
||||
<span class="kw-sub">复制关键词 + 添加关键词,按时间倒序;绿色为本人的记录</span>
|
||||
</div>
|
||||
<div class="kw-panel-body">
|
||||
<div class="kw-table-wrap" v-loading="usageLoading">
|
||||
<table v-if="usageRows.length" class="kw-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="width: 130px">使用者</th>
|
||||
<th style="width: 90px">国家</th>
|
||||
<th>关键词</th>
|
||||
<th style="width: 140px">使用场景</th>
|
||||
<th style="width: 170px">使用时间</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="(row, index) in usageRows" :key="`usage-${row.time}-${index}`" :class="{ mine: row.userName === me }">
|
||||
<td class="kw-user">{{ row.userName || '—' }}</td>
|
||||
<td>{{ row.country || '—' }}</td>
|
||||
<td class="kw-keyword">{{ row.keyword || '—' }}</td>
|
||||
<td>
|
||||
<span class="kw-tag" :class="row.scene === '添加关键词' ? 'add' : 'copy'">
|
||||
{{ keywordUsageSceneLabel(row.scene) }}
|
||||
</span>
|
||||
</td>
|
||||
<td class="kw-time">{{ formatDateTime(row.time) }}</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<div v-else-if="!usageLoading" class="kw-empty">暂无使用记录</div>
|
||||
</div>
|
||||
<div class="kw-footer">
|
||||
<span class="kw-summary">{{ usageSummary }}</span>
|
||||
<OldPagination
|
||||
v-if="usageTotal"
|
||||
:total="usageTotal"
|
||||
:page="usagePage"
|
||||
:page-size="usagePageSize"
|
||||
@change="loadUsage"
|
||||
@size-change="changeUsagePageSize"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section v-show="activeTab === 'deletes'" class="kw-panel">
|
||||
<div class="kw-panel-head">
|
||||
删除记录
|
||||
<span class="kw-sub">只读、不可删除、永久留痕可追溯;红色为本人的记录</span>
|
||||
</div>
|
||||
<div class="kw-panel-body">
|
||||
<div class="kw-table-wrap" v-loading="deleteLoading">
|
||||
<table v-if="deleteRows.length" class="kw-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="width: 130px">删除者</th>
|
||||
<th style="width: 90px">国家</th>
|
||||
<th>关键词</th>
|
||||
<th style="width: 170px">删除时间</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="(row, index) in deleteRows" :key="`delete-${row.time}-${index}`" :class="{ mine: row.userName === me }">
|
||||
<td class="kw-user">{{ row.userName || '—' }}</td>
|
||||
<td>{{ row.country || '—' }}</td>
|
||||
<td class="kw-keyword">{{ row.keyword || '—' }}</td>
|
||||
<td class="kw-time">{{ formatDateTime(row.time) }}</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<div v-else-if="!deleteLoading" class="kw-empty">暂无删除记录</div>
|
||||
</div>
|
||||
<div class="kw-footer">
|
||||
<span class="kw-summary">共 {{ deleteTotal }} 条删除记录 · 只读留痕</span>
|
||||
<OldPagination
|
||||
v-if="deleteTotal"
|
||||
:total="deleteTotal"
|
||||
:page="deletePage"
|
||||
:page-size="deletePageSize"
|
||||
@change="loadDeletes"
|
||||
@size-change="changeDeletePageSize"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.kw-page {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 14px;
|
||||
}
|
||||
|
||||
.kw-cards {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
|
||||
gap: 10px;
|
||||
}
|
||||
.kw-card {
|
||||
background: #fff;
|
||||
border: 1px solid #e5e9f0;
|
||||
border-top: 3px solid #9ca3af;
|
||||
border-radius: 10px;
|
||||
box-shadow: 0 1px 2px rgba(20, 30, 60, 0.05), 0 4px 18px rgba(20, 30, 60, 0.06);
|
||||
padding: 12px 14px;
|
||||
}
|
||||
.kw-card.primary { border-top-color: #2e5be6; }
|
||||
.kw-card.ok { border-top-color: #1f9d62; }
|
||||
.kw-card.danger { border-top-color: #d63a4a; }
|
||||
.kw-card.info { border-top-color: #0d9488; }
|
||||
.kw-card.violet { border-top-color: #7c5cd6; }
|
||||
.kw-card .kw-n {
|
||||
font-size: 24px;
|
||||
font-weight: 700;
|
||||
color: #1c2230;
|
||||
font-variant-numeric: tabular-nums;
|
||||
line-height: 1.2;
|
||||
}
|
||||
.kw-card .kw-t {
|
||||
margin-top: 3px;
|
||||
font-size: 11.5px;
|
||||
color: #98a1b1;
|
||||
}
|
||||
|
||||
.kw-panel {
|
||||
background: #fff;
|
||||
border: 1px solid #e5e9f0;
|
||||
border-radius: 10px;
|
||||
box-shadow: 0 1px 2px rgba(20, 30, 60, 0.05), 0 4px 18px rgba(20, 30, 60, 0.06);
|
||||
overflow: hidden;
|
||||
}
|
||||
.kw-panel-head {
|
||||
padding: 11px 15px;
|
||||
background: #fafbfd;
|
||||
border-bottom: 1px solid #e9edf3;
|
||||
font-size: 13px;
|
||||
font-weight: 700;
|
||||
color: #1c2230;
|
||||
}
|
||||
.kw-panel-head::before {
|
||||
content: "";
|
||||
display: inline-block;
|
||||
width: 7px;
|
||||
height: 7px;
|
||||
border-radius: 2px;
|
||||
background: #2dd4bf;
|
||||
margin-right: 8px;
|
||||
vertical-align: 1px;
|
||||
}
|
||||
.kw-sub {
|
||||
margin-left: 8px;
|
||||
font-size: 11.5px;
|
||||
font-weight: 400;
|
||||
color: #98a1b1;
|
||||
}
|
||||
.kw-panel-body {
|
||||
padding: 14px 15px;
|
||||
}
|
||||
|
||||
.kw-filters {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: flex-end;
|
||||
gap: 10px 12px;
|
||||
}
|
||||
.kw-field {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 5px;
|
||||
min-width: 150px;
|
||||
}
|
||||
.kw-field-wide {
|
||||
min-width: 220px;
|
||||
}
|
||||
.kw-field > span {
|
||||
font-size: 12px;
|
||||
color: #5a6474;
|
||||
}
|
||||
.kw-field input,
|
||||
.kw-field select {
|
||||
height: 34px;
|
||||
padding: 0 10px;
|
||||
border: 1px solid #d7dce5;
|
||||
border-radius: 7px;
|
||||
background: #fbfcfe;
|
||||
color: #1c2230;
|
||||
font-family: inherit;
|
||||
font-size: 13px;
|
||||
outline: none;
|
||||
}
|
||||
.kw-field input:focus,
|
||||
.kw-field select:focus {
|
||||
border-color: #2e5be6;
|
||||
box-shadow: 0 0 0 3px rgba(46, 91, 230, 0.12);
|
||||
background: #fff;
|
||||
}
|
||||
.kw-actions {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.kw-btn {
|
||||
height: 34px;
|
||||
padding: 0 15px;
|
||||
border: 1px solid transparent;
|
||||
border-radius: 7px;
|
||||
font-family: inherit;
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
}
|
||||
.kw-btn-primary {
|
||||
background: #2e5be6;
|
||||
border-color: #2e5be6;
|
||||
color: #fff;
|
||||
}
|
||||
.kw-btn-primary:hover { background: #1e4bd8; border-color: #1e4bd8; }
|
||||
.kw-btn-ghost {
|
||||
background: #fff;
|
||||
border-color: #d7dce5;
|
||||
color: #5a6474;
|
||||
}
|
||||
.kw-btn-ghost:hover { border-color: #2e5be6; color: #2e5be6; }
|
||||
|
||||
.kw-tabs {
|
||||
display: flex;
|
||||
gap: 4px;
|
||||
background: #fff;
|
||||
border: 1px solid #e5e9f0;
|
||||
border-radius: 9px;
|
||||
padding: 4px;
|
||||
align-self: flex-start;
|
||||
}
|
||||
.kw-tab {
|
||||
padding: 7px 18px;
|
||||
border: none;
|
||||
border-radius: 7px;
|
||||
background: none;
|
||||
color: #5a6474;
|
||||
font-family: inherit;
|
||||
font-size: 13px;
|
||||
font-weight: 600;
|
||||
cursor: pointer;
|
||||
}
|
||||
.kw-tab:hover { color: #2e5be6; }
|
||||
.kw-tab.active { background: #2e5be6; color: #fff; }
|
||||
|
||||
.kw-table-wrap {
|
||||
overflow-x: auto;
|
||||
border: 1px solid #e5e9f0;
|
||||
border-radius: 9px;
|
||||
min-height: 90px;
|
||||
}
|
||||
.kw-table {
|
||||
width: 100%;
|
||||
border-collapse: separate;
|
||||
border-spacing: 0;
|
||||
font-size: 12.5px;
|
||||
}
|
||||
.kw-table th {
|
||||
position: sticky;
|
||||
top: 0;
|
||||
z-index: 1;
|
||||
padding: 10px;
|
||||
background: #f7f9fc;
|
||||
border-bottom: 1px solid #e5e9f0;
|
||||
color: #5a6474;
|
||||
font-size: 12px;
|
||||
font-weight: 600;
|
||||
text-align: left;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.kw-table td {
|
||||
padding: 9px 10px;
|
||||
border-bottom: 1px solid #eff2f7;
|
||||
color: #1c2230;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.kw-table tbody tr:hover td { background: #f7faff; }
|
||||
.kw-table tbody tr:last-child td { border-bottom: none; }
|
||||
.kw-table tr.mine td { color: #0f9d58; font-weight: 600; }
|
||||
.kw-user { font-weight: 500; }
|
||||
.kw-keyword { max-width: 320px; overflow: hidden; text-overflow: ellipsis; }
|
||||
.kw-time { color: #5a6474; font-size: 11.5px; }
|
||||
|
||||
.kw-tag {
|
||||
display: inline-block;
|
||||
padding: 2px 8px;
|
||||
border-radius: 6px;
|
||||
font-size: 11px;
|
||||
font-weight: 600;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.kw-tag.copy { background: #e8f7f4; color: #0d9488; }
|
||||
.kw-tag.add { background: #eff6ff; color: #2563eb; }
|
||||
|
||||
.kw-empty {
|
||||
padding: 42px 16px;
|
||||
text-align: center;
|
||||
color: #98a1b1;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.kw-footer {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 10px;
|
||||
margin-top: 12px;
|
||||
}
|
||||
.kw-summary {
|
||||
font-size: 11.5px;
|
||||
color: #98a1b1;
|
||||
}
|
||||
</style>
|
||||
@@ -1,4 +1,4 @@
|
||||
/** 品牌Gating库模型(任务:并入「店铺数据重复检查」页签):与 /api/admin/brand-gating 的 camel 契约对齐,纯逻辑。 */
|
||||
/** 品牌Gating库模型(独立菜单页):与 /api/admin/brand-gating 的 camel 契约对齐,纯逻辑。 */
|
||||
|
||||
export const GATING_COUNTRIES = ['全部', '英国', '德国', '法国', '西班牙', '意大利'] as const
|
||||
export const GATING_TYPES = ['品牌gating', '类目gating', '账户不符合'] as const
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
/** 关键词使用记录接口适配:/api/admin/keyword-usage 的使用记录、删除记录与概览。 */
|
||||
import { http } from '@/api/http'
|
||||
import { unwrap } from '@/api/envelope'
|
||||
import {
|
||||
parseKeywordUsageOverview,
|
||||
parseKeywordUsagePage,
|
||||
type KeywordUsageOverview,
|
||||
type KeywordUsagePage,
|
||||
type KeywordUsageQuery,
|
||||
} from './keyword-usage-model.ts'
|
||||
|
||||
export const KEYWORD_USAGE_ENDPOINT = '/api/admin/keyword-usage'
|
||||
|
||||
function toQuery(params: KeywordUsageQuery): Record<string, unknown> {
|
||||
const query: Record<string, unknown> = {}
|
||||
if (params.companyId != null) query.company_id = params.companyId
|
||||
if (params.user) query.user = params.user
|
||||
if (params.country) query.country = params.country
|
||||
if (params.keyword) query.keyword = params.keyword
|
||||
if (params.scene) query.scene = params.scene
|
||||
if (params.page != null) query.page = params.page
|
||||
if (params.pageSize != null) query.page_size = params.pageSize
|
||||
return query
|
||||
}
|
||||
|
||||
export async function fetchKeywordUsage(params: KeywordUsageQuery): Promise<KeywordUsagePage> {
|
||||
const { data } = await http.get<unknown>(KEYWORD_USAGE_ENDPOINT, { params: toQuery(params) })
|
||||
return parseKeywordUsagePage(unwrap<unknown>(data))
|
||||
}
|
||||
|
||||
export async function fetchKeywordUsageDeletes(params: KeywordUsageQuery): Promise<KeywordUsagePage> {
|
||||
const { data } = await http.get<unknown>(`${KEYWORD_USAGE_ENDPOINT}/deletes`, { params: toQuery(params) })
|
||||
return parseKeywordUsagePage(unwrap<unknown>(data))
|
||||
}
|
||||
|
||||
export async function fetchKeywordUsageOverview(params: KeywordUsageQuery): Promise<KeywordUsageOverview> {
|
||||
const { data } = await http.get<unknown>(`${KEYWORD_USAGE_ENDPOINT}/overview`, { params: toQuery(params) })
|
||||
return parseKeywordUsageOverview(unwrap<unknown>(data))
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
/** 关键词使用记录(独立菜单 admin_keyword_usage):与 /api/admin/keyword-usage 的 camel 契约对齐,纯逻辑。 */
|
||||
|
||||
export interface KeywordUsageQuery {
|
||||
companyId?: number | null
|
||||
user?: string
|
||||
country?: string
|
||||
keyword?: string
|
||||
scene?: string
|
||||
page?: number
|
||||
pageSize?: number
|
||||
}
|
||||
|
||||
export interface KeywordUsageFilter {
|
||||
user: string
|
||||
country: string
|
||||
keyword: string
|
||||
scene: string
|
||||
}
|
||||
|
||||
export interface KeywordUsageRow {
|
||||
userName: string
|
||||
country: string
|
||||
keyword: string
|
||||
scene: string
|
||||
time: string
|
||||
}
|
||||
|
||||
export interface KeywordUsageCounts {
|
||||
copy: number
|
||||
copyExisting: number
|
||||
copyDeleted: number
|
||||
add: number
|
||||
}
|
||||
|
||||
export interface KeywordUsagePage {
|
||||
total: number
|
||||
rows: KeywordUsageRow[]
|
||||
counts: KeywordUsageCounts
|
||||
}
|
||||
|
||||
export interface KeywordUsageStats {
|
||||
used: number
|
||||
exist: number
|
||||
deletedKw: number
|
||||
added: number
|
||||
totalUse: number
|
||||
deletes: number
|
||||
}
|
||||
|
||||
export interface KeywordUsageOverview {
|
||||
stats: KeywordUsageStats
|
||||
users: string[]
|
||||
countries: string[]
|
||||
}
|
||||
|
||||
export const KEYWORD_USAGE_SCENES = ['已使用', '添加关键词'] as const
|
||||
|
||||
/** 初始每页条数:必须落在 OldPagination 的档位(10/20/50/100)里(CR-26)。 */
|
||||
export const KEYWORD_USAGE_PAGE_SIZE = 10
|
||||
|
||||
export function createKeywordUsageFilter(): KeywordUsageFilter {
|
||||
return { user: '', country: '', keyword: '', scene: '' }
|
||||
}
|
||||
|
||||
function asRecord(value: unknown): Record<string, unknown> | null {
|
||||
return value && typeof value === 'object' && !Array.isArray(value) ? value as Record<string, unknown> : null
|
||||
}
|
||||
|
||||
function text(value: unknown): string {
|
||||
return typeof value === 'string' ? value : value == null ? '' : String(value)
|
||||
}
|
||||
|
||||
function count(value: unknown): number {
|
||||
const n = Number(value)
|
||||
return Number.isFinite(n) && n >= 0 ? n : 0
|
||||
}
|
||||
|
||||
function textList(value: unknown): string[] {
|
||||
return Array.isArray(value) ? value.map(text).filter((item) => item !== '') : []
|
||||
}
|
||||
|
||||
export function parseKeywordUsageRow(value: unknown): KeywordUsageRow | null {
|
||||
const row = asRecord(value)
|
||||
if (!row) return null
|
||||
return {
|
||||
userName: text(row.userName),
|
||||
country: text(row.country),
|
||||
keyword: text(row.keyword),
|
||||
scene: text(row.scene),
|
||||
time: text(row.time),
|
||||
}
|
||||
}
|
||||
|
||||
export function parseKeywordUsagePage(value: unknown): KeywordUsagePage {
|
||||
const page = asRecord(value)
|
||||
const counts = asRecord(page?.counts)
|
||||
const rows = Array.isArray(page?.rows)
|
||||
? page.rows.map(parseKeywordUsageRow).filter((row): row is KeywordUsageRow => row !== null)
|
||||
: []
|
||||
return {
|
||||
total: count(page?.total),
|
||||
rows,
|
||||
counts: {
|
||||
copy: count(counts?.copy),
|
||||
copyExisting: count(counts?.copyExisting),
|
||||
copyDeleted: count(counts?.copyDeleted),
|
||||
add: count(counts?.add),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export function parseKeywordUsageOverview(value: unknown): KeywordUsageOverview {
|
||||
const overview = asRecord(value)
|
||||
const stats = asRecord(overview?.stats)
|
||||
return {
|
||||
stats: {
|
||||
used: count(stats?.used),
|
||||
exist: count(stats?.exist),
|
||||
deletedKw: count(stats?.deletedKw),
|
||||
added: count(stats?.added),
|
||||
totalUse: count(stats?.totalUse),
|
||||
deletes: count(stats?.deletes),
|
||||
},
|
||||
users: textList(overview?.users),
|
||||
countries: textList(overview?.countries),
|
||||
}
|
||||
}
|
||||
|
||||
export function keywordUsageSceneLabel(scene: string): string {
|
||||
if (scene === '复制关键词') return '已使用(复制)'
|
||||
if (scene === '添加关键词') return '添加关键词'
|
||||
if (scene === '删除关键词') return '删除关键词'
|
||||
return scene || '—'
|
||||
}
|
||||
|
||||
/** 使用记录口径:复制 = 现存词 + 已删除词留痕;添加单列。 */
|
||||
export function keywordUsageCountsText(counts: KeywordUsageCounts, total: number): string {
|
||||
return `复制关键词 ${counts.copy} = 现存词 ${counts.copyExisting} + 已删除词 ${counts.copyDeleted}`
|
||||
+ ` · 添加关键词 ${counts.add} · 合计 ${total} 条`
|
||||
}
|
||||
@@ -25,6 +25,8 @@ export const adminPages: AdminPageDef[] = [
|
||||
{ path: 'account/user-secret-usage', menuKey: 'admin_user_secret_usage', title: '密钥用量统计', companyScoped: true, load: () => import('@/pages/account/UserSecretUsagePage.vue') },
|
||||
{ path: 'account/companies', menuKey: 'admin_companies', title: '公司管理', superAdminOnly: true, load: () => import('@/pages/account/CompaniesPage.vue') },
|
||||
{ path: 'shop-center/duplicate-check', menuKey: 'admin_shop_data_duplicate_check', title: '店铺撞款监控', companyScoped: true, load: () => import('@/pages/tasks/DuplicateCheckPage.vue') },
|
||||
{ path: 'shop-center/brand-gating', menuKey: 'admin_brand_gating', title: '品牌Gating库', companyScoped: false, load: () => import('@/pages/tasks/BrandGatingPanel.vue') },
|
||||
{ path: 'shop-center/keyword-usage', menuKey: 'admin_keyword_usage', title: '关键词使用记录', companyScoped: true, load: () => import('@/pages/tasks/KeywordUsagePage.vue') },
|
||||
{ path: 'shop-center/keys', menuKey: 'admin_shop_keys', title: '店铺密钥管理', companyScoped: true, load: () => import('@/pages/shop/ShopKeysPage.vue') },
|
||||
{ path: 'shop-center/shops', menuKey: 'admin_shop_manage', title: '店铺管理', companyScoped: true, load: () => import('@/pages/shop/ShopManagePage.vue') },
|
||||
{ path: 'records/history', menuKey: 'admin_history', title: '生成记录', companyScoped: true, load: () => import('@/pages/records/RecordsHistoryPage.vue') },
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* 只让最后一次请求落地(CR-27)。
|
||||
*
|
||||
* 快速切公司/筛选时,先发的请求可能后返回;如果直接写 ref,旧响应会覆盖新结果。
|
||||
* 每个加载函数在发请求前 `begin()` 拿一个 token,回写前 `isCurrent(token)` 验一次;
|
||||
* 新请求一发出,旧 token 立即作废(不需要 AbortController 也能丢掉晚到的响应)。
|
||||
*/
|
||||
export interface LatestRequestGate {
|
||||
/** 开一次新请求并作废之前所有 token(含未返回的那些)。 */
|
||||
begin(): number
|
||||
/** 这个 token 是否仍是最后一次 `begin()` 发出的。 */
|
||||
isCurrent(token: number): boolean
|
||||
}
|
||||
|
||||
export function createLatestRequestGate(): LatestRequestGate {
|
||||
let latest = 0
|
||||
return {
|
||||
begin(): number {
|
||||
latest += 1
|
||||
return latest
|
||||
},
|
||||
isCurrent(token: number): boolean {
|
||||
// token 只由 begin() 发出(从 1 起);0 不是合法 token,避免未发请求时误判为当前
|
||||
return token > 0 && token === latest
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,10 @@ import { adminPages } from '../src/router/routes.ts'
|
||||
|
||||
const MIGRATION = join(process.cwd(), '..', 'backend-java', 'src', 'main', 'resources', 'db',
|
||||
'V143__company_and_users_company_id.sql')
|
||||
const BRAND_GATING_MIGRATION = join(process.cwd(), '..', 'backend-java', 'src', 'main', 'resources', 'db',
|
||||
'V174__restore_admin_brand_gating_menu.sql')
|
||||
const KEYWORD_USAGE_MIGRATION = join(process.cwd(), '..', 'backend-java', 'src', 'main', 'resources', 'db',
|
||||
'V175__move_gating_and_add_keyword_usage_menu.sql')
|
||||
|
||||
interface MigrationMenu {
|
||||
name: string
|
||||
@@ -57,3 +61,37 @@ test('V143 的「公司管理」菜单落到已注册的公司管理页', () =>
|
||||
assert.equal(page.path, companies.routePath, '库里的 route_path 应与注册表路径一致')
|
||||
assert.equal(page.title, companies.name, '页面标题应与菜单名一致')
|
||||
})
|
||||
|
||||
test('V174 恢复的「品牌Gating库」菜单 key 必须在路由注册表登记', () => {
|
||||
const sql = readFileSync(BRAND_GATING_MIGRATION, 'utf8')
|
||||
const menus = menusInsertedBy(sql)
|
||||
const insertCount = (sql.match(/INSERT INTO `columns`/g) || []).length
|
||||
assert.ok(menus.length > 0, 'V174 应当插入后台菜单,一条都没解析出来说明正则失效')
|
||||
assert.equal(menus.length, insertCount, `解析到 ${menus.length} 条,但迁移有 ${insertCount} 条 INSERT INTO columns`)
|
||||
|
||||
const gating = menus.find((menu) => menu.columnKey === 'admin_brand_gating')
|
||||
assert.ok(gating, 'V174 必须插入 admin_brand_gating 菜单')
|
||||
const page = adminPages.find((item) => item.menuKey === 'admin_brand_gating')
|
||||
assert.ok(page, 'routes.ts 必须有 admin_brand_gating 页面,否则菜单点进去 404')
|
||||
assert.equal(page.title, gating.name, '页面标题应与菜单名一致')
|
||||
assert.equal(page.companyScoped, false, 'Gating 库全平台共享,不参与公司 tab')
|
||||
})
|
||||
|
||||
test('V175 新增的关键词使用记录菜单 key 必须在路由注册表登记', () => {
|
||||
const sql = readFileSync(KEYWORD_USAGE_MIGRATION, 'utf8')
|
||||
const menus = menusInsertedBy(sql)
|
||||
const insertCount = (sql.match(/INSERT INTO `columns`/g) || []).length
|
||||
assert.ok(menus.length > 0, 'V175 应当插入后台菜单,一条都没解析出来说明正则失效')
|
||||
assert.equal(menus.length, insertCount, `解析到 ${menus.length} 条,但迁移有 ${insertCount} 条 INSERT INTO columns`)
|
||||
|
||||
const menu = menus.find((item) => item.columnKey === 'admin_keyword_usage')
|
||||
assert.ok(menu, 'V175 必须插入 admin_keyword_usage 菜单')
|
||||
const page = adminPages.find((item) => item.menuKey === 'admin_keyword_usage')
|
||||
assert.ok(page, 'routes.ts 必须有 admin_keyword_usage 页面,否则菜单点进去 404')
|
||||
assert.equal(page.title, menu.name, '页面标题应与菜单名一致')
|
||||
assert.equal(page.companyScoped, true, '关键词记录按公司 tab 取数')
|
||||
|
||||
// 品牌Gating库由数据管理组迁到店铺管理组,排序 85;关键词记录 86,紧随其后。
|
||||
assert.ok(sql.includes('SET col.`parent_id` = grp.`id`'), 'V175 必须把品牌Gating库迁到店铺管理组')
|
||||
assert.ok(sql.includes('col.`sort_order` = 85'), '品牌Gating库排序应为 85')
|
||||
})
|
||||
|
||||
@@ -227,6 +227,14 @@ const COMPANY_ID_SOURCE_PINS: Readonly<Record<string, readonly CompanyIdPin[]>>
|
||||
// 批次 3 的两页同样由 SEND_POINTS 现算(撞款控制台三处发送口 = 三条钉)。
|
||||
admin_shop_data_duplicate_check: pinsFor('admin_shop_data_duplicate_check'),
|
||||
admin_shop_data_crawl_tasks: pinsFor('admin_shop_data_crawl_tasks'),
|
||||
// 2026-10-25 关键词使用记录:三个 GET 共用 toQuery,company_id 只许取函数自己的 params.companyId。
|
||||
admin_keyword_usage: [
|
||||
{
|
||||
file: 'src/pages/tasks/keyword-usage-api.ts',
|
||||
shape: /company_id\s*=\s*params\.companyId\b/,
|
||||
note: 'toQuery 是白名单构造,公司值只能来自调用方传入的 params.companyId(页面从 companyTab.requestCompanyId 取)',
|
||||
},
|
||||
],
|
||||
admin_users: pinsFor('admin_users'),
|
||||
// 批次 5(店铺密钥)同样由 SEND_POINTS 现算:这一页只有一个发送口(列表),
|
||||
// 新增/编辑/删除/白名单检测四支都不带公司参数——归属由后端从操作者会话推导(writeCompanyId 闸门)。
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import {
|
||||
KEYWORD_USAGE_PAGE_SIZE,
|
||||
createKeywordUsageFilter,
|
||||
keywordUsageCountsText,
|
||||
keywordUsageSceneLabel,
|
||||
parseKeywordUsageOverview,
|
||||
parseKeywordUsagePage,
|
||||
} from '../src/pages/tasks/keyword-usage-model.ts'
|
||||
|
||||
test('keyword_usage_default_page_size_matches_pagination_options', () => {
|
||||
// OldPagination 的档位是 10/20/50/100;初始值不在档位里时下拉显示与实际请求不一致(CR-26)
|
||||
assert.equal(KEYWORD_USAGE_PAGE_SIZE, 10)
|
||||
assert.ok([10, 20, 50, 100].includes(KEYWORD_USAGE_PAGE_SIZE))
|
||||
})
|
||||
|
||||
test('keyword_usage_filter_defaults_and_scene_labels', () => {
|
||||
assert.deepEqual(createKeywordUsageFilter(), { user: '', country: '', keyword: '', scene: '' })
|
||||
assert.equal(keywordUsageSceneLabel('复制关键词'), '已使用(复制)')
|
||||
assert.equal(keywordUsageSceneLabel('添加关键词'), '添加关键词')
|
||||
assert.equal(keywordUsageSceneLabel('删除关键词'), '删除关键词')
|
||||
assert.equal(keywordUsageSceneLabel(''), '—')
|
||||
})
|
||||
|
||||
test('keyword_usage_page_parses_counts_and_skips_invalid_rows', () => {
|
||||
const page = parseKeywordUsagePage({
|
||||
total: 7,
|
||||
rows: [
|
||||
{ userName: 'alice', country: '英国', keyword: 'dress', scene: '复制关键词', time: '2026-10-25T10:00:00' },
|
||||
null,
|
||||
{ userName: 'bob', keyword: 'shoes', scene: '添加关键词', time: '2026-10-25T11:00:00' },
|
||||
],
|
||||
counts: { copy: 5, copyExisting: 4, copyDeleted: 1, add: 2 },
|
||||
})
|
||||
assert.equal(page.total, 7)
|
||||
assert.equal(page.rows.length, 2)
|
||||
assert.equal(page.rows[1].country, '')
|
||||
assert.deepEqual(page.counts, { copy: 5, copyExisting: 4, copyDeleted: 1, add: 2 })
|
||||
})
|
||||
|
||||
test('keyword_usage_page_tolerates_missing_fields', () => {
|
||||
const page = parseKeywordUsagePage({})
|
||||
assert.equal(page.total, 0)
|
||||
assert.deepEqual(page.rows, [])
|
||||
assert.deepEqual(page.counts, { copy: 0, copyExisting: 0, copyDeleted: 0, add: 0 })
|
||||
})
|
||||
|
||||
test('keyword_usage_overview_parses_stats_and_options', () => {
|
||||
const overview = parseKeywordUsageOverview({
|
||||
stats: { used: 10, exist: 7, deletedKw: 3, added: 2, totalUse: 12, deletes: 1 },
|
||||
users: ['alice', '', 3],
|
||||
countries: ['英国', '德国'],
|
||||
})
|
||||
assert.equal(overview.stats.used, 10)
|
||||
assert.equal(overview.stats.totalUse, 12)
|
||||
assert.deepEqual(overview.users, ['alice', '3'])
|
||||
assert.deepEqual(overview.countries, ['英国', '德国'])
|
||||
})
|
||||
|
||||
test('keyword_usage_counts_text_matches_desktop_formula', () => {
|
||||
assert.equal(
|
||||
keywordUsageCountsText({ copy: 5, copyExisting: 4, copyDeleted: 1, add: 2 }, 7),
|
||||
'复制关键词 5 = 现存词 4 + 已删除词 1 · 添加关键词 2 · 合计 7 条',
|
||||
)
|
||||
})
|
||||
@@ -0,0 +1,21 @@
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { createLatestRequestGate } from '../src/utils/latest-request.ts'
|
||||
|
||||
test('latest_request_gate_only_accepts_the_newest_token', () => {
|
||||
const gate = createLatestRequestGate()
|
||||
const first = gate.begin()
|
||||
assert.equal(gate.isCurrent(first), true)
|
||||
|
||||
const second = gate.begin()
|
||||
assert.equal(gate.isCurrent(second), true)
|
||||
assert.equal(gate.isCurrent(first), false, '新请求一发出,旧 token 必须立即失效')
|
||||
})
|
||||
|
||||
test('latest_request_gate_starts_with_no_current_token', () => {
|
||||
const gate = createLatestRequestGate()
|
||||
assert.equal(gate.isCurrent(0), false, '未发请求前不能用 0 号 token 回写')
|
||||
const token = gate.begin()
|
||||
assert.ok(token > 0)
|
||||
assert.equal(gate.isCurrent(token), true)
|
||||
})
|
||||
@@ -0,0 +1,201 @@
|
||||
/**
|
||||
* 切公司/快速改筛选时,旧响应不得覆盖新结果(CR-27)。
|
||||
*
|
||||
* 跑真组件 + 真 pinia store,API 用可控 deferred 顶掉:
|
||||
* 先让新请求落地,再让旧请求后返回,断言页面仍显示新结果。
|
||||
*
|
||||
* 跑法:`npx vitest run tests/mount/stale-response-drop.mount.test.ts`
|
||||
*/
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { flushPromises, mount, type VueWrapper } from '@vue/test-utils'
|
||||
import { createPinia, setActivePinia, type Pinia } from 'pinia'
|
||||
|
||||
import KeywordUsagePage from '@/pages/tasks/KeywordUsagePage.vue'
|
||||
import BrandGatingPanel from '@/pages/tasks/BrandGatingPanel.vue'
|
||||
import { useAdminSessionStore } from '@/stores/admin-session'
|
||||
import { useCompanyTabStore } from '@/stores/company-tab'
|
||||
|
||||
const keywordApi = vi.hoisted(() => ({
|
||||
fetchKeywordUsage: vi.fn(),
|
||||
fetchKeywordUsageDeletes: vi.fn(),
|
||||
fetchKeywordUsageOverview: vi.fn(),
|
||||
}))
|
||||
const gatingApi = vi.hoisted(() => ({
|
||||
fetchBrandGatingPage: vi.fn(),
|
||||
fetchBrandGatingStats: vi.fn(),
|
||||
deleteBrandGating: vi.fn(),
|
||||
importBrandGatingText: vi.fn(),
|
||||
submitBrandGating: vi.fn(),
|
||||
updateBrandGatingStatus: vi.fn(),
|
||||
}))
|
||||
|
||||
vi.mock('@/pages/tasks/keyword-usage-api.ts', () => keywordApi)
|
||||
vi.mock('@/pages/tasks/brand-gating-api.ts', () => gatingApi)
|
||||
|
||||
interface Deferred<T> {
|
||||
promise: Promise<T>
|
||||
resolve: (value: T) => void
|
||||
}
|
||||
|
||||
function deferred<T>(): Deferred<T> {
|
||||
let resolve!: (value: T) => void
|
||||
const promise = new Promise<T>((done) => {
|
||||
resolve = done
|
||||
})
|
||||
return { promise, resolve }
|
||||
}
|
||||
|
||||
const loadingDirective = { mounted() {}, updated() {} }
|
||||
|
||||
function mountPage(component: Parameters<typeof mount>[0], pinia: Pinia): VueWrapper {
|
||||
return mount(component, {
|
||||
global: {
|
||||
plugins: [pinia],
|
||||
directives: { loading: loadingDirective },
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
function usagePage(keyword: string) {
|
||||
return {
|
||||
total: 1,
|
||||
rows: [{
|
||||
userName: 'alice',
|
||||
country: '英国',
|
||||
keyword,
|
||||
scene: '复制关键词',
|
||||
time: '2026-10-25 10:00:00',
|
||||
}],
|
||||
counts: { copy: 1, copyExisting: 1, copyDeleted: 0, add: 0 },
|
||||
}
|
||||
}
|
||||
|
||||
function gatingPage(brand: string) {
|
||||
return {
|
||||
total: 1,
|
||||
rows: [{
|
||||
id: 1,
|
||||
brand,
|
||||
asin: '',
|
||||
country: '英国',
|
||||
gatingType: '品牌gating',
|
||||
status: 1,
|
||||
source: 'MANUAL',
|
||||
shopName: '',
|
||||
remark: '',
|
||||
createdAt: '2026-10-25 10:00:00',
|
||||
updatedAt: '',
|
||||
disabledAt: '',
|
||||
}],
|
||||
}
|
||||
}
|
||||
|
||||
describe('CR-27:切换期间晚到的响应必须丢弃', () => {
|
||||
let pinia: Pinia
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
pinia = createPinia()
|
||||
setActivePinia(pinia)
|
||||
useAdminSessionStore().user = { id: 1, username: 'root', role: 'super_admin' }
|
||||
// 只有超管才带 company_id;company tab 的 activeId 直接改,不依赖公司目录接口
|
||||
useCompanyTabStore().setActiveId(1)
|
||||
keywordApi.fetchKeywordUsageOverview.mockResolvedValue({
|
||||
stats: { used: 0, exist: 0, deletedKw: 0, added: 0, totalUse: 0, deletes: 0 },
|
||||
users: [],
|
||||
countries: [],
|
||||
})
|
||||
keywordApi.fetchKeywordUsageDeletes.mockResolvedValue({
|
||||
total: 0,
|
||||
rows: [],
|
||||
counts: { copy: 0, copyExisting: 0, copyDeleted: 0, add: 0 },
|
||||
})
|
||||
gatingApi.fetchBrandGatingStats.mockResolvedValue({
|
||||
total: 0,
|
||||
active: 0,
|
||||
disabled: 0,
|
||||
brands: 0,
|
||||
byType: [],
|
||||
byCountry: [],
|
||||
bySource: [],
|
||||
})
|
||||
})
|
||||
|
||||
it('关键词使用记录:切公司后旧公司响应后到,页面仍显示新公司数据', async () => {
|
||||
const calls: Array<{
|
||||
companyId?: number | null
|
||||
pageSize?: number
|
||||
deferred: Deferred<ReturnType<typeof usagePage>>
|
||||
}> = []
|
||||
keywordApi.fetchKeywordUsage.mockImplementation((params: { companyId?: number | null; pageSize?: number }) => {
|
||||
const pending = deferred<ReturnType<typeof usagePage>>()
|
||||
calls.push({ companyId: params.companyId, pageSize: params.pageSize, deferred: pending })
|
||||
return pending.promise
|
||||
})
|
||||
|
||||
const wrapper = mountPage(KeywordUsagePage, pinia)
|
||||
await flushPromises()
|
||||
expect(calls).toHaveLength(1)
|
||||
expect(calls[0].companyId).toBe(1)
|
||||
expect(calls[0].pageSize).toBe(10)
|
||||
|
||||
useCompanyTabStore().setActiveId(2)
|
||||
await flushPromises()
|
||||
expect(calls).toHaveLength(2)
|
||||
expect(calls[1].companyId).toBe(2)
|
||||
|
||||
calls[1].deferred.resolve(usagePage('beta-keyword'))
|
||||
await flushPromises()
|
||||
expect(wrapper.text()).toContain('beta-keyword')
|
||||
|
||||
// 旧公司的响应晚到:序号闸必须把它丢掉
|
||||
calls[0].deferred.resolve(usagePage('alpha-keyword'))
|
||||
await flushPromises()
|
||||
expect(wrapper.text()).toContain('beta-keyword')
|
||||
expect(wrapper.text()).not.toContain('alpha-keyword')
|
||||
})
|
||||
|
||||
it('品牌 Gating:快速改筛选时旧筛选响应后到,列表仍显示新筛选结果', async () => {
|
||||
const calls: Array<{
|
||||
keyword: string
|
||||
deferred: Deferred<ReturnType<typeof gatingPage>>
|
||||
}> = []
|
||||
gatingApi.fetchBrandGatingPage.mockImplementation((filter: { keyword: string }) => {
|
||||
const pending = deferred<ReturnType<typeof gatingPage>>()
|
||||
calls.push({ keyword: filter.keyword, deferred: pending })
|
||||
return pending.promise
|
||||
})
|
||||
|
||||
const wrapper = mountPage(BrandGatingPanel, pinia)
|
||||
await flushPromises()
|
||||
expect(calls).toHaveLength(1)
|
||||
calls[0].deferred.resolve(gatingPage('initial-brand'))
|
||||
await flushPromises()
|
||||
expect(wrapper.text()).toContain('initial-brand')
|
||||
|
||||
const input = wrapper.get('input[placeholder="搜索品牌 / ASIN"]')
|
||||
const queryButton = wrapper.findAll('button').find((button) => button.text() === '查询')
|
||||
expect(queryButton, '页面里应有「查询」按钮').toBeDefined()
|
||||
|
||||
await input.setValue('old')
|
||||
await queryButton!.trigger('click')
|
||||
await flushPromises()
|
||||
expect(calls).toHaveLength(2)
|
||||
expect(calls[1].keyword).toBe('old')
|
||||
|
||||
await input.setValue('new')
|
||||
await queryButton!.trigger('click')
|
||||
await flushPromises()
|
||||
expect(calls).toHaveLength(3)
|
||||
expect(calls[2].keyword).toBe('new')
|
||||
|
||||
calls[2].deferred.resolve(gatingPage('new-brand'))
|
||||
await flushPromises()
|
||||
expect(wrapper.text()).toContain('new-brand')
|
||||
|
||||
calls[1].deferred.resolve(gatingPage('old-brand'))
|
||||
await flushPromises()
|
||||
expect(wrapper.text()).toContain('new-brand')
|
||||
expect(wrapper.text()).not.toContain('old-brand')
|
||||
})
|
||||
})
|
||||
@@ -38,6 +38,12 @@ public class CostGuardProperties {
|
||||
/** 当日代理提取次数上限;0=关闭。 */
|
||||
private int dailyProxyThreshold = 8_000;
|
||||
|
||||
/**
|
||||
* 平台自费(无归属 uid)代理提取的当日告警线;0=关闭。
|
||||
* 这笔钱无法归到任何账号名下,不能走暂停闸,只能推管理员告警。
|
||||
*/
|
||||
private int platformProxyDailyThreshold = 2_000;
|
||||
|
||||
/** 当日 LLM 调用次数上限;0=关闭。 */
|
||||
private int dailyLlmCallsThreshold = 6_000;
|
||||
|
||||
|
||||
+13
-7
@@ -1156,7 +1156,13 @@ public class AppearancePatentLlmClient {
|
||||
row.setPatentRisk(result.patent());
|
||||
row.setConclusion(buildConclusion(row.getTitleRisk(), row.getAppearanceRisk(), result.result()));
|
||||
row.setStatus(result.status());
|
||||
row.setTitleReason(result.titleReason());
|
||||
// 商标查询失败时把上游给的真实原因(如「用户未配置代理」「品牌检测服务不可达」)
|
||||
// 落进「原因」sheet:只写「商标查询失败」时,用户和运维都看不出该去配代理还是等上游恢复。
|
||||
String titleReason = result.titleReason();
|
||||
if ((titleReason == null || titleReason.isBlank()) && isBrandQueryFailed(row.getTitleRisk())) {
|
||||
titleReason = brandCheck == null ? "" : brandCheck.reason();
|
||||
}
|
||||
row.setTitleReason(titleReason);
|
||||
row.setAppearanceReason(result.appearanceReason());
|
||||
row.setPatentReason(result.patentReason());
|
||||
row.setScore(result.score());
|
||||
@@ -1212,16 +1218,16 @@ public class AppearancePatentLlmClient {
|
||||
return firstNonBlank(fallbackConclusion, "");
|
||||
}
|
||||
|
||||
private static final Pattern NO_INFRINGEMENT_WORDING = Pattern.compile(
|
||||
"(?:无侵权|未侵权|不侵权|没有侵权|非侵权|(?:未发现|无明显|不存在|不构成|未构成|不涉及|未涉及)[^。;;]*侵权)"); // 否定措辞优先,裸 contains("侵权") 会反判
|
||||
private boolean isInfringement(String value) {
|
||||
String normalized = normalize(value);
|
||||
return normalized.contains("侵权")
|
||||
&& !normalized.contains("无侵权")
|
||||
&& !normalized.contains("未侵权");
|
||||
String normalized = normalize(value).replaceAll("[\\p{Z}\\p{Cf}\\s]+", "");
|
||||
return normalized.contains("侵权") && !isNoInfringement(normalized);
|
||||
}
|
||||
|
||||
private boolean isNoInfringement(String value) {
|
||||
String normalized = normalize(value);
|
||||
return normalized.contains("无侵权") || normalized.contains("未侵权");
|
||||
String normalized = normalize(value).replaceAll("[\\p{Z}\\p{Cf}\\s]+", "");
|
||||
return NO_INFRINGEMENT_WORDING.matcher(normalized).find();
|
||||
}
|
||||
|
||||
private boolean isBrandQueryFailed(String value) {
|
||||
|
||||
+12
-3
@@ -159,6 +159,10 @@ public class AppearancePatentDeliverySupport {
|
||||
* 「因为慢而抛满重试」不再等于「永远补不了」。
|
||||
*
|
||||
* <p>预算耗尽、或重排没接上时仍按已有结果出表:「起码失败了也可以下载」那条保证不变。
|
||||
*
|
||||
* <p>父任务已终态时不再排补检:B28 的终态守卫不会让结果作业再跑,重排只会被下一轮
|
||||
* claim 立刻打回 FAILED,白转一圈(现网 32217/32220 锁续 86 分钟就是这条循环的旧形态)。
|
||||
* 终态任务直接按已有分片出部分结果,缺口由行账文案标在表里。
|
||||
*/
|
||||
public boolean scheduleFallbackRecheck(FileTaskEntity task, FileResultEntity result,
|
||||
AppearancePatentRowOutcome.Facts facts) {
|
||||
@@ -167,15 +171,20 @@ public class AppearancePatentDeliverySupport {
|
||||
|| facts == null || maxRounds <= 0) {
|
||||
return false;
|
||||
}
|
||||
if (facts.missingRows() <= 0 || facts.missingKeys().isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
if (TaskStatuses.FileTask.TERMINAL.contains(task.getStatus())) {
|
||||
log.warn("[appearance-patent] 任务已终态,补检不再执行,按已有结果交付 taskId={} status={} missingRows={} keys={}",
|
||||
task.getId(), task.getStatus(), facts.missingRows(), facts.missingKeys().size());
|
||||
return false;
|
||||
}
|
||||
long done = taskCacheService.recheckRounds(task.getId());
|
||||
if (done >= maxRounds) {
|
||||
log.warn("[appearance-patent] 补检预算已耗尽,按已有结果交付 taskId={} rounds={}/{}",
|
||||
task.getId(), done, maxRounds);
|
||||
return false;
|
||||
}
|
||||
if (facts.missingRows() <= 0 || facts.missingKeys().isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
taskCacheService.releaseRowInspection(task.getId(), facts.missingKeys(), true);
|
||||
TaskFileJobEntity reset = taskFileJobService.resetTerminalFailedForRecovery(
|
||||
task.getId(), moduleType, result.getId());
|
||||
|
||||
+56
-7
@@ -212,7 +212,13 @@ public class AppearancePatentResultJobSupport {
|
||||
}
|
||||
}
|
||||
}
|
||||
boolean failed = finalError != null && !finalError.isBlank();
|
||||
// 抓取缺行是“部分数据缺口”,不是致命失败:只要已有可用结果记录、且错误里没有
|
||||
// 出表/任务级故障,就先进入组装;最终是否成功由 completeLlmFileJob 按覆盖率判定。
|
||||
// 只有真正凑不出结果(result == null)时才维持 FAILED。
|
||||
boolean hasError = finalError != null && !finalError.isBlank();
|
||||
boolean partialGapSalvageable = hasError && isPartialDataGapError(finalError)
|
||||
&& assembleWorkbook && result != null;
|
||||
boolean failed = hasError && !partialGapSalvageable;
|
||||
boolean waitingForAssemble = !failed && assembleWorkbook && !shouldAssembleSynchronously();
|
||||
task.setStatus(statusResolver.resolve(waitingForAssemble, failed));
|
||||
task.setSuccessFileCount(failed ? 0 : 1);
|
||||
@@ -375,13 +381,16 @@ public class AppearancePatentResultJobSupport {
|
||||
AppearancePatentRowOutcome.Facts deliveryFacts = assembleResultWorkbook(task, result);
|
||||
progressSupport.saveFileBuildProgress(task, job, totalProgressUnits, totalProgressUnits - 1, "正在上传结果文件");
|
||||
fileResultMapper.updateById(result);
|
||||
boolean taskAlreadyFailed = STATUS_FAILED.equals(task.getStatus())
|
||||
|| (task.getErrorMessage() != null && !task.getErrorMessage().isBlank());
|
||||
String existingError = task.getErrorMessage();
|
||||
task.setStatus(statusResolver.resolve(false, taskAlreadyFailed));
|
||||
task.setSuccessFileCount(taskAlreadyFailed ? 0 : 1);
|
||||
task.setFailedFileCount(taskAlreadyFailed ? 1 : 0);
|
||||
task.setErrorMessage(deliverySupport.appendDeliveryGapMessage(taskAlreadyFailed ? existingError : null, deliveryFacts));
|
||||
boolean taskAlreadyFailed = STATUS_FAILED.equals(task.getStatus());
|
||||
boolean failed = shouldFailAfterAssembly(taskAlreadyFailed, existingError,
|
||||
deliveryFacts.expectedRows(), deliveryFacts.coverage(),
|
||||
properties.getLlmCoverageMinRatio());
|
||||
task.setStatus(statusResolver.resolve(false, failed));
|
||||
task.setSuccessFileCount(failed ? 0 : 1);
|
||||
task.setFailedFileCount(failed ? 1 : 0);
|
||||
// 成功时也保留缺口文案:用户要看到“N 行抓取失败”,但不能因此把整单判死、逼他重跑。
|
||||
task.setErrorMessage(deliverySupport.appendDeliveryGapMessage(existingError, deliveryFacts));
|
||||
task.setUpdatedAt(LocalDateTime.now());
|
||||
task.setFinishedAt(LocalDateTime.now());
|
||||
fileTaskMapper.updateById(task);
|
||||
@@ -390,6 +399,46 @@ public class AppearancePatentResultJobSupport {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 出表后的最终状态判定:已判死 / 覆盖率低于下限 / 非部分缺口的错误 → FAILED。
|
||||
*
|
||||
* <p>抓取缺行只允许把缺口写进消息,不允许把已经拿到 95% 以上结论的任务整单判死——
|
||||
* 那会让用户重跑,二次烧掉整单 LLM 密钥(现网 32288/32252 就是 3/2 行缺口烧掉整单)。
|
||||
*/
|
||||
static boolean shouldFailAfterAssembly(boolean taskAlreadyFailed, String existingError,
|
||||
int expectedRows, double coverage, double minCoverage) {
|
||||
if (taskAlreadyFailed) {
|
||||
return true;
|
||||
}
|
||||
boolean hasError = existingError != null && !existingError.isBlank();
|
||||
if (hasError && !isPartialDataGapError(existingError)) {
|
||||
return true;
|
||||
}
|
||||
if (expectedRows <= 0) {
|
||||
return hasError;
|
||||
}
|
||||
return coverage + 1e-9 < minCoverage;
|
||||
}
|
||||
|
||||
/**
|
||||
* 只认“少量行抓取/采集缺失”这类可带缺口交付的错误;出表失败、任务级故障仍按致命处理。
|
||||
*/
|
||||
static boolean isPartialDataGapError(String error) {
|
||||
if (error == null || error.isBlank()) {
|
||||
return false;
|
||||
}
|
||||
if (error.contains("结果文件生成失败")
|
||||
|| error.contains("结果文件生成多次失败")
|
||||
|| error.contains("Generate appearance patent result failed")
|
||||
|| error.contains("任务不存在")
|
||||
|| error.contains("任务已自动结束")) {
|
||||
return false;
|
||||
}
|
||||
return error.contains("行抓取失败(未取得商品页数据)")
|
||||
|| error.contains("行未采集到数据")
|
||||
|| error.contains("行未完成检测");
|
||||
}
|
||||
|
||||
/**
|
||||
* 失败兜底:组装作业重试耗尽(WAIT_LLM 停放被 stuck 棘轮判死、或组装异常连吃 5 次重试)后调用,
|
||||
* 用已回传的分片与已拿到结论的行尽力生成可下载的部分结果文件。
|
||||
|
||||
@@ -260,13 +260,27 @@ public class AuthService {
|
||||
return authProperties.getCookieName();
|
||||
}
|
||||
|
||||
/** 登录推进世代;返回 0 行说明用户行已消失,不能再签发一个「世代比库还新」的令牌。 */
|
||||
/**
|
||||
* 登录推进世代;返回 0 行说明用户行已消失,不能再签发一个「世代比库还新」的令牌。
|
||||
*
|
||||
* <p>CR-07:自增用原子 UPDATE,但令牌必须按<b>回读到的库值</b>签发。两路并发登录
|
||||
* 都读到 N 时,库里会被推到 N+2;若各自按内存 N+1 签,两张 token 都落后一代,
|
||||
* 登录成功后第一次请求就被 session_epoch 校验打成 401。回读牺牲「两张 token 世代
|
||||
* 完全相同」的极端情形,换取任何一张都不会低于库值。</p>
|
||||
*/
|
||||
private void advanceSessionEpoch(LoginUserEntity user) {
|
||||
int updated = loginUserMapper.incrementSessionEpoch(user.getId());
|
||||
if (updated != 1) {
|
||||
log.warn("[auth] 登录推进会话世代失败 userId={} updated={}", user.getId(), updated);
|
||||
throw new BusinessException(500, "登录状态更新失败,请重试");
|
||||
}
|
||||
LoginUserEntity refreshed = loginUserMapper.selectById(user.getId());
|
||||
if (refreshed != null && refreshed.getSessionEpoch() != null) {
|
||||
user.setSessionEpoch(refreshed.getSessionEpoch());
|
||||
return;
|
||||
}
|
||||
// 自增已成功却回读不到(极端主从延迟/半提交):退回内存 +1 只作防御,
|
||||
// 不掩盖问题——下一轮校验会按库值把 stale token 拒掉。
|
||||
long current = user.getSessionEpoch() == null ? 0L : user.getSessionEpoch();
|
||||
user.setSessionEpoch(current + 1);
|
||||
}
|
||||
|
||||
+31
-10
@@ -1,8 +1,10 @@
|
||||
package com.nanri.aiimage.modules.brandgating.controller;
|
||||
|
||||
import com.nanri.aiimage.common.api.ApiResponse;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import com.nanri.aiimage.common.security.AdminAuthSupport;
|
||||
import com.nanri.aiimage.common.security.DeviceSessionPolicy;
|
||||
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingAddRequest;
|
||||
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingImportRequest;
|
||||
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingStatusRequest;
|
||||
@@ -20,6 +22,7 @@ import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
@@ -33,20 +36,20 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
* 品牌Gating库(管理后台):独立小网页后台(gating_web_admin.py)并入数富管理后台,
|
||||
* 共用主库同一张 biz_brand_gating 表。
|
||||
*
|
||||
* <p>本模块以「品牌Gating库」页签嵌在「店铺数据重复检查」页(DuplicateCheckPage.vue)里,
|
||||
* 不再占用独立菜单行(V164 菜单已由 V166 删除)。因此页面权限键跟随宿主页
|
||||
* {@code admin_shop_data_duplicate_check},经
|
||||
* <p>本模块占用独立菜单「品牌Gating库」{@code admin_brand_gating}(V164 建菜单、V166 回滚、
|
||||
* V174 恢复),页面权限键与菜单键一致,经
|
||||
* {@link PermissionMenuService#requireConsolePageAccess} 把门;鉴权第一道门由
|
||||
* AdminApiGuardFilter 的 /api/admin 前缀兜底(见 ApiGuardCoverageAuditor 口径)。</p>
|
||||
*/
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
@RequestMapping("/api/admin/brand-gating")
|
||||
@Tag(name = "品牌Gating库(后台)", description = "品牌Gating库的列表/筛选/统计、单条新增、启停、删除与批量导入。")
|
||||
public class BrandGatingController {
|
||||
|
||||
/** 宿主页「店铺数据重复检查」的后台菜单权限键(columns.column_key)。 */
|
||||
private static final String PERMISSION_KEY = "admin_shop_data_duplicate_check";
|
||||
/** 「品牌Gating库」独立菜单的后台权限键(columns.column_key)。 */
|
||||
private static final String PERMISSION_KEY = "admin_brand_gating";
|
||||
private static final String DENIAL_MESSAGE = "无权访问品牌Gating库";
|
||||
|
||||
private final BrandGatingService brandGatingService;
|
||||
@@ -80,7 +83,7 @@ public class BrandGatingController {
|
||||
public ApiResponse<BrandGatingAddResultVo> add(
|
||||
HttpServletRequest request,
|
||||
@Valid @RequestBody BrandGatingAddRequest body) {
|
||||
requireBrandGatingAccess(request);
|
||||
requirePlatformSuperAdmin(request);
|
||||
return ApiResponse.success(brandGatingService.add(body));
|
||||
}
|
||||
|
||||
@@ -89,7 +92,7 @@ public class BrandGatingController {
|
||||
public ApiResponse<BrandGatingDeleteResultVo> delete(
|
||||
HttpServletRequest request,
|
||||
@PathVariable("id") Long id) {
|
||||
requireBrandGatingAccess(request);
|
||||
requirePlatformSuperAdmin(request);
|
||||
return ApiResponse.success(brandGatingService.delete(id));
|
||||
}
|
||||
|
||||
@@ -99,7 +102,7 @@ public class BrandGatingController {
|
||||
HttpServletRequest request,
|
||||
@PathVariable("id") Long id,
|
||||
@Valid @RequestBody BrandGatingStatusRequest body) {
|
||||
requireBrandGatingAccess(request);
|
||||
requirePlatformSuperAdmin(request);
|
||||
return ApiResponse.success(brandGatingService.updateStatus(id, body.getStatus()));
|
||||
}
|
||||
|
||||
@@ -108,13 +111,31 @@ public class BrandGatingController {
|
||||
public ApiResponse<BrandGatingImportResultVo> importRows(
|
||||
HttpServletRequest request,
|
||||
@RequestBody BrandGatingImportRequest body) {
|
||||
requireBrandGatingAccess(request);
|
||||
requirePlatformSuperAdmin(request);
|
||||
return ApiResponse.success(brandGatingService.importRows(body));
|
||||
}
|
||||
|
||||
/** 逐端点把门:登录态 + 宿主页后台菜单权限(admin_shop_data_duplicate_check)。 */
|
||||
/** 只读端点:登录态 + 「品牌Gating库」后台菜单权限(admin_brand_gating)。 */
|
||||
private void requireBrandGatingAccess(HttpServletRequest request) {
|
||||
AdminUserEntity operator = adminAuthSupport.requireUser(request);
|
||||
permissionMenuService.requireConsolePageAccess(operator, DENIAL_MESSAGE, PERMISSION_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
* 写端点(新增/启停/删除/批量导入):在菜单权限之上再要求平台超级管理员。
|
||||
*
|
||||
* <p>CR-11:{@code biz_brand_gating} 全平台共享、没有 company_id;任一公司管理员被授予
|
||||
* {@code admin_brand_gating} 后就能物理删除/启停影响全平台的 gating 行。读保持原口径,
|
||||
* 写收窄到平台超管(与 {@code AdminDeviceLogController.requireSuperAdmin} 同一判据)。</p>
|
||||
*/
|
||||
private AdminUserEntity requirePlatformSuperAdmin(HttpServletRequest request) {
|
||||
AdminUserEntity operator = adminAuthSupport.requireUser(request);
|
||||
permissionMenuService.requireConsolePageAccess(operator, DENIAL_MESSAGE, PERMISSION_KEY);
|
||||
if (!DeviceSessionPolicy.isSuper(adminAuthSupport.currentRole(operator))) {
|
||||
log.warn("[brand-gating] 非平台超管写操作被拒 userId={} username={} role={}",
|
||||
operator.getId(), operator.getUsername(), adminAuthSupport.currentRole(operator));
|
||||
throw new BusinessException(403, "仅平台超级管理员可以修改品牌Gating库");
|
||||
}
|
||||
return operator;
|
||||
}
|
||||
}
|
||||
|
||||
+34
@@ -24,6 +24,9 @@ import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.HexFormat;
|
||||
@@ -351,6 +354,37 @@ public class OssStorageService {
|
||||
return HexFormat.of().formatHex(digest.digest());
|
||||
}
|
||||
|
||||
/**
|
||||
* 把工作台客户端包流式落到本地临时文件,供 confirm 阶段做 zip 内容检查(CR-02)。
|
||||
*
|
||||
* <p>包体上限 512MB,不能整包读入内存;下载按 1MB 块流式拷贝,临时文件由调用方负责删除。
|
||||
* 该方法只做传输,不做内容判定。</p>
|
||||
*/
|
||||
public Path downloadWorkbenchClientObjectToTempFile(String objectKey) {
|
||||
requireWorkbenchClientKey(objectKey);
|
||||
Path temp = null;
|
||||
try {
|
||||
temp = Files.createTempFile("workbench-client-package-", ".zip");
|
||||
try (InputStream stream = buildClient().getObject(GetObjectArgs.builder()
|
||||
.bucket(publicClientBucket())
|
||||
.object(objectKey)
|
||||
.build())) {
|
||||
Files.copy(stream, temp, StandardCopyOption.REPLACE_EXISTING);
|
||||
}
|
||||
return temp;
|
||||
} catch (Exception ex) {
|
||||
if (temp != null) {
|
||||
try {
|
||||
Files.deleteIfExists(temp);
|
||||
} catch (Exception cleanupFailure) {
|
||||
log.warn("[oss] 清理工作台安装包临时文件失败 path={} err={}",
|
||||
temp, cleanupFailure.getMessage());
|
||||
}
|
||||
}
|
||||
throw storageFailure("download", objectKey, ex);
|
||||
}
|
||||
}
|
||||
|
||||
/** 工作台客户端包公开下载 URL:优先 download-endpoint(主机 B 反代回源),未配置回退 public-endpoint。 */
|
||||
public String getWorkbenchClientDownloadUrl(String objectKey) {
|
||||
requireWorkbenchClientKey(objectKey);
|
||||
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
package com.nanri.aiimage.modules.keywordusage.controller;
|
||||
|
||||
import com.nanri.aiimage.common.api.ApiResponse;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import com.nanri.aiimage.common.security.AdminAuthSupport;
|
||||
import com.nanri.aiimage.common.service.DataReadScope;
|
||||
import com.nanri.aiimage.modules.keywordusage.model.vo.KeywordUsageOverviewVo;
|
||||
import com.nanri.aiimage.modules.keywordusage.model.vo.KeywordUsagePageVo;
|
||||
import com.nanri.aiimage.modules.keywordusage.service.KeywordUsageAdminService;
|
||||
import com.nanri.aiimage.modules.permission.service.PermissionMenuService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 关键词使用记录(管理后台):只读展示 keyword_usage / keyword_add_log / keyword_delete_log。
|
||||
*
|
||||
* <p>独立菜单「关键词使用记录」{@code admin_keyword_usage}(V175),按公司 tab 取数:
|
||||
* 平台超管可收窄到请求里的 company_id,其余账号由 DataReadScope 强制本公司。
|
||||
* 留痕表由桌面端与 keywordshare 模块共用,本页不写、不删。</p>
|
||||
*/
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@RequestMapping("/api/admin/keyword-usage")
|
||||
@Tag(name = "关键词使用记录(后台)", description = "关键词复制/添加/删除留痕的只读分页与统计。")
|
||||
public class AdminKeywordUsageController {
|
||||
|
||||
private static final String PERMISSION_KEY = "admin_keyword_usage";
|
||||
private static final String DENIAL_MESSAGE = "无权访问关键词使用记录";
|
||||
|
||||
private final KeywordUsageAdminService keywordUsageAdminService;
|
||||
private final AdminAuthSupport adminAuthSupport;
|
||||
private final PermissionMenuService permissionMenuService;
|
||||
private final DataReadScope dataReadScope;
|
||||
|
||||
@GetMapping
|
||||
@Operation(summary = "使用记录分页", description = "复制关键词 + 添加关键词合并按时间倒序;scene=已使用/添加关键词可只看一支。")
|
||||
public ApiResponse<KeywordUsagePageVo> usage(
|
||||
HttpServletRequest request,
|
||||
@Parameter(description = "公司 ID") @RequestParam(name = "company_id", required = false) Long companyId,
|
||||
@Parameter(description = "使用者") @RequestParam(name = "user", required = false) String user,
|
||||
@Parameter(description = "国家") @RequestParam(name = "country", required = false) String country,
|
||||
@Parameter(description = "关键词模糊搜索") @RequestParam(name = "keyword", required = false) String keyword,
|
||||
@Parameter(description = "场景:已使用 / 添加关键词") @RequestParam(name = "scene", required = false) String scene,
|
||||
@Parameter(description = "页码(1 起)") @RequestParam(name = "page", required = false) Long page,
|
||||
@Parameter(description = "每页数量(默认 15,上限 100)")
|
||||
@RequestParam(name = "page_size", required = false) Long pageSize) {
|
||||
AdminUserEntity operator = requireAccess(request);
|
||||
Long scopedCompanyId = dataReadScope.readCompanyIdOrOwn(operator, companyId);
|
||||
return ApiResponse.success(
|
||||
keywordUsageAdminService.usage(scopedCompanyId, user, country, keyword, scene, page, pageSize));
|
||||
}
|
||||
|
||||
@GetMapping("/deletes")
|
||||
@Operation(summary = "删除记录分页", description = "keyword_delete_log 按删除时间倒序;只读、不可删。")
|
||||
public ApiResponse<KeywordUsagePageVo> deletes(
|
||||
HttpServletRequest request,
|
||||
@Parameter(description = "公司 ID") @RequestParam(name = "company_id", required = false) Long companyId,
|
||||
@Parameter(description = "删除者") @RequestParam(name = "user", required = false) String user,
|
||||
@Parameter(description = "国家") @RequestParam(name = "country", required = false) String country,
|
||||
@Parameter(description = "关键词模糊搜索") @RequestParam(name = "keyword", required = false) String keyword,
|
||||
@Parameter(description = "页码(1 起)") @RequestParam(name = "page", required = false) Long page,
|
||||
@Parameter(description = "每页数量(默认 15,上限 100)")
|
||||
@RequestParam(name = "page_size", required = false) Long pageSize) {
|
||||
AdminUserEntity operator = requireAccess(request);
|
||||
Long scopedCompanyId = dataReadScope.readCompanyIdOrOwn(operator, companyId);
|
||||
return ApiResponse.success(
|
||||
keywordUsageAdminService.deletes(scopedCompanyId, user, country, keyword, page, pageSize));
|
||||
}
|
||||
|
||||
@GetMapping("/overview")
|
||||
@Operation(summary = "统计卡与筛选候选", description = "统计卡 + 有操作记录的使用者/国家(五国在首位)。")
|
||||
public ApiResponse<KeywordUsageOverviewVo> overview(
|
||||
HttpServletRequest request,
|
||||
@Parameter(description = "公司 ID") @RequestParam(name = "company_id", required = false) Long companyId) {
|
||||
AdminUserEntity operator = requireAccess(request);
|
||||
Long scopedCompanyId = dataReadScope.readCompanyIdOrOwn(operator, companyId);
|
||||
return ApiResponse.success(keywordUsageAdminService.overview(scopedCompanyId));
|
||||
}
|
||||
|
||||
/** 逐端点把门:登录态 + 「关键词使用记录」后台菜单权限(admin_keyword_usage)。 */
|
||||
private AdminUserEntity requireAccess(HttpServletRequest request) {
|
||||
AdminUserEntity operator = adminAuthSupport.requireUser(request);
|
||||
permissionMenuService.requireConsolePageAccess(operator, DENIAL_MESSAGE, PERMISSION_KEY);
|
||||
return operator;
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.nanri.aiimage.modules.keywordusage.mapper;
|
||||
|
||||
import com.nanri.aiimage.modules.keywordusage.model.dto.KeywordUsageFilterQuery;
|
||||
import com.nanri.aiimage.modules.keywordusage.model.vo.KeywordUsageRowVo;
|
||||
import com.nanri.aiimage.modules.keywordusage.model.vo.KeywordUsageStatsVo;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 关键词使用记录(管理后台只读视图)数据访问。
|
||||
*
|
||||
* <p>表 {@code keyword_usage} / {@code keyword_add_log} / {@code keyword_delete_log} /
|
||||
* {@code keyword_share} 由桌面端与 keywordshare 模块共用,本 Mapper 只读不写;
|
||||
* SQL 全部收口在 {@code mapper/keywordusage/KeywordUsageAdminMapper.xml}。</p>
|
||||
*/
|
||||
@Mapper
|
||||
public interface KeywordUsageAdminMapper {
|
||||
|
||||
long countUsageRows(@Param("f") KeywordUsageFilterQuery filter);
|
||||
|
||||
List<KeywordUsageRowVo> selectUsageRows(@Param("f") KeywordUsageFilterQuery filter);
|
||||
|
||||
long countCopy(@Param("f") KeywordUsageFilterQuery filter);
|
||||
|
||||
long countCopyExisting(@Param("f") KeywordUsageFilterQuery filter);
|
||||
|
||||
long countAdd(@Param("f") KeywordUsageFilterQuery filter);
|
||||
|
||||
long countDeletes(@Param("f") KeywordUsageFilterQuery filter);
|
||||
|
||||
List<KeywordUsageRowVo> selectDeleteRows(@Param("f") KeywordUsageFilterQuery filter);
|
||||
|
||||
KeywordUsageStatsVo selectStats(@Param("companyId") Long companyId);
|
||||
|
||||
List<String> selectUsers(@Param("companyId") Long companyId);
|
||||
|
||||
List<String> selectCountries(@Param("companyId") Long companyId);
|
||||
}
|
||||
+25
@@ -0,0 +1,25 @@
|
||||
package com.nanri.aiimage.modules.keywordusage.model.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 关键词留痕查询条件——Mapper XML 的唯一入参载体({@code @Param("f")})。
|
||||
* 筛选值一律走 {@code #{}} 占位符,不做字符串拼接。
|
||||
*/
|
||||
@Data
|
||||
public class KeywordUsageFilterQuery {
|
||||
|
||||
/** null = 平台超管未指定公司(跨公司全量);非 null = 收窄到该公司。 */
|
||||
private Long companyId;
|
||||
|
||||
private String user;
|
||||
private String country;
|
||||
private String keyword;
|
||||
|
||||
/** 使用记录合并视图:是否包含「复制关键词」/「添加关键词」两支。 */
|
||||
private boolean includeCopy;
|
||||
private boolean includeAdd;
|
||||
|
||||
private long offset;
|
||||
private long limit;
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
package com.nanri.aiimage.modules.keywordusage.model.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
/** 使用记录口径计数:复制 = 现存词 + 已删除词留痕。 */
|
||||
@Data
|
||||
@Schema(description = "关键词使用记录计数")
|
||||
public class KeywordUsageCountsVo {
|
||||
|
||||
@Schema(description = "复制关键词(标记已使用)总数")
|
||||
private long copy;
|
||||
|
||||
@Schema(description = "复制关键词中仍存在于 keyword_share 的数量")
|
||||
private long copyExisting;
|
||||
|
||||
@Schema(description = "复制关键词中已从 keyword_share 删除的数量")
|
||||
private long copyDeleted;
|
||||
|
||||
@Schema(description = "添加关键词总数")
|
||||
private long add;
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.nanri.aiimage.modules.keywordusage.model.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** 关键词使用记录页首屏:统计卡 + 筛选下拉候选。 */
|
||||
@Data
|
||||
@Schema(description = "关键词使用记录概览")
|
||||
public class KeywordUsageOverviewVo {
|
||||
|
||||
@Schema(description = "统计卡")
|
||||
private KeywordUsageStatsVo stats = new KeywordUsageStatsVo();
|
||||
|
||||
@Schema(description = "有操作记录的使用者")
|
||||
private List<String> users = List.of();
|
||||
|
||||
@Schema(description = "有操作记录的国家(五国在首位)")
|
||||
private List<String> countries = List.of();
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.nanri.aiimage.modules.keywordusage.model.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/** 关键词留痕分页负载。 */
|
||||
@Data
|
||||
@Schema(description = "关键词使用/删除记录分页")
|
||||
public class KeywordUsagePageVo {
|
||||
|
||||
@Schema(description = "总条数")
|
||||
private long total;
|
||||
|
||||
@Schema(description = "当前页行")
|
||||
private List<KeywordUsageRowVo> rows = List.of();
|
||||
|
||||
@Schema(description = "使用记录口径计数(删除记录页为空对象)")
|
||||
private KeywordUsageCountsVo counts = new KeywordUsageCountsVo();
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.nanri.aiimage.modules.keywordusage.model.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/** 关键词使用/删除留痕单行。 */
|
||||
@Data
|
||||
@Schema(description = "关键词使用记录行")
|
||||
public class KeywordUsageRowVo {
|
||||
|
||||
@Schema(description = "操作人")
|
||||
private String userName;
|
||||
|
||||
@Schema(description = "国家")
|
||||
private String country;
|
||||
|
||||
@Schema(description = "关键词")
|
||||
private String keyword;
|
||||
|
||||
@Schema(description = "场景:复制关键词 / 添加关键词 / 删除关键词")
|
||||
private String scene;
|
||||
|
||||
@Schema(description = "操作时间")
|
||||
private LocalDateTime time;
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.nanri.aiimage.modules.keywordusage.model.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
/** 关键词使用记录统计卡。 */
|
||||
@Data
|
||||
@Schema(description = "关键词使用记录统计")
|
||||
public class KeywordUsageStatsVo {
|
||||
|
||||
@Schema(description = "复制关键词(标记已使用)总数")
|
||||
private long used;
|
||||
|
||||
@Schema(description = "复制关键词中仍存在于 keyword_share 的数量")
|
||||
private long exist;
|
||||
|
||||
@Schema(description = "已删除词留痕 = used - exist")
|
||||
private long deletedKw;
|
||||
|
||||
@Schema(description = "添加关键词总数")
|
||||
private long added;
|
||||
|
||||
@Schema(description = "使用记录合计 = used + added")
|
||||
private long totalUse;
|
||||
|
||||
@Schema(description = "删除关键词记录总数")
|
||||
private long deletes;
|
||||
}
|
||||
+143
@@ -0,0 +1,143 @@
|
||||
package com.nanri.aiimage.modules.keywordusage.service;
|
||||
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.modules.keywordusage.mapper.KeywordUsageAdminMapper;
|
||||
import com.nanri.aiimage.modules.keywordusage.model.dto.KeywordUsageFilterQuery;
|
||||
import com.nanri.aiimage.modules.keywordusage.model.vo.KeywordUsageCountsVo;
|
||||
import com.nanri.aiimage.modules.keywordusage.model.vo.KeywordUsageOverviewVo;
|
||||
import com.nanri.aiimage.modules.keywordusage.model.vo.KeywordUsagePageVo;
|
||||
import com.nanri.aiimage.modules.keywordusage.model.vo.KeywordUsageRowVo;
|
||||
import com.nanri.aiimage.modules.keywordusage.model.vo.KeywordUsageStatsVo;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 关键词使用记录(管理后台只读视图)。
|
||||
*
|
||||
* <p>口径对齐独立小网页后台 v1.2.6g:使用记录 = 复制关键词(keyword_usage)+ 添加关键词
|
||||
* (keyword_add_log)合并按时间倒序;删除记录单独一页;统计卡里的「已删除词留痕」
|
||||
* = 复制总数 - 仍存在于 keyword_share 的数量。公司范围由 Controller 经 DataReadScope 解析后传入,
|
||||
* 本层不再自行从请求参数取公司。</p>
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class KeywordUsageAdminService {
|
||||
|
||||
private static final long DEFAULT_PAGE_SIZE = 15;
|
||||
private static final long MAX_PAGE_SIZE = 100;
|
||||
/**
|
||||
* 页码上限(CR-18):三表 UNION 的深分页本身昂贵,且 {@code (page-1)*pageSize}
|
||||
* 在 {@code page=Long.MAX_VALUE} 时会溢出成负数,生成 {@code LIMIT -N,15} 直接 500。
|
||||
* 这里钳到安全上限;10000 页 × 100 条 = 100 万行,已远超实际可翻范围。
|
||||
*/
|
||||
private static final long MAX_PAGE = 10_000;
|
||||
private static final List<String> BASE_COUNTRIES = List.of("英国", "德国", "法国", "西班牙", "意大利");
|
||||
|
||||
private final KeywordUsageAdminMapper keywordUsageAdminMapper;
|
||||
|
||||
public KeywordUsagePageVo usage(Long companyId, String user, String country, String keyword,
|
||||
String scene, Long page, Long pageSize) {
|
||||
KeywordUsageFilterQuery filter = buildFilter(companyId, user, country, keyword, page, pageSize);
|
||||
applyScene(filter, scene);
|
||||
|
||||
KeywordUsagePageVo vo = new KeywordUsagePageVo();
|
||||
if (!filter.isIncludeCopy() && !filter.isIncludeAdd()) {
|
||||
vo.setTotal(0);
|
||||
vo.setRows(List.of());
|
||||
vo.setCounts(new KeywordUsageCountsVo());
|
||||
return vo;
|
||||
}
|
||||
|
||||
long total = keywordUsageAdminMapper.countUsageRows(filter);
|
||||
long copy = filter.isIncludeCopy() ? keywordUsageAdminMapper.countCopy(filter) : 0;
|
||||
long copyExisting = filter.isIncludeCopy() ? keywordUsageAdminMapper.countCopyExisting(filter) : 0;
|
||||
long add = filter.isIncludeAdd() ? keywordUsageAdminMapper.countAdd(filter) : 0;
|
||||
KeywordUsageCountsVo counts = new KeywordUsageCountsVo();
|
||||
counts.setCopy(copy);
|
||||
counts.setCopyExisting(copyExisting);
|
||||
counts.setCopyDeleted(Math.max(0, copy - copyExisting));
|
||||
counts.setAdd(add);
|
||||
|
||||
vo.setTotal(total);
|
||||
vo.setRows(keywordUsageAdminMapper.selectUsageRows(filter));
|
||||
vo.setCounts(counts);
|
||||
return vo;
|
||||
}
|
||||
|
||||
public KeywordUsagePageVo deletes(Long companyId, String user, String country, String keyword,
|
||||
Long page, Long pageSize) {
|
||||
KeywordUsageFilterQuery filter = buildFilter(companyId, user, country, keyword, page, pageSize);
|
||||
KeywordUsagePageVo vo = new KeywordUsagePageVo();
|
||||
vo.setTotal(keywordUsageAdminMapper.countDeletes(filter));
|
||||
vo.setRows(keywordUsageAdminMapper.selectDeleteRows(filter));
|
||||
vo.setCounts(new KeywordUsageCountsVo());
|
||||
return vo;
|
||||
}
|
||||
|
||||
public KeywordUsageOverviewVo overview(Long companyId) {
|
||||
KeywordUsageStatsVo stats = keywordUsageAdminMapper.selectStats(companyId);
|
||||
if (stats == null) {
|
||||
stats = new KeywordUsageStatsVo();
|
||||
}
|
||||
stats.setDeletedKw(Math.max(0, stats.getUsed() - stats.getExist()));
|
||||
stats.setTotalUse(stats.getUsed() + stats.getAdded());
|
||||
|
||||
List<String> countries = new ArrayList<>(BASE_COUNTRIES);
|
||||
for (String country : safeList(keywordUsageAdminMapper.selectCountries(companyId))) {
|
||||
if (!countries.contains(country)) {
|
||||
countries.add(country);
|
||||
}
|
||||
}
|
||||
|
||||
KeywordUsageOverviewVo vo = new KeywordUsageOverviewVo();
|
||||
vo.setStats(stats);
|
||||
vo.setUsers(safeList(keywordUsageAdminMapper.selectUsers(companyId)));
|
||||
vo.setCountries(countries);
|
||||
return vo;
|
||||
}
|
||||
|
||||
private KeywordUsageFilterQuery buildFilter(Long companyId, String user, String country, String keyword,
|
||||
Long page, Long pageSize) {
|
||||
KeywordUsageFilterQuery filter = new KeywordUsageFilterQuery();
|
||||
filter.setCompanyId(companyId);
|
||||
filter.setUser(trimToNull(user));
|
||||
filter.setCountry(normalizeCountry(country));
|
||||
filter.setKeyword(trimToNull(keyword));
|
||||
long safePage = page == null || page < 1 ? 1 : Math.min(page, MAX_PAGE);
|
||||
long safeSize = pageSize == null ? DEFAULT_PAGE_SIZE : Math.min(Math.max(pageSize, 1), MAX_PAGE_SIZE);
|
||||
filter.setOffset((safePage - 1) * safeSize);
|
||||
filter.setLimit(safeSize);
|
||||
return filter;
|
||||
}
|
||||
|
||||
private void applyScene(KeywordUsageFilterQuery filter, String scene) {
|
||||
String value = trimToNull(scene);
|
||||
boolean copy = value == null || "已使用".equals(value) || "复制关键词".equals(value);
|
||||
boolean add = value == null || "添加关键词".equals(value);
|
||||
if (!copy && !add) {
|
||||
throw new BusinessException("scene 只支持 已使用 / 添加关键词");
|
||||
}
|
||||
filter.setIncludeCopy(copy);
|
||||
filter.setIncludeAdd(add);
|
||||
}
|
||||
|
||||
private String normalizeCountry(String country) {
|
||||
String value = trimToNull(country);
|
||||
return value == null || "全部".equals(value) ? null : value;
|
||||
}
|
||||
|
||||
private String trimToNull(String value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
String trimmed = value.trim();
|
||||
return trimmed.isEmpty() ? null : trimmed;
|
||||
}
|
||||
|
||||
private List<String> safeList(List<String> values) {
|
||||
return values == null ? List.of() : values;
|
||||
}
|
||||
}
|
||||
+2
@@ -87,6 +87,8 @@ public class PermissionMenuSchemaInitializer {
|
||||
new DefaultAdminMenu("最低价ASIN设置", "admin_skip_price_asin", "skip-price-asin", 60, "admin_group_shop"),
|
||||
new DefaultAdminMenu("店铺数据记录", "admin_shop_data_crawl_tasks", "shop-data-crawl-tasks", 82, "admin_group_shop"),
|
||||
new DefaultAdminMenu("店铺数据重复检查", "admin_shop_data_duplicate_check", "shop-data-duplicate-check", 84, "admin_group_shop"),
|
||||
new DefaultAdminMenu("品牌Gating库", "admin_brand_gating", "brand-gating", 85, "admin_group_shop"),
|
||||
new DefaultAdminMenu("关键词使用记录", "admin_keyword_usage", "keyword-usage", 86, "admin_group_shop"),
|
||||
new DefaultAdminMenu("生成记录", "admin_history", "history", 70, "admin_group_record"),
|
||||
new DefaultAdminMenu("视频任务记录", "admin_image_video_tasks", "image-video-tasks", 75, "admin_group_record"),
|
||||
new DefaultAdminMenu("软件版本管理", "admin_version", "version", 80, "admin_group_record"),
|
||||
|
||||
+7
@@ -3033,6 +3033,13 @@ public class ShopDataCrawlTaskService extends AbstractFileTaskPipeline<ShopDataC
|
||||
FileTaskEntity task = fileTaskMapper.selectById(job.getTaskId());
|
||||
if (task == null || !MODULE_TYPE.equals(task.getModuleType())) return;
|
||||
ensureTaskOwnedByCurrentInstance(task, "fail shop data crawl result file job");
|
||||
// CR-09:终态守卫。终态父任务上仍可能遗留 PENDING/RUNNING 的结果作业,
|
||||
// 兜底扫描走到这里时不能把已 SUCCESS/CANCELLED/FAILED 的任务无条件改写成 FAILED。
|
||||
if (isTerminalTaskStatus(task.getStatus())) {
|
||||
log.info("[shop-data-crawl] skip result job failure because parent task is terminal taskId={} status={} jobId={}",
|
||||
task.getId(), task.getStatus(), job.getId());
|
||||
return;
|
||||
}
|
||||
FileResultEntity row = job.getResultId() == null ? null : fileResultMapper.selectById(job.getResultId());
|
||||
if (row != null && MODULE_TYPE.equals(row.getModuleType())) markResultFailed(row, message);
|
||||
List<FileResultEntity> rows = listScopeRows(job.getTaskId());
|
||||
|
||||
+5
@@ -264,6 +264,11 @@ public class SimilarAsinLlmService {
|
||||
for (int i = 0; i < futures.size(); i++) {
|
||||
try {
|
||||
merged.add(FutureDeadline.await(futures.get(i), futureDeadlineMillis(), "LLM 单行处理"));
|
||||
} catch (InterruptedException ex) {
|
||||
// CR-20:中断不是「这一行失败」。恢复中断标志并向上抛,不能落入下面的
|
||||
// 宽泛 Exception 分支把 InterruptedException 吞掉(worker 会继续跑完剩余行)。
|
||||
Thread.currentThread().interrupt();
|
||||
throw new IllegalStateException("LLM 单行等待被中断", ex);
|
||||
} catch (Exception ex) {
|
||||
log.warn("[similar-asin][llm] row failed index={} asin={} err={}",
|
||||
i, rows.get(i).getAsin(), failureMessage(ex));
|
||||
|
||||
+27
-5
@@ -239,10 +239,16 @@ public class TaskFileJobService {
|
||||
return status != null && TaskStatuses.FileTask.TERMINAL.contains(status);
|
||||
}
|
||||
|
||||
/** 终态父任务的 job 直接拉满重试并打终态回调:不执行、不再被下一轮扫到。 */
|
||||
/**
|
||||
* 终态父任务的 job 直接拉满重试:不执行、不再被下一轮扫到。
|
||||
*
|
||||
* <p>终态回调要留给卡死扫描的失败兜底({@code fallbackAssembleOnFailure}):外观专利等模块
|
||||
* 靠它用已回传分片出部分结果文件,「起码失败了也可以下载」才不会被 B28 的终态守卫顺手掐掉。
|
||||
* 这里若直接打 {@code terminal_callback_at},兜底扫描再也捞不到这行,用户只剩红字、连半张表都没有。
|
||||
*/
|
||||
private void skipJobOfTerminalTask(TaskFileJobEntity job, String stage) {
|
||||
String message = "父任务已终态,结果作业不再执行";
|
||||
markFailedPermanent(job, message);
|
||||
markFailedPermanentAwaitingFallback(job, message);
|
||||
log.info("[task-file-job] skipped because parent task is terminal jobId={} taskId={} moduleType={} stage={}",
|
||||
job.getId(), job.getTaskId(), job.getModuleType(), stage);
|
||||
}
|
||||
@@ -801,15 +807,31 @@ public class TaskFileJobService {
|
||||
* 适用于 task/result 已经不存在等无法通过重试恢复的场景。
|
||||
*/
|
||||
public void markFailedPermanent(TaskFileJobEntity job, String message) {
|
||||
markFailedPermanent(job, message, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 父任务已终态时的作业收口:同样判死并拉满重试,但**不打 terminal_callback_at**。
|
||||
*
|
||||
* <p>不打标不是漏收尾:卡死扫描专门捞「FAILED + retry 到顶 + terminal_callback_at 为空」的行,
|
||||
* 由它调用模块的失败兜底出部分结果(外观专利任务 31302/31354 的「起码失败了也可以下载」)。
|
||||
* 这里一旦打标,兜底路径就被永久跳过,失败任务的 result_file_url 永远为空,前端连下载按钮都没有。
|
||||
*/
|
||||
public void markFailedPermanentAwaitingFallback(TaskFileJobEntity job, String message) {
|
||||
markFailedPermanent(job, message, false);
|
||||
}
|
||||
|
||||
private void markFailedPermanent(TaskFileJobEntity job, String message, boolean finalizeCallback) {
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
taskFileJobMapper.update(null, new LambdaUpdateWrapper<TaskFileJobEntity>()
|
||||
.eq(TaskFileJobEntity::getId, job.getId())
|
||||
.ne(TaskFileJobEntity::getStatus, "SUCCESS")
|
||||
.set(TaskFileJobEntity::getStatus, "FAILED")
|
||||
.set(TaskFileJobEntity::getRetryCount, MAX_RETRY_COUNT)
|
||||
.set(TaskFileJobEntity::getErrorMessage, message == null ? "结果文件生成失败" : message)
|
||||
.set(TaskFileJobEntity::getUpdatedAt, LocalDateTime.now())
|
||||
.set(TaskFileJobEntity::getFinishedAt, LocalDateTime.now())
|
||||
.set(TaskFileJobEntity::getTerminalCallbackAt, LocalDateTime.now()));
|
||||
.set(TaskFileJobEntity::getUpdatedAt, now)
|
||||
.set(TaskFileJobEntity::getFinishedAt, now)
|
||||
.set(TaskFileJobEntity::getTerminalCallbackAt, finalizeCallback ? now : null));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+40
-6
@@ -102,6 +102,14 @@ public class TaskResultFileJobWorker {
|
||||
result.resetCount(), result.exhaustedJobs().size(), stuckTimeoutMinutes);
|
||||
}
|
||||
for (TaskFileJobEntity job : result.exhaustedJobs()) {
|
||||
// 终态兜底出表必须落在归属实例:fallbackAssembleResultFile 在非归属机会直接返回 false,
|
||||
// 但 finalizeRetryExhausted 随后仍会打 terminal_callback_at,归属机的兜底就永远跑不到了。
|
||||
if (isOwnerScopedJob(job) && !isOwnedByCurrentInstance(job)) {
|
||||
log.info("[task-file-job] skip exhausted owner-scoped job of another instance jobId={} taskId={} moduleType={} owner={} current={} stage=SKIP_OWNER",
|
||||
job.getId(), job.getTaskId(), job.getModuleType(),
|
||||
ownerFromScopeKey(job.getScopeKey()), currentInstanceId());
|
||||
continue;
|
||||
}
|
||||
String message = job.getErrorMessage() == null || job.getErrorMessage().isBlank()
|
||||
? "文件生成任务运行超时,已达到最大重试次数"
|
||||
: job.getErrorMessage();
|
||||
@@ -198,7 +206,8 @@ public class TaskResultFileJobWorker {
|
||||
processClaimedWithHeartbeat(job, claim);
|
||||
}
|
||||
|
||||
private ScheduledFuture<?> startJobHeartbeat(TaskFileJobEntity job) {
|
||||
private ScheduledFuture<?> startJobHeartbeat(TaskFileJobEntity job,
|
||||
AtomicBoolean parentTerminalObserved) {
|
||||
long interval = Math.max(1000L, heartbeatIntervalMillis);
|
||||
AtomicBoolean terminalStopped = new AtomicBoolean(false);
|
||||
return jobHeartbeatExecutor.scheduleWithFixedDelay(() -> {
|
||||
@@ -208,6 +217,7 @@ public class TaskResultFileJobWorker {
|
||||
try {
|
||||
if (taskFileJobService.isOwningTaskTerminal(job.getTaskId())) {
|
||||
if (terminalStopped.compareAndSet(false, true)) {
|
||||
parentTerminalObserved.set(true);
|
||||
TaskDistributedLockService.LockHandle handle = activeLockHandles.get(job.getId());
|
||||
if (handle != null) {
|
||||
// 良性停续期:父任务已终态,不是续期失败,不能打 renewalLost=true 造成假告警
|
||||
@@ -215,8 +225,11 @@ public class TaskResultFileJobWorker {
|
||||
}
|
||||
// 终态后不再 touchRunning:否则 job 的 updated_at 一直被刷新,
|
||||
// job 级 stuck 扫描永远看不到这行,成为不可回收的 RUNNING 遗骸。
|
||||
taskFileJobService.markFailedPermanent(job, "父任务已终态,结果作业不再执行");
|
||||
log.info("[task-file-job] parent task terminal; stopped renewal and failed job jobId={} taskId={} moduleType={} stage=TERMINAL_STOP_RENEW",
|
||||
// CR-08:这里只停续期/停心跳,**不判死**。执行线程可能正在写完整结果,
|
||||
// 心跳抢先把行翻成 FAILED 会让随后的 markSuccess(.eq(status,RUNNING))
|
||||
// 丢掉成功状态,下一次兜底扫描立刻出部分结果。终态判定统一放到
|
||||
// processClaimedWithHeartbeat 的 finally —— 执行线程退出后再看这一行。
|
||||
log.info("[task-file-job] parent task terminal; stopped renewal, waiting for execution thread jobId={} taskId={} moduleType={} stage=TERMINAL_STOP_RENEW",
|
||||
job.getId(), job.getTaskId(), job.getModuleType());
|
||||
}
|
||||
return;
|
||||
@@ -231,7 +244,7 @@ public class TaskResultFileJobWorker {
|
||||
|
||||
private void processClaimedWithHeartbeat(TaskFileJobEntity job, TaskFileJobEntity claim) {
|
||||
if (taskFileJobService.isOwningTaskTerminal(job.getTaskId())) {
|
||||
taskFileJobService.markFailedPermanent(job, "父任务已终态,结果作业不再执行");
|
||||
taskFileJobService.markFailedPermanentAwaitingFallback(job, "父任务已终态,结果作业不再执行");
|
||||
log.info("[task-file-job] skipped because parent task is terminal jobId={} taskId={} moduleType={} stage=TERMINAL_SKIP",
|
||||
job.getId(), job.getTaskId(), job.getModuleType());
|
||||
return;
|
||||
@@ -241,14 +254,35 @@ public class TaskResultFileJobWorker {
|
||||
job.getId(), job.getTaskId(), job.getModuleType(), claim.getUpdatedAt());
|
||||
return;
|
||||
}
|
||||
ScheduledFuture<?> heartbeat = startJobHeartbeat(job);
|
||||
AtomicBoolean parentTerminalObserved = new AtomicBoolean(false);
|
||||
ScheduledFuture<?> heartbeat = startJobHeartbeat(job, parentTerminalObserved);
|
||||
try {
|
||||
processInternal(job);
|
||||
} finally {
|
||||
heartbeat.cancel(false);
|
||||
// CR-08:心跳只负责停续期;终态判定必须等执行线程退出后再做。
|
||||
// 此时若 markSuccess 已落库,finishAfterExecution 会保留 SUCCESS;
|
||||
// 只有执行线程没写出成功状态的行才判死并留给兜底扫描出部分结果。
|
||||
finishAfterExecution(job, parentTerminalObserved);
|
||||
}
|
||||
}
|
||||
|
||||
/** 执行线程退出后的终态收口:心跳期间观察到父任务终态才触发,绝不覆盖已落库的 SUCCESS。 */
|
||||
void finishAfterExecution(TaskFileJobEntity job, AtomicBoolean parentTerminalObserved) {
|
||||
if (job == null || !parentTerminalObserved.get()) {
|
||||
return;
|
||||
}
|
||||
TaskFileJobEntity latest = taskFileJobService.findById(job.getId());
|
||||
if (latest != null && "SUCCESS".equals(latest.getStatus())) {
|
||||
log.info("[task-file-job] parent task terminal after execution but job already succeeded; keep success jobId={} taskId={} moduleType={} stage=TERMINAL_KEEP_SUCCESS",
|
||||
job.getId(), job.getTaskId(), job.getModuleType());
|
||||
return;
|
||||
}
|
||||
taskFileJobService.markFailedPermanentAwaitingFallback(job, "父任务已终态,结果作业不再执行");
|
||||
log.info("[task-file-job] parent task terminal after execution; failed job for fallback jobId={} taskId={} moduleType={} stage=TERMINAL_AFTER_EXECUTION",
|
||||
job.getId(), job.getTaskId(), job.getModuleType());
|
||||
}
|
||||
|
||||
private void processInternal(TaskFileJobEntity job) {
|
||||
long startedAt = System.currentTimeMillis();
|
||||
try {
|
||||
@@ -279,7 +313,7 @@ public class TaskResultFileJobWorker {
|
||||
}
|
||||
if (taskFileJobService.isOwningTaskTerminal(job.getTaskId())) {
|
||||
try (lockHandle) {
|
||||
taskFileJobService.markFailedPermanent(job, "父任务已终态,结果作业不再执行");
|
||||
taskFileJobService.markFailedPermanentAwaitingFallback(job, "父任务已终态,结果作业不再执行");
|
||||
}
|
||||
log.info("[task-file-job] skipped after acquiring lock because parent task is terminal jobId={} taskId={} moduleType={} stage=TERMINAL_AFTER_LOCK",
|
||||
job.getId(), job.getTaskId(), job.getModuleType());
|
||||
|
||||
+76
-2
@@ -40,6 +40,9 @@ public class CostGuardScheduler {
|
||||
|
||||
static final String LOCK_NAME = "cost-guard-scan";
|
||||
|
||||
/** 无归属购买的汇总桶(见 UserSecretUsageService.PLATFORM_USER_ID):不暂停,只告警管理员。 */
|
||||
static final long PLATFORM_USER_ID = UserSecretUsageService.PLATFORM_USER_ID;
|
||||
|
||||
private final CostGuardProperties properties;
|
||||
private final UserSecretUsageMapper usageMapper;
|
||||
private final UserTaskGuardService guardService;
|
||||
@@ -77,7 +80,9 @@ public class CostGuardScheduler {
|
||||
void runOnce() {
|
||||
long nowMillis = System.currentTimeMillis();
|
||||
int windowSeconds = Math.max(60, properties.getWindowSeconds());
|
||||
Map<Long, UserTotals> current = loadTodayTotals();
|
||||
Map<Long, UserTotals> all = loadAllTotals();
|
||||
UserTotals platform = all.remove(PLATFORM_USER_ID);
|
||||
Map<Long, UserTotals> current = all;
|
||||
Map<Long, WindowDelta> deltas = new LinkedHashMap<>();
|
||||
int paused = 0;
|
||||
for (Map.Entry<Long, UserTotals> entry : current.entrySet()) {
|
||||
@@ -123,11 +128,73 @@ public class CostGuardScheduler {
|
||||
}
|
||||
lastDeltas.clear();
|
||||
lastDeltas.putAll(deltas);
|
||||
scanPlatform(platform, nowMillis);
|
||||
maybeRefreshShared(true);
|
||||
log.info("[cost-guard] 扫描完成 用户={} 本轮暂停={} 共享密钥快照={}ms",
|
||||
current.size(), paused, sharedSecretMonitorService.snapshotAtMillis());
|
||||
}
|
||||
|
||||
/**
|
||||
* 平台自费桶只告警、不暂停:无归属购买没有可暂停的账号,但金额是真实的,
|
||||
* 超过告警线就推全体管理员,避免像 2026-10 那次一样在后台账上完全不可见
|
||||
* (cookie 生成器一天买了 3900+ 次 3 分钟档 IP,页面上一个数字都没有)。
|
||||
*
|
||||
* <p>基线同样落共享 store,跨实例按真实 elapsed 归一化;同一天只报一次
|
||||
* (crossed 只在「本轮跨过」时为真),告警内容的数字随当日累计更新。
|
||||
*/
|
||||
private void scanPlatform(UserTotals platform, long nowMillis) {
|
||||
if (platform == null) {
|
||||
return;
|
||||
}
|
||||
int threshold = properties.getPlatformProxyDailyThreshold();
|
||||
if (threshold <= 0) {
|
||||
return;
|
||||
}
|
||||
CostGuardBaselineStore.Baseline before = baselineStore.get(PLATFORM_USER_ID);
|
||||
String reason = null;
|
||||
// 跨天时当日累计会变小:此时上一天的基线(可能已高于告警线)不能拿来判 crossed,
|
||||
// 否则今天首次越过告警线会被静默吞掉。
|
||||
boolean newDay = before != null && platform.proxy < before.proxy;
|
||||
if (before == null || newDay) {
|
||||
if (platform.proxy >= threshold) {
|
||||
reason = platformReason(platform.proxy, threshold);
|
||||
}
|
||||
} else if (crossed(before.proxy, platform.proxy, threshold)) {
|
||||
reason = platformReason(platform.proxy, threshold);
|
||||
}
|
||||
if (reason == null) {
|
||||
baselineStore.put(PLATFORM_USER_ID, new CostGuardBaselineStore.Baseline(
|
||||
platform.proxy, platform.llmCalls, platform.llmTokens, nowMillis));
|
||||
return;
|
||||
}
|
||||
// CR-10:告警先推送成功、再推进基线。先 put 后 push 时,推送抛异常/返回 0
|
||||
// 会让当天基线已越过阈值,下一轮 crossed 不再成立,告警永久丢失。
|
||||
int pushed;
|
||||
try {
|
||||
pushed = notificationDispatchService.pushToAdmins(NotificationService.SCENE_COST_GUARD,
|
||||
NotificationService.LEVEL_WARNING, "平台自费代理提取超告警线",
|
||||
reason + "。这笔消耗没有归属账号、不能自动暂停,请检查 cookie 生成器 / 平台兜底池的提取链接档位。",
|
||||
"cost_guard_platform_proxy:" + LocalDate.now(), null);
|
||||
} catch (RuntimeException ex) {
|
||||
log.warn("[cost-guard] 平台自费告警推送失败,本轮不推进基线待下轮重试 次数={} 阈值={} err={}",
|
||||
platform.proxy, threshold, ex.getMessage());
|
||||
return;
|
||||
}
|
||||
if (pushed <= 0) {
|
||||
log.warn("[cost-guard] 平台自费告警没有落库到任何管理员,本轮不推进基线待下轮重试 次数={} 阈值={}",
|
||||
platform.proxy, threshold);
|
||||
return;
|
||||
}
|
||||
baselineStore.put(PLATFORM_USER_ID, new CostGuardBaselineStore.Baseline(
|
||||
platform.proxy, platform.llmCalls, platform.llmTokens, nowMillis));
|
||||
log.warn("[cost-guard] 平台自费代理提取告警 次数={} 阈值={} 管理员落库={}",
|
||||
platform.proxy, threshold, pushed);
|
||||
}
|
||||
|
||||
private String platformReason(long count, int threshold) {
|
||||
return "今日平台自费(无归属)代理提取累计 " + count + " 次,超过告警线 " + threshold + " 次";
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户点击继续时调用:把基线对齐到当前值(而不是删掉基线)。删掉会让下一轮走「首轮」
|
||||
* 分支;首轮现在会按日累计判暂停,用户刚点继续就会被立刻再暂停。
|
||||
@@ -148,10 +215,17 @@ public class CostGuardScheduler {
|
||||
}
|
||||
|
||||
public Map<Long, UserTotals> loadTodayTotals() {
|
||||
Map<Long, UserTotals> totals = loadAllTotals();
|
||||
totals.remove(PLATFORM_USER_ID);
|
||||
return totals;
|
||||
}
|
||||
|
||||
/** 含平台自费桶(0 号)的当日聚合;按账号暂停的那条路必须先摘掉它。 */
|
||||
private Map<Long, UserTotals> loadAllTotals() {
|
||||
List<UserSecretUsageAggregate> rows = usageMapper.sumByUserAndModule(LocalDate.now());
|
||||
Map<Long, UserTotals> totals = new LinkedHashMap<>();
|
||||
for (UserSecretUsageAggregate row : rows) {
|
||||
if (row.getUserId() == null || row.getUserId() <= 0) {
|
||||
if (row.getUserId() == null || row.getUserId() < 0) {
|
||||
continue;
|
||||
}
|
||||
accumulate(totals.computeIfAbsent(row.getUserId(), key -> new UserTotals()), row);
|
||||
|
||||
+32
-19
@@ -42,6 +42,13 @@ public class UserSecretUsageService {
|
||||
/** 单次上报上限:防止异常客户端把计数写爆。 */
|
||||
private static final int MAX_REPORT_COUNT = 10_000;
|
||||
|
||||
/**
|
||||
* 平台自费桶:无归属 uid 的代理提取(cookie 生成器、平台兜底池)记到 0 号,
|
||||
* 后台用量统计显示为「平台自费」,损耗扫描只告警不暂停。只允许 proxy 走这个桶。
|
||||
*/
|
||||
public static final long PLATFORM_USER_ID = 0L;
|
||||
static final String PLATFORM_USER_LABEL = "平台自费";
|
||||
|
||||
private final UserSecretUsageMapper usageMapper;
|
||||
private final AdminUserMapper adminUserMapper;
|
||||
private final AdminAuthSupport adminAuthSupport;
|
||||
@@ -66,13 +73,18 @@ public class UserSecretUsageService {
|
||||
|
||||
/** 内部通道上报入口:模块非法返回 false(供接口回执),写库失败只告警。 */
|
||||
public boolean recordReported(Long userId, String moduleKey, int count) {
|
||||
if (userId == null || userId <= 0 || count <= 0) {
|
||||
if (userId == null || userId < 0 || count <= 0) {
|
||||
return false;
|
||||
}
|
||||
UserSecretModule module = UserSecretModule.of(moduleKey).orElse(null);
|
||||
if (module == null) {
|
||||
return false;
|
||||
}
|
||||
// 0 号只收代理:LLM 一定挂在具体账号的密钥上,平台没有"公共 LLM 密钥"这回事。
|
||||
if (userId == PLATFORM_USER_ID && module != UserSecretModule.PROXY) {
|
||||
log.warn("[user-secret-usage] 平台自费桶只收 proxy 计次 userId=0 module={}", moduleKey);
|
||||
return false;
|
||||
}
|
||||
return recordModule(userId, module, Math.min(count, MAX_REPORT_COUNT), 0L, 0L);
|
||||
}
|
||||
|
||||
@@ -270,31 +282,32 @@ public class UserSecretUsageService {
|
||||
private void fillRowMeta(List<AdminUserSecretUsageRowVo> pageItems) {
|
||||
List<Long> userIds = pageItems.stream()
|
||||
.map(AdminUserSecretUsageRowVo::getUserId)
|
||||
.filter(id -> id != null)
|
||||
.filter(id -> id != null && id > PLATFORM_USER_ID)
|
||||
.toList();
|
||||
if (userIds.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
Map<Long, String> usernames = new HashMap<>();
|
||||
for (AdminUserEntity user : adminUserMapper.selectBatchIds(userIds)) {
|
||||
if (user.getId() != null) {
|
||||
usernames.put(user.getId(), normalize(user.getUsername()));
|
||||
}
|
||||
}
|
||||
Map<Long, List<String>> groups = new LinkedHashMap<>();
|
||||
for (UserGroupRef ref : adminGroupMapper.selectGroupNamesByUserIds(userIds)) {
|
||||
if (ref.getUserId() == null) {
|
||||
continue;
|
||||
if (!userIds.isEmpty()) {
|
||||
for (AdminUserEntity user : adminUserMapper.selectBatchIds(userIds)) {
|
||||
if (user.getId() != null) {
|
||||
usernames.put(user.getId(), normalize(user.getUsername()));
|
||||
}
|
||||
}
|
||||
String name = normalize(ref.getGroupName());
|
||||
if (name.isEmpty()) {
|
||||
continue;
|
||||
for (UserGroupRef ref : adminGroupMapper.selectGroupNamesByUserIds(userIds)) {
|
||||
if (ref.getUserId() == null) {
|
||||
continue;
|
||||
}
|
||||
String name = normalize(ref.getGroupName());
|
||||
if (name.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
groups.computeIfAbsent(ref.getUserId(), key -> new ArrayList<>()).add(name);
|
||||
}
|
||||
groups.computeIfAbsent(ref.getUserId(), key -> new ArrayList<>()).add(name);
|
||||
}
|
||||
for (AdminUserSecretUsageRowVo item : pageItems) {
|
||||
item.setUsername(usernames.getOrDefault(item.getUserId(), ""));
|
||||
item.setGroups(groups.getOrDefault(item.getUserId(), List.of()));
|
||||
boolean platform = item.getUserId() != null && item.getUserId() == PLATFORM_USER_ID;
|
||||
item.setUsername(platform ? PLATFORM_USER_LABEL
|
||||
: usernames.getOrDefault(item.getUserId(), ""));
|
||||
item.setGroups(platform ? List.of() : groups.getOrDefault(item.getUserId(), List.of()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+167
-10
@@ -7,12 +7,18 @@ import com.nanri.aiimage.modules.workbenchclient.mapper.WorkbenchClientPackageMa
|
||||
import com.nanri.aiimage.modules.workbenchclient.model.entity.WorkbenchClientPackageEntity;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.math.BigInteger;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
@@ -21,6 +27,8 @@ import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
/**
|
||||
* 数富AI工作台客户端安装/更新包管理。
|
||||
@@ -44,8 +52,30 @@ public class WorkbenchClientPackageService {
|
||||
/** 版本号长度上限,与 biz_workbench_client_package.version VARCHAR(64) 对齐。 */
|
||||
private static final int MAX_VERSION_LENGTH = 64;
|
||||
|
||||
/** 直传预签名有效期:3 分钟足够数百 MB 级安装包上传。 */
|
||||
private static final int PRESIGN_EXPIRY_SECONDS = 180;
|
||||
/** 直传预签名默认有效期:90MB 级包在慢公网入口上传时,3 分钟窗口会在途过期。 */
|
||||
private static final int DEFAULT_PRESIGN_EXPIRY_SECONDS = 3600;
|
||||
|
||||
/**
|
||||
* 安装包内禁止出现的凭据文件基名(大小写不敏感)。
|
||||
*
|
||||
* <p>CR-02:工作台安装包是免登录公开下载的,包内一旦带 {@code kw_share_cfg.json}
|
||||
* 这类文件就等于把 API 口令连同下载链接一起公开。此清单只做发布前硬拒绝,
|
||||
* 真实凭据轮换由工作台客户端仓库完成(本仓不打包它)。</p>
|
||||
*/
|
||||
private static final Set<String> FORBIDDEN_CREDENTIAL_NAMES = Set.of(
|
||||
"kw_share_cfg.json", ".env", ".netrc", ".git-credentials",
|
||||
"id_rsa", "id_dsa", "id_ecdsa", "id_ed25519");
|
||||
|
||||
/** 凭据类后缀:私钥/证书/密钥库,命中任意一个即拒绝。 */
|
||||
private static final List<String> FORBIDDEN_CREDENTIAL_SUFFIXES =
|
||||
List.of(".pem", ".key", ".p12", ".pfx", ".jks", ".keystore");
|
||||
|
||||
/** zip 条目数上限:只读中央目录,但仍要防病态包把 confirm 拖成分钟级。 */
|
||||
private static final int MAX_ZIP_ENTRIES = 200_000;
|
||||
|
||||
/** 允许配置覆盖;上下限防止误配成 0 或过长窗口。 */
|
||||
@Value("${aiimage.workbench-client.presign-expiry-seconds:3600}")
|
||||
private int presignExpirySeconds = DEFAULT_PRESIGN_EXPIRY_SECONDS;
|
||||
|
||||
/** 公开版本列表上限:够客户端做「指定版本更新」下拉即可。 */
|
||||
private static final int PUBLIC_LIST_LIMIT = 50;
|
||||
@@ -118,10 +148,18 @@ public class WorkbenchClientPackageService {
|
||||
return result;
|
||||
}
|
||||
|
||||
private int effectivePresignExpirySeconds() {
|
||||
return Math.max(60, Math.min(presignExpirySeconds, 24 * 3600));
|
||||
}
|
||||
|
||||
/**
|
||||
* 签发直传预签名:对象 key 由「毫秒时间戳 + 随机 UUID + 安全版本号」组成。
|
||||
* UUID 是 CR-08 的修复点:只靠毫秒时间戳时,多实例时钟相同或同毫秒两名管理员
|
||||
* 上传同一版本会生成同一个 key,后一次 PUT 覆盖前一次对象、先确认的 DB 行摘要失效。
|
||||
*
|
||||
* <p>CR-21:返回的上传 URL 视为<b>一次性凭据</b>——任何拿到它的人都能在 TTL 内写对象。
|
||||
* 3600 秒是覆盖慢公网 90MB 级包上传的最小必要窗口,只存在响应体里,
|
||||
* <b>不得写入日志、审计文本或异常信息</b>;确认完成后即失效,不提供续签。</p>
|
||||
*/
|
||||
public Map<String, Object> presignPackage(String fileName, String version) {
|
||||
String normalizedName = requireZipFileName(fileName);
|
||||
@@ -130,9 +168,10 @@ public class WorkbenchClientPackageService {
|
||||
+ OBJECT_NAME_FORMATTER.format(LocalDateTime.now())
|
||||
+ "-" + UUID.randomUUID().toString().replace("-", "")
|
||||
+ "-" + safeVersionKey(normalizedVersion) + ".zip";
|
||||
int expirySeconds = effectivePresignExpirySeconds();
|
||||
log.info("[workbench-client] 签发客户端包直传预签名 version={} fileName={} objectKey={} expirySeconds={}",
|
||||
normalizedVersion, normalizedName, objectKey, PRESIGN_EXPIRY_SECONDS);
|
||||
String uploadUrl = ossStorageService.presignWorkbenchClientUpload(objectKey, PRESIGN_EXPIRY_SECONDS);
|
||||
normalizedVersion, normalizedName, objectKey, expirySeconds);
|
||||
String uploadUrl = ossStorageService.presignWorkbenchClientUpload(objectKey, expirySeconds);
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
result.put("object_key", objectKey);
|
||||
result.put("upload_url", uploadUrl);
|
||||
@@ -145,6 +184,11 @@ public class WorkbenchClientPackageService {
|
||||
/**
|
||||
* 直传确认:校验对象存在、大小上限、版本号与对象名一致,服务端复算 sha256 后落库。
|
||||
* <p>sha256 为空或长度不对直接拒绝——宁可让管理员重传,也不让客户端拿到没有锚点的包。
|
||||
* <p>CR-02:落库前把对象流式落到临时文件、用 {@link ZipFile} 只读扫描条目名;
|
||||
* 包内出现 {@code kw_share_cfg.json}、{@code .env}、{@code *.pem}、{@code *.key}
|
||||
* 等凭据文件即拒绝并清理已上传对象,避免公开桶里出现带口令的安装包。
|
||||
* <p>CR-05:同 objectKey 并发 confirm 幂等返回已登记行;同 version 不同 objectKey
|
||||
* 由数据库唯一键 {@code uk_workbench_client_version} 兜底,冲突时清理对象并返回业务错误。
|
||||
*/
|
||||
public Map<String, Object> confirmPackage(String objectKey, String fileName, String version) {
|
||||
if (objectKey == null || !objectKey.startsWith(OBJECT_KEY_PREFIX)) {
|
||||
@@ -160,10 +204,7 @@ public class WorkbenchClientPackageService {
|
||||
throw new BusinessException("版本号与上传对象不匹配,请重新上传");
|
||||
}
|
||||
// 幂等:重复 confirm 同一对象返回已登记行,不再报错(CR-08)
|
||||
WorkbenchClientPackageEntity existing = packageMapper.selectOne(
|
||||
new LambdaQueryWrapper<WorkbenchClientPackageEntity>()
|
||||
.eq(WorkbenchClientPackageEntity::getObjectKey, objectKey)
|
||||
.last("LIMIT 1"));
|
||||
WorkbenchClientPackageEntity existing = findByObjectKey(objectKey);
|
||||
if (existing != null) {
|
||||
log.info("[workbench-client] 直传确认幂等命中 id={} objectKey={}", existing.getId(), objectKey);
|
||||
return toItemMap(existing);
|
||||
@@ -187,9 +228,11 @@ public class WorkbenchClientPackageService {
|
||||
Long sameVersion = packageMapper.selectCount(new LambdaQueryWrapper<WorkbenchClientPackageEntity>()
|
||||
.eq(WorkbenchClientPackageEntity::getVersion, normalizedVersion));
|
||||
if (sameVersion != null && sameVersion > 0) {
|
||||
// 同版本不同字节会让已升级的装机按版本号比较后不升级,客户端还会拿到错误摘要
|
||||
// 同版本不同字节会让已升级的装机按版本号比较后不升级,客户端还会拿到错误摘要。
|
||||
// 拒绝时必须清理对象:512MB 级残留留在公开桶可被反复触发耗存储(CR-05)。
|
||||
log.warn("[workbench-client] 直传确认失败:版本已存在 version={} objectKey={}",
|
||||
normalizedVersion, objectKey);
|
||||
removeRejectedObject(objectKey);
|
||||
throw new BusinessException("该版本已存在,请提升版本号或先删除旧包");
|
||||
}
|
||||
WorkbenchClientPackageEntity latest = newest();
|
||||
@@ -197,8 +240,11 @@ public class WorkbenchClientPackageService {
|
||||
// 按上传时间取最新时,低版本后传会顶掉当前更新目标,已升级装机反而被判"无需更新"
|
||||
log.warn("[workbench-client] 直传确认失败:版本低于当前最新 version={} latest={} objectKey={}",
|
||||
normalizedVersion, latest.getVersion(), objectKey);
|
||||
removeRejectedObject(objectKey);
|
||||
throw new BusinessException("版本号不能低于当前最新版本 " + latest.getVersion());
|
||||
}
|
||||
// 内容检查排在版本/大小之后:先做便宜判定,避免为大包白下载一遍
|
||||
rejectCredentialFiles(objectKey);
|
||||
String sha256 = ossStorageService.workbenchClientObjectSha256(objectKey);
|
||||
if (sha256 == null || sha256.length() != 64) {
|
||||
log.warn("[workbench-client] 直传确认失败:摘要计算异常 objectKey={} sha256={}", objectKey, sha256);
|
||||
@@ -211,13 +257,124 @@ public class WorkbenchClientPackageService {
|
||||
entity.setObjectKey(objectKey);
|
||||
entity.setFileSize(size);
|
||||
entity.setSha256(sha256);
|
||||
packageMapper.insert(entity);
|
||||
try {
|
||||
packageMapper.insert(entity);
|
||||
} catch (DuplicateKeyException duplicate) {
|
||||
// 同 objectKey 并发 confirm:另一线程已登记,按幂等返回(CR-05)
|
||||
WorkbenchClientPackageEntity concurrent = findByObjectKey(objectKey);
|
||||
if (concurrent != null) {
|
||||
log.info("[workbench-client] 并发直传确认幂等命中 id={} objectKey={}",
|
||||
concurrent.getId(), objectKey);
|
||||
return toItemMap(concurrent);
|
||||
}
|
||||
// 同 version 不同 objectKey:唯一键 uk_workbench_client_version 拦下后一行
|
||||
log.warn("[workbench-client] 直传确认失败:版本并发冲突 version={} objectKey={}",
|
||||
normalizedVersion, objectKey);
|
||||
removeRejectedObject(objectKey);
|
||||
throw new BusinessException("该版本已存在,请提升版本号或先删除旧包");
|
||||
}
|
||||
log.info("[workbench-client] 客户端包登记成功 id={} version={} fileName={} bytes={} sha256={}",
|
||||
entity.getId(), normalizedVersion, normalizedName, size, sha256);
|
||||
WorkbenchClientPackageEntity saved = packageMapper.selectById(entity.getId());
|
||||
return toItemMap(saved == null ? entity : saved);
|
||||
}
|
||||
|
||||
private WorkbenchClientPackageEntity findByObjectKey(String objectKey) {
|
||||
return packageMapper.selectOne(new LambdaQueryWrapper<WorkbenchClientPackageEntity>()
|
||||
.eq(WorkbenchClientPackageEntity::getObjectKey, objectKey)
|
||||
.last("LIMIT 1"));
|
||||
}
|
||||
|
||||
/**
|
||||
* 发布前内容检查:把对象流式落到临时文件,只读扫描 zip 条目名。
|
||||
*
|
||||
* <p>不整包读入内存、不解压内容;{@link ZipFile} 只读中央目录。命中凭据文件或
|
||||
* 非法 zip 时删除已上传对象,确保公开桶里不会留下不可登记的大对象。</p>
|
||||
*/
|
||||
private void rejectCredentialFiles(String objectKey) {
|
||||
Path packageFile = ossStorageService.downloadWorkbenchClientObjectToTempFile(objectKey);
|
||||
try (ZipFile zipFile = new ZipFile(packageFile.toFile())) {
|
||||
Enumeration<? extends ZipEntry> entries = zipFile.entries();
|
||||
int scanned = 0;
|
||||
while (entries.hasMoreElements()) {
|
||||
ZipEntry entry = entries.nextElement();
|
||||
if (++scanned > MAX_ZIP_ENTRIES) {
|
||||
log.warn("[workbench-client] 直传确认拒绝:zip 条目数超限 objectKey={} limit={}",
|
||||
objectKey, MAX_ZIP_ENTRIES);
|
||||
removeRejectedObject(objectKey);
|
||||
throw new BusinessException("安装包内文件条目过多,请重新打包上传");
|
||||
}
|
||||
if (entry.isDirectory()) {
|
||||
continue;
|
||||
}
|
||||
String baseName = baseName(entry.getName());
|
||||
if (isCredentialFileName(baseName)) {
|
||||
log.warn("[workbench-client] 直传确认拒绝:安装包含凭据文件 objectKey={} entry={}",
|
||||
objectKey, entry.getName());
|
||||
removeRejectedObject(objectKey);
|
||||
throw new BusinessException("安装包内含凭据配置文件(" + baseName
|
||||
+ "),禁止发布,请移除后重新打包上传");
|
||||
}
|
||||
}
|
||||
} catch (BusinessException ex) {
|
||||
throw ex;
|
||||
} catch (IOException ex) {
|
||||
log.warn("[workbench-client] 直传确认失败:安装包不是有效 zip objectKey={} err={}",
|
||||
objectKey, ex.getMessage());
|
||||
removeRejectedObject(objectKey);
|
||||
throw new BusinessException("上传的安装包不是有效的 zip 文件,请重新打包上传");
|
||||
} finally {
|
||||
deleteTempQuietly(packageFile);
|
||||
}
|
||||
}
|
||||
|
||||
private static String baseName(String entryName) {
|
||||
if (entryName == null) {
|
||||
return "";
|
||||
}
|
||||
String normalized = entryName.replace('\\', '/');
|
||||
int slash = normalized.lastIndexOf('/');
|
||||
return slash < 0 ? normalized : normalized.substring(slash + 1);
|
||||
}
|
||||
|
||||
private static boolean isCredentialFileName(String fileName) {
|
||||
String lower = fileName == null ? "" : fileName.trim().toLowerCase(Locale.ROOT);
|
||||
if (lower.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
if (FORBIDDEN_CREDENTIAL_NAMES.contains(lower) || lower.startsWith(".env.")) {
|
||||
return true;
|
||||
}
|
||||
for (String suffix : FORBIDDEN_CREDENTIAL_SUFFIXES) {
|
||||
if (lower.endsWith(suffix)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** 拒绝分支统一清对象;清理失败只告警,不让业务错误被存储异常盖掉。 */
|
||||
private void removeRejectedObject(String objectKey) {
|
||||
try {
|
||||
ossStorageService.removeWorkbenchClientObject(objectKey);
|
||||
} catch (RuntimeException ex) {
|
||||
log.warn("[workbench-client] 清理被拒安装包对象失败 objectKey={} err={}",
|
||||
objectKey, ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void deleteTempQuietly(Path packageFile) {
|
||||
if (packageFile == null) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Files.deleteIfExists(packageFile);
|
||||
} catch (IOException ex) {
|
||||
log.warn("[workbench-client] 删除安装包临时文件失败 path={} err={}",
|
||||
packageFile, ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 按 id 批量删除记录;MinIO 对象仅在删除后无任何剩余记录引用时才清除。
|
||||
* <p>最新包被删后,客户端下次检查自动回落到上一版(按上传时间取最新)。
|
||||
|
||||
@@ -14,6 +14,7 @@ AIIMAGE_COST_GUARD_ENABLED=true
|
||||
AIIMAGE_COST_GUARD_PROXY_WINDOW_THRESHOLD=600
|
||||
AIIMAGE_COST_GUARD_LLM_WINDOW_CALLS_THRESHOLD=180
|
||||
AIIMAGE_COST_GUARD_LLM_WINDOW_TOKENS_THRESHOLD=600000
|
||||
AIIMAGE_COST_GUARD_PLATFORM_PROXY_DAILY_THRESHOLD=2000
|
||||
AIIMAGE_COST_GUARD_SHARED_ACCOUNTS_THRESHOLD=3
|
||||
|
||||
AIIMAGE_DB_URL=jdbc:mysql://127.0.0.1:3306/aiimage?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false
|
||||
@@ -29,6 +30,9 @@ AIIMAGE_DIGITAL_HUMAN_OSS_BUCKET=nanri-ai-digital-human
|
||||
AIIMAGE_OSS_ACCESS_KEY_ID=change-me
|
||||
AIIMAGE_OSS_ACCESS_KEY_SECRET=change-me
|
||||
|
||||
# 工作台客户端包直传预签名有效期(秒);90MB 级包在慢公网入口需要更长窗口。
|
||||
AIIMAGE_WORKBENCH_CLIENT_PRESIGN_EXPIRY_SECONDS=3600
|
||||
|
||||
AIIMAGE_TRANSIENT_STORAGE_ENABLED=true
|
||||
AIIMAGE_TRANSIENT_STORAGE_ENDPOINT=http://121.196.149.225:9000
|
||||
AIIMAGE_TRANSIENT_STORAGE_BUCKET=change-me
|
||||
|
||||
@@ -217,6 +217,9 @@ aiimage:
|
||||
heap-ratio-warn: ${AIIMAGE_CAPACITY_PLAN_HEAP_RATIO_WARN:0.70}
|
||||
direct-ratio-warn: ${AIIMAGE_CAPACITY_PLAN_DIRECT_RATIO_WARN:0.15}
|
||||
temp-disk-ratio-warn: ${AIIMAGE_CAPACITY_PLAN_TEMP_DISK_RATIO_WARN:0.50}
|
||||
workbench-client:
|
||||
# 直传预签名有效期:90MB 级安装包在慢公网入口上传时,3 分钟窗口会在途过期。
|
||||
presign-expiry-seconds: ${AIIMAGE_WORKBENCH_CLIENT_PRESIGN_EXPIRY_SECONDS:3600}
|
||||
brand-progress:
|
||||
ttl-hours: ${AIIMAGE_BRAND_PROGRESS_TTL_HOURS:24}
|
||||
failed-ttl-hours: ${AIIMAGE_BRAND_PROGRESS_FAILED_TTL_HOURS:2}
|
||||
@@ -512,6 +515,8 @@ aiimage:
|
||||
llm-window-calls-threshold: ${AIIMAGE_COST_GUARD_LLM_WINDOW_CALLS_THRESHOLD:180}
|
||||
llm-window-tokens-threshold: ${AIIMAGE_COST_GUARD_LLM_WINDOW_TOKENS_THRESHOLD:600000}
|
||||
daily-proxy-threshold: ${AIIMAGE_COST_GUARD_DAILY_PROXY_THRESHOLD:8000}
|
||||
# 无归属购买(平台自费)没有可暂停的账号:只推管理员告警,不暂停。
|
||||
platform-proxy-daily-threshold: ${AIIMAGE_COST_GUARD_PLATFORM_PROXY_DAILY_THRESHOLD:2000}
|
||||
daily-llm-calls-threshold: ${AIIMAGE_COST_GUARD_DAILY_LLM_CALLS_THRESHOLD:6000}
|
||||
daily-llm-tokens-threshold: ${AIIMAGE_COST_GUARD_DAILY_LLM_TOKENS_THRESHOLD:20000000}
|
||||
shared-accounts-threshold: ${AIIMAGE_COST_GUARD_SHARED_ACCOUNTS_THRESHOLD:3}
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
-- =============================================================
|
||||
-- 迁移 V174__restore_admin_brand_gating_menu
|
||||
--
|
||||
-- 1. 变更目的:按 2026-10-25 决定,把「品牌Gating库」恢复为独立后台菜单
|
||||
-- (column_key='admin_brand_gating',route_path='brand-gating'),不再作为
|
||||
-- 「店铺数据重复检查」页的第三个页签。V164 首次落库、V166 回滚删除;已应用迁移
|
||||
-- 一个字节都不许改,故由本迁移重新插入。
|
||||
--
|
||||
-- 2. 口径:沿用 V164 的数据形状——挂在数据管理组 admin_group_data 下、排序 36,
|
||||
-- company_id = 1 为模板行,其余公司(含 status=0 的停用公司)逐家复制一份;
|
||||
-- 子行 parent_id 必须认本公司 admin_group_data 组行,严禁跨公司认父。
|
||||
-- 幂等守卫按 (company_id, column_key) 判存,同时防 (menu_type, route_path)
|
||||
-- 唯一键 uk_company_menu_type_route_path 撞行。
|
||||
--
|
||||
-- 3. 默认授权:本迁移不预授 user_column_permission;管理员角色链可直进,
|
||||
-- 普通账号由管理员在「菜单权限配置」里按公司分配。
|
||||
--
|
||||
-- 4. 影响表与数据量:columns +N 行(N = 公司数),字典小表,毫秒级,不碰锁热点表。
|
||||
--
|
||||
-- 5. 【可重跑性】两段 INSERT 都带 NOT EXISTS 守卫;重跑逐条 no-op。
|
||||
-- =============================================================
|
||||
|
||||
-- ---------- 1. 公司 1 模板行(认公司 1 的 admin_group_data 组行) ----------
|
||||
INSERT INTO `columns` (`name`, `column_key`, `menu_type`, `route_path`, `sort_order`, `parent_id`, `company_id`)
|
||||
SELECT '品牌Gating库', 'admin_brand_gating', 'admin', 'brand-gating', 36, grp.`id`, 1
|
||||
FROM `columns` grp
|
||||
WHERE grp.`company_id` = 1 AND grp.`column_key` = 'admin_group_data'
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM `columns`
|
||||
WHERE `company_id` = 1
|
||||
AND (`column_key` = 'admin_brand_gating'
|
||||
OR (`menu_type` = 'admin' AND `route_path` = 'brand-gating'))
|
||||
);
|
||||
|
||||
-- ---------- 2. 复制到其余公司(含停用公司),各自认本公司组行 ----------
|
||||
INSERT INTO `columns` (`name`, `column_key`, `menu_type`, `route_path`, `sort_order`, `parent_id`, `company_id`)
|
||||
SELECT '品牌Gating库', 'admin_brand_gating', 'admin', 'brand-gating', 36, grp.`id`, c.`id`
|
||||
FROM `company` c
|
||||
JOIN `columns` grp ON grp.`company_id` = c.`id` AND grp.`column_key` = 'admin_group_data'
|
||||
WHERE c.`id` <> 1
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM `columns` col
|
||||
WHERE col.`company_id` = c.`id`
|
||||
AND (col.`column_key` = 'admin_brand_gating'
|
||||
OR (col.`menu_type` = 'admin' AND col.`route_path` = 'brand-gating'))
|
||||
);
|
||||
|
||||
-- ---------- 3. 自检闸门:不为 0 就抛 1048(列 must_be_zero NOT NULL) ----------
|
||||
CREATE TEMPORARY TABLE IF NOT EXISTS `_v174_gate` (`must_be_zero` INT NOT NULL);
|
||||
|
||||
-- 闸门 1:每家公司都必须恰好一行 admin_brand_gating(少一家也算错)
|
||||
INSERT INTO `_v174_gate`
|
||||
SELECT IF(COUNT(*) = 0, 0, NULL)
|
||||
FROM (
|
||||
SELECT c.`id`
|
||||
FROM `company` c
|
||||
LEFT JOIN `columns` col ON col.`company_id` = c.`id` AND col.`column_key` = 'admin_brand_gating'
|
||||
GROUP BY c.`id`
|
||||
HAVING COUNT(col.`id`) <> 1
|
||||
) bad;
|
||||
|
||||
-- 闸门 2:admin/brand-gating 路由不允许被别的 column_key 占行
|
||||
INSERT INTO `_v174_gate`
|
||||
SELECT IF(COUNT(*) = 0, 0, NULL)
|
||||
FROM `columns` col
|
||||
WHERE col.`menu_type` = 'admin'
|
||||
AND col.`route_path` = 'brand-gating'
|
||||
AND col.`column_key` <> 'admin_brand_gating';
|
||||
|
||||
-- 闸门 3:admin_brand_gating 行的父必须是本公司的 admin_group_data 组行
|
||||
INSERT INTO `_v174_gate`
|
||||
SELECT IF(COUNT(*) = 0, 0, NULL)
|
||||
FROM (
|
||||
SELECT col.`id`
|
||||
FROM `columns` col
|
||||
LEFT JOIN `columns` grp ON grp.`id` = col.`parent_id` AND grp.`column_key` = 'admin_group_data'
|
||||
WHERE col.`column_key` = 'admin_brand_gating'
|
||||
AND (grp.`id` IS NULL OR grp.`company_id` <> col.`company_id`)
|
||||
) bad;
|
||||
|
||||
DROP TEMPORARY TABLE `_v174_gate`;
|
||||
@@ -0,0 +1,101 @@
|
||||
-- =============================================================
|
||||
-- 迁移 V175__move_gating_and_add_keyword_usage_menu
|
||||
--
|
||||
-- 1. 变更目的(2026-10-25 控制方截图定案):
|
||||
-- a) 「品牌Gating库」从数据管理组迁到店铺管理组 admin_group_shop,排序 85,
|
||||
-- 紧跟「店铺数据重复检查」(84);
|
||||
-- b) 新增「关键词使用记录」独立菜单 admin_keyword_usage,挂在店铺管理组,排序 86。
|
||||
--
|
||||
-- 2. 口径:
|
||||
-- - 两行都按每家公司一套菜单树维护:company_id=1 为模板行,其余公司(含停用公司)
|
||||
-- 逐家复制;子行 parent_id 必须认本公司 admin_group_shop 组行,严禁跨公司认父。
|
||||
-- - 品牌Gating库行由 V174 恢复,V175 只改 parent_id/sort_order,不重建、不删授权。
|
||||
-- - 关键词使用记录只读页面,默认不预授 user_column_permission;管理员角色链可直进,
|
||||
-- 普通账号由管理员在「菜单权限配置」里按公司分配。
|
||||
-- - 幂等守卫按 (company_id, column_key) 判存,同时防 (menu_type, route_path) 唯一键
|
||||
-- uk_company_menu_type_route_path 撞行。
|
||||
--
|
||||
-- 3. 影响表与数据量:columns 更新 N 行 + 新增 N 行(N = 公司数),字典小表,毫秒级。
|
||||
--
|
||||
-- 4. 【可重跑性】UPDATE 重复执行结果一致;两段 INSERT 都带 NOT EXISTS 守卫,重跑逐条 no-op。
|
||||
-- =============================================================
|
||||
|
||||
-- ---------- 1. 品牌Gating库归位:店铺管理组、排序 85 ----------
|
||||
UPDATE `columns` col
|
||||
JOIN `columns` grp
|
||||
ON grp.`company_id` = col.`company_id`
|
||||
AND grp.`column_key` = 'admin_group_shop'
|
||||
SET col.`parent_id` = grp.`id`,
|
||||
col.`sort_order` = 85
|
||||
WHERE col.`column_key` = 'admin_brand_gating';
|
||||
|
||||
-- ---------- 2. 公司 1 模板行:关键词使用记录 ----------
|
||||
INSERT INTO `columns` (`name`, `column_key`, `menu_type`, `route_path`, `sort_order`, `parent_id`, `company_id`)
|
||||
SELECT '关键词使用记录', 'admin_keyword_usage', 'admin', 'keyword-usage', 86, grp.`id`, 1
|
||||
FROM `columns` grp
|
||||
WHERE grp.`company_id` = 1 AND grp.`column_key` = 'admin_group_shop'
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM `columns`
|
||||
WHERE `company_id` = 1
|
||||
AND (`column_key` = 'admin_keyword_usage'
|
||||
OR (`menu_type` = 'admin' AND `route_path` = 'keyword-usage'))
|
||||
);
|
||||
|
||||
-- ---------- 3. 复制到其余公司(含停用公司),各自认本公司店铺管理组行 ----------
|
||||
INSERT INTO `columns` (`name`, `column_key`, `menu_type`, `route_path`, `sort_order`, `parent_id`, `company_id`)
|
||||
SELECT '关键词使用记录', 'admin_keyword_usage', 'admin', 'keyword-usage', 86, grp.`id`, c.`id`
|
||||
FROM `company` c
|
||||
JOIN `columns` grp ON grp.`company_id` = c.`id` AND grp.`column_key` = 'admin_group_shop'
|
||||
WHERE c.`id` <> 1
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM `columns` col
|
||||
WHERE col.`company_id` = c.`id`
|
||||
AND (col.`column_key` = 'admin_keyword_usage'
|
||||
OR (col.`menu_type` = 'admin' AND col.`route_path` = 'keyword-usage'))
|
||||
);
|
||||
|
||||
-- ---------- 4. 自检闸门:不为 0 就抛 1048(列 must_be_zero NOT NULL) ----------
|
||||
CREATE TEMPORARY TABLE IF NOT EXISTS `_v175_gate` (`must_be_zero` INT NOT NULL);
|
||||
|
||||
-- 闸门 1:每家公司恰好一行品牌Gating库,且父必须是本公司店铺管理组、排序 85
|
||||
INSERT INTO `_v175_gate`
|
||||
SELECT IF(COUNT(*) = 0, 0, NULL)
|
||||
FROM (
|
||||
SELECT c.`id`
|
||||
FROM `company` c
|
||||
LEFT JOIN `columns` col ON col.`company_id` = c.`id` AND col.`column_key` = 'admin_brand_gating'
|
||||
LEFT JOIN `columns` grp ON grp.`id` = col.`parent_id`
|
||||
AND grp.`company_id` = c.`id` AND grp.`column_key` = 'admin_group_shop'
|
||||
GROUP BY c.`id`
|
||||
HAVING COUNT(col.`id`) <> 1 OR COUNT(grp.`id`) <> 1 OR MAX(col.`sort_order`) <> 85
|
||||
) bad;
|
||||
|
||||
-- 闸门 2:每家公司恰好一行关键词使用记录,且父必须是本公司店铺管理组、排序 86
|
||||
INSERT INTO `_v175_gate`
|
||||
SELECT IF(COUNT(*) = 0, 0, NULL)
|
||||
FROM (
|
||||
SELECT c.`id`
|
||||
FROM `company` c
|
||||
LEFT JOIN `columns` col ON col.`company_id` = c.`id` AND col.`column_key` = 'admin_keyword_usage'
|
||||
LEFT JOIN `columns` grp ON grp.`id` = col.`parent_id`
|
||||
AND grp.`company_id` = c.`id` AND grp.`column_key` = 'admin_group_shop'
|
||||
GROUP BY c.`id`
|
||||
HAVING COUNT(col.`id`) <> 1 OR COUNT(grp.`id`) <> 1 OR MAX(col.`sort_order`) <> 86
|
||||
) bad;
|
||||
|
||||
-- 闸门 3:两条路由不允许被别的 column_key 占行
|
||||
INSERT INTO `_v175_gate`
|
||||
SELECT IF(COUNT(*) = 0, 0, NULL)
|
||||
FROM `columns` col
|
||||
WHERE col.`menu_type` = 'admin'
|
||||
AND col.`route_path` IN ('brand-gating', 'keyword-usage')
|
||||
AND col.`column_key` NOT IN ('admin_brand_gating', 'admin_keyword_usage');
|
||||
|
||||
-- 闸门 4:两个 key 的 route_path 必须各自保持原值(防止历史行被别的迁移改名)
|
||||
INSERT INTO `_v175_gate`
|
||||
SELECT IF(COUNT(*) = 0, 0, NULL)
|
||||
FROM `columns` col
|
||||
WHERE (col.`column_key` = 'admin_brand_gating' AND col.`route_path` <> 'brand-gating')
|
||||
OR (col.`column_key` = 'admin_keyword_usage' AND col.`route_path` <> 'keyword-usage');
|
||||
|
||||
DROP TEMPORARY TABLE `_v175_gate`;
|
||||
@@ -0,0 +1,23 @@
|
||||
-- =============================================================
|
||||
-- 迁移 V176__workbench_client_package_version_unique
|
||||
-- 立账:ops 仓 docs/audit/CODE_REVIEW_REPORT.md CR-05(2026-10-26)
|
||||
--
|
||||
-- 1. 变更目的:给「数富AI工作台客户端包」的版本号加唯一约束。
|
||||
-- confirmPackage 原实现是 SELECT COUNT(version) 再 INSERT,两步之间无数据库
|
||||
-- 约束保护:两名管理员并发确认同一版本、不同 objectKey 时,两次 count 都读到 0,
|
||||
-- 最终落两行同 version、不同 sha256 的记录,客户端会拿到错误的完整性锚点。
|
||||
--
|
||||
-- 2. 口径:
|
||||
-- - 唯一键名 uk_workbench_client_version,列 version;
|
||||
-- - objectKey 已有 uk_workbench_client_object_key(V167),两把唯一键各守一条
|
||||
-- 并发路径:同对象幂等、同版本拒绝;
|
||||
-- - 线上只读核验(2026-10-26):当前 7 行、7 个不同 version,无重复,可安全加键。
|
||||
--
|
||||
-- 3. 影响表与数据量:biz_workbench_client_package 加一把唯一索引;表为 KB 级字典表。
|
||||
--
|
||||
-- 4. 回滚(如需):ALTER TABLE `biz_workbench_client_package`
|
||||
-- DROP INDEX `uk_workbench_client_version`;
|
||||
-- =============================================================
|
||||
|
||||
ALTER TABLE `biz_workbench_client_package`
|
||||
ADD UNIQUE KEY `uk_workbench_client_version` (`version`);
|
||||
@@ -0,0 +1,79 @@
|
||||
-- =============================================================
|
||||
-- 迁移 V177__keyword_usage_tables
|
||||
-- 立账:ops 仓 docs/audit/CODE_REVIEW_REPORT.md CR-18(2026-10-26)
|
||||
--
|
||||
-- 1. 变更目的:给「关键词使用记录」依赖的四张表补 Flyway DDL。
|
||||
-- keyword_usage / keyword_add_log / keyword_delete_log / keyword_share 在现网主库
|
||||
-- 早已存在(桌面 Tkinter 工具与 web 版共用),此前没有入库的建表脚本;新环境首次
|
||||
-- 打开管理后台会因表缺失直接 500。本迁移只服务新环境,线上四表已存在。
|
||||
--
|
||||
-- 2. 口径:
|
||||
-- - 一律 CREATE TABLE IF NOT EXISTS,线上重跑逐条 no-op;
|
||||
-- - 字段按 KeywordUsageAdminMapper.xml / KeywordShareMapper.xml 与
|
||||
-- KeywordUsageEntity / KeywordAddLogEntity / KeywordDeleteLogEntity /
|
||||
-- KeywordShareEntity 逐列核对,不臆造业务列;
|
||||
-- - keyword_share 与 keyword_usage 的 uk_comp_cw(company_id, country, keyword)
|
||||
-- 是既有 UPSERT(ON DUPLICATE KEY UPDATE)与删除/标记语义的依赖键,必须一并建;
|
||||
-- - keyword_delete_log 只留痕,不建唯一键(同一词可被反复添加/删除)。
|
||||
--
|
||||
-- 3. 影响表与数据量:新环境建 4 张空表;已有环境 4 条 no-op。
|
||||
--
|
||||
-- 4. 回滚(如需,按依赖顺序):
|
||||
-- DROP TABLE IF EXISTS `keyword_usage`;
|
||||
-- DROP TABLE IF EXISTS `keyword_add_log`;
|
||||
-- DROP TABLE IF EXISTS `keyword_delete_log`;
|
||||
-- DROP TABLE IF EXISTS `keyword_share`;
|
||||
-- =============================================================
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `keyword_share` (
|
||||
`id` BIGINT NOT NULL AUTO_INCREMENT,
|
||||
`country` VARCHAR(32) NOT NULL COMMENT '国家(英国/德国/法国/西班牙/意大利等)',
|
||||
`keyword` VARCHAR(255) NOT NULL COMMENT '关键词',
|
||||
`chinese` VARCHAR(255) DEFAULT NULL COMMENT '中文释义',
|
||||
`ktype` VARCHAR(64) DEFAULT NULL COMMENT '关键词类型',
|
||||
`source` VARCHAR(64) DEFAULT NULL COMMENT '来源',
|
||||
`category` VARCHAR(128) DEFAULT NULL COMMENT '一级类目',
|
||||
`sub_category` VARCHAR(128) DEFAULT NULL COMMENT '二级类目',
|
||||
`category_l3` VARCHAR(128) DEFAULT NULL COMMENT '三级类目',
|
||||
`category_l4` VARCHAR(128) DEFAULT NULL COMMENT '四级类目',
|
||||
`user_name` VARCHAR(128) NOT NULL DEFAULT '' COMMENT '添加人',
|
||||
`add_time` DATETIME DEFAULT NULL COMMENT '添加时间',
|
||||
`company_id` BIGINT NOT NULL DEFAULT 1 COMMENT '公司隔离键',
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `uk_comp_cw` (`company_id`, `country`, `keyword`),
|
||||
KEY `idx_keyword_share_company_category` (`company_id`, `category`, `sub_category`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='五国关键词共享-词本体';
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `keyword_usage` (
|
||||
`id` BIGINT NOT NULL AUTO_INCREMENT,
|
||||
`country` VARCHAR(32) NOT NULL COMMENT '国家',
|
||||
`keyword` VARCHAR(255) NOT NULL COMMENT '关键词',
|
||||
`user_name` VARCHAR(128) NOT NULL DEFAULT '' COMMENT '使用者',
|
||||
`used_time` DATETIME DEFAULT NULL COMMENT '最近使用时间',
|
||||
`company_id` BIGINT NOT NULL DEFAULT 1 COMMENT '公司隔离键',
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `uk_comp_cw` (`company_id`, `country`, `keyword`),
|
||||
KEY `idx_keyword_usage_company_time` (`company_id`, `used_time`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='五国关键词共享-使用状态';
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `keyword_add_log` (
|
||||
`id` BIGINT NOT NULL AUTO_INCREMENT,
|
||||
`country` VARCHAR(32) NOT NULL COMMENT '国家',
|
||||
`keyword` VARCHAR(255) NOT NULL COMMENT '关键词',
|
||||
`user_name` VARCHAR(128) NOT NULL DEFAULT '' COMMENT '添加人',
|
||||
`add_time` DATETIME DEFAULT NULL COMMENT '添加时间',
|
||||
`company_id` BIGINT NOT NULL DEFAULT 1 COMMENT '公司隔离键',
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_keyword_add_log_company_time` (`company_id`, `add_time`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='五国关键词共享-添加留痕';
|
||||
|
||||
CREATE TABLE IF NOT EXISTS `keyword_delete_log` (
|
||||
`id` BIGINT NOT NULL AUTO_INCREMENT,
|
||||
`country` VARCHAR(32) NOT NULL COMMENT '国家',
|
||||
`keyword` VARCHAR(255) NOT NULL COMMENT '关键词',
|
||||
`user_name` VARCHAR(128) NOT NULL DEFAULT '' COMMENT '删除人',
|
||||
`delete_time` DATETIME DEFAULT NULL COMMENT '删除时间',
|
||||
`company_id` BIGINT NOT NULL DEFAULT 1 COMMENT '公司隔离键',
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `idx_keyword_delete_log_company_time` (`company_id`, `delete_time`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='五国关键词共享-删除留痕';
|
||||
@@ -0,0 +1,158 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.nanri.aiimage.modules.keywordusage.mapper.KeywordUsageAdminMapper">
|
||||
<!-- 关键词使用记录(管理后台只读视图)。四张表由桌面端/keywordshare 模块维护,这里只查不写;
|
||||
筛选值一律 #{} 占位符,LIKE 由 CONCAT 拼通配符,不拼接用户输入。 -->
|
||||
|
||||
<sql id="usageConds">
|
||||
<if test="f.companyId != null">AND k.company_id = #{f.companyId}</if>
|
||||
<if test="f.user != null and f.user != ''">AND k.user_name = #{f.user}</if>
|
||||
<if test="f.country != null and f.country != ''">AND k.country = #{f.country}</if>
|
||||
<if test="f.keyword != null and f.keyword != ''">
|
||||
AND k.keyword LIKE CONCAT('%', #{f.keyword}, '%')
|
||||
</if>
|
||||
</sql>
|
||||
|
||||
<sql id="addConds">
|
||||
<if test="f.companyId != null">AND a.company_id = #{f.companyId}</if>
|
||||
<if test="f.user != null and f.user != ''">AND a.user_name = #{f.user}</if>
|
||||
<if test="f.country != null and f.country != ''">AND a.country = #{f.country}</if>
|
||||
<if test="f.keyword != null and f.keyword != ''">
|
||||
AND a.keyword LIKE CONCAT('%', #{f.keyword}, '%')
|
||||
</if>
|
||||
</sql>
|
||||
|
||||
<sql id="deleteConds">
|
||||
<if test="f.companyId != null">AND d.company_id = #{f.companyId}</if>
|
||||
<if test="f.user != null and f.user != ''">AND d.user_name = #{f.user}</if>
|
||||
<if test="f.country != null and f.country != ''">AND d.country = #{f.country}</if>
|
||||
<if test="f.keyword != null and f.keyword != ''">
|
||||
AND d.keyword LIKE CONCAT('%', #{f.keyword}, '%')
|
||||
</if>
|
||||
</sql>
|
||||
|
||||
<select id="countUsageRows" resultType="long">
|
||||
SELECT
|
||||
<choose>
|
||||
<when test="f.includeCopy and f.includeAdd">
|
||||
(SELECT COUNT(*) FROM keyword_usage k WHERE 1=1 <include refid="usageConds"/>)
|
||||
+ (SELECT COUNT(*) FROM keyword_add_log a WHERE 1=1 <include refid="addConds"/>)
|
||||
</when>
|
||||
<when test="f.includeCopy">
|
||||
(SELECT COUNT(*) FROM keyword_usage k WHERE 1=1 <include refid="usageConds"/>)
|
||||
</when>
|
||||
<when test="f.includeAdd">
|
||||
(SELECT COUNT(*) FROM keyword_add_log a WHERE 1=1 <include refid="addConds"/>)
|
||||
</when>
|
||||
<otherwise>0</otherwise>
|
||||
</choose>
|
||||
</select>
|
||||
|
||||
<select id="selectUsageRows" resultType="com.nanri.aiimage.modules.keywordusage.model.vo.KeywordUsageRowVo">
|
||||
SELECT x.user_name AS userName,
|
||||
x.country AS country,
|
||||
x.keyword AS keyword,
|
||||
x.scene AS scene,
|
||||
x.op_time AS `time`
|
||||
FROM (
|
||||
<if test="f.includeCopy">
|
||||
SELECT k.user_name, k.country, k.keyword, '复制关键词' AS scene,
|
||||
k.used_time AS op_time, k.id AS sid
|
||||
FROM keyword_usage k
|
||||
WHERE 1=1 <include refid="usageConds"/>
|
||||
</if>
|
||||
<if test="f.includeCopy and f.includeAdd">UNION ALL</if>
|
||||
<if test="f.includeAdd">
|
||||
SELECT a.user_name, a.country, a.keyword, '添加关键词' AS scene,
|
||||
a.add_time AS op_time, a.id AS sid
|
||||
FROM keyword_add_log a
|
||||
WHERE 1=1 <include refid="addConds"/>
|
||||
</if>
|
||||
) x
|
||||
ORDER BY x.op_time DESC, x.sid DESC
|
||||
LIMIT #{f.offset}, #{f.limit}
|
||||
</select>
|
||||
|
||||
<select id="countCopy" resultType="long">
|
||||
SELECT COUNT(*) FROM keyword_usage k WHERE 1=1 <include refid="usageConds"/>
|
||||
</select>
|
||||
|
||||
<select id="countCopyExisting" resultType="long">
|
||||
SELECT COUNT(*)
|
||||
FROM keyword_usage k
|
||||
INNER JOIN keyword_share s
|
||||
ON s.company_id = k.company_id
|
||||
AND s.country = k.country
|
||||
AND s.keyword = k.keyword
|
||||
WHERE 1=1 <include refid="usageConds"/>
|
||||
</select>
|
||||
|
||||
<select id="countAdd" resultType="long">
|
||||
SELECT COUNT(*) FROM keyword_add_log a WHERE 1=1 <include refid="addConds"/>
|
||||
</select>
|
||||
|
||||
<select id="countDeletes" resultType="long">
|
||||
SELECT COUNT(*) FROM keyword_delete_log d WHERE 1=1 <include refid="deleteConds"/>
|
||||
</select>
|
||||
|
||||
<select id="selectDeleteRows" resultType="com.nanri.aiimage.modules.keywordusage.model.vo.KeywordUsageRowVo">
|
||||
SELECT d.user_name AS userName,
|
||||
d.country AS country,
|
||||
d.keyword AS keyword,
|
||||
'删除关键词' AS scene,
|
||||
d.delete_time AS `time`
|
||||
FROM keyword_delete_log d
|
||||
WHERE 1=1 <include refid="deleteConds"/>
|
||||
ORDER BY d.delete_time DESC, d.id DESC
|
||||
LIMIT #{f.offset}, #{f.limit}
|
||||
</select>
|
||||
|
||||
<select id="selectStats" resultType="com.nanri.aiimage.modules.keywordusage.model.vo.KeywordUsageStatsVo">
|
||||
SELECT
|
||||
(SELECT COUNT(*) FROM keyword_usage k
|
||||
WHERE 1=1
|
||||
<if test="companyId != null">AND k.company_id = #{companyId}</if>) AS used,
|
||||
(SELECT COUNT(*)
|
||||
FROM keyword_usage k
|
||||
INNER JOIN keyword_share s
|
||||
ON s.company_id = k.company_id
|
||||
AND s.country = k.country
|
||||
AND s.keyword = k.keyword
|
||||
WHERE 1=1
|
||||
<if test="companyId != null">AND k.company_id = #{companyId}</if>) AS exist,
|
||||
(SELECT COUNT(*) FROM keyword_add_log a
|
||||
WHERE 1=1
|
||||
<if test="companyId != null">AND a.company_id = #{companyId}</if>) AS added,
|
||||
(SELECT COUNT(*) FROM keyword_delete_log d
|
||||
WHERE 1=1
|
||||
<if test="companyId != null">AND d.company_id = #{companyId}</if>) AS deletes
|
||||
</select>
|
||||
|
||||
<select id="selectUsers" resultType="string">
|
||||
SELECT DISTINCT user_name
|
||||
FROM (
|
||||
SELECT user_name, company_id FROM keyword_usage
|
||||
UNION
|
||||
SELECT user_name, company_id FROM keyword_add_log
|
||||
UNION
|
||||
SELECT user_name, company_id FROM keyword_delete_log
|
||||
) t
|
||||
WHERE user_name IS NOT NULL AND user_name != ''
|
||||
<if test="companyId != null">AND company_id = #{companyId}</if>
|
||||
ORDER BY user_name
|
||||
</select>
|
||||
|
||||
<select id="selectCountries" resultType="string">
|
||||
SELECT DISTINCT country
|
||||
FROM (
|
||||
SELECT country, company_id FROM keyword_usage
|
||||
UNION
|
||||
SELECT country, company_id FROM keyword_add_log
|
||||
UNION
|
||||
SELECT country, company_id FROM keyword_delete_log
|
||||
) t
|
||||
WHERE country IS NOT NULL AND country != ''
|
||||
<if test="companyId != null">AND company_id = #{companyId}</if>
|
||||
ORDER BY country
|
||||
</select>
|
||||
</mapper>
|
||||
@@ -63,7 +63,9 @@ class MigratedSqlEquivalenceTest {
|
||||
// (SqlLayeringConventionTest.XML_STATEMENT_TOTAL 同轮 124 → 126)。
|
||||
// 88 → 89:B28 新增 TaskFileJobMapper#selectFileTaskStatus(按主键读父任务状态),逐条点入
|
||||
// (SqlLayeringConventionTest.XML_STATEMENT_TOTAL 同轮 128 → 129)。
|
||||
private static final int XML_SELECT_TOTAL = 89;
|
||||
// 89 → 99:关键词使用记录管理端(keywordusage 模块)新写 10 条只读 select,逐条点入
|
||||
// (SqlLayeringConventionTest.XML_STATEMENT_TOTAL 同轮 130 → 140)。
|
||||
private static final int XML_SELECT_TOTAL = 99;
|
||||
|
||||
/**
|
||||
* 不是「从注解搬过来」的语句:注解时代没有原文可对撞,等价性这支只能跳过它们。
|
||||
@@ -103,7 +105,18 @@ class MigratedSqlEquivalenceTest {
|
||||
// B28(2026-10-25)新写的 1 条主键点查:父任务是否已终态。
|
||||
"TaskFileJobMapper#selectFileTaskStatus",
|
||||
// CR-01(2026-10-25)新写的 1 条写侧语句:会话世代原子自增。
|
||||
"LoginUserMapper#incrementSessionEpoch");
|
||||
"LoginUserMapper#incrementSessionEpoch",
|
||||
// 关键词使用记录管理端(2026-10-25)新写的 10 条只读 select。
|
||||
"KeywordUsageAdminMapper#countUsageRows",
|
||||
"KeywordUsageAdminMapper#selectUsageRows",
|
||||
"KeywordUsageAdminMapper#countCopy",
|
||||
"KeywordUsageAdminMapper#countCopyExisting",
|
||||
"KeywordUsageAdminMapper#countAdd",
|
||||
"KeywordUsageAdminMapper#countDeletes",
|
||||
"KeywordUsageAdminMapper#selectDeleteRows",
|
||||
"KeywordUsageAdminMapper#selectStats",
|
||||
"KeywordUsageAdminMapper#selectUsers",
|
||||
"KeywordUsageAdminMapper#selectCountries");
|
||||
|
||||
/** 账本 5 列:接口 / 方法 / @Select 种类 / 是否含常量插值 / 注解原文(Java 反转义后那条串)。 */
|
||||
private static final int LEDGER_COLUMNS = 5;
|
||||
|
||||
@@ -82,7 +82,8 @@ class SqlLayeringConventionTest {
|
||||
// 新写语句按约定直接进 XML,同轮登记进 MigratedSqlEquivalenceTest.NOT_MIGRATED(88 → 89)。
|
||||
// 129 → 130:CR-01 新增 {@code LoginUserMapper#incrementSessionEpoch}(会话世代原子自增),
|
||||
// 新写语句直接进 mapper/auth/LoginUserMapper.xml,同轮登记进 NOT_MIGRATED。
|
||||
private static final int XML_STATEMENT_TOTAL = 130;
|
||||
// 130 → 140:关键词使用记录管理端(keywordusage 模块,1 份 XML 10 条只读 select)新写语句。
|
||||
private static final int XML_STATEMENT_TOTAL = 140;
|
||||
|
||||
/**
|
||||
* wrapper 里夹带函数级 SQL 的现状("这不是简单单表"的证据),文件名 → 条数:
|
||||
|
||||
@@ -49,8 +49,9 @@ class CatchSilenceRatchetTest {
|
||||
// 10-18 外观专利失败兜底自吞抬 561 -> 562,逐处出处同下。
|
||||
// 10-24 损耗监控三处带锚点的 fail-open 抬 562 -> 564,逐处出处见 STRICT_NOT_RETHROW 上方注释。
|
||||
// 10-25 B15 join 无界收口抬 564 -> 569,逐处出处见 STRICT_NOT_RETHROW 上方注释。
|
||||
// 10-26 第二轮 CR-Fix 2 抬 569 -> 573,逐处出处见 STRICT_NOT_RETHROW 上方注释(4 处必要 fail-open)。
|
||||
static final int GIVEN_TOTAL = 1000;
|
||||
static final int GIVEN_NOT_RETHROW = 569;
|
||||
static final int GIVEN_NOT_RETHROW = 573;
|
||||
static final int GIVEN_LOG_ONLY = 249;
|
||||
static final int GIVEN_EMPTY = 56;
|
||||
|
||||
@@ -120,8 +121,28 @@ class CatchSilenceRatchetTest {
|
||||
* 逐行链路的缓存,缺结果的行会走单行回退,所以等待侧超时不需要炸掉整条任务。</li>
|
||||
* </ul>
|
||||
* 同批实测 notRethrow 569、logOnly 176、empty 56;气味三格只按这三处实测值抬,形状未放宽。
|
||||
*
|
||||
* <p>10-26 抬 1015 -> 1024(第二轮 CR-Fix 2,共 9 处新增 catch,逐处记名):
|
||||
* <ul>
|
||||
* <li>{@code WorkbenchClientPackageService.confirmPackage} 一枚:{@code DuplicateKeyException}
|
||||
* 并发冲突按 objectKey 幂等或转业务错误(RETHROW);</li>
|
||||
* <li>{@code WorkbenchClientPackageService.rejectCredentialFiles} 两枚:{@code BusinessException}
|
||||
* 原样重抛、{@code IOException} 清对象后转业务错误(RETHROW);</li>
|
||||
* <li>{@code WorkbenchClientPackageService.removeRejectedObject} 一枚:{@code RuntimeException}
|
||||
* 清理失败只 WARN,不让存储异常盖掉业务错误(LOG_ONLY);</li>
|
||||
* <li>{@code WorkbenchClientPackageService.deleteTempQuietly} 一枚:{@code IOException}
|
||||
* 删临时文件失败只 WARN(LOG_ONLY);</li>
|
||||
* <li>{@code OssStorageService.downloadWorkbenchClientObjectToTempFile} 两枚:外层
|
||||
* {@code Exception} 转 {@code storageFailure} 重抛(RETHROW),内层清理失败只 WARN(LOG_ONLY);</li>
|
||||
* <li>{@code CostGuardScheduler} 一枚:平台自费告警推送失败只 WARN 并保留基线待下轮重试
|
||||
* (NOT_RETHROW,log+return);</li>
|
||||
* <li>{@code SimilarAsinLlmService} 一枚:{@code InterruptedException} 恢复中断位后上抛
|
||||
* (RETHROW)。</li>
|
||||
* </ul>
|
||||
* 9 处均为 CR-02/05/10/20 的必要出口,不是把已有异常改回吞掉;同批实测 total 1024、
|
||||
* notRethrow 573、logOnly 179、empty 56。
|
||||
*/
|
||||
static final int STRICT_TOTAL = 1015;
|
||||
static final int STRICT_TOTAL = 1024;
|
||||
/**
|
||||
* 重抛型 catch 的**下限**(不是上限):本轮把两处吞异常改成重抛之后实测 388。
|
||||
* 这一格只能涨不能跌——它是"异常会往外走"的总量,把它钉成"只许减"就等于拦着别人修吞异常。
|
||||
@@ -207,8 +228,14 @@ class CatchSilenceRatchetTest {
|
||||
* {@code AppearancePatentLlmClient.runBatches} 两枚(超时/执行失败、中断各收口成该批失败),
|
||||
* {@code SimilarAsinLlmService} 类目与合规两个 prepass 各两枚(超时/执行失败只 WARN,
|
||||
* 中断复位中断位后退出等待)。两处预计算的缺行都会走单行回退,不是把已有出口改回吞掉。
|
||||
* 10-26 抬 569 -> 573(第二轮 CR-Fix 2,4 处新增不重抛,逐处记名):
|
||||
* {@code WorkbenchClientPackageService.removeRejectedObject}(清理失败只 WARN,不让存储异常
|
||||
* 盖掉业务错误)、{@code deleteTempQuietly}(删临时文件失败只 WARN)、
|
||||
* {@code OssStorageService.downloadWorkbenchClientObjectToTempFile} 内层清理(同上)、
|
||||
* {@code CostGuardScheduler}(平台自费告警推送失败 log+return,保留基线待下轮重试)。
|
||||
* 四处的误差方向都是「不让清理/告警失败掩盖主错误」,不是把异常静默吞掉。
|
||||
*/
|
||||
static final int STRICT_NOT_RETHROW = 569;
|
||||
static final int STRICT_NOT_RETHROW = 573;
|
||||
/**
|
||||
* 10-17 抬 172 -> 173,出处就是上面点名过的 {@code TaskHeartbeatLivenessService.stamp()}:
|
||||
* 块体只有「取到 key 名 + log.warn」一句,写心跳证据失败时既不重试也不抛出——
|
||||
@@ -221,8 +248,11 @@ class CatchSilenceRatchetTest {
|
||||
* 两个超时出口之一(两处形状相同,其中一处按块内语句判定落进本格,另一处因复位中断位
|
||||
* 多一条语句只进 NOT_RETHROW)。块体只有一句带 {@code [similar-asin][llm]} 锚点的 WARN,
|
||||
* 缺结果的行走单行回退,不是无声吞掉。
|
||||
* 10-26 抬 176 -> 179(第二轮 CR-Fix 2,3 处新增 log-only):{@code WorkbenchClientPackageService}
|
||||
* 的 removeRejectedObject/deleteTempQuietly 与 {@code OssStorageService} 的临时文件清理内层,
|
||||
* 三处都只 WARN 且带 path/objectKey 锚点;主错误由外层重抛,清理失败不得盖掉它。
|
||||
*/
|
||||
static final int STRICT_LOG_ONLY = 176; // 严格口径:块内每条顶层语句都是 log 调用才算
|
||||
static final int STRICT_LOG_ONLY = 179; // 严格口径:块内每条顶层语句都是 log 调用才算
|
||||
static final int STRICT_EMPTY = 56;
|
||||
|
||||
/**
|
||||
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
package com.nanri.aiimage.config;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/** CR-18:V177 给关键词使用记录四张表补新环境可用的 Flyway DDL。 */
|
||||
class KeywordUsageTablesMigrationV177Test {
|
||||
|
||||
private static final Path DB_DIR =
|
||||
Path.of(System.getProperty("user.dir"), "src/main/resources/db");
|
||||
|
||||
private static Path migration() throws IOException {
|
||||
try (Stream<Path> files = Files.list(DB_DIR)) {
|
||||
List<Path> hits = files
|
||||
.filter(p -> p.getFileName().toString().startsWith("V177__"))
|
||||
.toList();
|
||||
assertEquals(1, hits.size(), "V177 必须恰好一个迁移文件");
|
||||
return hits.get(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void createsAllFourKeywordTablesIdempotently() throws IOException {
|
||||
String sql = Files.readString(migration());
|
||||
assertTrue(sql.contains("迁移 V177__keyword_usage_tables"), "头注释版本必须与文件名一致");
|
||||
for (String table : List.of("keyword_usage", "keyword_add_log", "keyword_delete_log", "keyword_share")) {
|
||||
assertTrue(sql.contains("CREATE TABLE IF NOT EXISTS `" + table + "`"),
|
||||
"缺少新环境建表(必须 IF NOT EXISTS,线上已存在): " + table);
|
||||
}
|
||||
assertTrue(sql.contains("UNIQUE KEY `uk_comp_cw` (`company_id`, `country`, `keyword`)"),
|
||||
"keyword_share/keyword_usage 的 UPSERT 依赖 (company_id,country,keyword) 唯一键");
|
||||
assertTrue(sql.contains("`company_id` BIGINT NOT NULL DEFAULT 1"),
|
||||
"四张表都必须带公司隔离键");
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -103,7 +103,8 @@ class TaskResultFileJobWorkerLogAuditTest {
|
||||
// 2026-10-24 B28:父任务终态后不再领取/执行/续期,三条出口各有一个阶段字面量。
|
||||
List<String> allowed = List.of("SKIP_OWNER", "REQUEUE", "WAIT_LLM", "DEFER", "SUCCESS",
|
||||
"SUCCESS_LOST", "ORPHAN", "FAILED", "FINALIZE", "HEARTBEAT", "FALLBACK",
|
||||
"TERMINAL_SKIP", "TERMINAL_AFTER_LOCK", "TERMINAL_STOP_RENEW");
|
||||
"TERMINAL_SKIP", "TERMINAL_AFTER_LOCK", "TERMINAL_STOP_RENEW",
|
||||
"TERMINAL_KEEP_SUCCESS", "TERMINAL_AFTER_EXECUTION");
|
||||
for (String l : perJob) {
|
||||
if (l.contains("stage=")) {
|
||||
int idx = l.indexOf("stage=");
|
||||
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.nanri.aiimage.config;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/** CR-05:V176 用数据库唯一键兜住「同 version 不同 objectKey」的并发登记。 */
|
||||
class WorkbenchClientPackageVersionUniqueMigrationV176Test {
|
||||
|
||||
private static final Path DB_DIR =
|
||||
Path.of(System.getProperty("user.dir"), "src/main/resources/db");
|
||||
|
||||
private static Path migration() throws IOException {
|
||||
try (Stream<Path> files = Files.list(DB_DIR)) {
|
||||
List<Path> hits = files
|
||||
.filter(p -> p.getFileName().toString().startsWith("V176__"))
|
||||
.toList();
|
||||
assertEquals(1, hits.size(), "V176 必须恰好一个迁移文件");
|
||||
return hits.get(0);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void addsUniqueKeyOnVersionWithoutTouchingAppliedMigrations() throws IOException {
|
||||
String sql = Files.readString(migration());
|
||||
assertTrue(sql.contains("迁移 V176__workbench_client_package_version_unique"),
|
||||
"头注释版本必须与文件名一致");
|
||||
assertTrue(sql.contains("ALTER TABLE `biz_workbench_client_package`"),
|
||||
"只在客户端包表上加键");
|
||||
assertTrue(sql.contains("ADD UNIQUE KEY `uk_workbench_client_version` (`version`)"),
|
||||
"同版本并发登记必须由数据库唯一键兜底");
|
||||
}
|
||||
}
|
||||
+30
@@ -78,6 +78,36 @@ class AppearancePatentBrandBatchTest {
|
||||
assertThat(merged.get(1).getTitleRisk()).as("干净行不得继承别行的侵权词").isEqualTo("无侵权");
|
||||
}
|
||||
|
||||
/**
|
||||
* 商标查询失败时,上游给的真实原因要落进行级原因,不能只留一句「商标查询失败」。
|
||||
* 2026-10-25 账号 1210 的 32300/32309 整批就是这个形状:服务端缺代理,
|
||||
* 交付表上每行只有「商标查询失败」,用户和运维都看不出该去配代理。
|
||||
*/
|
||||
@Test
|
||||
void brandQueryFailureReasonIsKeptForTheResult() throws Exception {
|
||||
when(brandCheckClient.checkTitleTexts(anyList(), eq("Terms"), any())).thenAnswer(invocation -> {
|
||||
List<String> titles = invocation.getArgument(0);
|
||||
List<BrandCheckClient.BrandCheckBatchResult> results = new ArrayList<>();
|
||||
for (String title : titles) {
|
||||
results.add(new BrandCheckClient.BrandCheckBatchResult(
|
||||
List.of(title), List.of(), List.of(title), "用户未配置代理"));
|
||||
}
|
||||
return results;
|
||||
});
|
||||
List<AppearancePatentResultRowDto> rows = List.of(row("1"));
|
||||
String dataText = """
|
||||
{"data":[{"row_id":"1","title":"小米","appearance":"无侵权","result":"无侵权"}]}
|
||||
""";
|
||||
|
||||
List<AppearancePatentResultRowDto> merged = client.mergeRowsFromDataText(rows, dataText);
|
||||
|
||||
assertThat(merged.get(0).getTitleRisk()).startsWith("商标查询失败");
|
||||
assertThat(merged.get(0).getConclusion()).isEqualTo("外观无侵权·商标待复核");
|
||||
assertThat(merged.get(0).getTitleReason())
|
||||
.as("原因列要能直接告诉用户/运维缺的是代理")
|
||||
.isEqualTo("用户未配置代理");
|
||||
}
|
||||
|
||||
@Test
|
||||
void batchWithNothingToCheckSendsNoBrandQueryAtAll() throws Exception {
|
||||
List<AppearancePatentResultRowDto> rows = List.of(row("1"));
|
||||
|
||||
+31
@@ -64,6 +64,20 @@ class AppearancePatentLlmClientTest {
|
||||
assertThat(client.buildConclusion("无侵权", "无侵权", "")).isEqualTo("无侵权");
|
||||
}
|
||||
|
||||
/** 外观/标题回「不侵权」「未发现明显…侵权风险」时也必须算无侵权,不能被 contains("侵权") 反判。 */
|
||||
@Test
|
||||
void conclusionTreatsChineseNoInfringementWordingAsNoInfringement() {
|
||||
assertThat(client.buildConclusion("无侵权", "不侵权", "")).isEqualTo("无侵权");
|
||||
assertThat(client.buildConclusion("无侵权", "未发现明显外观设计专利侵权风险。", "")).isEqualTo("无侵权");
|
||||
assertThat(client.buildConclusion("未发现明显商标侵权风险", "无侵权", "")).isEqualTo("无侵权");
|
||||
assertThat(client.buildConclusion("无明显侵权", "无侵权风险", "")).isEqualTo("无侵权");
|
||||
// 现网截图:两列肉眼都是「无侵权」,但模型回包夹了空白/零宽字符
|
||||
assertThat(client.buildConclusion("无侵权", "无 侵权", "")).isEqualTo("无侵权");
|
||||
assertThat(client.buildConclusion("无侵权", "无\u200B侵权", "")).isEqualTo("无侵权");
|
||||
assertThat(client.buildConclusion("无侵权", "无\u00A0侵权", "")).isEqualTo("无侵权");
|
||||
assertThat(client.buildConclusion("无侵权", "未 发现 明显 侵权 风险", "")).isEqualTo("无侵权");
|
||||
}
|
||||
|
||||
/**
|
||||
* 商标维度查询失败、外观已判定:结论必须带着外观结论并标出「商标待复核」,
|
||||
* 不能整格写「商标查询失败」把已判出的外观结论盖掉 —— 用户据此以为整单没检测,
|
||||
@@ -115,6 +129,23 @@ class AppearancePatentLlmClientTest {
|
||||
.isEqualTo(AppearancePatentLlmClient.BRAND_UPSTREAM_UNAVAILABLE);
|
||||
}
|
||||
|
||||
@Test
|
||||
void spacedNoInfringementAppearanceDoesNotLeakIntoConclusion() throws Exception {
|
||||
BrandCheckClient brandCheckClient = mock(BrandCheckClient.class);
|
||||
AppearancePatentResultRowDto row = new AppearancePatentResultRowDto();
|
||||
row.setId("1");
|
||||
String dataText = """
|
||||
{"data":[{"row_id":"1","title":"无","appearance":"无 侵权","result":"侵权"}]}
|
||||
""";
|
||||
|
||||
List<AppearancePatentResultRowDto> merged = clientWith(brandCheckClient)
|
||||
.mergeRowsFromDataText(List.of(row), dataText);
|
||||
|
||||
assertThat(merged.get(0).getTitleRisk()).isEqualTo("无侵权");
|
||||
assertThat(merged.get(0).getAppearanceRisk()).isEqualTo("无 侵权");
|
||||
assertThat(merged.get(0).getConclusion()).isEqualTo("无侵权");
|
||||
}
|
||||
|
||||
@Test
|
||||
void skipBrandCheckWhenLlmTitleIsNone() throws Exception {
|
||||
BrandCheckClient brandCheckClient = mock(BrandCheckClient.class);
|
||||
|
||||
+15
-2
@@ -371,7 +371,7 @@ class AppearancePatentDeliveryGateTest {
|
||||
when(taskFileJobService.resetTerminalFailedForRecovery(TASK_ID, "APPEARANCE_PATENT", 700L))
|
||||
.thenReturn(pendingJob(500L));
|
||||
|
||||
assertTrue(scheduleFallbackRecheck(task("FAILED"), result(700L), facts));
|
||||
assertTrue(scheduleFallbackRecheck(task("RUNNING"), result(700L), facts));
|
||||
verify(taskCacheService).releaseRowInspection(eq(TASK_ID), anyList(), eq(true));
|
||||
verify(taskCacheService).countRecheckRound(TASK_ID);
|
||||
}
|
||||
@@ -382,7 +382,20 @@ class AppearancePatentDeliveryGateTest {
|
||||
AppearancePatentRowOutcome.Facts facts = summarize(rows, submittedRows(rows, 1, true));
|
||||
when(taskCacheService.recheckRounds(TASK_ID)).thenReturn(2L);
|
||||
|
||||
assertFalse(scheduleFallbackRecheck(task("RUNNING"), result(700L), facts));
|
||||
verify(taskFileJobService, never()).resetTerminalFailedForRecovery(anyLong(), any(), anyLong());
|
||||
}
|
||||
|
||||
@Test
|
||||
void terminalTaskWithGapShipsPartialInsteadOfRechecking() throws Exception {
|
||||
// B28 起终态任务的结果作业不会再被 claim;再排补检只会被下一轮终态守卫立刻打回 FAILED。
|
||||
// 终态任务直接按已有分片出部分结果,缺口由行账文案标在表里。
|
||||
List<AppearancePatentParsedRowVo> rows = sheetRows(2);
|
||||
AppearancePatentRowOutcome.Facts facts = summarize(rows, submittedRows(rows, 1, true));
|
||||
|
||||
assertFalse(scheduleFallbackRecheck(task("FAILED"), result(700L), facts));
|
||||
verify(taskCacheService, never()).recheckRounds(anyLong());
|
||||
verify(taskCacheService, never()).releaseRowInspection(anyLong(), anyList(), eq(true));
|
||||
verify(taskFileJobService, never()).resetTerminalFailedForRecovery(anyLong(), any(), anyLong());
|
||||
}
|
||||
|
||||
@@ -406,7 +419,7 @@ class AppearancePatentDeliveryGateTest {
|
||||
when(taskFileJobService.resetTerminalFailedForRecovery(TASK_ID, "APPEARANCE_PATENT", 700L))
|
||||
.thenReturn(job(500L));
|
||||
|
||||
assertFalse(scheduleFallbackRecheck(task("FAILED"), result(700L), facts));
|
||||
assertFalse(scheduleFallbackRecheck(task("RUNNING"), result(700L), facts));
|
||||
verify(taskCacheService, never()).countRecheckRound(anyLong());
|
||||
}
|
||||
|
||||
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
package com.nanri.aiimage.modules.appearancepatent.service.support;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class AppearancePatentPartialGapStatusTest {
|
||||
|
||||
private static final String PARTIAL_GAP =
|
||||
"有 3 行抓取失败(未取得商品页数据):B0833QW1CB;B0CZ41T8ZQ;B08R1WH7SS";
|
||||
|
||||
@Test
|
||||
void crawlGapIsPartialNotFatal() {
|
||||
assertTrue(AppearancePatentResultJobSupport.isPartialDataGapError(PARTIAL_GAP));
|
||||
assertTrue(AppearancePatentResultJobSupport.isPartialDataGapError(
|
||||
"有 1 行未完成检测、有 2 行未采集到数据(结果文件中这些行已单独标注)"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void assemblyFailureIsStillFatal() {
|
||||
assertFalse(AppearancePatentResultJobSupport.isPartialDataGapError(
|
||||
"已收到 12 个结果分片,但结果文件生成多次失败,任务已自动结束"));
|
||||
assertFalse(AppearancePatentResultJobSupport.isPartialDataGapError(
|
||||
"Generate appearance patent result failed"));
|
||||
assertFalse(AppearancePatentResultJobSupport.isPartialDataGapError(null));
|
||||
assertFalse(AppearancePatentResultJobSupport.isPartialDataGapError(" "));
|
||||
}
|
||||
|
||||
@Test
|
||||
void coverageAtOrAboveFloorSurvivesCrawlGap() {
|
||||
assertFalse(AppearancePatentResultJobSupport.shouldFailAfterAssembly(
|
||||
false, PARTIAL_GAP, 1_000, 0.997, 0.95));
|
||||
assertFalse(AppearancePatentResultJobSupport.shouldFailAfterAssembly(
|
||||
false, PARTIAL_GAP, 1_000, 0.95, 0.95));
|
||||
}
|
||||
|
||||
@Test
|
||||
void coverageBelowFloorStillFails() {
|
||||
assertTrue(AppearancePatentResultJobSupport.shouldFailAfterAssembly(
|
||||
false, PARTIAL_GAP, 1_000, 0.90, 0.95));
|
||||
assertTrue(AppearancePatentResultJobSupport.shouldFailAfterAssembly(
|
||||
false, null, 1_000, 0.50, 0.95));
|
||||
}
|
||||
|
||||
@Test
|
||||
void fatalErrorOrTerminalTaskNeverRevives() {
|
||||
assertTrue(AppearancePatentResultJobSupport.shouldFailAfterAssembly(
|
||||
true, PARTIAL_GAP, 1_000, 1.0, 0.95));
|
||||
assertTrue(AppearancePatentResultJobSupport.shouldFailAfterAssembly(
|
||||
false, "结果文件生成多次失败,任务已自动结束", 1_000, 1.0, 0.95));
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyRowAccountKeepsLegacyOutcome() {
|
||||
assertFalse(AppearancePatentResultJobSupport.shouldFailAfterAssembly(
|
||||
false, null, 0, 0.0, 0.95));
|
||||
assertTrue(AppearancePatentResultJobSupport.shouldFailAfterAssembly(
|
||||
false, PARTIAL_GAP, 0, 0.0, 0.95));
|
||||
}
|
||||
}
|
||||
+24
@@ -106,6 +106,30 @@ class AuthServiceTest {
|
||||
|
||||
// ---------- check_login:被顶下线必须拦截且不续期 ----------
|
||||
|
||||
@Test
|
||||
void loginSignsTokenWithSessionEpochReadBackFromDatabase() {
|
||||
// CR-07:并发登录时库里的世代可能已被另一路推到 N+2;内存 +1 会签出 stale token,
|
||||
// 登录成功后第一次请求立刻 401。令牌必须用自增后回读到的库值。
|
||||
Fixture f = fixture(true);
|
||||
LoginUserEntity user = f.user("devA");
|
||||
user.setSessionEpoch(0L);
|
||||
LoginUserEntity refreshed = f.user("devA");
|
||||
refreshed.setSessionEpoch(41L);
|
||||
when(f.loginUserMapper.selectOne(any())).thenReturn(user);
|
||||
when(f.loginUserMapper.selectById(7L)).thenReturn(refreshed);
|
||||
when(f.passwordEncoder.matches("pwd", "hash")).thenReturn(true);
|
||||
when(f.jwtService.issue(eq(7L), anyString(), anyString(), anyLong())).thenReturn("jwt-token");
|
||||
|
||||
LoginRequest request = new LoginRequest();
|
||||
request.setUsername("u7");
|
||||
request.setPassword("pwd");
|
||||
request.setDeviceId("devB");
|
||||
|
||||
f.service.login(request);
|
||||
|
||||
verify(f.jwtService).issue(7L, "u7", "devB", 41L);
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkLoginRejectsSupersededDeviceWithoutRenewal() {
|
||||
Fixture f = fixture(true);
|
||||
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
package com.nanri.aiimage.modules.brandgating.controller;
|
||||
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import com.nanri.aiimage.common.security.AdminAuthSupport;
|
||||
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingAddRequest;
|
||||
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingImportRequest;
|
||||
import com.nanri.aiimage.modules.brandgating.model.dto.BrandGatingStatusRequest;
|
||||
import com.nanri.aiimage.modules.brandgating.model.vo.BrandGatingPageVo;
|
||||
import com.nanri.aiimage.modules.brandgating.service.BrandGatingService;
|
||||
import com.nanri.aiimage.modules.permission.service.PermissionMenuService;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* CR-11:{@code biz_brand_gating} 全平台共享,写操作只允许平台超级管理员;
|
||||
* 公司管理员(即使持有 admin_brand_gating 菜单)只能读。
|
||||
*/
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class BrandGatingControllerTest {
|
||||
|
||||
@Mock private BrandGatingService brandGatingService;
|
||||
@Mock private AdminAuthSupport adminAuthSupport;
|
||||
@Mock private PermissionMenuService permissionMenuService;
|
||||
@Mock private HttpServletRequest request;
|
||||
|
||||
private BrandGatingController controller;
|
||||
private AdminUserEntity companyAdmin;
|
||||
private AdminUserEntity platformSuperAdmin;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
controller = new BrandGatingController(brandGatingService, adminAuthSupport, permissionMenuService);
|
||||
companyAdmin = user(51L, "company-admin", "company_super_admin");
|
||||
platformSuperAdmin = user(1L, "root", "super_admin");
|
||||
when(adminAuthSupport.requireUser(request)).thenReturn(companyAdmin);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("公司管理员写操作全部 403:新增/删除/启停/批量导入都不触碰业务层")
|
||||
void companyAdminCannotWrite() {
|
||||
when(adminAuthSupport.currentRole(companyAdmin)).thenReturn("company_super_admin");
|
||||
BrandGatingStatusRequest status = new BrandGatingStatusRequest();
|
||||
status.setStatus(1);
|
||||
|
||||
assertForbidden(() -> controller.add(request, new BrandGatingAddRequest()));
|
||||
assertForbidden(() -> controller.delete(request, 9L));
|
||||
assertForbidden(() -> controller.updateStatus(request, 9L, status));
|
||||
assertForbidden(() -> controller.importRows(request, new BrandGatingImportRequest()));
|
||||
|
||||
verify(brandGatingService, never()).add(any());
|
||||
verify(brandGatingService, never()).delete(any());
|
||||
verify(brandGatingService, never()).updateStatus(any(), any());
|
||||
verify(brandGatingService, never()).importRows(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("只读查询不受影响:公司管理员有 admin_brand_gating 菜单即可分页")
|
||||
void companyAdminStillReads() {
|
||||
when(brandGatingService.page(any(), any(), any(), any(), any(), any(), any()))
|
||||
.thenReturn(new BrandGatingPageVo());
|
||||
|
||||
controller.page(request, null, null, null, null, null, 1L, 10L);
|
||||
|
||||
verify(permissionMenuService).requireConsolePageAccess(
|
||||
eq(companyAdmin), any(), eq("admin_brand_gating"));
|
||||
verify(brandGatingService).page(any(), any(), any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("平台超管写操作放行")
|
||||
void superAdminCanWrite() {
|
||||
when(adminAuthSupport.requireUser(request)).thenReturn(platformSuperAdmin);
|
||||
when(adminAuthSupport.currentRole(platformSuperAdmin)).thenReturn("super_admin");
|
||||
BrandGatingAddRequest body = new BrandGatingAddRequest();
|
||||
|
||||
controller.add(request, body);
|
||||
|
||||
verify(brandGatingService).add(body);
|
||||
}
|
||||
|
||||
private static void assertForbidden(org.junit.jupiter.api.function.Executable action) {
|
||||
assertThatThrownBy(action::execute)
|
||||
.isInstanceOf(BusinessException.class)
|
||||
.satisfies(ex -> assertThat(((BusinessException) ex).getCode()).isEqualTo(403));
|
||||
}
|
||||
|
||||
private static AdminUserEntity user(long id, String username, String role) {
|
||||
AdminUserEntity user = new AdminUserEntity();
|
||||
user.setId(id);
|
||||
user.setUsername(username);
|
||||
user.setRole(role);
|
||||
user.setIsAdmin(1);
|
||||
return user;
|
||||
}
|
||||
}
|
||||
+10
-6
@@ -464,16 +464,20 @@ class BrandGatingServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("控制器契约:随宿主页用 admin_shop_data_duplicate_check 权限键、逐端点 requireUser + requireConsolePageAccess 把门")
|
||||
void controllerGatesEveryEndpointWithPermissionKey() throws Exception {
|
||||
@DisplayName("控制器契约:读用 admin_brand_gating 菜单权限,写再收窄到平台超管(CR-11)")
|
||||
void controllerGatesReadsWithMenuAndWritesWithPlatformSuperAdmin() throws Exception {
|
||||
String source = Files.readString(Path.of(
|
||||
"src/main/java/com/nanri/aiimage/modules/brandgating/controller/BrandGatingController.java"),
|
||||
StandardCharsets.UTF_8);
|
||||
assertThat(source).contains("PERMISSION_KEY = \"admin_shop_data_duplicate_check\"");
|
||||
assertThat(source).contains("PERMISSION_KEY = \"admin_brand_gating\"");
|
||||
assertThat(source).contains("adminAuthSupport.requireUser(request)");
|
||||
assertThat(source).contains("requireConsolePageAccess(operator, DENIAL_MESSAGE, PERMISSION_KEY)");
|
||||
// 6 个端点方法都必须先过把门助手
|
||||
long gated = source.split("requireBrandGatingAccess\\(request\\);", -1).length - 1;
|
||||
assertThat(gated).isEqualTo(6);
|
||||
// 2 个只读端点(page/stats)继续走菜单权限
|
||||
long readGated = source.split("requireBrandGatingAccess\\(request\\);", -1).length - 1;
|
||||
assertThat(readGated).isEqualTo(2);
|
||||
// 4 个写端点(add/delete/status/import)叠加平台超管闸门
|
||||
long writeGated = source.split("requirePlatformSuperAdmin\\(request\\);", -1).length - 1;
|
||||
assertThat(writeGated).isEqualTo(4);
|
||||
assertThat(source).contains("DeviceSessionPolicy.isSuper(adminAuthSupport.currentRole(operator))");
|
||||
}
|
||||
}
|
||||
|
||||
+163
@@ -0,0 +1,163 @@
|
||||
package com.nanri.aiimage.modules.keywordusage.service;
|
||||
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.modules.keywordusage.mapper.KeywordUsageAdminMapper;
|
||||
import com.nanri.aiimage.modules.keywordusage.model.dto.KeywordUsageFilterQuery;
|
||||
import com.nanri.aiimage.modules.keywordusage.model.vo.KeywordUsageOverviewVo;
|
||||
import com.nanri.aiimage.modules.keywordusage.model.vo.KeywordUsagePageVo;
|
||||
import com.nanri.aiimage.modules.keywordusage.model.vo.KeywordUsageStatsVo;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class KeywordUsageAdminServiceTest {
|
||||
|
||||
@Mock
|
||||
private KeywordUsageAdminMapper keywordUsageAdminMapper;
|
||||
|
||||
@InjectMocks
|
||||
private KeywordUsageAdminService service;
|
||||
|
||||
@Test
|
||||
@DisplayName("概览:已删除词=复制-现存,合计=复制+添加,五国在筛选国家首位")
|
||||
void overviewComputesDerivedStatsAndCountryOrder() {
|
||||
KeywordUsageStatsVo stats = new KeywordUsageStatsVo();
|
||||
stats.setUsed(10);
|
||||
stats.setExist(7);
|
||||
stats.setAdded(3);
|
||||
stats.setDeletes(2);
|
||||
when(keywordUsageAdminMapper.selectStats(1L)).thenReturn(stats);
|
||||
when(keywordUsageAdminMapper.selectCountries(1L)).thenReturn(List.of("美国", "英国"));
|
||||
when(keywordUsageAdminMapper.selectUsers(1L)).thenReturn(List.of("alice"));
|
||||
|
||||
KeywordUsageOverviewVo vo = service.overview(1L);
|
||||
|
||||
assertThat(vo.getStats().getDeletedKw()).isEqualTo(3);
|
||||
assertThat(vo.getStats().getTotalUse()).isEqualTo(13);
|
||||
assertThat(vo.getCountries()).containsExactly("英国", "德国", "法国", "西班牙", "意大利", "美国");
|
||||
assertThat(vo.getUsers()).containsExactly("alice");
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("使用记录 scene=已使用:只查复制支,添加支为零且不发起查询")
|
||||
void usageSceneCopyOnly() {
|
||||
when(keywordUsageAdminMapper.countUsageRows(any())).thenReturn(5L);
|
||||
when(keywordUsageAdminMapper.countCopy(any())).thenReturn(5L);
|
||||
when(keywordUsageAdminMapper.countCopyExisting(any())).thenReturn(4L);
|
||||
when(keywordUsageAdminMapper.selectUsageRows(any())).thenReturn(List.of());
|
||||
|
||||
KeywordUsagePageVo vo = service.usage(1L, "alice", null, null, "已使用", 1L, 15L);
|
||||
|
||||
assertThat(vo.getTotal()).isEqualTo(5);
|
||||
assertThat(vo.getCounts().getCopy()).isEqualTo(5);
|
||||
assertThat(vo.getCounts().getCopyDeleted()).isEqualTo(1);
|
||||
assertThat(vo.getCounts().getAdd()).isZero();
|
||||
verify(keywordUsageAdminMapper, never()).countAdd(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("未知场景直接拒绝,不落任何查询")
|
||||
void unknownSceneRejected() {
|
||||
assertThatThrownBy(() -> service.usage(1L, null, null, null, "未知场景", 1L, 15L))
|
||||
.isInstanceOf(BusinessException.class)
|
||||
.hasMessageContaining("scene");
|
||||
verify(keywordUsageAdminMapper, never()).countUsageRows(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("CR-18:page=Long.MAX_VALUE 必须钳到安全上限,offset 不得溢出为负")
|
||||
void hugePageIsClampedToSafeOffset() {
|
||||
when(keywordUsageAdminMapper.countUsageRows(any())).thenReturn(0L);
|
||||
when(keywordUsageAdminMapper.countCopy(any())).thenReturn(0L);
|
||||
when(keywordUsageAdminMapper.countCopyExisting(any())).thenReturn(0L);
|
||||
when(keywordUsageAdminMapper.countAdd(any())).thenReturn(0L);
|
||||
when(keywordUsageAdminMapper.selectUsageRows(any())).thenReturn(List.of());
|
||||
|
||||
service.usage(1L, null, null, null, null, Long.MAX_VALUE, 15L);
|
||||
|
||||
ArgumentCaptor<KeywordUsageFilterQuery> captor = ArgumentCaptor.forClass(KeywordUsageFilterQuery.class);
|
||||
verify(keywordUsageAdminMapper).selectUsageRows(captor.capture());
|
||||
assertThat(captor.getValue().getOffset()).isEqualTo(149_985L)
|
||||
.isNotNegative();
|
||||
assertThat(captor.getValue().getLimit()).isEqualTo(15L);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("CR-18:删除记录页同样钳制 page,offset 不溢出")
|
||||
void hugePageIsClampedForDeletes() {
|
||||
when(keywordUsageAdminMapper.countDeletes(any())).thenReturn(0L);
|
||||
when(keywordUsageAdminMapper.selectDeleteRows(any())).thenReturn(List.of());
|
||||
|
||||
service.deletes(1L, null, null, null, Long.MAX_VALUE, 100L);
|
||||
|
||||
ArgumentCaptor<KeywordUsageFilterQuery> captor = ArgumentCaptor.forClass(KeywordUsageFilterQuery.class);
|
||||
verify(keywordUsageAdminMapper).selectDeleteRows(captor.capture());
|
||||
assertThat(captor.getValue().getOffset()).isEqualTo(999_900L).isNotNegative();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("分页参数:page<1 归一为 1,page_size 上限 100")
|
||||
void pageParamsAreClamped() {
|
||||
when(keywordUsageAdminMapper.countUsageRows(any())).thenReturn(0L);
|
||||
when(keywordUsageAdminMapper.countCopy(any())).thenReturn(0L);
|
||||
when(keywordUsageAdminMapper.countCopyExisting(any())).thenReturn(0L);
|
||||
when(keywordUsageAdminMapper.countAdd(any())).thenReturn(0L);
|
||||
when(keywordUsageAdminMapper.selectUsageRows(any())).thenReturn(List.of());
|
||||
|
||||
service.usage(1L, null, null, null, null, 0L, 1000L);
|
||||
|
||||
ArgumentCaptor<KeywordUsageFilterQuery> captor = ArgumentCaptor.forClass(KeywordUsageFilterQuery.class);
|
||||
verify(keywordUsageAdminMapper).selectUsageRows(captor.capture());
|
||||
assertThat(captor.getValue().getOffset()).isZero();
|
||||
assertThat(captor.getValue().getLimit()).isEqualTo(100);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("控制器与 Mapper 契约:独立菜单权限键、逐端点走公司范围门、XML 只读四表")
|
||||
void controllerAndMapperContract() throws Exception {
|
||||
String controller = Files.readString(Path.of(
|
||||
"src/main/java/com/nanri/aiimage/modules/keywordusage/controller/AdminKeywordUsageController.java"),
|
||||
StandardCharsets.UTF_8);
|
||||
assertThat(controller).contains("PERMISSION_KEY = \"admin_keyword_usage\"");
|
||||
assertThat(count(controller, "readCompanyIdOrOwn(operator, companyId)")).isEqualTo(3);
|
||||
assertThat(count(controller, "@RequestParam(name = \"company_id\"")).isEqualTo(3);
|
||||
assertThat(controller).contains("adminAuthSupport.requireUser(request)");
|
||||
assertThat(controller).contains("requireConsolePageAccess(operator, DENIAL_MESSAGE, PERMISSION_KEY)");
|
||||
|
||||
String xml = Files.readString(Path.of(
|
||||
"src/main/resources/mapper/keywordusage/KeywordUsageAdminMapper.xml"), StandardCharsets.UTF_8);
|
||||
assertThat(xml).contains("keyword_usage")
|
||||
.contains("keyword_add_log")
|
||||
.contains("keyword_delete_log")
|
||||
.contains("keyword_share");
|
||||
assertThat(xml).doesNotContain("${");
|
||||
assertThat(count(xml, "<select ")).isEqualTo(10);
|
||||
}
|
||||
|
||||
private static int count(String text, String token) {
|
||||
int hits = 0;
|
||||
int at = 0;
|
||||
while ((at = text.indexOf(token, at)) >= 0) {
|
||||
hits++;
|
||||
at += token.length();
|
||||
}
|
||||
return hits;
|
||||
}
|
||||
}
|
||||
+28
@@ -91,6 +91,34 @@ class ShopDataCrawlTaskServiceOwnerTest {
|
||||
verify(ossStorageService).deleteObject("result/shared.xlsx");
|
||||
}
|
||||
|
||||
@Test
|
||||
void resultFileJobFailureNeverRewritesTerminalParentTask() {
|
||||
// CR-09:终态父任务可能遗留 PENDING/RUNNING 结果作业;失败回调不得把
|
||||
// SUCCESS/CANCELLED/FAILED 无条件改写成 FAILED。
|
||||
com.nanri.aiimage.modules.task.service.TaskDistributedLockService.LockHandle lock =
|
||||
org.mockito.Mockito.mock(com.nanri.aiimage.modules.task.service.TaskDistributedLockService.LockHandle.class);
|
||||
when(taskDistributedLockService.acquire("SHOP_DATA_CRAWL", 21L)).thenReturn(lock);
|
||||
FileTaskEntity task = new FileTaskEntity();
|
||||
task.setId(21L);
|
||||
task.setModuleType("SHOP_DATA_CRAWL");
|
||||
when(fileTaskMapper.selectById(21L)).thenReturn(task);
|
||||
com.nanri.aiimage.modules.task.model.entity.TaskFileJobEntity job =
|
||||
new com.nanri.aiimage.modules.task.model.entity.TaskFileJobEntity();
|
||||
job.setId(901L);
|
||||
job.setTaskId(21L);
|
||||
job.setModuleType("SHOP_DATA_CRAWL");
|
||||
|
||||
for (String terminal : java.util.List.of("SUCCESS", "CANCELLED", "FAILED")) {
|
||||
task.setStatus(terminal);
|
||||
service.handleResultFileJobFailure(job, "retry exhausted");
|
||||
|
||||
assertEquals(terminal, task.getStatus(), "终态父任务不得被失败回调改写: " + terminal);
|
||||
}
|
||||
|
||||
verify(fileTaskMapper, never()).updateById(any(FileTaskEntity.class));
|
||||
verify(fileResultMapper, never()).updateById(any(com.nanri.aiimage.modules.task.model.entity.FileResultEntity.class));
|
||||
}
|
||||
|
||||
private FileTaskEntity task(String owner) {
|
||||
FileTaskEntity task = new FileTaskEntity();
|
||||
task.setId(1L);
|
||||
|
||||
+39
@@ -271,6 +271,45 @@ class SimilarAsinLlmServiceTest {
|
||||
assertNull(result.get(0).getPuzzleImg1());
|
||||
}
|
||||
|
||||
@Test
|
||||
void inspectRowsRestoresInterruptFlagWhenAwaitIsInterrupted() throws Exception {
|
||||
// CR-20:等待被中断时不能落入 catch (Exception) 吞掉;必须恢复中断标志并向上抛。
|
||||
java.util.concurrent.CountDownLatch entered = new java.util.concurrent.CountDownLatch(1);
|
||||
java.util.concurrent.CountDownLatch release = new java.util.concurrent.CountDownLatch(1);
|
||||
SimilarAsinLlmClient client = mock(SimilarAsinLlmClient.class);
|
||||
when(client.hasApiKey("k")).thenReturn(true);
|
||||
when(client.invokeChat(anyString(), anyString(), anyString(), anyString())).thenAnswer(invocation -> {
|
||||
entered.countDown();
|
||||
release.await(5, java.util.concurrent.TimeUnit.SECONDS);
|
||||
return "{\"first\":\"类目A\",\"second\":\"类目B\"}";
|
||||
});
|
||||
SimilarAsinLlmService service = service(client);
|
||||
SimilarAsinResultRowDto row = rowOf("B01", "A");
|
||||
|
||||
java.util.concurrent.atomic.AtomicReference<Throwable> failure =
|
||||
new java.util.concurrent.atomic.AtomicReference<>();
|
||||
Thread caller = new Thread(() -> {
|
||||
try {
|
||||
service.inspectRows(List.of(row), "", "k", false, true);
|
||||
} catch (Throwable ex) {
|
||||
failure.set(ex);
|
||||
}
|
||||
}, "interrupt-await-test");
|
||||
try {
|
||||
caller.start();
|
||||
assertTrue(entered.await(5, java.util.concurrent.TimeUnit.SECONDS),
|
||||
"worker 必须先真正阻塞在等待上");
|
||||
caller.interrupt();
|
||||
caller.join(5_000L);
|
||||
assertFalse(caller.isAlive(), "中断后等待必须立刻返回,不能继续等 worker");
|
||||
assertTrue(failure.get() instanceof IllegalStateException,
|
||||
"中断必须向上抛出,不能被 catch(Exception) 吞成行级失败: " + failure.get());
|
||||
assertTrue(caller.isInterrupted(), "中断标志必须恢复(Thread.currentThread().interrupt())");
|
||||
} finally {
|
||||
release.countDown();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void imageCompareStopsOnStockAndFillsFields() {
|
||||
// 第 1 次 chat:合并类目调用一次给出一级+二级名称(Java 按名回查取真实 ID);第 2 次:合规检查。
|
||||
|
||||
+5
@@ -92,6 +92,9 @@ class TaskFileJobTerminalTaskGuardTest {
|
||||
assertTrue(hasParam(wrapper, "FAILED"), "必须把作业拉成 FAILED,下一轮扫描才捞不到");
|
||||
assertTrue(hasParam(wrapper, 5), "retry_count 必须一次拉满到上限");
|
||||
assertFalse(hasParam(wrapper, "RUNNING"), "不得再翻转成 RUNNING");
|
||||
assertTrue(wrapper.getSqlSet().contains("terminal_callback_at"),
|
||||
"terminal_callback_at 必须显式清空:留给兜底扫描出部分结果,失败任务仍可下载");
|
||||
assertTrue(hasParam(wrapper, null), "terminal_callback_at 置空后兜底扫描才捞得到这一行");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -118,6 +121,8 @@ class TaskFileJobTerminalTaskGuardTest {
|
||||
verify(taskFileJobMapper, times(2)).update(isNull(), captor.capture());
|
||||
assertTrue(hasParam(captor.getAllValues().get(0), "FAILED"), "第一笔是终态收尾");
|
||||
assertTrue(hasParam(captor.getAllValues().get(1), "RUNNING"), "第二笔才是正常 claim");
|
||||
assertTrue(captor.getAllValues().get(0).getSqlSet().contains("terminal_callback_at"),
|
||||
"终态作业必须留给兜底扫描,不能提前打终态回调");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+163
-7
@@ -353,9 +353,9 @@ class TaskResultFileJobWorkerStuckScanTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void terminalParentHeartbeatStopsTouchingAndFailsJob() throws Exception {
|
||||
// 父任务在 worker 执行中变终态:心跳不得再 touchRunning(否则 stuck 扫描永远看不到),
|
||||
// 并主动把 job 收口为 FAILED(终态回调由 markFailedPermanent 打标)。
|
||||
void terminalParentHeartbeatStopsTouchingButLeavesTerminalVerdictToExecutionThread() throws Exception {
|
||||
// CR-08:父任务在 worker 执行中变终态:心跳只停续期、停 touchRunning,
|
||||
// 不再抢先把仍在执行的 job 翻成 FAILED——否则执行线程的 markSuccess 会被丢弃。
|
||||
TaskResultFileJobWorker worker = buildWorker();
|
||||
setLong(worker, "heartbeatIntervalMillis", 10L);
|
||||
TaskFileJobEntity job = new TaskFileJobEntity();
|
||||
@@ -363,23 +363,179 @@ class TaskResultFileJobWorkerStuckScanTest {
|
||||
job.setTaskId(15L);
|
||||
job.setModuleType("SIMILAR_ASIN");
|
||||
job.setStatus("RUNNING");
|
||||
TaskDistributedLockService.LockHandle lock = mock(TaskDistributedLockService.LockHandle.class);
|
||||
Field handlesField = TaskResultFileJobWorker.class.getDeclaredField("activeLockHandles");
|
||||
handlesField.setAccessible(true);
|
||||
@SuppressWarnings("unchecked")
|
||||
java.util.Map<Long, TaskDistributedLockService.LockHandle> handles =
|
||||
(java.util.Map<Long, TaskDistributedLockService.LockHandle>) handlesField.get(worker);
|
||||
handles.put(77L, lock);
|
||||
when(taskFileJobService.isOwningTaskTerminal(15L)).thenReturn(true);
|
||||
|
||||
java.lang.reflect.Method method = TaskResultFileJobWorker.class
|
||||
.getDeclaredMethod("startJobHeartbeat", TaskFileJobEntity.class);
|
||||
.getDeclaredMethod("startJobHeartbeat", TaskFileJobEntity.class,
|
||||
java.util.concurrent.atomic.AtomicBoolean.class);
|
||||
method.setAccessible(true);
|
||||
java.util.concurrent.ScheduledFuture<?> future =
|
||||
(java.util.concurrent.ScheduledFuture<?>) method.invoke(worker, job);
|
||||
(java.util.concurrent.ScheduledFuture<?>) method.invoke(worker, job,
|
||||
new java.util.concurrent.atomic.AtomicBoolean(false));
|
||||
try {
|
||||
// heartbeat 间隔下限 1000ms,首轮在 1s 后才跑;给足调度余量
|
||||
verify(taskFileJobService, timeout(3000))
|
||||
.markFailedPermanent(eq(job), contains("父任务已终态"));
|
||||
verify(lock, timeout(3000)).stopRenewal("parent-task-terminal", false);
|
||||
verify(taskFileJobService, after(150).never()).touchRunning(77L);
|
||||
verify(taskFileJobService, never())
|
||||
.markFailedPermanentAwaitingFallback(any(), anyString());
|
||||
} finally {
|
||||
future.cancel(true);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void terminalVerdictAfterExecutionKeepsSuccessWrittenByTheExecutionThread() throws Exception {
|
||||
// 竞态的另一半:心跳观察到终态时执行线程仍可能成功落库;退出后的收口必须保留 SUCCESS。
|
||||
TaskResultFileJobWorker worker = buildWorker();
|
||||
TaskFileJobEntity job = new TaskFileJobEntity();
|
||||
job.setId(78L);
|
||||
job.setTaskId(16L);
|
||||
job.setModuleType("SIMILAR_ASIN");
|
||||
TaskFileJobEntity success = new TaskFileJobEntity();
|
||||
success.setId(78L);
|
||||
success.setTaskId(16L);
|
||||
success.setModuleType("SIMILAR_ASIN");
|
||||
success.setStatus("SUCCESS");
|
||||
when(taskFileJobService.findById(78L)).thenReturn(success);
|
||||
|
||||
worker.finishAfterExecution(job, new java.util.concurrent.atomic.AtomicBoolean(true));
|
||||
|
||||
verify(taskFileJobService, never()).markFailedPermanentAwaitingFallback(any(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void terminalVerdictAfterExecutionFailsJobWhenExecutionDidNotSucceed() throws Exception {
|
||||
TaskResultFileJobWorker worker = buildWorker();
|
||||
TaskFileJobEntity job = new TaskFileJobEntity();
|
||||
job.setId(79L);
|
||||
job.setTaskId(17L);
|
||||
job.setModuleType("SIMILAR_ASIN");
|
||||
TaskFileJobEntity running = new TaskFileJobEntity();
|
||||
running.setId(79L);
|
||||
running.setTaskId(17L);
|
||||
running.setStatus("RUNNING");
|
||||
when(taskFileJobService.findById(79L)).thenReturn(running);
|
||||
|
||||
worker.finishAfterExecution(job, new java.util.concurrent.atomic.AtomicBoolean(true));
|
||||
|
||||
verify(taskFileJobService).markFailedPermanentAwaitingFallback(job, "父任务已终态,结果作业不再执行");
|
||||
}
|
||||
|
||||
/**
|
||||
* 真实竞态:执行线程还在跑时心跳发现父任务终态,随后执行线程成功落库。
|
||||
* 心跳不得抢翻 FAILED,退出后的收口必须保留 SUCCESS。
|
||||
*/
|
||||
@Test
|
||||
void heartbeatTerminalRaceDoesNotDiscardExecutionThreadSuccess() throws Exception {
|
||||
java.util.concurrent.CountDownLatch entered = new java.util.concurrent.CountDownLatch(1);
|
||||
java.util.concurrent.CountDownLatch release = new java.util.concurrent.CountDownLatch(1);
|
||||
java.util.concurrent.atomic.AtomicBoolean terminal = new java.util.concurrent.atomic.AtomicBoolean(false);
|
||||
ResultFileJobHandler handler = new ResultFileJobHandler() {
|
||||
@Override
|
||||
public String moduleType() {
|
||||
return "RACE_TEST";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean process(TaskFileJobEntity job) {
|
||||
entered.countDown();
|
||||
try {
|
||||
release.await(5, java.util.concurrent.TimeUnit.SECONDS);
|
||||
} catch (InterruptedException ex) {
|
||||
Thread.currentThread().interrupt();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
ResultFileJobHandlerRegistry registry = new ResultFileJobHandlerRegistry(List.of(handler));
|
||||
TaskResultFileJobWorker worker = new TaskResultFileJobWorker(
|
||||
taskFileJobService, taskDistributedLockService, mock(FileResultMapper.class),
|
||||
mock(TaskFileJobLocalDispatcher.class), instanceMetadata, registry);
|
||||
setLong(worker, "heartbeatIntervalMillis", 10L);
|
||||
|
||||
TaskFileJobEntity job = new TaskFileJobEntity();
|
||||
job.setId(80L);
|
||||
job.setTaskId(18L);
|
||||
job.setModuleType("RACE_TEST");
|
||||
job.setStatus("RUNNING");
|
||||
TaskFileJobEntity claim = new TaskFileJobEntity();
|
||||
claim.setId(80L);
|
||||
claim.setTaskId(18L);
|
||||
claim.setModuleType("RACE_TEST");
|
||||
claim.setStatus("RUNNING");
|
||||
claim.setUpdatedAt(LocalDateTime.now());
|
||||
TaskFileJobEntity success = new TaskFileJobEntity();
|
||||
success.setId(80L);
|
||||
success.setTaskId(18L);
|
||||
success.setModuleType("RACE_TEST");
|
||||
success.setStatus("SUCCESS");
|
||||
TaskDistributedLockService.LockHandle lock = mock(TaskDistributedLockService.LockHandle.class);
|
||||
when(taskFileJobService.isOwningTaskTerminal(18L)).thenAnswer(invocation -> terminal.get());
|
||||
when(taskFileJobService.activateRunningClaim(claim)).thenReturn(true);
|
||||
when(taskDistributedLockService.acquire("RACE_TEST", 18L,
|
||||
TaskDistributedLockService.DEFAULT_WAIT_MILLIS)).thenReturn(lock);
|
||||
when(taskFileJobService.findById(80L))
|
||||
.thenReturn(job, job, success);
|
||||
when(taskFileJobService.markSuccess(job, null)).thenReturn(true);
|
||||
|
||||
java.lang.reflect.Method method = TaskResultFileJobWorker.class.getDeclaredMethod(
|
||||
"processClaimedWithHeartbeat", TaskFileJobEntity.class, TaskFileJobEntity.class);
|
||||
method.setAccessible(true);
|
||||
java.util.concurrent.ExecutorService runner = java.util.concurrent.Executors.newSingleThreadExecutor();
|
||||
try {
|
||||
java.util.concurrent.Future<?> run = runner.submit(() -> {
|
||||
try {
|
||||
method.invoke(worker, job, claim);
|
||||
} catch (Exception ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
});
|
||||
assertTrue(entered.await(5, java.util.concurrent.TimeUnit.SECONDS),
|
||||
"执行线程必须已进入 handler");
|
||||
terminal.set(true);
|
||||
// heartbeat 间隔下限 1000ms;留足一轮调度余量,让心跳先看到终态
|
||||
Thread.sleep(1500L);
|
||||
verify(taskFileJobService, never()).markFailedPermanentAwaitingFallback(any(), anyString());
|
||||
release.countDown();
|
||||
run.get(5, java.util.concurrent.TimeUnit.SECONDS);
|
||||
|
||||
verify(taskFileJobService).markSuccess(job, null);
|
||||
verify(taskFileJobService, never()).markFailedPermanentAwaitingFallback(any(), anyString());
|
||||
} finally {
|
||||
release.countDown();
|
||||
runner.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 终态兜底出表只在归属实例做:非归属实例若抢先 markFailureFinalized,归属机的
|
||||
* fallbackAssembleOnFailure 就永远跑不到,失败任务的结果文件也就永远不生成。
|
||||
*/
|
||||
@Test
|
||||
void exhaustedOwnerScopedJobOfAnotherInstanceIsLeftToItsOwner() throws Exception {
|
||||
TaskResultFileJobWorker worker = buildWorker();
|
||||
when(instanceMetadata.getInstanceId()).thenReturn("instance-a");
|
||||
TaskFileJobEntity job = exhaustedJob(9L, 19L, "父任务已终态,结果作业不再执行");
|
||||
job.setModuleType("APPEARANCE_PATENT");
|
||||
job.setScopeKey("appearance-patent:19:owner:instance-b");
|
||||
TaskFileJobService.StuckJobResetResult result =
|
||||
new TaskFileJobService.StuckJobResetResult(0, List.of(job));
|
||||
when(taskFileJobService.resetStuckRunningJobsDetailed(0, 0)).thenReturn(result);
|
||||
|
||||
worker.resetStuckJobs();
|
||||
|
||||
verify(appearance, never()).fallbackAssembleResultFile(any());
|
||||
verify(taskFileJobService, never()).markFailureFinalized(any(), anyString());
|
||||
}
|
||||
|
||||
private static void setLong(TaskResultFileJobWorker worker, String fieldName, long value) throws Exception {
|
||||
Field field = TaskResultFileJobWorker.class.getDeclaredField(fieldName);
|
||||
field.setAccessible(true);
|
||||
|
||||
+72
@@ -96,6 +96,78 @@ class CostGuardSchedulerTest {
|
||||
anyString(), contains("继续任务"), isNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
void platformProxyDailyOverLimitNotifiesAdminsWithoutPausingAnyone() {
|
||||
// 无归属购买记在 0 号桶:金额真实,但没有可暂停的账号,只能推管理员告警。
|
||||
when(usageMapper.sumByUserAndModule(any(LocalDate.class)))
|
||||
.thenReturn(List.of(row(0L, "proxy", 2_500, 0)));
|
||||
|
||||
scheduler.runOnce();
|
||||
|
||||
verify(guardService, never()).isPaused(anyLong());
|
||||
verify(guardService, never()).pause(anyLong(), anyString());
|
||||
verify(notificationDispatchService).pushToAdmins(
|
||||
eq(NotificationService.SCENE_COST_GUARD), eq(NotificationService.LEVEL_WARNING),
|
||||
anyString(), contains("平台自费"), anyString(), isNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
void platformNewDayBaselineDoesNotSwallowAlert() {
|
||||
// 昨天基线已高于告警线;今天累计变小是跨天,今天首次越线仍必须告警。
|
||||
baselineStore.put(0L, new CostGuardBaselineStore.Baseline(
|
||||
5_000, 0, 0, System.currentTimeMillis() - 120_000L));
|
||||
when(usageMapper.sumByUserAndModule(any(LocalDate.class)))
|
||||
.thenReturn(List.of(row(0L, "proxy", 2_100, 0)));
|
||||
|
||||
scheduler.runOnce();
|
||||
|
||||
verify(notificationDispatchService).pushToAdmins(
|
||||
eq(NotificationService.SCENE_COST_GUARD), eq(NotificationService.LEVEL_WARNING),
|
||||
anyString(), contains("平台自费"), anyString(), isNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
void platformAlertWithoutAnyAdminPersistedDoesNotAdvanceBaselineAndRetries() {
|
||||
// CR-10:推送返回 0(没有管理员落库)时基线不得推进,否则下一轮 crossed 不再成立、告警永久丢失。
|
||||
when(usageMapper.sumByUserAndModule(any(LocalDate.class)))
|
||||
.thenReturn(List.of(row(0L, "proxy", 2_500, 0)));
|
||||
when(notificationDispatchService.pushToAdmins(
|
||||
eq(NotificationService.SCENE_COST_GUARD), eq(NotificationService.LEVEL_WARNING),
|
||||
anyString(), contains("平台自费"), anyString(), isNull()))
|
||||
.thenReturn(0, 1);
|
||||
|
||||
scheduler.runOnce();
|
||||
assertThat(baselineStore.get(0L)).isNull();
|
||||
|
||||
scheduler.runOnce();
|
||||
assertThat(baselineStore.get(0L).proxy).isEqualTo(2_500L);
|
||||
|
||||
scheduler.runOnce();
|
||||
verify(notificationDispatchService, org.mockito.Mockito.times(2)).pushToAdmins(
|
||||
eq(NotificationService.SCENE_COST_GUARD), eq(NotificationService.LEVEL_WARNING),
|
||||
anyString(), contains("平台自费"), anyString(), isNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
void platformAlertPushExceptionDoesNotAdvanceBaselineAndRetries() {
|
||||
when(usageMapper.sumByUserAndModule(any(LocalDate.class)))
|
||||
.thenReturn(List.of(row(0L, "proxy", 2_500, 0)));
|
||||
when(notificationDispatchService.pushToAdmins(
|
||||
eq(NotificationService.SCENE_COST_GUARD), eq(NotificationService.LEVEL_WARNING),
|
||||
anyString(), contains("平台自费"), anyString(), isNull()))
|
||||
.thenThrow(new IllegalStateException("redis down"))
|
||||
.thenReturn(1);
|
||||
|
||||
scheduler.runOnce();
|
||||
assertThat(baselineStore.get(0L)).isNull();
|
||||
|
||||
scheduler.runOnce();
|
||||
assertThat(baselineStore.get(0L).proxy).isEqualTo(2_500L);
|
||||
verify(notificationDispatchService, org.mockito.Mockito.times(2)).pushToAdmins(
|
||||
eq(NotificationService.SCENE_COST_GUARD), eq(NotificationService.LEVEL_WARNING),
|
||||
anyString(), contains("平台自费"), anyString(), isNull());
|
||||
}
|
||||
|
||||
@Test
|
||||
void llmCallsWindowJumpPauses() {
|
||||
when(usageMapper.sumByUserAndModule(any(LocalDate.class)))
|
||||
|
||||
+34
@@ -111,6 +111,40 @@ class UserSecretUsageServiceTest {
|
||||
verify(usageMapper).increment(eq(7L), eq("proxy"), eq(LocalDate.now()), eq(10_000));
|
||||
}
|
||||
|
||||
@Test
|
||||
void recordReportedAllowsPlatformBucketOnlyForProxy() {
|
||||
UserSecretUsageService service = newService();
|
||||
when(usageMapper.increment(any(), any(), any(), any(Integer.class))).thenReturn(1);
|
||||
|
||||
assertThat(service.recordReported(0L, "proxy", 3)).isTrue();
|
||||
assertThat(service.recordReported(0L, "appearance-patent", 3)).isFalse();
|
||||
assertThat(service.recordReported(-1L, "proxy", 3)).isFalse();
|
||||
|
||||
verify(usageMapper).increment(eq(0L), eq("proxy"), eq(LocalDate.now()), eq(3));
|
||||
}
|
||||
|
||||
@Test
|
||||
void adminPageLabelsPlatformBucket() {
|
||||
UserSecretUsageService service = newService();
|
||||
when(adminAuthSupport.currentRole(any())).thenReturn("super_admin");
|
||||
when(usageMapper.selectList(any())).thenReturn(List.of(
|
||||
usageRow(0L, "proxy", 50),
|
||||
usageRow(1L, "proxy", 2)));
|
||||
AdminUserEntity user1 = new AdminUserEntity();
|
||||
user1.setId(1L);
|
||||
user1.setUsername("张三");
|
||||
when(adminUserMapper.selectBatchIds(any())).thenReturn(List.of(user1));
|
||||
when(adminGroupMapper.selectGroupNamesByUserIds(any())).thenReturn(List.of());
|
||||
|
||||
var page = service.adminPage(new AdminUserEntity(), new AdminUserSecretUsageQuery(), ReadScope.unfiltered());
|
||||
|
||||
var platformRow = page.getItems().stream()
|
||||
.filter(item -> item.getUserId() == 0L).findFirst().orElseThrow();
|
||||
assertThat(platformRow.getUsername()).isEqualTo("平台自费");
|
||||
assertThat(platformRow.getProxyCount()).isEqualTo(50);
|
||||
assertThat(platformRow.getGroups()).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void adminPageAggregatesByUserAndSortsByTotal() {
|
||||
UserSecretUsageService service = newService();
|
||||
|
||||
+263
-2
@@ -1,20 +1,42 @@
|
||||
package com.nanri.aiimage.modules.workbenchclient.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.MybatisConfiguration;
|
||||
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
|
||||
import com.nanri.aiimage.common.exception.BusinessException;
|
||||
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
|
||||
import com.nanri.aiimage.modules.workbenchclient.mapper.WorkbenchClientPackageMapper;
|
||||
import com.nanri.aiimage.modules.workbenchclient.model.entity.WorkbenchClientPackageEntity;
|
||||
import ch.qos.logback.classic.Logger;
|
||||
import ch.qos.logback.classic.spi.ILoggingEvent;
|
||||
import ch.qos.logback.core.read.ListAppender;
|
||||
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipOutputStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
@@ -40,10 +62,33 @@ class WorkbenchClientPackageServiceTest {
|
||||
private final WorkbenchClientPackageMapper packageMapper = mock(WorkbenchClientPackageMapper.class);
|
||||
private final OssStorageService ossStorageService = mock(OssStorageService.class);
|
||||
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
private WorkbenchClientPackageService service() {
|
||||
return new WorkbenchClientPackageService(packageMapper, ossStorageService);
|
||||
}
|
||||
|
||||
/** 每次调用返回一个新的合法 zip:confirm 扫描后会把临时文件删掉,不能复用同一个路径。 */
|
||||
private void stubCleanZip() {
|
||||
when(ossStorageService.downloadWorkbenchClientObjectToTempFile(anyString()))
|
||||
.thenAnswer(invocation -> zip("ShuFuAI/app.exe", "ShuFuAI/readme.txt"));
|
||||
}
|
||||
|
||||
private Path zip(String... entries) {
|
||||
Path file = tempDir.resolve("pkg-" + UUID.randomUUID() + ".zip");
|
||||
try (ZipOutputStream out = new ZipOutputStream(Files.newOutputStream(file))) {
|
||||
for (String entry : entries) {
|
||||
out.putNextEntry(new ZipEntry(entry));
|
||||
out.write("test".getBytes(java.nio.charset.StandardCharsets.UTF_8));
|
||||
out.closeEntry();
|
||||
}
|
||||
return file;
|
||||
} catch (IOException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeAll
|
||||
static void initializeMybatisMetadata() {
|
||||
TableInfoHelper.initTableInfo(
|
||||
@@ -64,7 +109,7 @@ class WorkbenchClientPackageServiceTest {
|
||||
|
||||
@Test
|
||||
void presignUsesIndependentPrefixAndReturnsUploadUrl() {
|
||||
when(ossStorageService.presignWorkbenchClientUpload(anyString(), eq(180)))
|
||||
when(ossStorageService.presignWorkbenchClientUpload(anyString(), eq(3600)))
|
||||
.thenReturn("https://oss.aishufu.top/client/workbench-client/versions/put");
|
||||
when(ossStorageService.getWorkbenchClientDownloadUrl(anyString()))
|
||||
.thenReturn("https://download.aishufu.top/client/workbench-client/versions/1.2.3.zip");
|
||||
@@ -82,7 +127,7 @@ class WorkbenchClientPackageServiceTest {
|
||||
@Test
|
||||
void presignKeysAreUniqueForTheSameVersion() {
|
||||
// 同毫秒/同版本两次预签名必须不同:只靠时间戳会在 A/B 实例或并发上传时覆盖对象
|
||||
when(ossStorageService.presignWorkbenchClientUpload(anyString(), eq(180))).thenReturn("put");
|
||||
when(ossStorageService.presignWorkbenchClientUpload(anyString(), eq(3600))).thenReturn("put");
|
||||
when(ossStorageService.getWorkbenchClientDownloadUrl(anyString())).thenReturn("download");
|
||||
|
||||
String first = (String) service().presignPackage("pkg.zip", "1.2.3").get("object_key");
|
||||
@@ -103,6 +148,7 @@ class WorkbenchClientPackageServiceTest {
|
||||
void confirmRecomputesSha256BeforeInsert() {
|
||||
String objectKey = PREFIX + "20261024100000123-1.2.3.zip";
|
||||
when(ossStorageService.workbenchClientObjectSize(objectKey)).thenReturn(1234L);
|
||||
stubCleanZip();
|
||||
when(ossStorageService.workbenchClientObjectSha256(objectKey)).thenReturn(SHA);
|
||||
when(ossStorageService.getWorkbenchClientDownloadUrl(objectKey))
|
||||
.thenReturn("https://download.aishufu.top/client/" + objectKey);
|
||||
@@ -153,11 +199,13 @@ class WorkbenchClientPackageServiceTest {
|
||||
void confirmRejectsDuplicateVersionToPreventSameVersionDifferentBytes() {
|
||||
String objectKey = PREFIX + "20261024100000123-uuid-1.2.3.zip";
|
||||
when(ossStorageService.workbenchClientObjectSize(objectKey)).thenReturn(1234L);
|
||||
stubCleanZip();
|
||||
when(packageMapper.selectCount(any())).thenReturn(1L);
|
||||
|
||||
assertThrows(BusinessException.class, () -> service().confirmPackage(
|
||||
objectKey, "ShuFuAI-Workbench-1.2.3.zip", "1.2.3"));
|
||||
|
||||
verify(ossStorageService).removeWorkbenchClientObject(objectKey);
|
||||
verify(ossStorageService, never()).workbenchClientObjectSha256(anyString());
|
||||
verify(packageMapper, never()).insert(any(WorkbenchClientPackageEntity.class));
|
||||
}
|
||||
@@ -166,6 +214,7 @@ class WorkbenchClientPackageServiceTest {
|
||||
void confirmRejectsVersionLowerThanCurrentLatest() {
|
||||
String objectKey = PREFIX + "20261024100000123-uuid-1.2.2.zip";
|
||||
when(ossStorageService.workbenchClientObjectSize(objectKey)).thenReturn(1234L);
|
||||
stubCleanZip();
|
||||
when(packageMapper.selectCount(any())).thenReturn(0L);
|
||||
// 第一次 selectOne 是幂等查(无同对象),第二次才是取当前最新版本
|
||||
when(packageMapper.selectOne(any()))
|
||||
@@ -175,10 +224,222 @@ class WorkbenchClientPackageServiceTest {
|
||||
objectKey, "ShuFuAI-Workbench-1.2.2.zip", "1.2.2"));
|
||||
|
||||
assertTrue(ex.getMessage().contains("1.2.3"));
|
||||
verify(ossStorageService).removeWorkbenchClientObject(objectKey);
|
||||
verify(ossStorageService, never()).workbenchClientObjectSha256(anyString());
|
||||
verify(packageMapper, never()).insert(any(WorkbenchClientPackageEntity.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void confirmRejectsPackageContainingKeywordShareCredentialConfig() {
|
||||
// CR-02:公开下载的安装包内不得出现 kw_share_cfg.json(明文 API 口令载体)
|
||||
String objectKey = PREFIX + "20261024100000123-uuid-1.2.3.zip";
|
||||
when(ossStorageService.workbenchClientObjectSize(objectKey)).thenReturn(1234L);
|
||||
when(ossStorageService.downloadWorkbenchClientObjectToTempFile(objectKey))
|
||||
.thenReturn(zip("ShuFuAI/app.exe", "ShuFuAI/config/kw_share_cfg.json"));
|
||||
|
||||
BusinessException ex = assertThrows(BusinessException.class, () -> service().confirmPackage(
|
||||
objectKey, "ShuFuAI-Workbench-1.2.3.zip", "1.2.3"));
|
||||
|
||||
assertTrue(ex.getMessage().contains("kw_share_cfg.json"), "错误里要点名命中的凭据文件");
|
||||
verify(ossStorageService).removeWorkbenchClientObject(objectKey);
|
||||
verify(ossStorageService, never()).workbenchClientObjectSha256(anyString());
|
||||
verify(packageMapper, never()).insert(any(WorkbenchClientPackageEntity.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void confirmRejectsEnvAndPemFiles() {
|
||||
String objectKey = PREFIX + "20261024100000123-uuid-1.2.3.zip";
|
||||
when(ossStorageService.workbenchClientObjectSize(objectKey)).thenReturn(1234L);
|
||||
when(ossStorageService.downloadWorkbenchClientObjectToTempFile(objectKey))
|
||||
.thenReturn(zip("ShuFuAI/app.exe", "deploy/.env", "cert/server.pem"));
|
||||
|
||||
assertThrows(BusinessException.class, () -> service().confirmPackage(
|
||||
objectKey, "ShuFuAI-Workbench-1.2.3.zip", "1.2.3"));
|
||||
|
||||
verify(ossStorageService).removeWorkbenchClientObject(objectKey);
|
||||
verify(packageMapper, never()).insert(any(WorkbenchClientPackageEntity.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void confirmRejectsNonZipPayloadAndCleansItUp() throws Exception {
|
||||
String objectKey = PREFIX + "20261024100000123-uuid-1.2.3.zip";
|
||||
Path notZip = tempDir.resolve("not-a-zip.zip");
|
||||
Files.writeString(notZip, "this is not a zip");
|
||||
when(ossStorageService.workbenchClientObjectSize(objectKey)).thenReturn(1234L);
|
||||
when(ossStorageService.downloadWorkbenchClientObjectToTempFile(objectKey)).thenReturn(notZip);
|
||||
|
||||
assertThrows(BusinessException.class, () -> service().confirmPackage(
|
||||
objectKey, "ShuFuAI-Workbench-1.2.3.zip", "1.2.3"));
|
||||
|
||||
verify(ossStorageService).removeWorkbenchClientObject(objectKey);
|
||||
verify(packageMapper, never()).insert(any(WorkbenchClientPackageEntity.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void presignDoesNotLogTheOneTimeUploadUrl() {
|
||||
// CR-21:预签名 URL 是一次性凭据,不得出现在日志里
|
||||
String secretUrl = "https://oss.aishufu.top/client/workbench-client/versions/put?X-Amz-Signature=SECRET-URL-999";
|
||||
when(ossStorageService.presignWorkbenchClientUpload(anyString(), eq(3600))).thenReturn(secretUrl);
|
||||
when(ossStorageService.getWorkbenchClientDownloadUrl(anyString())).thenReturn("download");
|
||||
|
||||
Logger logger = (Logger) LoggerFactory.getLogger(WorkbenchClientPackageService.class);
|
||||
ListAppender<ILoggingEvent> appender = new ListAppender<>();
|
||||
appender.start();
|
||||
logger.addAppender(appender);
|
||||
try {
|
||||
Map<String, Object> result = service().presignPackage("pkg.zip", "1.2.3");
|
||||
|
||||
assertEquals(secretUrl, result.get("upload_url"));
|
||||
String logs = String.join("\n", appender.list.stream()
|
||||
.map(ILoggingEvent::getFormattedMessage).toList());
|
||||
assertFalse(logs.contains("SECRET-URL-999"), "预签名 URL 不得入日志: " + logs);
|
||||
} finally {
|
||||
logger.detachAppender(appender);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void concurrentConfirmsOfSameObjectKeyAreIdempotent() throws Exception {
|
||||
String objectKey = PREFIX + "20261024100000123-uuid-1.2.3.zip";
|
||||
when(ossStorageService.workbenchClientObjectSize(objectKey)).thenReturn(1234L);
|
||||
when(ossStorageService.workbenchClientObjectSha256(objectKey)).thenReturn(SHA);
|
||||
when(ossStorageService.getWorkbenchClientDownloadUrl(anyString())).thenReturn("");
|
||||
stubCleanZip();
|
||||
Map<String, WorkbenchClientPackageEntity> rows = new ConcurrentHashMap<>();
|
||||
AtomicLong nextId = new AtomicLong(99L);
|
||||
CountDownLatch insertBarrier = new CountDownLatch(2);
|
||||
when(packageMapper.selectOne(any())).thenAnswer(invocation -> findRow(invocation.getArgument(0), rows));
|
||||
when(packageMapper.selectCount(any())).thenReturn(0L);
|
||||
when(packageMapper.insert(any(WorkbenchClientPackageEntity.class))).thenAnswer(invocation -> {
|
||||
WorkbenchClientPackageEntity entity = invocation.getArgument(0);
|
||||
insertBarrier.countDown();
|
||||
assertTrue(insertBarrier.await(5, TimeUnit.SECONDS), "两路必须都到达 insert 才构成并发");
|
||||
synchronized (rows) {
|
||||
if (rows.containsKey(entity.getObjectKey())) {
|
||||
throw new DuplicateKeyException("uk_workbench_client_object_key");
|
||||
}
|
||||
entity.setId(nextId.incrementAndGet());
|
||||
rows.put(entity.getObjectKey(), entity);
|
||||
return 1;
|
||||
}
|
||||
});
|
||||
when(packageMapper.selectById(any())).thenAnswer(invocation -> rows.values().stream()
|
||||
.filter(row -> row.getId().equals(invocation.getArgument(0))).findFirst().orElse(null));
|
||||
|
||||
List<Object> results = runConcurrently(
|
||||
() -> service().confirmPackage(objectKey, "ShuFuAI-Workbench-1.2.3.zip", "1.2.3"),
|
||||
() -> service().confirmPackage(objectKey, "ShuFuAI-Workbench-1.2.3.zip", "1.2.3"));
|
||||
|
||||
assertEquals(1, rows.size(), "同 objectKey 并发 confirm 只能落一行");
|
||||
for (Object result : results) {
|
||||
assertTrue(result instanceof Map, "两路都应幂等成功: " + result);
|
||||
}
|
||||
assertEquals(((Map<?, ?>) results.get(0)).get("id"), ((Map<?, ?>) results.get(1)).get("id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void concurrentConfirmsOfSameVersionDifferentObjectsAllowOnlyOneRow() throws Exception {
|
||||
String keyA = PREFIX + "20261024100000123-aaa-1.2.3.zip";
|
||||
String keyB = PREFIX + "20261024100000123-bbb-1.2.3.zip";
|
||||
when(ossStorageService.workbenchClientObjectSize(anyString())).thenReturn(1234L);
|
||||
when(ossStorageService.workbenchClientObjectSha256(anyString())).thenReturn(SHA);
|
||||
when(ossStorageService.getWorkbenchClientDownloadUrl(anyString())).thenReturn("");
|
||||
stubCleanZip();
|
||||
Map<String, WorkbenchClientPackageEntity> rows = new ConcurrentHashMap<>();
|
||||
AtomicLong nextId = new AtomicLong(99L);
|
||||
CountDownLatch countBarrier = new CountDownLatch(2);
|
||||
CountDownLatch insertBarrier = new CountDownLatch(2);
|
||||
AtomicInteger countCalls = new AtomicInteger();
|
||||
when(packageMapper.selectCount(any())).thenAnswer(invocation -> {
|
||||
if (countCalls.incrementAndGet() <= 2) {
|
||||
countBarrier.countDown();
|
||||
assertTrue(countBarrier.await(5, TimeUnit.SECONDS), "两路都必须先读到 0 才能复现竞态");
|
||||
}
|
||||
return 0L;
|
||||
});
|
||||
when(packageMapper.selectOne(any())).thenAnswer(invocation -> findRow(invocation.getArgument(0), rows));
|
||||
when(packageMapper.insert(any(WorkbenchClientPackageEntity.class))).thenAnswer(invocation -> {
|
||||
WorkbenchClientPackageEntity entity = invocation.getArgument(0);
|
||||
insertBarrier.countDown();
|
||||
assertTrue(insertBarrier.await(5, TimeUnit.SECONDS), "两路必须都到达 insert 才构成并发");
|
||||
synchronized (rows) {
|
||||
if (rows.containsKey(entity.getObjectKey())) {
|
||||
throw new DuplicateKeyException("uk_workbench_client_object_key");
|
||||
}
|
||||
if (rows.values().stream().anyMatch(row -> row.getVersion().equals(entity.getVersion()))) {
|
||||
throw new DuplicateKeyException("uk_workbench_client_version");
|
||||
}
|
||||
entity.setId(nextId.incrementAndGet());
|
||||
rows.put(entity.getObjectKey(), entity);
|
||||
return 1;
|
||||
}
|
||||
});
|
||||
when(packageMapper.selectById(any())).thenAnswer(invocation -> rows.values().stream()
|
||||
.filter(row -> row.getId().equals(invocation.getArgument(0))).findFirst().orElse(null));
|
||||
|
||||
List<Object> results = runConcurrently(
|
||||
() -> service().confirmPackage(keyA, "ShuFuAI-Workbench-1.2.3.zip", "1.2.3"),
|
||||
() -> service().confirmPackage(keyB, "ShuFuAI-Workbench-1.2.3.zip", "1.2.3"));
|
||||
|
||||
assertEquals(1, rows.size(), "同 version 不同 objectKey 并发 confirm 只能落一行");
|
||||
long success = results.stream().filter(Map.class::isInstance).count();
|
||||
long rejected = results.stream().filter(BusinessException.class::isInstance).count();
|
||||
assertEquals(1, success, "必须恰好一路成功: " + results);
|
||||
assertEquals(1, rejected, "第二路必须是明确的业务错误而非 500: " + results);
|
||||
// 失败那一路的 512MB 级对象必须被清理,不能悬挂在公开桶
|
||||
verify(ossStorageService).removeWorkbenchClientObject(anyString());
|
||||
}
|
||||
|
||||
private static WorkbenchClientPackageEntity findRow(Object wrapper,
|
||||
Map<String, WorkbenchClientPackageEntity> rows) {
|
||||
if (!(wrapper instanceof AbstractWrapper<?, ?, ?> query)) {
|
||||
return null;
|
||||
}
|
||||
String sql = query.getSqlSegment();
|
||||
if (sql == null || !sql.contains("object_key")) {
|
||||
return null;
|
||||
}
|
||||
for (Object value : query.getParamNameValuePairs().values()) {
|
||||
if (value instanceof String key && rows.containsKey(key)) {
|
||||
return rows.get(key);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static List<Object> runConcurrently(ThrowingSupplier first, ThrowingSupplier second)
|
||||
throws Exception {
|
||||
CountDownLatch start = new CountDownLatch(1);
|
||||
ExecutorService pool = Executors.newFixedThreadPool(2);
|
||||
try {
|
||||
Future<Object> a = pool.submit(() -> {
|
||||
start.await();
|
||||
try {
|
||||
return first.get();
|
||||
} catch (Exception ex) {
|
||||
return ex;
|
||||
}
|
||||
});
|
||||
Future<Object> b = pool.submit(() -> {
|
||||
start.await();
|
||||
try {
|
||||
return second.get();
|
||||
} catch (Exception ex) {
|
||||
return ex;
|
||||
}
|
||||
});
|
||||
start.countDown();
|
||||
return List.of(a.get(10, TimeUnit.SECONDS), b.get(10, TimeUnit.SECONDS));
|
||||
} finally {
|
||||
pool.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
private interface ThrowingSupplier {
|
||||
Object get() throws Exception;
|
||||
}
|
||||
|
||||
@Test
|
||||
void versionComparisonHandlesDifferentSegmentCounts() {
|
||||
assertTrue(WorkbenchClientPackageService.versionIsLower("1.2.3", "1.2.10"));
|
||||
|
||||
@@ -73,7 +73,7 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, onUnmounted, ref } from 'vue'
|
||||
import { userNotice } from '@/shared/utils/user-notice'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||||
import AmazonToolPageShell from '@/pages/amazon/components/AmazonToolPageShell.vue';
|
||||
import FileUploadZone from '@/shared/components/FileUploadZone.vue'
|
||||
import ModuleTemplateDownload from '@/shared/components/ModuleTemplateDownload.vue'
|
||||
@@ -105,7 +105,7 @@ import { checkParseResult } from '@/shared/dispatch-guard'
|
||||
import { passGuard } from '@/shared/dispatch-guard-ui'
|
||||
import { formatDateTime } from '@/shared/utils/datetime'
|
||||
import { resultBuildFailed, taskStatusClass, taskStatusText } from '@/shared/utils/task-status-text'
|
||||
import { getStoredApiSecret } from '@/shared/utils/api-secret-store'
|
||||
import { ensureBrandCheckProxy, getStoredApiSecret } from '@/shared/utils/api-secret-store'
|
||||
|
||||
// ---- 文件选择 / 上传(公共组合式函数)----
|
||||
const {
|
||||
@@ -392,6 +392,26 @@ function toTaskSummary(result: AppearancePatentParseVo): TaskSummary {
|
||||
* 逐行抓取用例钉住:maixiang_run_task/test/maixiang_ops/test_proxy_cost_leaks.py。
|
||||
*/
|
||||
|
||||
/**
|
||||
* 创建任务前确认服务端有代理:商标维度依赖服务端代理,缺代理时整批都会落成
|
||||
* 「商标查询失败」且状态为失败。服务端读不到(unknown)不拦,避免网络抖动误伤。
|
||||
*/
|
||||
async function ensureBrandCheckProxyReady(): Promise<boolean> {
|
||||
const check = await ensureBrandCheckProxy()
|
||||
if (check.ok) return true
|
||||
await ElMessageBox.alert(
|
||||
'当前账号未在服务端配置代理,商标维度将无法查询,整批结果会显示「商标查询失败」。'
|
||||
+ '请先点击右上角「密钥设置」,在「代理地址」中保存代理后再创建任务。',
|
||||
'未配置代理,无法检测商标',
|
||||
{ type: 'warning', confirmButtonText: '知道了' },
|
||||
).then(
|
||||
// 关闭弹窗会以 reject 收尾,属正常交互而不是错误,不新增 catch 块
|
||||
() => undefined,
|
||||
() => undefined,
|
||||
)
|
||||
return false
|
||||
}
|
||||
|
||||
async function parseFiles() {
|
||||
if (!uploadedFiles.value.length) {
|
||||
ElMessage.warning('请先选择 Excel')
|
||||
@@ -403,6 +423,9 @@ async function parseFiles() {
|
||||
}
|
||||
parsing.value = true
|
||||
try {
|
||||
if (!(await ensureBrandCheckProxyReady())) {
|
||||
return
|
||||
}
|
||||
const files: UploadedFileRef[] = uploadedFiles.value.map((f) => ({
|
||||
fileKey: f.fileKey,
|
||||
originalFilename: f.originalFilename,
|
||||
|
||||
@@ -102,8 +102,8 @@
|
||||
<span>自动登录</span>
|
||||
</label>
|
||||
</div>
|
||||
<button type="submit" class="btn-login" id="btnLogin" :disabled="loggingIn">
|
||||
{{ loggingIn ? '登录中...' : '登录' }}
|
||||
<button type="submit" class="btn-login" id="btnLogin" :disabled="loggingIn || logoutCleanupPending">
|
||||
{{ loggingIn ? '登录中...' : logoutCleanupPending ? '正在退出...' : '登录' }}
|
||||
</button>
|
||||
</form>
|
||||
</div>
|
||||
@@ -119,7 +119,7 @@ import { COMPANY_NAME_STORAGE_KEY, writeCompanyIdentity } from '@/shared/auth/co
|
||||
import { clearAccountExpiry, writeAccountExpiry } from '@/shared/auth/account-expiry'
|
||||
import { AUTH_TOKEN_KEY } from '@/shared/auth/session-keys'
|
||||
import { useVersionUpdate } from '@/shared/composables/useVersionUpdate'
|
||||
import { clearApiSecretCache } from '@/shared/utils/api-secret-store'
|
||||
import { captureApiSecretsForInheritance, clearApiSecretCache } from '@/shared/utils/api-secret-store'
|
||||
import UpdateProgressBar from '@/shared/components/UpdateProgressBar.vue'
|
||||
import UpdateLogList from '@/shared/components/UpdateLogList.vue'
|
||||
import VersionPicker from '@/shared/components/VersionPicker.vue'
|
||||
@@ -136,6 +136,11 @@ const username = ref('')
|
||||
const password = ref('')
|
||||
const passwordVisible = ref(false)
|
||||
const loggingIn = ref(false)
|
||||
/**
|
||||
* 登出清理进行中(CR-12):继承快照 capture 有超时,但等它返回前不能允许再提交登录,
|
||||
* 否则新账号 B 的 token/uid 会被上一轮 clearSignedOutState 删掉。
|
||||
*/
|
||||
const logoutCleanupPending = ref(false)
|
||||
const errorMessage = ref('')
|
||||
const kickedNotice = ref(false)
|
||||
const expiredNotice = ref(false)
|
||||
@@ -407,7 +412,8 @@ function toggleUpdate() {
|
||||
}
|
||||
|
||||
async function submitLogin() {
|
||||
if (loggingIn.value) return
|
||||
// 登出清理未完成时禁止提交:清理函数会删 token/uid,晚到的登录会被它清掉(CR-12)
|
||||
if (loggingIn.value || logoutCleanupPending.value) return
|
||||
const account = username.value.trim()
|
||||
if (!account || !password.value) {
|
||||
errorMessage.value = '请输入账号和密码'
|
||||
@@ -480,27 +486,35 @@ async function submitLogin() {
|
||||
onMounted(() => {
|
||||
const params = new URLSearchParams(window.location.search || '')
|
||||
if (params.get('logout') === '1' || params.get('switch') === '1') {
|
||||
// 登出/切号:uid 不彻底清除会被路由守卫当“已登录”自动带回首页(见 src/main.ts 守卫)
|
||||
try {
|
||||
window.localStorage.removeItem(AUTH_TOKEN_KEY)
|
||||
window.localStorage.removeItem('uid')
|
||||
window.localStorage.removeItem('username')
|
||||
window.localStorage.removeItem(COMPANY_NAME_STORAGE_KEY)
|
||||
clearAccountExpiry()
|
||||
} catch {
|
||||
/* 忽略 */
|
||||
}
|
||||
// 清空密钥缓存:内存 + 本地镜像(旧 v1 记录保留,供下次登录自动迁移)
|
||||
clearApiSecretCache()
|
||||
// 同步清掉客户端的登录用户标记:Python 端回退到全局/默认代理池
|
||||
try {
|
||||
const bridge = (window as unknown as { pywebview?: { api?: { save_config?: (data: Record<string, unknown>) => Promise<unknown> } } }).pywebview
|
||||
if (bridge?.api && typeof bridge.api.save_config === 'function') {
|
||||
void bridge.api.save_config({ current_uid: '' })
|
||||
logoutCleanupPending.value = true
|
||||
const clearSignedOutState = () => {
|
||||
logoutCleanupPending.value = false
|
||||
// 清缓存必须在移除 uid 之前:镜像键带 uid,反序会按 uid=0 删错位置,
|
||||
// 把本账号的明文镜像留在磁盘上给下一个账号(或网页脚本)读到。
|
||||
clearApiSecretCache()
|
||||
// 登出/切号:uid 不彻底清除会被路由守卫当“已登录”自动带回首页(见 src/main.ts 守卫)
|
||||
try {
|
||||
window.localStorage.removeItem(AUTH_TOKEN_KEY)
|
||||
window.localStorage.removeItem('uid')
|
||||
window.localStorage.removeItem('username')
|
||||
window.localStorage.removeItem(COMPANY_NAME_STORAGE_KEY)
|
||||
clearAccountExpiry()
|
||||
} catch {
|
||||
/* 忽略 */
|
||||
}
|
||||
// 同步清掉客户端的登录用户标记:Python 端回退到全局/默认代理池
|
||||
try {
|
||||
const bridge = (window as unknown as { pywebview?: { api?: { save_config?: (data: Record<string, unknown>) => Promise<unknown> } } }).pywebview
|
||||
if (bridge?.api && typeof bridge.api.save_config === 'function') {
|
||||
void bridge.api.save_config({ current_uid: '' })
|
||||
}
|
||||
} catch {
|
||||
/* 忽略 */
|
||||
}
|
||||
} catch {
|
||||
/* 忽略 */
|
||||
}
|
||||
// 先把本账号的密钥/代理留给本机下一个账号(桌面端),再清缓存与 uid;
|
||||
// capture 失败也必须照常登出,所以成功/失败两条路都走同一个清理函数。
|
||||
void captureApiSecretsForInheritance().then(clearSignedOutState, clearSignedOutState)
|
||||
// 显式登出/切号后不再自动登录(保留记住的账号,仅关闭自动登录)
|
||||
autoLogin.value = false
|
||||
lsSet(AUTO_LOGIN_KEY, '0')
|
||||
|
||||
@@ -33,13 +33,18 @@
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref } from 'vue'
|
||||
import { useRoute, useRouter } from 'vue-router'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||||
import ApiSecretSettingsPanel from '@/shared/components/ApiSecretSettingsPanel.vue'
|
||||
import NotificationBell from '@/shared/components/NotificationBell.vue'
|
||||
import {
|
||||
confirmApiSecretInheritance,
|
||||
declineApiSecretInheritance,
|
||||
getPendingApiSecretInheritance,
|
||||
getStoredApiSecretSnapshot,
|
||||
listApiSecretModules,
|
||||
loadApiSecrets,
|
||||
takeApiSecretInheritanceNotice,
|
||||
type ApiSecretInheritanceNotice,
|
||||
type ApiSecretModuleKey,
|
||||
} from '@/shared/utils/api-secret-store'
|
||||
|
||||
@@ -50,12 +55,71 @@ const submitting = ref(false)
|
||||
const username = ref('')
|
||||
const hint = ref('填写密钥后请先点「检测」确认可用再保存(检测通过的输入值保存时会沿用同一结果),全部检测通过后即可进入工具台。')
|
||||
|
||||
function inheritanceLabels(moduleKeys: string[], proxy: boolean): string[] {
|
||||
const labels = listApiSecretModules()
|
||||
.filter((module) => moduleKeys.includes(module.moduleKey))
|
||||
.map((module) => module.moduleLabel)
|
||||
if (proxy) labels.push('代理设置')
|
||||
return labels
|
||||
}
|
||||
|
||||
function showInheritanceNotice(inherited: ApiSecretInheritanceNotice | null): void {
|
||||
if (!inherited) return
|
||||
const labels = inheritanceLabels(inherited.moduleKeys, inherited.proxy)
|
||||
// 共享电脑上不把上一个账号的用户名展示给新账号(CR-12)
|
||||
hint.value = labels.length
|
||||
? `已继承本机上一个账号的${labels.join('、')},请点击上方对应模块的「检测」确认后保存进入。`
|
||||
: '已继承本机上一个账号的密钥配置,请点击上方「检测」逐项确认后保存进入。'
|
||||
}
|
||||
|
||||
/**
|
||||
* 继承确认(CR-12):同机换号不再静默继承上一个账号的密钥/代理。
|
||||
* 只在本机确实有快照、且当前账号服务端还有空缺时弹一次确认;取消即清快照。
|
||||
*/
|
||||
async function promptPendingInheritance(): Promise<void> {
|
||||
const pending = getPendingApiSecretInheritance()
|
||||
if (!pending) {
|
||||
showInheritanceNotice(takeApiSecretInheritanceNotice())
|
||||
return
|
||||
}
|
||||
const labels = inheritanceLabels(pending.moduleKeys, pending.proxy)
|
||||
const detail = labels.length ? `留下的${labels.join('、')}` : '留下的密钥配置'
|
||||
const confirmed = () => {
|
||||
void confirmApiSecretInheritance().then((notice) => {
|
||||
if (!notice) {
|
||||
hint.value = '未继承:服务端已有的项不会被覆盖,请手动填写并检测本账号密钥。'
|
||||
return
|
||||
}
|
||||
showInheritanceNotice(notice)
|
||||
})
|
||||
}
|
||||
const declined = () => {
|
||||
void declineApiSecretInheritance().then(() => {
|
||||
hint.value = '已跳过本机配置继承,请填写当前账号自己的密钥并检测后保存进入。'
|
||||
})
|
||||
}
|
||||
// 两参 then:取消/关闭只走拒绝分支,不会把「确认过程中的网络失败」误当成用户拒绝而清快照
|
||||
void ElMessageBox.confirm(
|
||||
`检测到本机上一个账号${detail}。是否导入到当前账号?`
|
||||
+ '仅在同一台电脑、同一 Windows 用户下、且确认这是你自己的配置时才选择「继承」;'
|
||||
+ '服务端已有的密钥/代理不会被覆盖,后台锁定的代理也不会继承。',
|
||||
'本机配置继承确认',
|
||||
{
|
||||
type: 'warning',
|
||||
confirmButtonText: '继承',
|
||||
cancelButtonText: '不继承',
|
||||
distinguishCancelAndClose: true,
|
||||
},
|
||||
).then(confirmed, declined)
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
try {
|
||||
username.value = window.localStorage.getItem('username') || ''
|
||||
} catch {
|
||||
/* 忽略存储异常 */
|
||||
}
|
||||
void promptPendingInheritance()
|
||||
})
|
||||
|
||||
/**
|
||||
|
||||
@@ -74,7 +74,16 @@ export interface DesktopConfigUpdate {
|
||||
proxy_url?: string;
|
||||
proxy_mode?: ProxyMode;
|
||||
current_uid?: string;
|
||||
/** 全局 proxy_url 的归属 uid(CR-04:只有 owner 匹配才允许回退)。 */
|
||||
proxy_uid?: string;
|
||||
/** 显式共享白名单:整机共享或指定 uid 列表(CR-04)。 */
|
||||
proxy_shared?: boolean;
|
||||
proxy_shared_uids?: string[];
|
||||
proxy_users?: Record<string, { proxy_url?: string; proxy_mode?: number }>;
|
||||
/** 本机继承快照的作用域标记(CR-12:同一 app.json 才能继承)。 */
|
||||
api_secret_os_scope?: string;
|
||||
/** 登出时留给本机下个账号的密钥快照(结构由 shared/utils/api-secret-store 定义)。 */
|
||||
api_secret_inherit?: Record<string, unknown> | null;
|
||||
aliprice_usename?: string;
|
||||
aliprice_pwd?: string;
|
||||
}
|
||||
|
||||
@@ -442,15 +442,16 @@ function proxyUserId() {
|
||||
}
|
||||
|
||||
/** 代理地址按登录用户隔离:proxy_users[uid],各自计费各自复用;
|
||||
* 未登录(uid=0)回退全局 proxy_url(兼容旧版本已保存的配置)。 */
|
||||
* 用户项为空(含老版本只存了全局 proxy_url 的账号)回退全局,口径与客户端任务链
|
||||
* `resolve_current_proxy` 一致——否则这些账号的代理既显示不出来,也补报不到服务端。 */
|
||||
function readUserProxy(config: Record<string, unknown> | null | undefined) {
|
||||
const uid = proxyUserId()
|
||||
if (uid !== '0') {
|
||||
const users = (config?.proxy_users ?? {}) as Record<string, unknown>
|
||||
const own = (users[uid] ?? {}) as Record<string, unknown>
|
||||
return {
|
||||
url: typeof own.proxy_url === 'string' ? own.proxy_url : '',
|
||||
mode: Number(own.proxy_mode) === 2 ? 2 : 1,
|
||||
const ownUrl = typeof own.proxy_url === 'string' ? own.proxy_url.trim() : ''
|
||||
if (ownUrl) {
|
||||
return { url: ownUrl, mode: Number(own.proxy_mode) === 2 ? 2 : 1 }
|
||||
}
|
||||
}
|
||||
return {
|
||||
|
||||
@@ -0,0 +1,289 @@
|
||||
/**
|
||||
* 本机账号切换时的密钥继承:A 登出时留快照,B 登录后**经用户显式确认**只补 B 服务端空缺的模块。
|
||||
*
|
||||
* 安全口径(CR-12):
|
||||
* - 不再静默继承:doLoad 只把可继承的快照登记成 pending,必须由设置页确认后才调 migrate;
|
||||
* - 同 OS 用户限制:快照只从桌面原生 app.json 读,且带 `osUserScope`(该文件自己的作用域标记)。
|
||||
* 作用域缺失/不一致(例如把别的 Windows 用户的 app.json 拷过来)一律视为无效并清键;
|
||||
* - 后台锁定的代理不参与继承:服务端已锁定时客户端不得再用上一个账号的出口;
|
||||
* - 无效/过期(>30 天)/结构不符的快照立即从 app.json 清掉,不让明文密钥无限期留在本机;
|
||||
* - 每个 await 之后都比 uid 与 epoch:登出/切号期间晚到的响应不得写进新账号。
|
||||
*
|
||||
* 这里不直接读写密钥镜像或服务端接口,全部经 host 回调交给 api-secret-store:
|
||||
* 两边共用同一份内存/镜像与「当前账号」,又不会互相 import 成环。
|
||||
* 只服务桌面客户端(由 host.isDesktopStorage 判定);网页形态不参与。
|
||||
*/
|
||||
import { migrateMyApiSecrets, type UserApiSecretItem } from '../api/types/modules/user-secret.ts'
|
||||
import { withTimeout } from './promise-timeout.ts'
|
||||
|
||||
export type SecretProxyMode = 1 | 2
|
||||
|
||||
/** 本机账号切换后继承到的配置摘要:供设置页提示「继承了什么、还需检测」。 */
|
||||
export interface ApiSecretInheritanceNotice {
|
||||
fromUid: string
|
||||
fromUsername: string
|
||||
moduleKeys: string[]
|
||||
proxy: boolean
|
||||
}
|
||||
|
||||
/** 待确认的继承快照摘要:设置页据此弹确认框,确认前不写服务端。 */
|
||||
export interface PendingApiSecretInheritance {
|
||||
fromUid: string
|
||||
fromUsername: string
|
||||
savedAt: number
|
||||
moduleKeys: string[]
|
||||
proxy: boolean
|
||||
}
|
||||
|
||||
export interface ApiSecretInheritanceHost {
|
||||
currentUid(): string
|
||||
/** 登出/切号/清缓存都会 +1;与 uid 一起作为「响应是否还属于当前会话」的判据。 */
|
||||
currentEpoch(): number
|
||||
isDesktopStorage(): boolean
|
||||
/** 参与继承的模块 key(服务端必填清单 + 默认清单)。 */
|
||||
moduleKeys(): Set<string>
|
||||
readSecret(moduleKey: string): string
|
||||
writeSecret(moduleKey: string, value: string): void
|
||||
readConfig(): Promise<Record<string, unknown> | null>
|
||||
saveConfig(patch: Record<string, unknown>): Promise<unknown>
|
||||
readLocalProxy(
|
||||
config: Record<string, unknown> | null | undefined,
|
||||
uid: string,
|
||||
): { url: string; mode: SecretProxyMode }
|
||||
}
|
||||
|
||||
export interface InheritanceOutcome {
|
||||
count: number
|
||||
notice: ApiSecretInheritanceNotice | null
|
||||
/** 快照是否已被消费/清掉;false 表示会话已变或失败,调用方应保留 pending 以便重试。 */
|
||||
cleared: boolean
|
||||
}
|
||||
|
||||
export type InheritedSecretStash = {
|
||||
schema: 2
|
||||
fromUid: string
|
||||
fromUsername: string
|
||||
savedAt: number
|
||||
osUserScope: string
|
||||
secrets: Record<string, string>
|
||||
proxyUrl: string
|
||||
proxyMode: SecretProxyMode
|
||||
}
|
||||
|
||||
export type InheritanceStashRead =
|
||||
| { state: 'none' }
|
||||
| { state: 'invalid' }
|
||||
| { state: 'valid'; stash: InheritedSecretStash }
|
||||
|
||||
const INHERIT_STASH_KEY = 'api_secret_inherit'
|
||||
/** 作用域标记住在同一个 app.json 里;app.json 本身按 OS 用户分目录,标记用于拒绝跨文件搬来的快照。 */
|
||||
const OS_SCOPE_KEY = 'api_secret_os_scope'
|
||||
const INHERIT_SCHEMA = 2
|
||||
/** 继承快照的有效期:太久以前的登出记录不再往新账号上写(避免半年前的密钥复活)。 */
|
||||
const INHERIT_MAX_AGE_MS = 30 * 24 * 60 * 60 * 1000
|
||||
/** 原生桥单次调用预算:登出不能被一个卡死的 read_config/save_config 拖住。 */
|
||||
const BRIDGE_TIMEOUT_MS = 1000
|
||||
|
||||
function asRecord(value: unknown): Record<string, unknown> | null {
|
||||
return value && typeof value === 'object' && !Array.isArray(value)
|
||||
? value as Record<string, unknown>
|
||||
: null
|
||||
}
|
||||
|
||||
function text(value: unknown): string {
|
||||
return typeof value === 'string' ? value.trim() : ''
|
||||
}
|
||||
|
||||
function newOsScope(): string {
|
||||
const cryptoBox = typeof globalThis !== 'undefined'
|
||||
? (globalThis as { crypto?: { randomUUID?: () => string } }).crypto
|
||||
: undefined
|
||||
if (cryptoBox?.randomUUID) return cryptoBox.randomUUID()
|
||||
return `${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 10)}`
|
||||
}
|
||||
|
||||
/** 当前 app.json 的作用域标记;缺失时由 capture 首次写入。 */
|
||||
export function readOsUserScope(config: Record<string, unknown> | null | undefined): string {
|
||||
return text(config?.[OS_SCOPE_KEY])
|
||||
}
|
||||
|
||||
function sessionCurrent(host: ApiSecretInheritanceHost, uid: string, epoch: number): boolean {
|
||||
return host.currentUid() === uid && host.currentEpoch() === epoch
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析继承快照。结构不符、过期、作用域不一致一律返回 invalid:
|
||||
* 调用方必须立即把 `api_secret_inherit` 清掉,不能让明文继续留在 app.json。
|
||||
*/
|
||||
export function readInheritedSecretStash(
|
||||
config: Record<string, unknown> | null | undefined,
|
||||
allowed: Set<string>,
|
||||
): InheritanceStashRead {
|
||||
const raw = config?.[INHERIT_STASH_KEY]
|
||||
if (raw == null) return { state: 'none' }
|
||||
const record = asRecord(raw)
|
||||
if (!record) return { state: 'invalid' }
|
||||
if (record.schema !== INHERIT_SCHEMA) return { state: 'invalid' }
|
||||
const fromUid = text(record.fromUid)
|
||||
const savedAt = typeof record.savedAt === 'number' ? record.savedAt : 0
|
||||
const osUserScope = text(record.osUserScope)
|
||||
const currentScope = readOsUserScope(config)
|
||||
if (!fromUid || !savedAt || !osUserScope || !currentScope) return { state: 'invalid' }
|
||||
// 同一份 app.json 内 scope 必须对得上;未来时间戳同样视为结构异常
|
||||
if (osUserScope !== currentScope) return { state: 'invalid' }
|
||||
if (savedAt > Date.now() + 5 * 60 * 1000) return { state: 'invalid' }
|
||||
if (Date.now() - savedAt > INHERIT_MAX_AGE_MS) return { state: 'invalid' }
|
||||
const secretRecord = asRecord(record.secrets)
|
||||
if (!secretRecord) return { state: 'invalid' }
|
||||
const proxyRaw = record.proxyUrl
|
||||
if (proxyRaw != null && typeof proxyRaw !== 'string') return { state: 'invalid' }
|
||||
const secrets: Record<string, string> = {}
|
||||
for (const [moduleKey, value] of Object.entries(secretRecord)) {
|
||||
if (allowed.has(moduleKey) && typeof value === 'string' && value.trim()) {
|
||||
secrets[moduleKey] = value.trim()
|
||||
}
|
||||
}
|
||||
const proxyUrl = text(proxyRaw)
|
||||
if (!Object.keys(secrets).length && !proxyUrl) return { state: 'invalid' }
|
||||
return {
|
||||
state: 'valid',
|
||||
stash: {
|
||||
schema: 2,
|
||||
fromUid,
|
||||
fromUsername: text(record.fromUsername),
|
||||
savedAt,
|
||||
osUserScope,
|
||||
secrets,
|
||||
proxyUrl,
|
||||
proxyMode: Number(record.proxyMode) === 2 ? 2 : 1,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/** 设置页确认框用的摘要(不含明文)。 */
|
||||
export function toPendingInheritance(stash: InheritedSecretStash): PendingApiSecretInheritance {
|
||||
return {
|
||||
fromUid: stash.fromUid,
|
||||
fromUsername: stash.fromUsername,
|
||||
savedAt: stash.savedAt,
|
||||
moduleKeys: Object.keys(stash.secrets),
|
||||
proxy: Boolean(stash.proxyUrl),
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 登出瞬间把当前账号的密钥与代理留一份到客户端 app.json,供本机下一个账号**确认后**继承。
|
||||
* 调用方必须在本页内存/本地镜像被清掉、且 uid 被移除之前调用。
|
||||
*/
|
||||
export async function captureSecretsForInheritance(host: ApiSecretInheritanceHost): Promise<void> {
|
||||
if (typeof window === 'undefined' || !host.isDesktopStorage()) return
|
||||
const uid = host.currentUid()
|
||||
if (uid === '0') return
|
||||
const epoch = host.currentEpoch()
|
||||
const secrets: Record<string, string> = {}
|
||||
for (const moduleKey of host.moduleKeys()) {
|
||||
const value = host.readSecret(moduleKey).trim()
|
||||
if (value) secrets[moduleKey] = value
|
||||
}
|
||||
const config = await withTimeout(host.readConfig(), BRIDGE_TIMEOUT_MS).catch((error) => {
|
||||
console.warn('[api-secret] 登出时读取本机配置失败,继承快照将不含代理:', error)
|
||||
return null
|
||||
})
|
||||
// 等配置期间可能已经被登出清理:旧账号的快照不能盖到新账号的作用域上
|
||||
if (!sessionCurrent(host, uid, epoch)) return
|
||||
const proxy = host.readLocalProxy(config, uid)
|
||||
const osUserScope = readOsUserScope(config) || newOsScope()
|
||||
const stash: InheritedSecretStash | null = Object.keys(secrets).length || proxy.url
|
||||
? {
|
||||
schema: 2,
|
||||
fromUid: uid,
|
||||
fromUsername: window.localStorage.getItem('username') || '',
|
||||
savedAt: Date.now(),
|
||||
osUserScope,
|
||||
secrets,
|
||||
proxyUrl: proxy.url,
|
||||
proxyMode: proxy.mode,
|
||||
}
|
||||
: null
|
||||
await withTimeout(host.saveConfig({ [OS_SCOPE_KEY]: osUserScope, [INHERIT_STASH_KEY]: stash }), BRIDGE_TIMEOUT_MS)
|
||||
.catch((error) => {
|
||||
console.warn('[api-secret] 登出时保存继承快照失败(不影响登出):', error)
|
||||
})
|
||||
}
|
||||
|
||||
/** 立即清掉 app.json 里的继承快照(无效快照、用户拒绝、已消费三条路径共用)。 */
|
||||
export async function clearInheritedSecrets(host: ApiSecretInheritanceHost): Promise<void> {
|
||||
if (typeof window === 'undefined' || !host.isDesktopStorage()) return
|
||||
await withTimeout(host.saveConfig({ [INHERIT_STASH_KEY]: null }), BRIDGE_TIMEOUT_MS).catch((error) => {
|
||||
console.warn('[api-secret] 清理本机继承快照失败:', error)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 应用已确认的继承快照:只补当前账号服务端空缺的模块,已有值一律不覆盖
|
||||
* (服务端 migrate 接口本身也是 fill-only,后台锁定的代理不会被顶掉)。
|
||||
* 检测状态不继承:那不是配置,不能跨账号冒充已通过。
|
||||
*
|
||||
* 每个 await 之后都同时比 uid 与 epoch:确认框打开期间用户可能已经切号/登出,
|
||||
* 晚到的 migrate 响应或写回都不能落到新账号。
|
||||
*/
|
||||
export async function applyInheritedSecrets(
|
||||
host: ApiSecretInheritanceHost,
|
||||
stash: InheritedSecretStash,
|
||||
config: Record<string, unknown> | null | undefined,
|
||||
serverItems: UserApiSecretItem[],
|
||||
serverProxyItem: UserApiSecretItem | null | undefined,
|
||||
): Promise<InheritanceOutcome> {
|
||||
if (typeof window === 'undefined' || !host.isDesktopStorage()) {
|
||||
return { count: 0, notice: null, cleared: false }
|
||||
}
|
||||
const uid = host.currentUid()
|
||||
if (uid === '0') return { count: 0, notice: null, cleared: false }
|
||||
const epoch = host.currentEpoch()
|
||||
const serverHas = new Set(serverItems.filter((item) => item?.exists).map((item) => item.moduleKey))
|
||||
const pending: Array<{ moduleKey: string; value: string }> = []
|
||||
for (const [moduleKey, value] of Object.entries(stash.secrets)) {
|
||||
if (!serverHas.has(moduleKey)) pending.push({ moduleKey, value })
|
||||
}
|
||||
// 本机已有代理时以本机为准:它会被代理补报逻辑上报给当前账号,
|
||||
// 这里再塞上一个账号的代理会让「服务端查商标」和「客户端跑任务」走两条不同的出口。
|
||||
// 服务端已存在/后台已锁定的代理同样不继承(CR-12:locked 代理必须排除)。
|
||||
const ownProxy = host.readLocalProxy(config, uid).url
|
||||
const inheritProxy = Boolean(stash.proxyUrl)
|
||||
&& Boolean(config)
|
||||
&& !serverProxyItem?.exists
|
||||
&& !serverProxyItem?.locked
|
||||
&& !ownProxy
|
||||
if (inheritProxy) pending.push({ moduleKey: 'proxy', value: stash.proxyUrl })
|
||||
if (!pending.length) {
|
||||
await clearInheritedSecrets(host)
|
||||
return { count: 0, notice: null, cleared: true }
|
||||
}
|
||||
const result = await migrateMyApiSecrets(pending).catch((error) => {
|
||||
console.warn('[api-secret] 继承本机账号密钥失败(下次确认时重试):', error)
|
||||
return null
|
||||
})
|
||||
if (!sessionCurrent(host, uid, epoch)) return { count: 0, notice: null, cleared: false }
|
||||
const migrated = Number(result?.migrated || 0)
|
||||
if (!result || migrated <= 0) return { count: 0, notice: null, cleared: false }
|
||||
// migrate 的 await 结束后已经重新比过 uid/epoch,这里才允许把明文写回当前会话的本地镜像
|
||||
for (const item of pending) {
|
||||
if (item.moduleKey !== 'proxy') host.writeSecret(item.moduleKey, item.value)
|
||||
}
|
||||
const patch: Record<string, unknown> = { [INHERIT_STASH_KEY]: null }
|
||||
if (inheritProxy) {
|
||||
patch.proxy_users = { [uid]: { proxy_url: stash.proxyUrl, proxy_mode: stash.proxyMode } }
|
||||
}
|
||||
await host.saveConfig(patch).catch((error) => {
|
||||
console.warn('[api-secret] 继承配置已写入服务端,但写回本机配置失败:', error)
|
||||
})
|
||||
if (!sessionCurrent(host, uid, epoch)) return { count: 0, notice: null, cleared: false }
|
||||
const notice: ApiSecretInheritanceNotice = {
|
||||
fromUid: stash.fromUid,
|
||||
fromUsername: stash.fromUsername,
|
||||
moduleKeys: pending.filter((item) => item.moduleKey !== 'proxy').map((item) => item.moduleKey),
|
||||
proxy: inheritProxy,
|
||||
}
|
||||
console.log(`[api-secret] 已确认继承本机账号 ${stash.fromUsername || stash.fromUid} 的 ${migrated} 项配置`)
|
||||
return { count: migrated, notice, cleared: true }
|
||||
}
|
||||
@@ -18,11 +18,29 @@ import {
|
||||
putMyApiSecret,
|
||||
type ApiSecretCheckStatus,
|
||||
type ApiSecretModuleKey,
|
||||
type UserApiSecretBundle,
|
||||
type UserApiSecretCheckResult,
|
||||
type UserApiSecretItem,
|
||||
} from '../api/types/modules/user-secret.ts'
|
||||
import {
|
||||
applyInheritedSecrets,
|
||||
captureSecretsForInheritance,
|
||||
clearInheritedSecrets,
|
||||
readInheritedSecretStash,
|
||||
toPendingInheritance,
|
||||
type ApiSecretInheritanceHost,
|
||||
type ApiSecretInheritanceNotice,
|
||||
type InheritedSecretStash,
|
||||
type PendingApiSecretInheritance,
|
||||
} from './api-secret-inheritance.ts'
|
||||
import { withTimeout } from './promise-timeout.ts'
|
||||
import type { DesktopConfigUpdate } from '../bridges/pywebview.ts'
|
||||
|
||||
export type { ApiSecretModuleKey, ApiSecretCheckStatus }
|
||||
export type {
|
||||
ApiSecretInheritanceNotice,
|
||||
PendingApiSecretInheritance,
|
||||
} from './api-secret-inheritance.ts'
|
||||
|
||||
export type { UserApiSecretCheckResult }
|
||||
|
||||
@@ -54,6 +72,12 @@ export interface ServerProxySnapshot extends ApiSecretSnapshot {
|
||||
full: string
|
||||
}
|
||||
|
||||
/** 商标检测前置代理检查结果:unknown 表示服务端读不到,按软失败放行(与密钥门禁同口径)。 */
|
||||
export interface BrandCheckProxyStatus {
|
||||
ok: boolean
|
||||
state: 'server' | 'missing' | 'unknown'
|
||||
}
|
||||
|
||||
interface ApiSecretCacheEntry extends ApiSecretSnapshot {
|
||||
schema: 2
|
||||
}
|
||||
@@ -64,6 +88,10 @@ const LEGACY_MODULE_KEYS = ['appearance-patent', 'appearance-patent-token', 'sim
|
||||
const MIGRATED_FLAG_PREFIX = 'brand:api-secret-migrated'
|
||||
const DEFAULT_REQUIRED_MODULES: ApiSecretModuleKey[] = ['appearance-patent', 'similar-asin']
|
||||
const FETCH_TIMEOUT_MS = 4000
|
||||
/** 登出继承快照的整体预算:原生桥卡死也必须让登出清理继续走。 */
|
||||
const CAPTURE_TIMEOUT_MS = 2500
|
||||
/** 读/写 app.json 的单次预算:doLoad 不能被一个永不返回的桥调用挂住。 */
|
||||
const CONFIG_TIMEOUT_MS = 3000
|
||||
|
||||
const memory = new Map<string, ApiSecretCacheEntry>()
|
||||
const listeners = new Set<() => void>()
|
||||
@@ -84,10 +112,40 @@ let bundleLoaded = false
|
||||
let inflightLoad: Promise<ApiSecretLoadState> | null = null
|
||||
/** 服务端代理状态(后台锁定/检测展示用);未成功加载过为 null。 */
|
||||
let serverProxy: ServerProxySnapshot | null = null
|
||||
/** 本次登录继承到的配置摘要:设置页消费一次,不落盘(避免多一个明文旁路)。 */
|
||||
let inheritanceNotice: ApiSecretInheritanceNotice | null = null
|
||||
/**
|
||||
* 本机上一个账号留下、等待用户显式确认的继承快照(CR-12)。
|
||||
* 确认前不写服务端、不写本地镜像;登出/切号时连同摘要一起作废。
|
||||
*/
|
||||
let pendingInheritance: {
|
||||
summary: PendingApiSecretInheritance
|
||||
stash: InheritedSecretStash
|
||||
} | null = null
|
||||
/** 登出/切号会让在飞的密钥加载失效,防止 A 的响应落到 B 的会话里。 */
|
||||
let cacheEpoch = 0
|
||||
/** 上一次读到的登录 uid:会话过期后直接换号登录时,内存里的旧账号明文必须就地作废。 */
|
||||
let cachedUid = ''
|
||||
|
||||
function currentUid(): string {
|
||||
if (typeof window === 'undefined') return '0'
|
||||
return window.localStorage.getItem('uid') || '0'
|
||||
const uid = window.localStorage.getItem('uid') || '0'
|
||||
if (cachedUid && cachedUid !== uid) {
|
||||
memory.clear()
|
||||
bundleLoaded = false
|
||||
inflightLoad = null
|
||||
serverProxy = null
|
||||
inheritanceNotice = null
|
||||
pendingInheritance = null
|
||||
cacheEpoch += 1
|
||||
}
|
||||
cachedUid = uid
|
||||
return uid
|
||||
}
|
||||
|
||||
/** 响应回写前的统一闸门:uid 与 epoch 必须同时还是发起请求时那一个。 */
|
||||
function sessionCurrent(uid: string, epoch: number): boolean {
|
||||
return currentUid() === uid && cacheEpoch === epoch
|
||||
}
|
||||
|
||||
function buildMirrorKey(moduleKey: string) {
|
||||
@@ -327,6 +385,22 @@ export function getServerProxySnapshot(): ServerProxySnapshot | null {
|
||||
return serverProxy
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建商标检测任务前的代理检查:服务端没有代理时先尝试把本地代理补报上去,
|
||||
* 仍没有就返回 missing 交给页面弹窗拦截。服务端读不到(unknown)不拦——
|
||||
* 那种情况下任务创建本身会报错,不能把网络抖动误判成"未配置代理"。
|
||||
*/
|
||||
export async function ensureBrandCheckProxy(): Promise<BrandCheckProxyStatus> {
|
||||
const state = await loadApiSecrets({ force: true })
|
||||
if (getServerProxySnapshot()?.exists) {
|
||||
return { ok: true, state: 'server' }
|
||||
}
|
||||
if (state === 'unknown') {
|
||||
return { ok: true, state: 'unknown' }
|
||||
}
|
||||
return { ok: false, state: 'missing' }
|
||||
}
|
||||
|
||||
export function subscribeApiSecrets(listener: () => void): () => void {
|
||||
listeners.add(listener)
|
||||
return () => {
|
||||
@@ -363,35 +437,71 @@ export function ensureApiSecretsLoaded(): Promise<ApiSecretLoadState> {
|
||||
return loadApiSecrets()
|
||||
}
|
||||
|
||||
async function applyServerBundle(
|
||||
bundle: UserApiSecretBundle | null | undefined,
|
||||
uid: string,
|
||||
epoch: number,
|
||||
): Promise<void> {
|
||||
// 先用调用方发请求时的 uid/epoch 验身,再动内存/镜像:
|
||||
// 不能在这里重新 currentUid()——那会把「已经换成 B」当成合法会话,把 A 的包写进 B。
|
||||
if (!sessionCurrent(uid, epoch)) return
|
||||
if (Array.isArray(bundle?.requiredModules) && bundle.requiredModules.length) {
|
||||
requiredModules = bundle.requiredModules
|
||||
}
|
||||
for (const item of bundle?.items || []) {
|
||||
if (item?.moduleKey) {
|
||||
applyServerItem(item.moduleKey, item)
|
||||
}
|
||||
}
|
||||
serverProxy = toServerProxy(bundle?.proxy)
|
||||
// 登出/换号可能发生在读配置的等待里:旧账号的锁定代理绝不能写进新账号的本地配置
|
||||
if (!sessionCurrent(uid, epoch)) return
|
||||
if (serverProxy?.locked && serverProxy.full) {
|
||||
// 后台锁定代理:把后台值对齐到本地配置(客户端任务链读本地),先比对、幂等、失败静默
|
||||
await syncLockedProxyToLocal(serverProxy.full, uid, epoch)
|
||||
// write 之前再比一次:sync 内部的 await 期间可能已经切号/登出
|
||||
if (!sessionCurrent(uid, epoch)) return
|
||||
}
|
||||
}
|
||||
|
||||
async function doLoad(): Promise<ApiSecretLoadState> {
|
||||
// 换号(含会话过期后直接登录 B)时先作废旧账号缓存,再取 epoch:
|
||||
// 反序会把本次加载自己判成“过期响应”,继承与门禁都走不到。
|
||||
const uid = currentUid()
|
||||
const epoch = cacheEpoch
|
||||
try {
|
||||
const bundle = await fetchMyApiSecrets()
|
||||
if (Array.isArray(bundle?.requiredModules) && bundle.requiredModules.length) {
|
||||
requiredModules = bundle.requiredModules
|
||||
}
|
||||
for (const item of bundle?.items || []) {
|
||||
if (item?.moduleKey) {
|
||||
applyServerItem(item.moduleKey, item)
|
||||
}
|
||||
}
|
||||
serverProxy = toServerProxy(bundle?.proxy)
|
||||
if (serverProxy?.locked && serverProxy.full) {
|
||||
// 后台锁定代理:把后台值对齐到本地配置(客户端任务链读本地),先比对、幂等、失败静默
|
||||
await syncLockedProxyToLocal(serverProxy.full)
|
||||
}
|
||||
let bundle = await fetchMyApiSecrets()
|
||||
if (!sessionCurrent(uid, epoch)) return 'unknown'
|
||||
await applyServerBundle(bundle, uid, epoch)
|
||||
if (!sessionCurrent(uid, epoch)) return 'unknown'
|
||||
bundleLoaded = true
|
||||
notify()
|
||||
const migrated = await tryMigrateLocalSecrets(bundle?.items || [])
|
||||
const migrated = await tryMigrateLocalSecrets(bundle?.items || [], uid, epoch)
|
||||
if (!sessionCurrent(uid, epoch)) return 'unknown'
|
||||
if (migrated > 0) {
|
||||
const refreshed = await fetchMyApiSecrets()
|
||||
for (const item of refreshed?.items || []) {
|
||||
if (item?.moduleKey) {
|
||||
applyServerItem(item.moduleKey, item)
|
||||
}
|
||||
}
|
||||
bundle = await fetchMyApiSecrets()
|
||||
if (!sessionCurrent(uid, epoch)) return 'unknown'
|
||||
await applyServerBundle(bundle, uid, epoch)
|
||||
if (!sessionCurrent(uid, epoch)) return 'unknown'
|
||||
notify()
|
||||
}
|
||||
// 继承必须排在旧本地密钥迁移之后:两者都走 fill-only,谁先占住服务端空缺谁生效,
|
||||
// 而当前账号自己的旧记录理应优先于上一个账号留下的快照。
|
||||
// CR-12:这里只登记待确认快照,不再自动 migrate——明文是否跨账号复用必须由用户拍板。
|
||||
if (bundle && Array.isArray(bundle.items)) {
|
||||
await stageInheritance(bundle.items, bundle.proxy, uid, epoch)
|
||||
if (!sessionCurrent(uid, epoch)) return 'unknown'
|
||||
}
|
||||
const backfilled = await tryBackfillProxyToServer(uid, epoch)
|
||||
if (!sessionCurrent(uid, epoch)) return 'unknown'
|
||||
if (backfilled) {
|
||||
// 补报已写入服务端:重拉一次让 serverProxy 反映新状态,
|
||||
// 否则任务提交前的代理检查会拿到上一轮的空快照。
|
||||
const refreshed = await fetchMyApiSecrets()
|
||||
if (!sessionCurrent(uid, epoch)) return 'unknown'
|
||||
serverProxy = toServerProxy(refreshed?.proxy)
|
||||
notify()
|
||||
}
|
||||
await tryBackfillProxyToServer()
|
||||
return computeApiSecretsComplete() ? 'complete' : 'incomplete'
|
||||
} catch (error) {
|
||||
console.warn('[api-secret] 服务端密钥拉取失败,本次按未知处理(不阻断使用):', error)
|
||||
@@ -400,7 +510,11 @@ async function doLoad(): Promise<ApiSecretLoadState> {
|
||||
}
|
||||
|
||||
/** 旧本地密钥一次性迁移:仅当服务端空缺且有本地明文时上报;失败静默,下次加载重试。 */
|
||||
async function tryMigrateLocalSecrets(serverItems: UserApiSecretItem[]): Promise<number> {
|
||||
async function tryMigrateLocalSecrets(
|
||||
serverItems: UserApiSecretItem[],
|
||||
uid: string,
|
||||
epoch: number,
|
||||
): Promise<number> {
|
||||
if (typeof window === 'undefined') return 0
|
||||
try {
|
||||
if (window.localStorage.getItem(migratedFlagKey()) === '1') {
|
||||
@@ -422,6 +536,8 @@ async function tryMigrateLocalSecrets(serverItems: UserApiSecretItem[]): Promise
|
||||
return 0
|
||||
}
|
||||
const result = await migrateMyApiSecrets(pending)
|
||||
// 等接口期间可能已登出/切号:旧账号的明文不能再写进当前账号的本地镜像
|
||||
if (!sessionCurrent(uid, epoch)) return 0
|
||||
const migrated = Number(result?.migrated || 0)
|
||||
// 迁移成功后本地保留明文(供提交任务直接使用),只清理旧记录与标记
|
||||
for (const item of pending) {
|
||||
@@ -440,33 +556,172 @@ async function tryMigrateLocalSecrets(serverItems: UserApiSecretItem[]): Promise
|
||||
|
||||
/**
|
||||
* 本地代理配置补报服务端(只写空缺、不覆盖;后台密钥管理观测/检测用)。
|
||||
* 非桌面环境、未登录、无本地代理时静默跳过;失败静默,下次加载重试。
|
||||
* 桥模块动态导入:node 测试环境不触发(currentUid 为 0 时直接返回),避免加载浏览器侧依赖。
|
||||
* 未登录、无本地代理时静默跳过;失败静默,下次加载重试。
|
||||
*
|
||||
* @returns 是否真的补报了一条(migrated > 0);调用方据此决定要不要重拉服务端快照。
|
||||
*/
|
||||
async function tryBackfillProxyToServer(): Promise<void> {
|
||||
async function tryBackfillProxyToServer(uid: string, epoch: number): Promise<boolean> {
|
||||
try {
|
||||
if (currentUid() === '0') return
|
||||
const { getPywebviewApi } = await import('../bridges/pywebview.ts')
|
||||
const api = getPywebviewApi()
|
||||
if (!api?.read_config) return
|
||||
const config = (await api.read_config()) as Record<string, unknown> | null
|
||||
const url = readLocalProxyUrl(config)
|
||||
if (!url) return
|
||||
await migrateMyApiSecrets([{ moduleKey: 'proxy', value: url }])
|
||||
if (uid === '0') return false
|
||||
const config = await withTimeout(readConfigForBackfill(), CONFIG_TIMEOUT_MS).catch((error) => {
|
||||
console.warn('[api-secret] 读取本机代理配置失败(下次加载重试):', error)
|
||||
return null
|
||||
})
|
||||
if (!config) return false
|
||||
// 等配置期间可能已经登出/换号:补报请求会带新账号令牌,必须放弃旧账号的本地代理
|
||||
if (!sessionCurrent(uid, epoch)) return false
|
||||
// 升级一次性归属迁移:老版本只存全局 proxy_url、没有 proxy_uid。
|
||||
// 第一个读到它的账号把它认领成自己的(写入 proxy_users[uid] + proxy_uid 并清空全局),
|
||||
// 之后其他账号再登录就不会静默回退到这个代理(CR-04)。
|
||||
const ownedConfig = await claimLegacyGlobalProxy(config, uid)
|
||||
if (!sessionCurrent(uid, epoch)) return false
|
||||
const url = readLocalProxyUrl(ownedConfig, uid)
|
||||
if (!url) return false
|
||||
const result = await migrateMyApiSecrets([{ moduleKey: 'proxy', value: url }])
|
||||
if (!sessionCurrent(uid, epoch)) return false
|
||||
return Number(result?.migrated || 0) > 0
|
||||
} catch (error) {
|
||||
console.warn('[api-secret] 代理配置补报失败(下次加载重试):', error)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/** 读取当前登录用户的本地代理地址:proxy_users[uid] 优先,未登录回退全局 proxy_url。 */
|
||||
function readLocalProxyUrl(config: Record<string, unknown> | null | undefined): string {
|
||||
const uid = currentUid()
|
||||
if (uid !== '0') {
|
||||
const users = (config?.proxy_users ?? {}) as Record<string, unknown>
|
||||
const own = (users[uid] ?? {}) as Record<string, unknown>
|
||||
return typeof own.proxy_url === 'string' ? own.proxy_url.trim() : ''
|
||||
/**
|
||||
* 读代理补报所需的本地配置:桌面真实桥优先(与 syncLockedProxyToLocal 同口径),
|
||||
* 纯浏览器再走降级桥。静态 import 会把 web-fallback 的 @ 别名依赖拖进 node 用例,
|
||||
* 所以降级那条只在需要时才动态加载。
|
||||
*/
|
||||
async function readConfigForBackfill(): Promise<Record<string, unknown> | null> {
|
||||
if (typeof window === 'undefined') return null
|
||||
const direct = window.pywebview?.api
|
||||
if (direct?.read_config) {
|
||||
return (await direct.read_config()) as Record<string, unknown> | null
|
||||
}
|
||||
return typeof config?.proxy_url === 'string' ? config.proxy_url.trim() : ''
|
||||
// 无 DOM 的 node 用例不是浏览器环境:降级桥模块带着 @ 别名依赖,加载只会留下噪声报错
|
||||
if (typeof document === 'undefined') return null
|
||||
const { getPywebviewApi } = await import('../bridges/pywebview.ts')
|
||||
const api = getPywebviewApi()
|
||||
if (!api?.read_config) return null
|
||||
return (await api.read_config()) as Record<string, unknown> | null
|
||||
}
|
||||
|
||||
/** 写回本机配置:与 readConfigForBackfill 同一套桥选择;写成功返回 true。 */
|
||||
async function saveConfigForBackfill(patch: Record<string, unknown>): Promise<boolean> {
|
||||
if (typeof window === 'undefined') return false
|
||||
const direct = window.pywebview?.api
|
||||
if (direct?.save_config) {
|
||||
await direct.save_config(patch as DesktopConfigUpdate)
|
||||
return true
|
||||
}
|
||||
if (typeof document === 'undefined') return false
|
||||
const { getPywebviewApi } = await import('../bridges/pywebview.ts')
|
||||
const api = getPywebviewApi()
|
||||
if (!api?.save_config) return false
|
||||
await api.save_config(patch as DesktopConfigUpdate)
|
||||
return true
|
||||
}
|
||||
|
||||
/**
|
||||
* 无归属的全局 proxy_url 一次性归给当前 uid(CR-04)。
|
||||
*
|
||||
* 老版本把代理存在全局 proxy_url(2026-10-25 账号 1210 的 32300/32309 就靠这条路径补报),
|
||||
* 但全局字段本身不带归属,A 留下的代理会被 B 静默继承。升级时由第一个读到它的账号认领:
|
||||
* 写入 `proxy_users[uid]` 与 `proxy_uid`,随后清空全局 `proxy_url`。
|
||||
* 认领失败(桥没有 save_config 或写入抛错)时不回退——宁可不补报,也不把出口写给别的账号。
|
||||
*/
|
||||
async function claimLegacyGlobalProxy(
|
||||
config: Record<string, unknown> | null | undefined,
|
||||
uid: string,
|
||||
): Promise<Record<string, unknown> | null | undefined> {
|
||||
if (!config || uid === '0') return config
|
||||
const globalUrl = typeof config.proxy_url === 'string' ? config.proxy_url.trim() : ''
|
||||
const owner = typeof config.proxy_uid === 'string' ? config.proxy_uid.trim() : ''
|
||||
// 归属 0 等同未登录时的匿名全局,按"无归属"处理;只有明确归属到别的 uid 才绝不碰
|
||||
if (!globalUrl || (owner && owner !== '0')) return config
|
||||
const users = (config.proxy_users ?? {}) as Record<string, unknown>
|
||||
const own = users[uid]
|
||||
const ownRecord = own && typeof own === 'object' && !Array.isArray(own)
|
||||
? own as Record<string, unknown>
|
||||
: {}
|
||||
const ownUrl = typeof ownRecord.proxy_url === 'string' ? ownRecord.proxy_url.trim() : ''
|
||||
if (ownUrl && !isSharedProxy(config, uid)) {
|
||||
// 当前账号已有自己的代理:全局字段是历史残留,归给当前 uid 后清空,
|
||||
// 避免下一个账号把它当成"无主代理"认领走。
|
||||
// 若全局是显式共享(proxy_shared/proxy_shared_uids)则保留给其他账号。
|
||||
const cleared = await saveConfigForBackfill({ proxy_url: '', proxy_uid: uid }).catch((error) => {
|
||||
console.warn('[api-secret] 清理本机残留全局代理失败(下次加载重试):', error)
|
||||
return false
|
||||
})
|
||||
return cleared ? { ...config, proxy_url: '', proxy_uid: uid } : config
|
||||
}
|
||||
const mode = Number(config.proxy_mode) === 2 ? 2 : 1
|
||||
const saved = await saveConfigForBackfill({
|
||||
proxy_url: '',
|
||||
proxy_uid: uid,
|
||||
proxy_users: { [uid]: { ...ownRecord, proxy_url: globalUrl, proxy_mode: mode } },
|
||||
}).catch((error) => {
|
||||
console.warn('[api-secret] 全局代理归属迁移失败(不静默回退给当前账号):', error)
|
||||
return false
|
||||
})
|
||||
if (!saved) return config
|
||||
return {
|
||||
...config,
|
||||
proxy_url: '',
|
||||
proxy_uid: uid,
|
||||
proxy_users: { ...users, [uid]: { ...ownRecord, proxy_url: globalUrl, proxy_mode: mode } },
|
||||
}
|
||||
}
|
||||
|
||||
/** 显式共享白名单:`proxy_shared=true`(整机共享)或 `proxy_shared_uids` 包含当前 uid。 */
|
||||
function isSharedProxy(config: Record<string, unknown> | null | undefined, uid: string): boolean {
|
||||
if (!config) return false
|
||||
if (config.proxy_shared === true) return true
|
||||
return Array.isArray(config.proxy_shared_uids)
|
||||
&& config.proxy_shared_uids.some((item) => String(item).trim() === uid)
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取当前登录用户的本地代理地址。
|
||||
*
|
||||
* 回退顺序(CR-04):proxy_users[uid] → 全局 proxy_url **且归属就是当前 uid**
|
||||
* (或显式共享白名单 `proxy_shared=true` / `proxy_shared_uids` 包含当前 uid)。
|
||||
* 全局字段未归属/归属别人时返回空,绝不再把上一个账号的出口补报给当前账号。
|
||||
* 未登录(uid=0)只认匿名全局或共享白名单,不碰实名归属的代理。
|
||||
*/
|
||||
function readLocalProxyEntry(
|
||||
config: Record<string, unknown> | null | undefined,
|
||||
uid = currentUid(),
|
||||
): { url: string; mode: 1 | 2 } {
|
||||
if (!config) return { url: '', mode: 1 }
|
||||
const globalUrl = typeof config.proxy_url === 'string' ? config.proxy_url.trim() : ''
|
||||
const globalMode: 1 | 2 = Number(config.proxy_mode) === 2 ? 2 : 1
|
||||
if (uid !== '0') {
|
||||
const users = (config.proxy_users ?? {}) as Record<string, unknown>
|
||||
const own = (users[uid] ?? {}) as Record<string, unknown>
|
||||
const ownUrl = typeof own.proxy_url === 'string' ? own.proxy_url.trim() : ''
|
||||
if (ownUrl) {
|
||||
return { url: ownUrl, mode: Number(own.proxy_mode) === 2 ? 2 : 1 }
|
||||
}
|
||||
}
|
||||
const owner = typeof config.proxy_uid === 'string' ? config.proxy_uid.trim() : ''
|
||||
const shared = isSharedProxy(config, uid)
|
||||
if (!globalUrl) return { url: '', mode: 1 }
|
||||
if (uid === '0') {
|
||||
// 未登录没有"账号归属"可言:只认匿名全局(无归属/归属 0)或显式共享
|
||||
return !owner || owner === '0' || shared
|
||||
? { url: globalUrl, mode: globalMode }
|
||||
: { url: '', mode: 1 }
|
||||
}
|
||||
return owner === uid || shared
|
||||
? { url: globalUrl, mode: globalMode }
|
||||
: { url: '', mode: 1 }
|
||||
}
|
||||
|
||||
function readLocalProxyUrl(
|
||||
config: Record<string, unknown> | null | undefined,
|
||||
uid = currentUid(),
|
||||
): string {
|
||||
return readLocalProxyEntry(config, uid).url
|
||||
}
|
||||
|
||||
function toServerProxy(proxy: UserApiSecretItem | null | undefined): ServerProxySnapshot | null {
|
||||
@@ -491,14 +746,19 @@ function toServerProxy(proxy: UserApiSecretItem | null | undefined): ServerProxy
|
||||
* 让客户端各任务链与后台值保持一致。先读后比对、幂等;失败静默,下次加载重试。
|
||||
* 只走桌面端真实桥(window.pywebview):Web 降级桥的配置与桌面 app.json 无关。
|
||||
*/
|
||||
async function syncLockedProxyToLocal(full: string): Promise<void> {
|
||||
async function syncLockedProxyToLocal(
|
||||
full: string,
|
||||
uidAtStart: string,
|
||||
epochAtStart: number,
|
||||
): Promise<void> {
|
||||
try {
|
||||
if (typeof window === 'undefined') return
|
||||
const api = window.pywebview?.api
|
||||
if (!api?.read_config || !api?.save_config) return
|
||||
const config = (await api.read_config()) as Record<string, unknown> | null
|
||||
if (readLocalProxyUrl(config) === full) return
|
||||
const uid = currentUid()
|
||||
if (!sessionCurrent(uidAtStart, epochAtStart)) return
|
||||
if (readLocalProxyUrl(config, uidAtStart) === full) return
|
||||
const uid = uidAtStart
|
||||
if (uid === '0') {
|
||||
const mode = Number(config?.proxy_mode) === 2 ? 2 : 1
|
||||
await api.save_config({ proxy_url: full, proxy_mode: mode })
|
||||
@@ -509,11 +769,148 @@ async function syncLockedProxyToLocal(full: string): Promise<void> {
|
||||
// save_config 对 proxy_users 只做 uid 级合并(保留其他用户),故同一对象内要带齐原有模式
|
||||
const mode = Number(own.proxy_mode) === 2 ? 2 : 1
|
||||
await api.save_config({ proxy_users: { [uid]: { proxy_url: full, proxy_mode: mode } } })
|
||||
// save 的 await 之后不再有写操作;保留一次检查让"迟到响应不落到新账号"的口径可被测试钉住
|
||||
if (!sessionCurrent(uidAtStart, epochAtStart)) return
|
||||
} catch (error) {
|
||||
console.warn('[api-secret] 锁定代理写入本地配置失败(下次加载重试):', error)
|
||||
}
|
||||
}
|
||||
|
||||
const inheritanceHost: ApiSecretInheritanceHost = {
|
||||
currentUid,
|
||||
currentEpoch: () => cacheEpoch,
|
||||
isDesktopStorage: isNativeDesktopStorage,
|
||||
moduleKeys: () => new Set<string>([...DEFAULT_REQUIRED_MODULES, ...requiredModules]),
|
||||
readSecret: (moduleKey) => readEntry(moduleKey).value,
|
||||
// 只有 applyInheritedSecrets 在 migrate 的 await 之后重新比过 uid/epoch 才会调到这里
|
||||
writeSecret: (moduleKey, value) => {
|
||||
const entry = readEntry(moduleKey)
|
||||
updateEntry(moduleKey, { value: entry.value || value })
|
||||
},
|
||||
readConfig: () => readConfigForBackfill(),
|
||||
saveConfig: (patch) => saveConfigForBackfill(patch),
|
||||
readLocalProxy: readLocalProxyEntry,
|
||||
}
|
||||
|
||||
/**
|
||||
* 登出瞬间把当前账号的密钥与代理留一份到客户端 app.json,供本机下一个账号**确认后**继承。
|
||||
*
|
||||
* 只走桌面真实桥(网页形态没有本地任务链,也不该把明文留给浏览器);
|
||||
* 必须在本页内存/本地镜像被 clearApiSecretCache 清掉、且 localStorage.uid 被移除之前调用,
|
||||
* 否则读到的会是空值或另一个 uid 的镜像。
|
||||
*
|
||||
* 外层再加一道整体超时:桥卡死时调用方(登录页)必须能继续清 token/uid,
|
||||
* 不能让登出停在"还在清"的状态里(CR-12)。
|
||||
*/
|
||||
export function captureApiSecretsForInheritance(): Promise<void> {
|
||||
return withTimeout(captureSecretsForInheritance(inheritanceHost), CAPTURE_TIMEOUT_MS)
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取本机继承快照并登记为待确认(CR-12:不再自动 migrate)。
|
||||
* 无效/过期/作用域不符的快照在这里立即清掉,避免明文密钥无限期留在 app.json。
|
||||
*/
|
||||
async function stageInheritance(
|
||||
serverItems: UserApiSecretItem[],
|
||||
serverProxyItem: UserApiSecretItem | null | undefined,
|
||||
uid: string,
|
||||
epoch: number,
|
||||
): Promise<void> {
|
||||
if (!isNativeDesktopStorage()) {
|
||||
pendingInheritance = null
|
||||
return
|
||||
}
|
||||
const config = await withTimeout(readConfigForBackfill(), CONFIG_TIMEOUT_MS).catch((error) => {
|
||||
console.warn('[api-secret] 读取本机继承快照失败(下次加载重试):', error)
|
||||
return null
|
||||
})
|
||||
if (!sessionCurrent(uid, epoch)) return
|
||||
const parsed = readInheritedSecretStash(config, inheritanceHost.moduleKeys())
|
||||
if (parsed.state === 'none') {
|
||||
pendingInheritance = null
|
||||
return
|
||||
}
|
||||
if (parsed.state === 'invalid' || parsed.stash.fromUid === uid) {
|
||||
await clearInheritedSecrets(inheritanceHost)
|
||||
if (sessionCurrent(uid, epoch)) pendingInheritance = null
|
||||
return
|
||||
}
|
||||
const stash = parsed.stash
|
||||
const serverHas = new Set(serverItems.filter((item) => item?.exists).map((item) => item.moduleKey))
|
||||
const secretNeeded = Object.keys(stash.secrets).some((moduleKey) => !serverHas.has(moduleKey))
|
||||
const proxyNeeded = Boolean(stash.proxyUrl) && !serverProxyItem?.exists && !serverProxyItem?.locked
|
||||
if (!secretNeeded && !proxyNeeded) {
|
||||
// 服务端已经没有可补的项:快照没有存在价值,立即清掉
|
||||
await clearInheritedSecrets(inheritanceHost)
|
||||
if (sessionCurrent(uid, epoch)) pendingInheritance = null
|
||||
return
|
||||
}
|
||||
if (!sessionCurrent(uid, epoch)) return
|
||||
pendingInheritance = { summary: toPendingInheritance(stash), stash }
|
||||
}
|
||||
|
||||
/** 设置页读取待确认的继承摘要(不含明文)。确认前不会写服务端。 */
|
||||
export function getPendingApiSecretInheritance(): PendingApiSecretInheritance | null {
|
||||
return pendingInheritance?.summary || null
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户点「继承」后执行迁移:只补服务端空缺项,服务端已锁定/已存在的代理不继承。
|
||||
* 成功返回本次继承摘要;网络失败或会话已变时保留 pending 供下次重试。
|
||||
*/
|
||||
export async function confirmApiSecretInheritance(): Promise<ApiSecretInheritanceNotice | null> {
|
||||
const pending = pendingInheritance
|
||||
if (!pending || !isNativeDesktopStorage()) return null
|
||||
const uid = currentUid()
|
||||
const epoch = cacheEpoch
|
||||
const config = await withTimeout(readConfigForBackfill(), CONFIG_TIMEOUT_MS).catch((error) => {
|
||||
console.warn('[api-secret] 确认继承时读取本机配置失败(保留快照待重试):', error)
|
||||
return null
|
||||
})
|
||||
if (!config || !sessionCurrent(uid, epoch)) return null
|
||||
const bundle = await fetchMyApiSecrets().catch((error) => {
|
||||
console.warn('[api-secret] 确认继承时拉取服务端密钥失败(保留快照待重试):', error)
|
||||
return null
|
||||
})
|
||||
if (!bundle || !sessionCurrent(uid, epoch)) return null
|
||||
const outcome = await applyInheritedSecrets(
|
||||
inheritanceHost,
|
||||
pending.stash,
|
||||
config,
|
||||
bundle.items || [],
|
||||
bundle.proxy,
|
||||
)
|
||||
if (!sessionCurrent(uid, epoch)) return null
|
||||
if (outcome.cleared) pendingInheritance = null
|
||||
if (!outcome.notice) return null
|
||||
inheritanceNotice = outcome.notice
|
||||
const refreshed = await fetchMyApiSecrets().catch((error) => {
|
||||
console.warn('[api-secret] 继承后刷新服务端密钥失败(下次加载会重试):', error)
|
||||
return null
|
||||
})
|
||||
if (refreshed && sessionCurrent(uid, epoch)) {
|
||||
await applyServerBundle(refreshed, uid, epoch)
|
||||
if (sessionCurrent(uid, epoch)) {
|
||||
bundleLoaded = true
|
||||
notify()
|
||||
}
|
||||
}
|
||||
return outcome.notice
|
||||
}
|
||||
|
||||
/** 用户点「不继承」:立即清掉本机快照,明文不再留在 app.json。 */
|
||||
export async function declineApiSecretInheritance(): Promise<void> {
|
||||
pendingInheritance = null
|
||||
await clearInheritedSecrets(inheritanceHost)
|
||||
}
|
||||
|
||||
/** 设置页消费一次继承提示(内存态,不落盘)。 */
|
||||
export function takeApiSecretInheritanceNotice(): ApiSecretInheritanceNotice | null {
|
||||
const notice = inheritanceNotice
|
||||
inheritanceNotice = null
|
||||
return notice
|
||||
}
|
||||
|
||||
/** 保存密钥到服务端;成功后刷新本地缓存(失败不写本地,避免本地有值服务端没有的假象)。 */
|
||||
export async function saveApiSecret(moduleKey: ApiSecretModuleKey, value: string): Promise<ApiSecretSnapshot> {
|
||||
const trimmed = value.trim()
|
||||
@@ -567,6 +964,9 @@ export function clearApiSecretCache(): void {
|
||||
bundleLoaded = false
|
||||
inflightLoad = null
|
||||
serverProxy = null
|
||||
inheritanceNotice = null
|
||||
pendingInheritance = null
|
||||
cacheEpoch += 1
|
||||
}
|
||||
|
||||
/** 供测试与调试:模块级状态快照。 */
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* 给桌面桥调用加一道硬超时。
|
||||
*
|
||||
* 登出时要把本账号配置留给本机下一个账号,但原生桥(read_config/save_config)在
|
||||
* 主进程卡死或 pywebview 未就绪时可能永不返回。没有超时,登出清理会一直等,
|
||||
* 令牌与 uid 留在 localStorage 里,用户还能在原页面继续操作(CR-12)。
|
||||
*
|
||||
* 超时只负责让调用方继续走清理,不取消底层 promise:桥调用本身没有取消接口,
|
||||
* 晚到的结果最多是写了一份已经过期的继承快照,不会回写登录态。
|
||||
*/
|
||||
export class PromiseTimeoutError extends Error {
|
||||
constructor(timeoutMs: number) {
|
||||
super(`操作超过 ${timeoutMs}ms 未完成`)
|
||||
this.name = 'PromiseTimeoutError'
|
||||
}
|
||||
}
|
||||
|
||||
export function withTimeout<T>(promise: Promise<T>, timeoutMs: number): Promise<T> {
|
||||
if (!(timeoutMs > 0)) return promise
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
const timer = setTimeout(() => reject(new PromiseTimeoutError(timeoutMs)), timeoutMs)
|
||||
promise.then(
|
||||
(value) => {
|
||||
clearTimeout(timer)
|
||||
resolve(value)
|
||||
},
|
||||
(error) => {
|
||||
clearTimeout(timer)
|
||||
reject(error)
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
@@ -138,6 +138,39 @@ test('登录页认停用/删除标记:各有判定入口、各自关自动登
|
||||
)
|
||||
})
|
||||
|
||||
test('登出先留继承快照再清缓存,且清缓存必须早于移除 uid', () => {
|
||||
const cleanupStart = loginPageSource.indexOf('const clearSignedOutState = () => {')
|
||||
const captureCall = loginPageSource.indexOf('void captureApiSecretsForInheritance()')
|
||||
assert.ok(cleanupStart > 0, '登出清理函数不见了')
|
||||
assert.ok(captureCall > cleanupStart, '登出时应先调用继承快照再执行清理')
|
||||
const cleanupBody = loginPageSource.slice(cleanupStart, captureCall)
|
||||
const clearAt = cleanupBody.indexOf('clearApiSecretCache()')
|
||||
const removeUidAt = cleanupBody.indexOf("removeItem('uid')")
|
||||
assert.ok(clearAt > 0, '登出清理没有清密钥缓存')
|
||||
assert.ok(removeUidAt > clearAt, '密钥镜像键带 uid,先移除 uid 会删错键、把本账号明文留在本机')
|
||||
assert.match(
|
||||
loginPageSource,
|
||||
/captureApiSecretsForInheritance\(\)\.then\(clearSignedOutState, clearSignedOutState\)/,
|
||||
'继承快照失败也必须照常登出',
|
||||
)
|
||||
})
|
||||
|
||||
test('登出清理期间禁用提交,且清理结束才恢复(CR-12)', () => {
|
||||
assert.match(loginPageSource, /logoutCleanupPending = ref\(false\)/, '缺清理中状态')
|
||||
assert.match(
|
||||
loginPageSource,
|
||||
/:disabled="loggingIn \|\| logoutCleanupPending"/,
|
||||
'清理期间登录按钮必须禁用',
|
||||
)
|
||||
assert.match(
|
||||
loginPageSource,
|
||||
/if \(loggingIn\.value \|\| logoutCleanupPending\.value\) return/,
|
||||
'回车/表单提交也要挡住,不能只靠按钮 disabled',
|
||||
)
|
||||
assert.match(loginPageSource, /logoutCleanupPending\.value = true/, '登出时应进入清理中状态')
|
||||
assert.match(loginPageSource, /logoutCleanupPending\.value = false/, '清理结束(含超时)必须恢复')
|
||||
})
|
||||
|
||||
test('停用/删除文案必须互不冒充:停用句不说删除、删除句不说停用', () => {
|
||||
const disabledLine = loginPageSource
|
||||
.split('\n')
|
||||
|
||||
@@ -43,3 +43,16 @@ test('test_failed_hint_marks_task_failed_and_download_later', () => {
|
||||
assert.match(body, /'FAILED'\s*\?\s*'任务已失败'/, '提示文案应按 FAILED 切换为任务已失败')
|
||||
assert.match(body, /下载按钮稍后出现/, '失败后仍提示结果文件生成后会给出下载按钮')
|
||||
})
|
||||
|
||||
// 服务端缺代理时商标维度整批失败,必须在创建任务前弹窗拦截(2026-10-25 32300/32309)。
|
||||
test('test_missing_proxy_blocks_task_creation_with_popup', () => {
|
||||
const parseBody = functionBody('parseFiles')
|
||||
const guardIdx = parseBody.indexOf('ensureBrandCheckProxyReady')
|
||||
const createIdx = parseBody.indexOf('parseAppearancePatent(')
|
||||
assert.ok(guardIdx >= 0, 'parseFiles 必须先做服务端代理检查')
|
||||
assert.ok(createIdx > guardIdx, '代理检查必须发生在创建任务之前')
|
||||
|
||||
const guardBody = functionBody('ensureBrandCheckProxyReady')
|
||||
assert.match(guardBody, /ElMessageBox\.alert/, '缺代理时应弹窗提示,不能只 console.warn')
|
||||
assert.match(guardBody, /密钥设置/, '弹窗要告诉用户去哪里补代理')
|
||||
})
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { PromiseTimeoutError, withTimeout } from '../src/shared/utils/promise-timeout.ts'
|
||||
|
||||
/**
|
||||
* 登出 capture 的超时底座(CR-12):原生桥卡死时必须让调用方继续清 token/uid。
|
||||
* 这里只验超时原语本身;capture 走超时后仍写快照/继续清理的分支在 secret-store.test.ts。
|
||||
*/
|
||||
|
||||
test('withTimeout 在期限内原样返回结果', async () => {
|
||||
await assert.doesNotReject(async () => {
|
||||
const value = await withTimeout(Promise.resolve('ok'), 50)
|
||||
assert.equal(value, 'ok')
|
||||
})
|
||||
})
|
||||
|
||||
test('withTimeout 对永不返回的 promise 按时抛 PromiseTimeoutError', async () => {
|
||||
const start = Date.now()
|
||||
await assert.rejects(
|
||||
() => withTimeout(new Promise(() => {}), 20),
|
||||
(error: unknown) => error instanceof PromiseTimeoutError && /超过 20ms 未完成/.test(error.message),
|
||||
)
|
||||
assert.ok(Date.now() - start < 1000, '超时必须及时触发,不能等底层 promise')
|
||||
})
|
||||
|
||||
test('withTimeout 透传底层拒绝(吞掉会伪造成功)', async () => {
|
||||
await assert.rejects(
|
||||
() => withTimeout(Promise.reject(new Error('bridge down')), 50),
|
||||
/bridge down/,
|
||||
)
|
||||
})
|
||||
@@ -2,13 +2,19 @@ import { test } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { http } from '../src/shared/api/http.ts'
|
||||
import {
|
||||
captureApiSecretsForInheritance,
|
||||
clearApiSecretCache,
|
||||
confirmApiSecretInheritance,
|
||||
declineApiSecretInheritance,
|
||||
ensureBrandCheckProxy,
|
||||
getPendingApiSecretInheritance,
|
||||
getServerProxySnapshot,
|
||||
getStoredApiSecret,
|
||||
getStoredApiSecretSnapshot,
|
||||
listApiSecretModules,
|
||||
loadApiSecrets,
|
||||
saveApiSecret,
|
||||
takeApiSecretInheritanceNotice,
|
||||
} from '../src/shared/utils/api-secret-store.ts'
|
||||
|
||||
type RequestConfig = { url?: string; method?: string; data?: unknown }
|
||||
@@ -191,6 +197,602 @@ test('test_secret_store_locked_proxy_aligns_local_config_once', async (t) => {
|
||||
assert.equal(saveCalls.length, 1, '本地配置已一致时不应重复写入')
|
||||
})
|
||||
|
||||
test('test_secret_store_backfills_legacy_global_proxy_to_server', async (t) => {
|
||||
setupWindow()
|
||||
clearApiSecretCache()
|
||||
const globalUrl = 'https://api.jikip.com/ip-get?num=2&key=legacy'
|
||||
const migratePayloads: Array<{ items?: Array<{ moduleKey?: string; value?: string }> }> = []
|
||||
const saveCalls: Array<Record<string, unknown>> = []
|
||||
let config: Record<string, unknown> = { proxy_url: globalUrl, proxy_mode: 2 }
|
||||
let bundleCalls = 0
|
||||
// 老版本只把代理存在全局 proxy_url,proxy_users 里没有该用户
|
||||
attachDesktopBridge(
|
||||
() => config,
|
||||
(patch) => {
|
||||
saveCalls.push(patch)
|
||||
config = mergeDesktopConfig(config, patch)
|
||||
return config
|
||||
},
|
||||
)
|
||||
mockRequest(t, (req) => {
|
||||
if ((req.url || '').includes('/migrate')) {
|
||||
migratePayloads.push(req.data as { items?: Array<{ moduleKey?: string; value?: string }> })
|
||||
return okResponse({ migrated: 1 })
|
||||
}
|
||||
bundleCalls += 1
|
||||
return okResponse({
|
||||
items: [serverItem('appearance-patent'), serverItem('similar-asin')],
|
||||
requiredModules: ['appearance-patent', 'similar-asin'],
|
||||
complete: false,
|
||||
// 补报前无代理;补报后服务端有了代理
|
||||
proxy: bundleCalls > 1
|
||||
? { ...serverItem('proxy'), exists: true, masked: 'https://api.jikip.com' }
|
||||
: undefined,
|
||||
})
|
||||
})
|
||||
|
||||
await loadApiSecrets({ force: true })
|
||||
|
||||
const proxyMigrate = migratePayloads
|
||||
.flatMap((payload) => payload.items || [])
|
||||
.find((item) => item.moduleKey === 'proxy')
|
||||
assert.equal(proxyMigrate?.value, globalUrl, '旧版全局代理应补报到服务端')
|
||||
assert.equal(getServerProxySnapshot()?.exists, true, '补报后应重拉服务端代理快照')
|
||||
// CR-04:升级时一次性归属迁移——全局 proxy 归给第一个读到它的 uid,并清空全局
|
||||
assert.equal(config.proxy_uid, '42', '全局代理应被当前 uid 认领')
|
||||
assert.equal(config.proxy_url, '', '认领后应清空无归属的全局 proxy_url')
|
||||
assert.equal(
|
||||
(config.proxy_users as Record<string, { proxy_url: string }>)['42'].proxy_url,
|
||||
globalUrl,
|
||||
'认领结果应写入 proxy_users[uid]',
|
||||
)
|
||||
assert.ok(saveCalls.some((patch) => patch.proxy_uid === '42'), '归属迁移必须落盘,不能只在内存里认领')
|
||||
})
|
||||
|
||||
test('test_secret_store_user_proxy_wins_over_legacy_global', async (t) => {
|
||||
setupWindow()
|
||||
clearApiSecretCache()
|
||||
const userUrl = 'https://api.jikip.com/ip-get?num=1&key=user'
|
||||
const globalUrl = 'https://api.jikip.com/ip-get?num=2&key=global'
|
||||
const migratePayloads: Array<{ items?: Array<{ moduleKey?: string; value?: string }> }> = []
|
||||
attachDesktopBridge(
|
||||
() => ({ proxy_users: { '42': { proxy_url: userUrl, proxy_mode: 1 } }, proxy_url: globalUrl, proxy_mode: 2 }),
|
||||
(patch) => patch,
|
||||
)
|
||||
mockRequest(t, (req) => {
|
||||
if ((req.url || '').includes('/migrate')) {
|
||||
migratePayloads.push(req.data as { items?: Array<{ moduleKey?: string; value?: string }> })
|
||||
return okResponse({ migrated: 1 })
|
||||
}
|
||||
return bundleResponse([serverItem('appearance-patent'), serverItem('similar-asin')])
|
||||
})
|
||||
|
||||
await loadApiSecrets({ force: true })
|
||||
|
||||
const proxyMigrate = migratePayloads
|
||||
.flatMap((payload) => payload.items || [])
|
||||
.find((item) => item.moduleKey === 'proxy')
|
||||
assert.equal(proxyMigrate?.value, userUrl, '用户自己的代理优先于旧版全局代理')
|
||||
})
|
||||
|
||||
test('test_secret_store_brand_check_proxy_missing_is_visible', async (t) => {
|
||||
setupWindow()
|
||||
clearApiSecretCache()
|
||||
mockRequest(t, () => bundleResponse([serverItem('appearance-patent'), serverItem('similar-asin')]))
|
||||
|
||||
const result = await ensureBrandCheckProxy()
|
||||
|
||||
assert.deepEqual(result, { ok: false, state: 'missing' })
|
||||
})
|
||||
|
||||
test('test_secret_store_brand_check_proxy_present_passes', async (t) => {
|
||||
setupWindow()
|
||||
clearApiSecretCache()
|
||||
mockRequest(t, () =>
|
||||
okResponse({
|
||||
items: [serverItem('appearance-patent'), serverItem('similar-asin')],
|
||||
requiredModules: ['appearance-patent', 'similar-asin'],
|
||||
complete: false,
|
||||
proxy: { ...serverItem('proxy'), exists: true, masked: 'https://api.jikip.com' },
|
||||
}),
|
||||
)
|
||||
|
||||
const result = await ensureBrandCheckProxy()
|
||||
|
||||
assert.deepEqual(result, { ok: true, state: 'server' })
|
||||
})
|
||||
|
||||
function seedDesktopMirror(
|
||||
localStorage: ReturnType<typeof createStorage>,
|
||||
uid: string,
|
||||
moduleKey: string,
|
||||
value: string,
|
||||
) {
|
||||
localStorage.setItem(`brand:api-secret-cache:${uid}:${moduleKey}`, JSON.stringify({
|
||||
schema: 2,
|
||||
value,
|
||||
masked: '',
|
||||
exists: true,
|
||||
locked: false,
|
||||
checkStatus: 'passed',
|
||||
checkCode: '',
|
||||
checkMessage: '',
|
||||
checkLatencyMs: null,
|
||||
checkedAt: null,
|
||||
updatedAt: null,
|
||||
}))
|
||||
}
|
||||
|
||||
/** 与 app_client/main.py::save_config 一致:顶层浅合并,proxy_users 再按 uid 深合并。 */
|
||||
function mergeDesktopConfig(current: Record<string, unknown>, patch: Record<string, unknown>) {
|
||||
const next = { ...current, ...patch }
|
||||
const users = patch.proxy_users as Record<string, unknown> | undefined
|
||||
if (users) {
|
||||
next.proxy_users = {
|
||||
...(current.proxy_users as Record<string, unknown> | undefined),
|
||||
...users,
|
||||
}
|
||||
}
|
||||
return next
|
||||
}
|
||||
|
||||
/** 可变的桌面 app.json:读当前值、写回按原生 save_config 口径合并,并记录每次 patch。 */
|
||||
function attachConfigStore(initial: Record<string, unknown>) {
|
||||
let config = initial
|
||||
const saveCalls: Array<Record<string, unknown>> = []
|
||||
attachDesktopBridge(
|
||||
() => config,
|
||||
(patch) => {
|
||||
saveCalls.push(patch)
|
||||
config = mergeDesktopConfig(config, patch)
|
||||
return config
|
||||
},
|
||||
)
|
||||
return {
|
||||
get config() {
|
||||
return config
|
||||
},
|
||||
saveCalls,
|
||||
}
|
||||
}
|
||||
|
||||
/** 合法的本机继承快照(schema 2 + 同作用域),测试按需覆盖字段。 */
|
||||
function validInheritanceConfig(overrides: Record<string, unknown> = {}) {
|
||||
return {
|
||||
current_uid: '43',
|
||||
api_secret_os_scope: 'scope-1',
|
||||
api_secret_inherit: {
|
||||
schema: 2,
|
||||
fromUid: '42',
|
||||
fromUsername: 'alice',
|
||||
savedAt: Date.now(),
|
||||
osUserScope: 'scope-1',
|
||||
secrets: { 'appearance-patent': 'sk-alice-appearance' },
|
||||
proxyUrl: '',
|
||||
proxyMode: 1,
|
||||
...overrides,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
test('test_secret_store_inherits_previous_account_config_on_desktop', async (t) => {
|
||||
const { localStorage } = setupWindow()
|
||||
clearApiSecretCache()
|
||||
const aliceProxy = 'https://api.jikip.com/ip-get?num=1&key=alice'
|
||||
let config: Record<string, unknown> = {
|
||||
current_uid: '42',
|
||||
proxy_users: { '42': { proxy_url: aliceProxy, proxy_mode: 2 } },
|
||||
}
|
||||
attachDesktopBridge(
|
||||
() => config,
|
||||
(patch) => {
|
||||
config = mergeDesktopConfig(config, patch)
|
||||
return config
|
||||
},
|
||||
)
|
||||
localStorage.setItem('username', 'alice')
|
||||
seedDesktopMirror(localStorage, '42', 'appearance-patent', 'sk-alice-appearance')
|
||||
seedDesktopMirror(localStorage, '42', 'similar-asin', 'sk-alice-similar')
|
||||
|
||||
await captureApiSecretsForInheritance()
|
||||
const stash = config.api_secret_inherit as Record<string, unknown> | null
|
||||
assert.equal(stash?.fromUid, '42', '登出时应把当前账号配置留成本机继承快照')
|
||||
assert.equal(stash?.schema, 2, '快照必须带当前 schema 与 OS 作用域')
|
||||
assert.equal(stash?.osUserScope, config.api_secret_os_scope, '快照作用域必须与本机 app.json 一致')
|
||||
|
||||
// 真实登出顺序:先清缓存(此时 uid 仍是 42),再移除 uid;登录 B 后 uid=43
|
||||
clearApiSecretCache()
|
||||
localStorage.removeItem('uid')
|
||||
localStorage.setItem('uid', '43')
|
||||
localStorage.setItem('username', 'bob')
|
||||
|
||||
const serverKeys = new Set<string>()
|
||||
let serverHasProxy = false
|
||||
const migratePayloads: Array<{ items?: Array<{ moduleKey?: string; value?: string }> }> = []
|
||||
mockRequest(t, (req) => {
|
||||
if ((req.url || '').includes('/migrate')) {
|
||||
const payload = req.data as { items?: Array<{ moduleKey?: string; value?: string }> }
|
||||
migratePayloads.push(payload)
|
||||
for (const item of payload.items || []) {
|
||||
if (item.moduleKey === 'proxy') serverHasProxy = true
|
||||
else if (item.moduleKey) serverKeys.add(item.moduleKey)
|
||||
}
|
||||
return okResponse({ migrated: payload.items?.length || 0 })
|
||||
}
|
||||
return okResponse({
|
||||
items: ['appearance-patent', 'similar-asin'].map((moduleKey) =>
|
||||
serverItem(moduleKey, { exists: serverKeys.has(moduleKey) })),
|
||||
requiredModules: ['appearance-patent', 'similar-asin'],
|
||||
complete: serverKeys.size === 2,
|
||||
proxy: serverHasProxy
|
||||
? { ...serverItem('proxy'), exists: true, masked: 'https://api.jikip.com' }
|
||||
: undefined,
|
||||
})
|
||||
})
|
||||
|
||||
await loadApiSecrets({ force: true })
|
||||
|
||||
// CR-12:加载只登记待确认,不再自动继承
|
||||
assert.deepEqual(migratePayloads, [], '用户未确认前不得调用 migrate')
|
||||
const pending = getPendingApiSecretInheritance()
|
||||
assert.equal(pending?.fromUid, '42')
|
||||
assert.equal(pending?.fromUsername, 'alice')
|
||||
assert.equal(pending?.proxy, true)
|
||||
assert.deepEqual(pending?.moduleKeys.slice().sort(), ['appearance-patent', 'similar-asin'])
|
||||
assert.equal(getStoredApiSecret('appearance-patent'), '', '确认前不得把上一个账号的明文写进本地镜像')
|
||||
|
||||
const notice = await confirmApiSecretInheritance()
|
||||
const migrated = migratePayloads.flatMap((payload) => payload.items || [])
|
||||
assert.equal(
|
||||
migrated.find((item) => item.moduleKey === 'appearance-patent')?.value,
|
||||
'sk-alice-appearance',
|
||||
'确认后上一个账号的外观专利密钥应继承给当前账号',
|
||||
)
|
||||
assert.equal(
|
||||
migrated.find((item) => item.moduleKey === 'similar-asin')?.value,
|
||||
'sk-alice-similar',
|
||||
'确认后上一个账号的货源查询密钥应继承给当前账号',
|
||||
)
|
||||
assert.equal(
|
||||
migrated.find((item) => item.moduleKey === 'proxy')?.value,
|
||||
aliceProxy,
|
||||
'服务端缺代理且未锁定时应继承上一个账号的代理',
|
||||
)
|
||||
assert.equal(getStoredApiSecret('appearance-patent'), 'sk-alice-appearance', '继承后当前账号本地应能直接取到明文')
|
||||
const users = config.proxy_users as Record<string, { proxy_url: string; proxy_mode: number }>
|
||||
assert.equal(users['43'].proxy_url, aliceProxy, '继承的代理应写进当前 uid 的本地配置')
|
||||
assert.equal(users['43'].proxy_mode, 2, '代理模式应一并继承')
|
||||
assert.equal(config.api_secret_inherit, null, '确认继承后快照应消费掉,避免被更后面的账号重复使用')
|
||||
assert.equal(getPendingApiSecretInheritance(), null, '确认后不应再有待确认摘要')
|
||||
|
||||
assert.equal(notice?.fromUid, '42')
|
||||
assert.equal(notice?.fromUsername, 'alice')
|
||||
assert.equal(notice?.proxy, true)
|
||||
assert.deepEqual(notice?.moduleKeys.slice().sort(), ['appearance-patent', 'similar-asin'])
|
||||
assert.equal(takeApiSecretInheritanceNotice()?.fromUid, '42', '设置页应能消费到本次继承提示')
|
||||
})
|
||||
|
||||
test('test_secret_store_inheritance_never_overwrites_existing_account_config', async (t) => {
|
||||
const { localStorage } = setupWindow()
|
||||
clearApiSecretCache()
|
||||
localStorage.setItem('uid', '43')
|
||||
const config: Record<string, unknown> = {
|
||||
current_uid: '43',
|
||||
api_secret_os_scope: 'scope-1',
|
||||
api_secret_inherit: {
|
||||
schema: 2,
|
||||
fromUid: '42',
|
||||
fromUsername: 'alice',
|
||||
savedAt: Date.now(),
|
||||
osUserScope: 'scope-1',
|
||||
secrets: { 'appearance-patent': 'sk-alice-appearance', 'similar-asin': 'sk-alice-similar' },
|
||||
proxyUrl: 'https://api.jikip.com/ip-get?num=1&key=alice',
|
||||
proxyMode: 2,
|
||||
},
|
||||
}
|
||||
attachDesktopBridge(() => config, (patch) => {
|
||||
Object.assign(config, patch)
|
||||
if (Object.prototype.hasOwnProperty.call(patch, 'api_secret_inherit')) {
|
||||
config.api_secret_inherit = patch.api_secret_inherit
|
||||
}
|
||||
return config
|
||||
})
|
||||
const migratePayloads: Array<{ items?: Array<{ moduleKey?: string; value?: string }> }> = []
|
||||
mockRequest(t, (req) => {
|
||||
if ((req.url || '').includes('/migrate')) {
|
||||
migratePayloads.push(req.data as { items?: Array<{ moduleKey?: string; value?: string }> })
|
||||
return okResponse({ migrated: 1 })
|
||||
}
|
||||
return okResponse({
|
||||
items: [
|
||||
serverItem('appearance-patent', { exists: true, masked: 'sk-b****5678' }),
|
||||
serverItem('similar-asin'),
|
||||
],
|
||||
requiredModules: ['appearance-patent', 'similar-asin'],
|
||||
complete: false,
|
||||
proxy: { ...serverItem('proxy'), exists: true, masked: 'https://api.jikip.com' },
|
||||
})
|
||||
})
|
||||
|
||||
await loadApiSecrets({ force: true })
|
||||
|
||||
assert.deepEqual(migratePayloads, [], '用户未确认前不得自动继承')
|
||||
assert.equal(getPendingApiSecretInheritance()?.fromUid, '42', '应登记待确认快照')
|
||||
await confirmApiSecretInheritance()
|
||||
|
||||
const migrated = migratePayloads.flatMap((payload) => payload.items || [])
|
||||
assert.deepEqual(
|
||||
migrated.map((item) => item.moduleKey),
|
||||
['similar-asin'],
|
||||
'只补当前账号空缺的模块,已有密钥和已有代理都不能被上一个账号覆盖',
|
||||
)
|
||||
assert.equal(getStoredApiSecret('appearance-patent'), '', '当前账号已有密钥时不得把上一个账号的明文塞进本地')
|
||||
assert.equal(config.api_secret_inherit, null, '继承完成后应消费快照')
|
||||
assert.equal(
|
||||
(config.proxy_users as Record<string, unknown> | undefined)?.['43'],
|
||||
undefined,
|
||||
'当前账号已有服务端代理时不得改写本地代理配置',
|
||||
)
|
||||
})
|
||||
|
||||
test('test_secret_store_declines_inheritance_and_clears_stash', async (t) => {
|
||||
const { localStorage } = setupWindow()
|
||||
localStorage.setItem('uid', '43')
|
||||
clearApiSecretCache()
|
||||
const store = attachConfigStore(validInheritanceConfig())
|
||||
const migratePayloads: Array<{ items?: Array<{ moduleKey?: string; value?: string }> }> = []
|
||||
mockRequest(t, (req) => {
|
||||
if ((req.url || '').includes('/migrate')) {
|
||||
migratePayloads.push(req.data as { items?: Array<{ moduleKey?: string; value?: string }> })
|
||||
return okResponse({ migrated: 1 })
|
||||
}
|
||||
return bundleResponse([serverItem('appearance-patent'), serverItem('similar-asin')])
|
||||
})
|
||||
|
||||
await loadApiSecrets({ force: true })
|
||||
assert.equal(getPendingApiSecretInheritance()?.fromUid, '42', '应先登记待确认快照')
|
||||
await declineApiSecretInheritance()
|
||||
|
||||
assert.equal(store.config.api_secret_inherit, null, '用户选择不继承后快照必须立即清掉')
|
||||
assert.equal(getPendingApiSecretInheritance(), null)
|
||||
assert.deepEqual(migratePayloads, [], '拒绝继承不得调用 migrate')
|
||||
})
|
||||
|
||||
test('test_secret_store_expired_inheritance_snapshot_is_cleared', async (t) => {
|
||||
const { localStorage } = setupWindow()
|
||||
localStorage.setItem('uid', '43')
|
||||
clearApiSecretCache()
|
||||
const store = attachConfigStore(validInheritanceConfig({
|
||||
savedAt: Date.now() - 31 * 24 * 60 * 60 * 1000,
|
||||
}))
|
||||
const migratePayloads: Array<{ items?: Array<{ moduleKey?: string; value?: string }> }> = []
|
||||
mockRequest(t, (req) => {
|
||||
if ((req.url || '').includes('/migrate')) {
|
||||
migratePayloads.push(req.data as { items?: Array<{ moduleKey?: string; value?: string }> })
|
||||
return okResponse({ migrated: 1 })
|
||||
}
|
||||
return bundleResponse([serverItem('appearance-patent'), serverItem('similar-asin')])
|
||||
})
|
||||
|
||||
await loadApiSecrets({ force: true })
|
||||
|
||||
assert.equal(store.config.api_secret_inherit, null, '过期快照必须立即从 app.json 清掉(明文不许长期留存)')
|
||||
assert.equal(getPendingApiSecretInheritance(), null)
|
||||
assert.deepEqual(migratePayloads, [])
|
||||
})
|
||||
|
||||
test('test_secret_store_malformed_inheritance_snapshot_is_cleared', async (t) => {
|
||||
const { localStorage } = setupWindow()
|
||||
localStorage.setItem('uid', '43')
|
||||
clearApiSecretCache()
|
||||
const store = attachConfigStore(validInheritanceConfig({ schema: 1 }))
|
||||
mockRequest(t, () => bundleResponse([serverItem('appearance-patent'), serverItem('similar-asin')]))
|
||||
|
||||
await loadApiSecrets({ force: true })
|
||||
|
||||
assert.equal(store.config.api_secret_inherit, null, '结构不符(旧 schema/缺作用域)必须立即清掉')
|
||||
assert.equal(getPendingApiSecretInheritance(), null)
|
||||
})
|
||||
|
||||
test('test_secret_store_inheritance_scope_mismatch_is_cleared', async (t) => {
|
||||
const { localStorage } = setupWindow()
|
||||
localStorage.setItem('uid', '43')
|
||||
clearApiSecretCache()
|
||||
// 别的 OS 用户 app.json 里拷来的快照:作用域标记对不上
|
||||
const store = attachConfigStore(validInheritanceConfig({ osUserScope: 'scope-from-another-os-user' }))
|
||||
mockRequest(t, () => bundleResponse([serverItem('appearance-patent'), serverItem('similar-asin')]))
|
||||
|
||||
await loadApiSecrets({ force: true })
|
||||
|
||||
assert.equal(store.config.api_secret_inherit, null, '跨 OS 用户作用域的快照必须清掉')
|
||||
assert.equal(getPendingApiSecretInheritance(), null)
|
||||
})
|
||||
|
||||
test('test_secret_store_locked_server_proxy_is_not_inherited', async (t) => {
|
||||
const { localStorage } = setupWindow()
|
||||
localStorage.setItem('uid', '43')
|
||||
clearApiSecretCache()
|
||||
const stashProxy = 'https://api.jikip.com/ip-get?num=1&key=alice'
|
||||
const store = attachConfigStore(validInheritanceConfig({ proxyUrl: stashProxy, proxyMode: 2 }))
|
||||
const migratePayloads: Array<{ items?: Array<{ moduleKey?: string; value?: string }> }> = []
|
||||
mockRequest(t, (req) => {
|
||||
if ((req.url || '').includes('/migrate')) {
|
||||
const payload = req.data as { items?: Array<{ moduleKey?: string; value?: string }> }
|
||||
migratePayloads.push(payload)
|
||||
return okResponse({ migrated: payload.items?.length || 0 })
|
||||
}
|
||||
return okResponse({
|
||||
items: [serverItem('appearance-patent'), serverItem('similar-asin')],
|
||||
requiredModules: ['appearance-patent', 'similar-asin'],
|
||||
complete: false,
|
||||
// 后台锁定但 exists 仍为 false:只靠 exists 判断会漏掉 locked 这一条
|
||||
proxy: { ...serverItem('proxy'), exists: false, locked: true },
|
||||
})
|
||||
})
|
||||
|
||||
await loadApiSecrets({ force: true })
|
||||
const notice = await confirmApiSecretInheritance()
|
||||
|
||||
const migrated = migratePayloads.flatMap((payload) => payload.items || [])
|
||||
assert.equal(
|
||||
migrated.some((item) => item.moduleKey === 'proxy'),
|
||||
false,
|
||||
'后台锁定的代理不得被上一个账号的快照覆盖',
|
||||
)
|
||||
assert.equal(notice?.proxy, false, '继承摘要要如实说明代理没有继承')
|
||||
assert.equal(
|
||||
(store.config.proxy_users as Record<string, unknown> | undefined)?.['43'],
|
||||
undefined,
|
||||
'锁定代理场景不得把快照代理写进本地配置',
|
||||
)
|
||||
})
|
||||
|
||||
test('test_secret_store_inheritance_write_aborts_when_uid_changes', async (t) => {
|
||||
const { localStorage } = setupWindow()
|
||||
localStorage.setItem('uid', '43')
|
||||
clearApiSecretCache()
|
||||
const store = attachConfigStore(validInheritanceConfig())
|
||||
let releaseMigrate: ((value: unknown) => void) | null = null
|
||||
let markStarted: (() => void) | null = null
|
||||
const migrateStarted = new Promise<void>((resolve) => {
|
||||
markStarted = resolve
|
||||
})
|
||||
mockRequest(t, (req) => {
|
||||
if ((req.url || '').includes('/migrate')) {
|
||||
markStarted?.()
|
||||
return new Promise((resolve) => {
|
||||
releaseMigrate = resolve
|
||||
})
|
||||
}
|
||||
return bundleResponse([serverItem('appearance-patent'), serverItem('similar-asin')])
|
||||
})
|
||||
|
||||
await loadApiSecrets({ force: true })
|
||||
assert.equal(getPendingApiSecretInheritance()?.fromUid, '42')
|
||||
const confirmPromise = confirmApiSecretInheritance()
|
||||
await migrateStarted
|
||||
// migrate 在飞时切号:晚到的成功响应不得把 A 的明文写进 B 的本地镜像
|
||||
localStorage.setItem('uid', '44')
|
||||
releaseMigrate?.({ data: { success: true, message: 'ok', data: { migrated: 1 } } })
|
||||
|
||||
const notice = await confirmPromise
|
||||
assert.equal(notice, null, '会话已变时确认流程必须返回空')
|
||||
assert.equal(getStoredApiSecret('appearance-patent'), '', '切号后不得写入上一个账号的明文')
|
||||
assert.equal(
|
||||
(store.config.proxy_users as Record<string, unknown> | undefined)?.['43'],
|
||||
undefined,
|
||||
'切号后不得写回上一个账号的代理',
|
||||
)
|
||||
assert.ok(store.config.api_secret_inherit, '会话已变时快照应保留给正确账号重试,而不是被误清')
|
||||
})
|
||||
|
||||
test('test_secret_store_does_not_backfill_foreign_global_proxy', async (t) => {
|
||||
const { localStorage } = setupWindow()
|
||||
localStorage.setItem('uid', '43')
|
||||
clearApiSecretCache()
|
||||
const aliceProxy = 'https://api.jikip.com/ip-get?num=1&key=alice'
|
||||
const store = attachConfigStore({
|
||||
proxy_users: { '43': {} },
|
||||
proxy_url: aliceProxy,
|
||||
proxy_uid: '42',
|
||||
})
|
||||
const migratePayloads: Array<{ items?: Array<{ moduleKey?: string; value?: string }> }> = []
|
||||
mockRequest(t, (req) => {
|
||||
if ((req.url || '').includes('/migrate')) {
|
||||
migratePayloads.push(req.data as { items?: Array<{ moduleKey?: string; value?: string }> })
|
||||
return okResponse({ migrated: 1 })
|
||||
}
|
||||
return bundleResponse([serverItem('appearance-patent'), serverItem('similar-asin')])
|
||||
})
|
||||
|
||||
await loadApiSecrets({ force: true })
|
||||
|
||||
assert.equal(
|
||||
migratePayloads.flatMap((payload) => payload.items || []).some((item) => item.moduleKey === 'proxy'),
|
||||
false,
|
||||
'全局代理归属 uid=42 时,uid=43 不得静默补报',
|
||||
)
|
||||
assert.equal(store.config.proxy_uid, '42', '不得把别人的全局代理改归属到当前账号')
|
||||
assert.deepEqual(store.config.proxy_users, { '43': {} }, '也不得写进当前 uid 的 proxy_users')
|
||||
assert.deepEqual(store.saveCalls, [], '归属不符时不应发生任何配置写回')
|
||||
})
|
||||
|
||||
test('test_secret_store_backfills_global_proxy_owned_by_current_uid', async (t) => {
|
||||
const { localStorage } = setupWindow()
|
||||
localStorage.setItem('uid', '43')
|
||||
clearApiSecretCache()
|
||||
const ownProxy = 'https://api.jikip.com/ip-get?num=1&key=owned'
|
||||
const store = attachConfigStore({ proxy_url: ownProxy, proxy_uid: '43' })
|
||||
const migratePayloads: Array<{ items?: Array<{ moduleKey?: string; value?: string }> }> = []
|
||||
mockRequest(t, (req) => {
|
||||
if ((req.url || '').includes('/migrate')) {
|
||||
migratePayloads.push(req.data as { items?: Array<{ moduleKey?: string; value?: string }> })
|
||||
return okResponse({ migrated: 1 })
|
||||
}
|
||||
return bundleResponse([serverItem('appearance-patent'), serverItem('similar-asin')])
|
||||
})
|
||||
|
||||
await loadApiSecrets({ force: true })
|
||||
|
||||
const proxyMigrate = migratePayloads
|
||||
.flatMap((payload) => payload.items || [])
|
||||
.find((item) => item.moduleKey === 'proxy')
|
||||
assert.equal(proxyMigrate?.value, ownProxy, 'proxy_uid 等于当前 uid 时应允许回退全局代理')
|
||||
assert.deepEqual(store.saveCalls, [], '已有归属时无需再做一次性迁移')
|
||||
})
|
||||
|
||||
test('test_secret_store_backfills_shared_global_proxy_whitelist', async (t) => {
|
||||
const { localStorage } = setupWindow()
|
||||
localStorage.setItem('uid', '43')
|
||||
clearApiSecretCache()
|
||||
const sharedProxy = 'https://api.jikip.com/ip-get?num=1&key=shared'
|
||||
attachConfigStore({
|
||||
proxy_url: sharedProxy,
|
||||
proxy_uid: '42',
|
||||
proxy_shared_uids: ['43'],
|
||||
})
|
||||
const migratePayloads: Array<{ items?: Array<{ moduleKey?: string; value?: string }> }> = []
|
||||
mockRequest(t, (req) => {
|
||||
if ((req.url || '').includes('/migrate')) {
|
||||
migratePayloads.push(req.data as { items?: Array<{ moduleKey?: string; value?: string }> })
|
||||
return okResponse({ migrated: 1 })
|
||||
}
|
||||
return bundleResponse([serverItem('appearance-patent'), serverItem('similar-asin')])
|
||||
})
|
||||
|
||||
await loadApiSecrets({ force: true })
|
||||
|
||||
const proxyMigrate = migratePayloads
|
||||
.flatMap((payload) => payload.items || [])
|
||||
.find((item) => item.moduleKey === 'proxy')
|
||||
assert.equal(proxyMigrate?.value, sharedProxy, '显式共享白名单内的账号可以回退全局代理')
|
||||
})
|
||||
|
||||
test('test_secret_store_capture_times_out_instead_of_blocking_logout', async () => {
|
||||
const { localStorage } = setupWindow()
|
||||
clearApiSecretCache()
|
||||
localStorage.setItem('username', 'alice')
|
||||
seedDesktopMirror(localStorage, '42', 'appearance-patent', 'sk-alice-appearance')
|
||||
const saveCalls: Array<Record<string, unknown>> = []
|
||||
// 原生桥卡死:read_config 永不返回。capture 自身必须超时,否则登录页会一直停在清理中
|
||||
attachDesktopBridge(
|
||||
(() => new Promise(() => {})) as unknown as () => Record<string, unknown>,
|
||||
(patch) => {
|
||||
saveCalls.push(patch)
|
||||
return patch
|
||||
},
|
||||
)
|
||||
const start = Date.now()
|
||||
|
||||
await captureApiSecretsForInheritance()
|
||||
|
||||
assert.ok(Date.now() - start < 5000, 'capture 超时必须及时返回,不能拖住登出清理')
|
||||
const saved = saveCalls[0]?.api_secret_inherit as Record<string, unknown> | undefined
|
||||
assert.equal(saved?.fromUid, '42', '读配置超时也不影响把已取到的密钥留成本机快照')
|
||||
assert.equal(saved?.proxyUrl, '', '读不到配置时不写代理,避免把不确定的出口留给下一个账号')
|
||||
})
|
||||
|
||||
|
||||
test('test_secret_store_locked_proxy_not_in_module_cards', async (t) => {
|
||||
setupWindow()
|
||||
clearApiSecretCache()
|
||||
|
||||
Reference in New Issue
Block a user