Files
crawler-plugin/admin-frontend-vue/src/pages/account/CompaniesPage.vue
T
huangzd1997 898e8a5ea9 feat(admin): 批量删除并入筛选行省一整行+公司管理加删除/批量删除(2026-11-23 用户口径)
- BatchDeleteActions margin-left:auto 右对齐、不再独占一行,14 页迁入各自筛选/工具行(Users/Groups/Menus/UserSecrets/QueryAsin/AsinInvalid/SkipPrice/DedupeRegistry/ProductCategory/DeviceLogs/DigitalHuman/ShopManage/ShopKeys/BrandGating);主按钮沿用全局 34px 同高,align-batch-toolbar-inline.test.ts 钉形状防回归
- 公司删除:DELETE /api/admin/companies/{id}(仅超管,名下有账号拒并带数量),只收公司行+columns 菜单树(PermissionMenuService.deleteTreeByCompany 与 copyTreeFromTemplate 对称),业务数据不级联;列表 VO 带 user_count 供前端置灰勾选/删除按钮;批量删除按用户管理同形走前端逐条循环
- CompaniesPage 加勾选列+行删除按钮(ops-cell 补 flex-wrap 过 ops-cell-must-wrap),删除后 refreshCompanyDirectory 同步 tab 条
- 测试:CompanyServiceDeleteTest 6+PermissionMenuServiceDeleteTreeTest 2+CompanyVoTest user_count 契约+CompanyControllerEndpointTest 翻转 exposesNoDeleteEndpoint→点名 DELETE 端点+CompanyReadPathMenuGateTest ensureSuperAdmin 3→4(delete 入册)+company-directory/row-buttons 补例;source-pin 基线重生成(265 文件/1859 例)
- 验证:admin-vue vue-tsc rc=0、node --test 1976 全绿、vitest mount 247 全绿;backend test-compile rc=0、Company*/PermissionMenu*/跨切守卫点名全绿(CatchSilenceRatchet 红因=另一路在飞件 JdPublishFillAiService 3 个 catch,归属京东窗不代抬)
2026-11-23 01:41:37 +08:00

358 lines
12 KiB
Vue

<script setup lang="ts">
/** 公司管理(设计文档 §2.3):列表 + 新建 + 改名/启停 + 删除/批量删除(2026-11-23,名下有账号不能删)。
* 可见范围由后端收窄(超管全部、公司内 admin 只自己公司);写操作仅超管,这里隐藏入口只是配合,
* 真正的把关在 CompanyController,页面不得当作权限边界。 */
import { computed, onMounted, ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
import { formatDateTime } from '@/utils/datetime'
import { useRowSelection } from '@/utils/use-row-selection'
import { deleteCompany, fetchCompanies } from '@/api/companies'
import { COMPANY_STATUS_ACTIVE, type CompanyItem } from '@/api/companies-dto'
import { useAdminSessionStore } from '@/stores/admin-session'
import { refreshCompanyDirectory } from '@/stores/company-tab'
import { usePlatformTabStore } from '@/stores/platform-tab'
import BatchDeleteActions from '@/components/BatchDeleteActions.vue'
import BatchDeleteCheckbox from '@/components/BatchDeleteCheckbox.vue'
import { openAdminConfirm } from '@/components/admin-confirm'
import OldPagination from '@/components/OldPagination.vue'
import CompanyEditorDialog from './CompanyEditorDialog.vue'
import { showAdminFeedback } from '@/components/admin-feedback-ui'
import LoadFailureBar from '@/components/LoadFailureBar.vue'
const session = useAdminSessionStore()
// 平台 tab(V206):公司列表按当前平台过滤,新建默认落当前平台。
const platformTab = usePlatformTabStore()
const loading = ref(false)
const loadFailure = ref('')
const rows = ref<CompanyItem[]>([])
const editorVisible = ref(false)
const editingCompany = ref<CompanyItem | null>(null)
const filterKeyword = ref('')
const canManage = computed(() => session.isSuperAdmin)
const filteredRows = computed(() => {
const kw = filterKeyword.value.trim().toLowerCase()
if (!kw) return rows.value
return rows.value.filter((c) => (c.companyName || '').toLowerCase().includes(kw))
})
const page = ref(1)
const pageSize = ref(10)
const pagedRows = computed(() => filteredRows.value.slice((page.value - 1) * pageSize.value, page.value * pageSize.value))
function changePage(p: number) { page.value = p }
function changeSize(size: number) { pageSize.value = size; page.value = 1 }
async function loadCompanies() {
loading.value = true
loadFailure.value = ''
try {
rows.value = await fetchCompanies(platformTab.active)
} catch (error) {
loadFailure.value = error instanceof Error ? error.message : '公司列表加载失败'
showAdminFeedback(loadFailure.value, 'error')
} finally {
loading.value = false
}
}
function openCreate() {
editingCompany.value = null
editorVisible.value = true
}
function openEdit(company: CompanyItem) {
editingCompany.value = company
editorVisible.value = true
}
/** 删除前置闸(勾选框置灰与删除按钮共用同一份理由;后端删除时还有权威校验兜底)。 */
function deleteBlockReasonOf(company: CompanyItem): string | undefined {
if (!canManage.value) return '只有超级管理员可以删除公司'
const userCount = company.userCount ?? 0
if (userCount > 0) return `该公司名下还有 ${userCount} 个账号,请先删除或转移后再删除公司`
return undefined
}
async function reloadCompaniesAndDirectory() {
await loadCompanies()
refreshCompanyDirectory().catch((error: unknown) => {
console.warn('[company-tab] 公司变动后目录重拉失败', error)
})
}
const batch = useRowSelection({
pageRows: () => pagedRows.value,
rowId: (row) => row.id,
rowLabel: (row) => row.companyName || `公司 ${row.id}`,
deleteOne: (id) => deleteCompany(id),
refresh: reloadCompaniesAndDirectory,
blocked: () => loading.value || !!loadFailure.value,
scope: () => [platformTab.active],
precheck: (row) => deleteBlockReasonOf(row),
})
async function removeCompany(company: CompanyItem) {
if (batch.busy) return
const reason = deleteBlockReasonOf(company)
if (reason) {
showAdminFeedback(reason, 'error')
return
}
// 对齐分组管理:统一确认出口,删除不做级联(账号与业务数据留在各自页面处理)。
if (!(await openAdminConfirm({ message: `确定删除公司“${company.companyName || company.id}”吗?删除后不可恢复。`, danger: true }))) return
try {
await deleteCompany(company.id)
batch.removeFromSelection(company.id)
ElMessage.success('删除成功')
await reloadCompaniesAndDirectory()
} catch (error) {
showAdminFeedback(error instanceof Error ? error.message : '删除失败', 'error')
}
}
// 保存成功后:本页列表照常重拉,共享公司目录(tab 条)强制重拉一份。
// 本页不参与公司隔离(控制方 2026-10-06「公司管理不要有 tab」)⇒ 走 store 的具名出口
// refreshCompanyDirectory,而不是自己持一份 company-tab store(后者形状与「给这页上了 tab」
// 分不开,company-config-entry-super-only 的 pageFiles 分母会当场红)。
// 重拉失败不弹新提示(§12 第 5 条未批的界面暴露):tab 条保留旧名单,下一次切平台 tab / 重进页面会再试。
async function onCompaniesSaved() {
await reloadCompaniesAndDirectory()
}
// 切平台 tab(V206)= 重新取数:公司归属按平台分列,两 tab 各看各的公司。
watch(() => platformTab.active, () => {
page.value = 1
void loadCompanies()
})
onMounted(loadCompanies)
</script>
<template>
<div class="companies-view">
<section class="panel-box">
<div class="companies-head">
<h3>公司列表</h3>
<button v-if="canManage" class="btn" type="button" @click="openCreate">新建公司</button>
</div>
<div class="form-row companies-filter-row">
<div class="form-group" style="min-width: 200px">
<label>公司名称</label>
<input v-model="filterKeyword" type="text" placeholder="输入公司名称" />
</div>
<BatchDeleteActions :batch="batch" />
</div>
<LoadFailureBar v-if="loadFailure" :message="loadFailure" @retry="loadCompanies" />
<div class="table-scroll">
<table>
<thead>
<tr>
<th class="batch-delete-cell" scope="col">
<BatchDeleteCheckbox label="全选当前页可删除公司" :checked="batch.pageAll" :partial="batch.pageSome"
:disabled="batch.disabled || batch.pageEmpty" @change="batch.togglePage" />
</th>
<th style="width: 58px">序号</th>
<th>公司名称</th>
<th style="width: 90px">状态</th>
<th style="width: 24%">备注</th>
<th style="width: 160px">创建时间</th>
<th style="width: 160px">修改时间</th>
<th style="width: 140px">操作</th>
</tr>
</thead>
<tbody>
<template v-if="filteredRows.length">
<tr v-for="(company, index) in pagedRows" :key="company.id" :class="{ 'batch-selected': batch.isSelected(company) }">
<td class="batch-delete-cell">
<BatchDeleteCheckbox :label="batch.reason(company) || `选择 ${batch.label(company)}`" :checked="batch.isSelected(company)"
:disabled="batch.disabled || !!batch.reason(company)" @change="batch.toggle(company)" />
</td>
<td>{{ index + 1 }}</td>
<td>{{ company.companyName || '-' }}</td>
<td>
<span :class="company.status === COMPANY_STATUS_ACTIVE ? 'state-on' : 'state-off'">
{{ company.status === COMPANY_STATUS_ACTIVE ? '启用' : '停用' }}
</span>
</td>
<td>{{ company.remark || '-' }}</td>
<td>{{ formatDateTime(company.createdAt) }}</td>
<td>{{ formatDateTime(company.updatedAt || company.createdAt) }}</td>
<td class="ops-cell">
<template v-if="canManage">
<button class="btn btn-sm" type="button" @click="openEdit(company)">编辑</button>
<button class="btn btn-sm btn-danger" type="button" :disabled="batch.busy || !!deleteBlockReasonOf(company)"
:title="deleteBlockReasonOf(company) || undefined" @click="removeCompany(company)">删除</button>
</template>
<span v-else class="cell-none">-</span>
</td>
</tr>
</template>
<tr v-else-if="loading">
<td colspan="8" class="empty-tip">加载中...</td>
</tr>
<tr v-else>
<td colspan="8" class="empty-tip">{{ filterKeyword.trim() ? '暂无匹配公司' : '暂无公司' }}</td>
</tr>
</tbody>
</table>
</div>
<OldPagination
v-if="filteredRows.length > 0"
:total="filteredRows.length"
:page="page"
:page-size="pageSize"
@change="changePage"
@size-change="changeSize"
/>
</section>
<CompanyEditorDialog v-model="editorVisible" :company="editingCompany" :platform="platformTab.active" @saved="onCompaniesSaved" />
</div>
</template>
<style scoped>
.companies-view {
display: flex;
flex-direction: column;
gap: 18px;
color: #24384d;
}
.panel-box {
width: 100%;
padding: 20px 22px 24px;
border: 1px solid #d8e3ee;
border-radius: 14px;
background: linear-gradient(145deg, #ffffff, #f9fbfd);
box-shadow: 0 1px 2px rgba(39, 67, 94, 0.04), 0 14px 30px -24px rgba(39, 67, 94, 0.28);
}
h3 {
margin: 0;
font-size: 15px;
font-weight: 650;
}
.companies-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 16px;
}
.companies-filter-row {
display: flex;
flex-wrap: wrap;
align-items: flex-end;
gap: 14px 18px;
margin-bottom: 16px;
}
.companies-filter-row .form-group {
flex: 1 1 200px;
}
.form-group {
display: flex;
flex-direction: column;
gap: 7px;
}
.form-group label {
color: #5b6f83;
font-size: 12.5px;
font-weight: 600;
}
.form-group input {
min-height: 42px;
padding: 8px 12px;
border: 1px solid #cbd9e6;
border-radius: 9px;
background: #f8fbfd;
color: #24384d;
font-size: 13.5px;
font-family: inherit;
outline: none;
}
.form-group input:focus {
background: #ffffff;
border-color: #5f85ad;
box-shadow: 0 0 0 3px rgba(95, 133, 173, 0.16);
}
.btn {
display: inline-flex;
align-items: center;
justify-content: center;
min-height: 42px;
padding: 9px 18px;
border: 1px solid #4f78a5;
border-radius: 9px;
background: linear-gradient(135deg, #5f85ad, #4f78a5);
color: #ffffff;
font-family: inherit;
font-size: 13.5px;
cursor: pointer;
}
.btn:hover:not(:disabled) {
background: linear-gradient(135deg, #7094ba, #5d83ac);
}
.btn-sm {
min-height: 36px;
padding: 7px 12px;
font-size: 12.5px;
}
.table-scroll {
width: 100%;
overflow-x: auto;
border: 1px solid #dbe5ee;
border-radius: 10px;
background: #ffffff;
}
.table-scroll table {
width: 100%;
border-collapse: collapse;
table-layout: fixed;
}
.table-scroll th,
.table-scroll td {
padding: 10px 12px;
text-align: left;
font-size: 13.5px;
line-height: 1.5;
border-bottom: 1px solid #e0e8ef;
vertical-align: middle;
word-break: break-word;
}
.table-scroll th {
background: #edf4fa;
color: #4e6479;
font-size: 12.5px;
font-weight: 600;
}
.table-scroll tbody tr:hover td {
background: #f1f7fb;
}
.table-scroll tbody tr:last-child td {
border-bottom: 0;
}
.state-on {
color: #2e7d5b;
}
.state-off {
color: #b35f6a;
}
.ops-cell {
display: flex;
align-items: center;
gap: 8px;
white-space: nowrap;
flex-wrap: wrap;
}
.cell-none {
color: #8293a5;
}
.empty-tip {
padding: 44px 24px;
text-align: center;
color: #8293a5;
font-size: 13.5px;
}
</style>