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:
@@ -0,0 +1,268 @@
|
||||
<script setup lang="ts">
|
||||
/** 公司管理(设计文档 §2.3):列表 + 新建 + 改名/启停。
|
||||
* 可见范围由后端收窄(超管全部、公司内 admin 只自己公司);写操作仅超管,这里隐藏入口只是配合,
|
||||
* 真正的把关在 CompanyController,页面不得当作权限边界。 */
|
||||
import { computed, onMounted, ref } from 'vue'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { formatDateTime } from '@/utils/datetime'
|
||||
import { fetchCompanies } from '@/api/companies'
|
||||
import { COMPANY_STATUS_ACTIVE, type CompanyItem } from '@/api/companies-dto'
|
||||
import { useAdminSessionStore } from '@/stores/admin-session'
|
||||
import OldPagination from '@/components/OldPagination.vue'
|
||||
import CompanyEditorDialog from './CompanyEditorDialog.vue'
|
||||
|
||||
const session = useAdminSessionStore()
|
||||
const loading = ref(false)
|
||||
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
|
||||
try {
|
||||
rows.value = await fetchCompanies()
|
||||
} catch (error) {
|
||||
ElMessage.error(error instanceof Error ? error.message : '公司列表加载失败')
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
function openCreate() {
|
||||
editingCompany.value = null
|
||||
editorVisible.value = true
|
||||
}
|
||||
|
||||
function openEdit(company: CompanyItem) {
|
||||
editingCompany.value = company
|
||||
editorVisible.value = true
|
||||
}
|
||||
|
||||
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>
|
||||
</div>
|
||||
|
||||
<div class="table-scroll">
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<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: 90px">操作</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<template v-if="filteredRows.length">
|
||||
<tr v-for="(company, index) in pagedRows" :key="company.id">
|
||||
<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">
|
||||
<button v-if="canManage" class="btn btn-sm" type="button" @click="openEdit(company)">编辑</button>
|
||||
<span v-else class="cell-none">-</span>
|
||||
</td>
|
||||
</tr>
|
||||
</template>
|
||||
<tr v-else-if="loading">
|
||||
<td colspan="7" class="empty-tip">加载中...</td>
|
||||
</tr>
|
||||
<tr v-else>
|
||||
<td colspan="7" 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" @saved="loadCompanies" />
|
||||
</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;
|
||||
}
|
||||
.cell-none {
|
||||
color: #8293a5;
|
||||
}
|
||||
.empty-tip {
|
||||
padding: 44px 24px;
|
||||
text-align: center;
|
||||
color: #8293a5;
|
||||
font-size: 13.5px;
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,89 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, watch } from 'vue'
|
||||
import { createCompany, updateCompany } from '@/api/companies'
|
||||
import {
|
||||
COMPANY_NAME_MAX_LENGTH,
|
||||
COMPANY_STATUS_ACTIVE,
|
||||
COMPANY_STATUS_DISABLED,
|
||||
validateCompanyDraft,
|
||||
type CompanyDraft,
|
||||
type CompanyItem,
|
||||
} from '@/api/companies-dto'
|
||||
import { showAdminFeedback } from '@/components/admin-feedback-ui'
|
||||
import { actionableErrorText } from '@/components/admin-feedback'
|
||||
|
||||
/** 新建/编辑公司(改名 + 启停 + 备注);写操作仅超管可用,权限由后端把关。 */
|
||||
const props = defineProps<{ modelValue: boolean; company: CompanyItem | null }>()
|
||||
const emit = defineEmits<{ (e: 'update:modelValue', v: boolean): void; (e: 'saved'): void }>()
|
||||
|
||||
const form = ref<CompanyDraft>({ companyName: '', status: COMPANY_STATUS_ACTIVE, remark: '' })
|
||||
const errorText = ref('')
|
||||
const busy = ref(false)
|
||||
|
||||
watch(
|
||||
() => props.modelValue,
|
||||
(open) => {
|
||||
if (!open) return
|
||||
form.value = props.company
|
||||
? { companyName: props.company.companyName, status: props.company.status, remark: props.company.remark || '' }
|
||||
: { companyName: '', status: COMPANY_STATUS_ACTIVE, remark: '' }
|
||||
errorText.value = ''
|
||||
},
|
||||
)
|
||||
|
||||
function close() {
|
||||
emit('update:modelValue', false)
|
||||
}
|
||||
|
||||
async function save() {
|
||||
errorText.value = validateCompanyDraft(form.value)
|
||||
if (errorText.value) return
|
||||
busy.value = true
|
||||
try {
|
||||
if (props.company) await updateCompany(props.company.id, form.value)
|
||||
else await createCompany(form.value)
|
||||
showAdminFeedback(props.company ? '公司已更新' : '公司已创建', 'success')
|
||||
emit('saved')
|
||||
close()
|
||||
} catch (error) {
|
||||
errorText.value = actionableErrorText(error)
|
||||
showAdminFeedback(errorText.value, 'error')
|
||||
} finally {
|
||||
busy.value = false
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<el-dialog
|
||||
:model-value="modelValue"
|
||||
:title="company ? '编辑公司' : '新建公司'"
|
||||
width="520px"
|
||||
:close-on-click-modal="false"
|
||||
destroy-on-close
|
||||
@update:model-value="(v: boolean) => emit('update:modelValue', v)"
|
||||
>
|
||||
<el-form label-position="top" @submit.prevent>
|
||||
<el-form-item label="公司名称" :error="errorText || undefined">
|
||||
<el-input
|
||||
v-model="form.companyName"
|
||||
:maxlength="COMPANY_NAME_MAX_LENGTH"
|
||||
placeholder="请输入公司名称(全平台唯一)"
|
||||
/>
|
||||
</el-form-item>
|
||||
<el-form-item label="状态">
|
||||
<el-select v-model="form.status" style="width: 100%">
|
||||
<el-option :value="COMPANY_STATUS_ACTIVE" label="启用" />
|
||||
<el-option :value="COMPANY_STATUS_DISABLED" label="停用" />
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
<el-form-item label="备注">
|
||||
<el-input v-model="form.remark" type="textarea" :rows="2" placeholder="可留空" />
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
<template #footer>
|
||||
<el-button @click="close">取消</el-button>
|
||||
<el-button type="primary" :loading="busy" @click="save">保存</el-button>
|
||||
</template>
|
||||
</el-dialog>
|
||||
</template>
|
||||
@@ -1,12 +1,16 @@
|
||||
<script setup lang="ts">
|
||||
import { reactive, ref, watch } from 'vue'
|
||||
import { computed, reactive, ref, watch } from 'vue'
|
||||
import { submitCreateUser } from './user-create-adapter'
|
||||
import {
|
||||
createEmptyCreateUserForm,
|
||||
initialCompanyId,
|
||||
selectableCompanies,
|
||||
type CreateOperatorScope,
|
||||
type CreateUserForm,
|
||||
type CreateUserFormErrors,
|
||||
validateCreateUserForm,
|
||||
} from './user-create-model'
|
||||
import type { CompanyItem } from '@/api/companies-dto'
|
||||
import { showAdminFeedback } from '@/components/admin-feedback-ui'
|
||||
import { actionableErrorText } from '@/components/admin-feedback'
|
||||
import UserMenuAuthTree from './UserMenuAuthTree.vue'
|
||||
@@ -16,6 +20,10 @@ const props = defineProps<{
|
||||
operatorSuper?: boolean
|
||||
/** 所属管理员候选(列表响应 admins;仅超管创建普通账号时展示)。 */
|
||||
adminOptions?: Array<{ id: number; username: string }>
|
||||
/** 整份公司目录(GET /api/admin/companies,后端已按操作者收窄)。 */
|
||||
companies?: CompanyItem[]
|
||||
/** 操作者角色与归属:决定公司下拉是否必选。 */
|
||||
operator?: CreateOperatorScope
|
||||
}>()
|
||||
const emit = defineEmits<{ (e: 'update:modelValue', v: boolean): void; (e: 'created'): void }>()
|
||||
|
||||
@@ -23,6 +31,9 @@ const form = reactive<CreateUserForm>(createEmptyCreateUserForm())
|
||||
const errors = reactive<CreateUserFormErrors>({})
|
||||
const busy = ref(false)
|
||||
|
||||
/** 可选项收窄规则与后端 resolveTargetCompanyId 同源,写在这里而不是父页模板里。 */
|
||||
const companyOptions = computed(() => selectableCompanies(props.companies || [], props.operator || {}))
|
||||
|
||||
/** 角色下拉(对齐 admin.html:普通账号/管理员;超管才显示可改)。 */
|
||||
const ROLE_CHOICES: Array<{ value: 'admin' | 'normal'; label: string }> = [
|
||||
{ value: 'normal', label: '普通账号' },
|
||||
@@ -38,8 +49,10 @@ watch(
|
||||
form.role = 'normal'
|
||||
form.columnIds = []
|
||||
form.createdBy = null
|
||||
form.companyId = initialCompanyId(props.operator || {}, props.companies || [])
|
||||
errors.username = undefined
|
||||
errors.password = undefined
|
||||
errors.company = undefined
|
||||
},
|
||||
)
|
||||
|
||||
@@ -48,7 +61,7 @@ function close(): void {
|
||||
}
|
||||
|
||||
async function create(): Promise<void> {
|
||||
const { valid, errors: errs } = validateCreateUserForm(form)
|
||||
const { valid, errors: errs } = validateCreateUserForm(form, props.operator)
|
||||
Object.assign(errors, errs)
|
||||
if (!valid) return
|
||||
busy.value = true
|
||||
@@ -107,6 +120,22 @@ async function create(): Promise<void> {
|
||||
/>
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
<el-form-item v-if="companyOptions.length" label="所属公司" :error="errors.company">
|
||||
<el-select
|
||||
v-model="form.companyId"
|
||||
filterable
|
||||
:placeholder="operatorSuper ? '请选择所属公司' : '所属公司'"
|
||||
style="width: 200px"
|
||||
@change="errors.company = undefined"
|
||||
>
|
||||
<el-option
|
||||
v-for="company in companyOptions"
|
||||
:key="company.id"
|
||||
:label="company.companyName"
|
||||
:value="company.id"
|
||||
/>
|
||||
</el-select>
|
||||
</el-form-item>
|
||||
<el-form-item label="菜单权限(勾选上级菜单会继承全部子菜单)">
|
||||
<div class="auth-tree-box">
|
||||
<UserMenuAuthTree v-model:checked="form.columnIds" />
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -1,11 +1,20 @@
|
||||
/** 新建用户表单模型(任务 45):纯逻辑,镜像 Java AdminUserCreateRequest 字段与后端校验。 */
|
||||
|
||||
// 相对路径而非 @/ 别名:本文件被 node --test 直接 import,别名只有 vite 认。
|
||||
import { COMPANY_STATUS_ACTIVE, type CompanyItem } from '../../api/companies-dto.ts'
|
||||
|
||||
export const CREATE_USERNAME_MIN_LEN = 2
|
||||
export const CREATE_PASSWORD_MIN_LEN = 6
|
||||
|
||||
/** 新建用户可选角色:super_admin 不可由表单创建(后端只收 admin|normal)。 */
|
||||
export type CreatableUserRole = 'normal' | 'admin'
|
||||
|
||||
/** 当前操作者的公司归属(来自 /api/admin/current-user)。 */
|
||||
export interface CreateOperatorScope {
|
||||
role?: string | null
|
||||
companyId?: number | null
|
||||
}
|
||||
|
||||
export interface CreateUserForm {
|
||||
username: string
|
||||
password: string
|
||||
@@ -14,11 +23,14 @@ export interface CreateUserForm {
|
||||
columnIds: number[]
|
||||
/** 所属管理员 id(超管创建普通账号时可指定,对齐 admin 的 created_by_id)。 */
|
||||
createdBy?: number | null
|
||||
/** 所属公司 id(V143)。超管必选;公司内 admin 由后端强制落自己公司。 */
|
||||
companyId?: number | null
|
||||
}
|
||||
|
||||
export interface CreateUserFormErrors {
|
||||
username?: string
|
||||
password?: string
|
||||
company?: string
|
||||
}
|
||||
|
||||
/** 发送 POST /api/admin/user 的请求体(与 Java AdminUserCreateRequest camelCase 对齐)。 */
|
||||
@@ -29,10 +41,11 @@ export interface CreateUserPayload {
|
||||
columnIds: number[]
|
||||
/** 仅 role=normal 且选了所属管理员时发送,与参考 admin.js 的 created_by_id 语义一致。 */
|
||||
createdById?: number
|
||||
companyId?: number
|
||||
}
|
||||
|
||||
export function createEmptyCreateUserForm(): CreateUserForm {
|
||||
return { username: '', password: '', role: 'normal', columnIds: [], createdBy: null }
|
||||
return { username: '', password: '', role: 'normal', columnIds: [], createdBy: null, companyId: null }
|
||||
}
|
||||
|
||||
/** 未知/空白/超管角色一律归一到 normal,与后端 "admin"/"normal" 白名单一致。 */
|
||||
@@ -40,8 +53,40 @@ export function normalizeCreateRole(role: string | null | undefined): CreatableU
|
||||
return (role || '').trim().toLowerCase() === 'admin' ? 'admin' : 'normal'
|
||||
}
|
||||
|
||||
/** 表单校验(镜像后端:用户名≥2、密码≥6、均必填);返回逐字段错误。 */
|
||||
export function validateCreateUserForm(form: CreateUserForm): { valid: boolean; errors: CreateUserFormErrors } {
|
||||
function positiveInt(value: unknown): number | null {
|
||||
return typeof value === 'number' && Number.isFinite(value) && value > 0 ? Math.floor(value) : null
|
||||
}
|
||||
|
||||
/**
|
||||
* 建号能选哪家公司,口径与后端 AdminUserService#resolveTargetCompanyId 一致:
|
||||
* 超管 = 全部启用中的公司(后端 requireActive 会拒停用公司);
|
||||
* 其余角色 = 只有自己也那一家,且不看启停——后端对非超管直接落 operator.companyId、
|
||||
* 不校验状态,前端若跟着过滤就会出现「下拉没得选、提交却能建号」的自相矛盾。
|
||||
*/
|
||||
export function selectableCompanies(companies: CompanyItem[], operator: CreateOperatorScope): CompanyItem[] {
|
||||
const rows = Array.isArray(companies) ? companies : []
|
||||
if (operator?.role === 'super_admin') {
|
||||
return rows.filter((company) => company.status === COMPANY_STATUS_ACTIVE)
|
||||
}
|
||||
const own = positiveInt(operator?.companyId)
|
||||
if (own === null) return []
|
||||
return rows.filter((company) => company.id === own)
|
||||
}
|
||||
|
||||
/**
|
||||
* 弹窗打开时预选哪家公司:候选只有一个(公司内 admin 的本公司)就落它;
|
||||
* 多个(超管)返回 null——给超管填默认值等于替他决定账号挂在哪家,这是会写进库的判断。
|
||||
*/
|
||||
export function initialCompanyId(operator: CreateOperatorScope, companies: CompanyItem[]): number | null {
|
||||
const options = selectableCompanies(companies, operator)
|
||||
return options.length === 1 ? options[0].id : null
|
||||
}
|
||||
|
||||
/** 表单校验(镜像后端:用户名≥2、密码≥6、均必填;超管必须显式选公司);返回逐字段错误。 */
|
||||
export function validateCreateUserForm(
|
||||
form: CreateUserForm,
|
||||
operator?: CreateOperatorScope,
|
||||
): { valid: boolean; errors: CreateUserFormErrors } {
|
||||
const errors: CreateUserFormErrors = {}
|
||||
const username = (form.username || '').trim()
|
||||
if (!username) {
|
||||
@@ -55,11 +100,15 @@ export function validateCreateUserForm(form: CreateUserForm): { valid: boolean;
|
||||
} else if (password.length < CREATE_PASSWORD_MIN_LEN) {
|
||||
errors.password = `密码至少${CREATE_PASSWORD_MIN_LEN}个字符`
|
||||
}
|
||||
if (operator?.role === 'super_admin' && positiveInt(form.companyId) === null) {
|
||||
errors.company = '请选择所属公司'
|
||||
}
|
||||
return { valid: Object.keys(errors).length === 0, errors }
|
||||
}
|
||||
|
||||
/** 表单 → 创建请求体:用户名去空格、角色归一、菜单 id 拷贝不共享引用;
|
||||
* createdById 仅在 role=normal 且选了所属管理员时携带(对齐 admin.js 的 created_by_id 语义)。 */
|
||||
* createdById 仅在 role=normal 且选了所属管理员时携带(对齐 admin.js 的 created_by_id 语义);
|
||||
* companyId 选了才带,未选时由后端落操作者自己公司。 */
|
||||
export function toCreateUserRequest(form: CreateUserForm): CreateUserPayload {
|
||||
const payload: CreateUserPayload = {
|
||||
username: (form.username || '').trim(),
|
||||
@@ -70,5 +119,7 @@ export function toCreateUserRequest(form: CreateUserForm): CreateUserPayload {
|
||||
if (payload.role === 'normal' && form.createdBy != null) {
|
||||
payload.createdById = form.createdBy
|
||||
}
|
||||
const companyId = positiveInt(form.companyId)
|
||||
if (companyId !== null) payload.companyId = companyId
|
||||
return payload
|
||||
}
|
||||
|
||||
@@ -9,18 +9,27 @@ export interface UserFilterState {
|
||||
createdByAdminId: number | null
|
||||
/** 角色过滤(''=全部;super_admin/admin/normal)。 */
|
||||
role: string
|
||||
/** 按所属公司过滤(null=全部);只有超管能用,公司内 admin 看到的本来就是本公司。 */
|
||||
companyId: number | null
|
||||
page: number
|
||||
pageSize: number
|
||||
}
|
||||
|
||||
export function createUserFilterState(): UserFilterState {
|
||||
return { keyword: '', createdByAdminId: null, role: '', page: USER_PAGE_MIN_PAGE, pageSize: USER_PAGE_DEFAULT_SIZE }
|
||||
return {
|
||||
keyword: '',
|
||||
createdByAdminId: null,
|
||||
role: '',
|
||||
companyId: null,
|
||||
page: USER_PAGE_MIN_PAGE,
|
||||
pageSize: USER_PAGE_DEFAULT_SIZE,
|
||||
}
|
||||
}
|
||||
|
||||
/** 修改筛选条件时重置回第一页(避免停在可能越界的旧页)。 */
|
||||
export function withFilterReset(
|
||||
state: UserFilterState,
|
||||
next: Partial<Pick<UserFilterState, 'keyword' | 'createdByAdminId' | 'role'>>,
|
||||
next: Partial<Pick<UserFilterState, 'keyword' | 'createdByAdminId' | 'role' | 'companyId'>>,
|
||||
): UserFilterState {
|
||||
return { ...state, ...next, page: USER_PAGE_MIN_PAGE }
|
||||
}
|
||||
@@ -32,7 +41,11 @@ export function withPage(state: UserFilterState, page: number): UserFilterState
|
||||
|
||||
/** 是否存在生效筛选(仅空白关键字不算)。 */
|
||||
export function isFilterActive(state: UserFilterState): boolean {
|
||||
return Boolean(state.keyword && state.keyword.trim()) || state.createdByAdminId != null
|
||||
return (
|
||||
Boolean(state.keyword && state.keyword.trim()) ||
|
||||
state.createdByAdminId != null ||
|
||||
state.companyId != null
|
||||
)
|
||||
}
|
||||
|
||||
/** 页面状态 → 查询适配层参数(空关键字不下发)。 */
|
||||
@@ -43,6 +56,7 @@ export function toUserListParams(state: UserFilterState): UserListParams {
|
||||
pageSize: state.pageSize,
|
||||
keyword: keyword || undefined,
|
||||
createdById: state.createdByAdminId,
|
||||
companyId: state.companyId,
|
||||
}
|
||||
const role = (state.role || '').trim()
|
||||
if (role) params.role = role
|
||||
|
||||
Reference in New Issue
Block a user