feat(多公司): 两端接上公司身份 —— 后台公司目录页、建号必选公司、顶栏显示所属公司

- 新后台新增「公司管理」页(列/新建/改名/启停,不提供删除),路由 menuKey 与
  columns 表的 admin_companies 逐字一致,由用例钉住;操作指引补该菜单一条。
- 建号弹窗对超管必须显式选一家(后端对"不传"仍落操作者自己公司,是给旧后台兜底);
  admin 只能落自己公司,越权直接 403。账号列表显示 company_name,可按公司筛。
- 客户端登录后把 companyId/companyName 落到 localStorage 并在顶栏显示;
  两个登出出口(handleKicked 与切号/登出)连公司名一起清 —— 残留会把上一个账号
  的归属显示给下一个账号。
- 契约分层:列表端点用 snake_case(company_id/company_name),current-user 与登录
  回包用 camelCase;解析器读到另一套写法必须忽略,防止前后端各猜一套。

admin-frontend-vue 1678/1678、frontend-vue 672/672,两边 build(含 vue-tsc --noEmit)干净。
This commit is contained in:
2026-10-04 07:11:47 +08:00
parent 061147c603
commit 076b0cd531
29 changed files with 1109 additions and 16 deletions
@@ -8,6 +8,9 @@ import type { AdminUser } from '@/types/admin'
import { roleLabel } from '@/types/admin'
import { useAdminSessionStore } from '@/stores/admin-session'
import { deleteUser, fetchUserList } from '@/api/users'
import { fetchCompanies } from '@/api/companies'
import type { CompanyItem } from '@/api/companies-dto'
import type { CreateOperatorScope } from './user-create-model'
import { createUserFilterState, toUserListParams, type UserFilterState } from './users-filter'
import { totalPageCount } from './user-pagination'
import { deleteBlockReason, deleteConfirmMessage } from './user-delete-model'
@@ -24,6 +27,12 @@ const rows = ref<AdminUser[]>([])
const total = ref(0)
/** 所属管理员候选(来自列表响应的 admins),供超管筛选下拉使用。 */
const admins = ref<Array<{ id: number; username: string }>>([])
/** 公司目录(GET /api/admin/companies):后端已按操作者收窄,前端不再自己判权限。 */
const companies = ref<CompanyItem[]>([])
const operatorScope = computed<CreateOperatorScope>(() => ({
role: session.user?.role,
companyId: session.user?.companyId ?? null,
}))
const filters = reactive<UserFilterState>(createUserFilterState())
const isSuperAdmin = computed(() => session.isSuperAdmin)
const currentUserId = computed(() => session.user?.id ?? null)
@@ -78,6 +87,18 @@ function retry() {
void loadUsers()
}
/**
* 公司目录只服务下拉,不参与表格状态机:拉失败时列表照常显示,
* 但超管建号会卡在"请选择所属公司"——所以要把原因说出来,别让人对着空下拉猜。
*/
async function loadCompanies() {
try {
companies.value = await fetchCompanies()
} catch (error) {
ElMessage.warning(error instanceof Error ? error.message : '公司目录加载失败')
}
}
/** 提交新筛选条件:重置回第一页再查询。 */
function search() {
filters.page = 1
@@ -121,7 +142,10 @@ function goJump() {
changePage(Math.min(Math.max(n, 1), totalPages.value))
}
onMounted(loadUsers)
onMounted(() => {
void loadCompanies()
void loadUsers()
})
</script>
<template>
@@ -138,6 +162,12 @@ onMounted(loadUsers)
<label>用户名(模糊搜索)</label>
<input v-model="filters.keyword" type="text" placeholder="输入用户名关键字" @keyup.enter="search" />
</div>
<div v-if="isSuperAdmin" class="form-group" style="min-width: 160px">
<label>所属公司</label>
<el-select v-model="filters.companyId" class="admin-filter" filterable clearable placeholder="全部公司" @change="search">
<el-option v-for="company in companies" :key="company.id" :label="company.companyName" :value="company.id" />
</el-select>
</div>
<div v-if="isSuperAdmin" class="form-group" style="min-width: 160px">
<label>所属管理员</label>
<el-select v-model="filters.createdByAdminId" class="admin-filter" filterable clearable placeholder="全部" @change="search">
@@ -167,6 +197,7 @@ onMounted(loadUsers)
<th style="width: 70px">ID</th>
<th style="width: 180px">用户名</th>
<th style="width: 140px">角色</th>
<th style="width: 140px">所属公司</th>
<th style="width: 160px">所属管理员</th>
<th style="width: 180px">创建时间</th>
<th style="width: 180px">更新时间</th>
@@ -179,6 +210,7 @@ onMounted(loadUsers)
<td>{{ row.id }}</td>
<td>{{ row.username }}</td>
<td>{{ roleLabel(row.role) }}</td>
<td>{{ row.companyName || '-' }}</td>
<td>{{ row.creatorUsername || '-' }}</td>
<td>{{ formatDateTime(row.createdAt) }}</td>
<td>{{ formatDateTime(row.updatedAt) }}</td>
@@ -205,10 +237,10 @@ onMounted(loadUsers)
</tr>
</template>
<tr v-else-if="loading">
<td colspan="7" class="empty-tip">加载中...</td>
<td colspan="8" class="empty-tip">加载中...</td>
</tr>
<tr v-else>
<td colspan="7" class="empty-tip">{{ userListEmptyHint() }}</td>
<td colspan="8" class="empty-tip">{{ userListEmptyHint() }}</td>
</tr>
</tbody>
</table>
@@ -217,7 +249,14 @@ onMounted(loadUsers)
<OldPagination :total="total" :page="filters.page" :page-size="filters.pageSize" @change="changePage" @size-change="changeSize" />
</section>
<CreateUserDialog v-model="createVisible" :operator-super="isSuperAdmin" :admin-options="admins" @created="loadUsers" />
<CreateUserDialog
v-model="createVisible"
:operator-super="isSuperAdmin"
:admin-options="admins"
:companies="companies"
:operator="operatorScope"
@created="loadUsers"
/>
<EditUserDialog v-model="editVisible" :user="editUser" :operator-super="isSuperAdmin" @updated="loadUsers" />
</div>
</template>
@@ -374,7 +413,7 @@ h3 {
table-layout: fixed;
}
.user-table-scroll > table {
min-width: 860px;
min-width: 1000px;
}
.table-scroll th,
.table-scroll td {