逻辑修改
This commit is contained in:
parent
46da547671
commit
e1da1f9ae7
|
|
@ -70,8 +70,47 @@ public class OrganizationsController : BaseApiController
|
||||||
[HttpDelete("{id}")]
|
[HttpDelete("{id}")]
|
||||||
public async Task<IActionResult> Delete(int id)
|
public async Task<IActionResult> Delete(int id)
|
||||||
{
|
{
|
||||||
await _organizationService.DeleteAsync(id);
|
try
|
||||||
return NoContent();
|
{
|
||||||
|
await _organizationService.DeleteAsync(id);
|
||||||
|
return NoContent();
|
||||||
|
}
|
||||||
|
catch (ArgumentException ex)
|
||||||
|
{
|
||||||
|
return NotFound(new { message = ex.Message });
|
||||||
|
}
|
||||||
|
catch (InvalidOperationException ex)
|
||||||
|
{
|
||||||
|
return BadRequest(new { message = ex.Message });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取组织关联的账户列表
|
||||||
|
/// </summary>
|
||||||
|
[HttpGet("{id}/accounts")]
|
||||||
|
public async Task<IActionResult> GetAccounts(int id)
|
||||||
|
{
|
||||||
|
var unit = await _organizationService.GetByIdAsync(id);
|
||||||
|
if (unit == null)
|
||||||
|
{
|
||||||
|
return NotFound(new { message = "组织不存在" });
|
||||||
|
}
|
||||||
|
|
||||||
|
var accounts = await _context.UserAccounts
|
||||||
|
.Where(u => u.OrganizationalUnitId == id)
|
||||||
|
.Select(u => new
|
||||||
|
{
|
||||||
|
u.Id,
|
||||||
|
u.Username,
|
||||||
|
u.DisplayName,
|
||||||
|
u.IsActive,
|
||||||
|
u.CreatedAt,
|
||||||
|
u.LastLoginAt
|
||||||
|
})
|
||||||
|
.ToListAsync();
|
||||||
|
|
||||||
|
return Ok(accounts);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -108,4 +147,24 @@ public class OrganizationsController : BaseApiController
|
||||||
|
|
||||||
return Ok(new { message = "账户创建成功", username = account.Username });
|
return Ok(new { message = "账户创建成功", username = account.Username });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 删除账户
|
||||||
|
/// </summary>
|
||||||
|
[HttpDelete("{organizationId}/accounts/{accountId}")]
|
||||||
|
public async Task<IActionResult> DeleteAccount(int organizationId, int accountId)
|
||||||
|
{
|
||||||
|
var account = await _context.UserAccounts
|
||||||
|
.FirstOrDefaultAsync(u => u.Id == accountId && u.OrganizationalUnitId == organizationId);
|
||||||
|
|
||||||
|
if (account == null)
|
||||||
|
{
|
||||||
|
return NotFound(new { message = "账户不存在" });
|
||||||
|
}
|
||||||
|
|
||||||
|
_context.UserAccounts.Remove(account);
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
return Ok(new { message = "账户删除成功" });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -135,6 +135,12 @@ public class OrganizationService : IOrganizationService
|
||||||
if (unit.Children.Any())
|
if (unit.Children.Any())
|
||||||
throw new InvalidOperationException("无法删除有下级单位的组织");
|
throw new InvalidOperationException("无法删除有下级单位的组织");
|
||||||
|
|
||||||
|
// 检查是否有关联的用户账户
|
||||||
|
var hasUsers = await _context.UserAccounts
|
||||||
|
.AnyAsync(u => u.OrganizationalUnitId == id);
|
||||||
|
if (hasUsers)
|
||||||
|
throw new InvalidOperationException("无法删除有关联用户的组织");
|
||||||
|
|
||||||
_context.OrganizationalUnits.Remove(unit);
|
_context.OrganizationalUnits.Remove(unit);
|
||||||
await _context.SaveChangesAsync();
|
await _context.SaveChangesAsync();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,5 +33,14 @@ export const organizationsApi = {
|
||||||
|
|
||||||
async createAccount(organizationId: number, accountData: { username: string; password: string }): Promise<void> {
|
async createAccount(organizationId: number, accountData: { username: string; password: string }): Promise<void> {
|
||||||
await apiClient.post(`/organizations/${organizationId}/accounts`, accountData)
|
await apiClient.post(`/organizations/${organizationId}/accounts`, accountData)
|
||||||
|
},
|
||||||
|
|
||||||
|
async getAccounts(organizationId: number): Promise<any[]> {
|
||||||
|
const response = await apiClient.get<any[]>(`/organizations/${organizationId}/accounts`)
|
||||||
|
return response.data
|
||||||
|
},
|
||||||
|
|
||||||
|
async deleteAccount(organizationId: number, accountId: number): Promise<void> {
|
||||||
|
await apiClient.delete(`/organizations/${organizationId}/accounts/${accountId}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@
|
||||||
{{ formatDate(row.createdAt) }}
|
{{ formatDate(row.createdAt) }}
|
||||||
</template>
|
</template>
|
||||||
</el-table-column>
|
</el-table-column>
|
||||||
<el-table-column label="操作" width="280" fixed="right">
|
<el-table-column label="操作" width="320" fixed="right">
|
||||||
<template #default="{ row }">
|
<template #default="{ row }">
|
||||||
<el-button
|
<el-button
|
||||||
v-if="row.level < 4"
|
v-if="row.level < 4"
|
||||||
|
|
@ -39,6 +39,7 @@
|
||||||
添加{{ getChildLevelName(row.level) }}
|
添加{{ getChildLevelName(row.level) }}
|
||||||
</el-button>
|
</el-button>
|
||||||
<el-button type="warning" text size="small" @click="handleEdit(row)">编辑</el-button>
|
<el-button type="warning" text size="small" @click="handleEdit(row)">编辑</el-button>
|
||||||
|
<el-button type="info" text size="small" @click="handleViewAccounts(row)">查看账号</el-button>
|
||||||
<el-button type="success" text size="small" @click="handleCreateAccount(row)">创建账户</el-button>
|
<el-button type="success" text size="small" @click="handleCreateAccount(row)">创建账户</el-button>
|
||||||
<el-button type="danger" text size="small" @click="handleDelete(row)">删除</el-button>
|
<el-button type="danger" text size="small" @click="handleDelete(row)">删除</el-button>
|
||||||
</template>
|
</template>
|
||||||
|
|
@ -80,6 +81,43 @@
|
||||||
<el-button type="primary" :loading="savingAccount" @click="handleSaveAccount">创建</el-button>
|
<el-button type="primary" :loading="savingAccount" @click="handleSaveAccount">创建</el-button>
|
||||||
</template>
|
</template>
|
||||||
</el-dialog>
|
</el-dialog>
|
||||||
|
|
||||||
|
<!-- View Accounts Dialog -->
|
||||||
|
<el-dialog v-model="showAccountsDialog" title="组织账号列表" width="900px">
|
||||||
|
<div v-if="selectedOrg" style="margin-bottom: 16px;">
|
||||||
|
<el-tag type="info">{{ selectedOrg.name }}</el-tag>
|
||||||
|
</div>
|
||||||
|
<el-table :data="accounts" v-loading="loadingAccounts">
|
||||||
|
<el-table-column prop="username" label="用户名" min-width="120" />
|
||||||
|
<el-table-column prop="displayName" label="显示名称" min-width="140" />
|
||||||
|
<el-table-column prop="isActive" label="状态" width="80">
|
||||||
|
<template #default="{ row }">
|
||||||
|
<el-tag :type="row.isActive ? 'success' : 'danger'" size="small">
|
||||||
|
{{ row.isActive ? '激活' : '禁用' }}
|
||||||
|
</el-tag>
|
||||||
|
</template>
|
||||||
|
</el-table-column>
|
||||||
|
<el-table-column prop="createdAt" label="创建时间" min-width="160">
|
||||||
|
<template #default="{ row }">
|
||||||
|
{{ formatDate(row.createdAt) }}
|
||||||
|
</template>
|
||||||
|
</el-table-column>
|
||||||
|
<el-table-column prop="lastLoginAt" label="最后登录" min-width="160">
|
||||||
|
<template #default="{ row }">
|
||||||
|
{{ row.lastLoginAt ? formatDate(row.lastLoginAt) : '从未登录' }}
|
||||||
|
</template>
|
||||||
|
</el-table-column>
|
||||||
|
<el-table-column label="操作" width="80" fixed="right">
|
||||||
|
<template #default="{ row }">
|
||||||
|
<el-button type="danger" text size="small" @click="handleDeleteAccount(row)">删除</el-button>
|
||||||
|
</template>
|
||||||
|
</el-table-column>
|
||||||
|
</el-table>
|
||||||
|
<el-empty v-if="!loadingAccounts && accounts.length === 0" description="该组织暂无账号" />
|
||||||
|
<template #footer>
|
||||||
|
<el-button @click="showAccountsDialog = false">关闭</el-button>
|
||||||
|
</template>
|
||||||
|
</el-dialog>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|
@ -94,11 +132,14 @@ import { OrganizationalLevel } from '@/types'
|
||||||
const organizations = ref<OrganizationalUnit[]>([])
|
const organizations = ref<OrganizationalUnit[]>([])
|
||||||
const showCreateDialog = ref(false)
|
const showCreateDialog = ref(false)
|
||||||
const showAccountDialog = ref(false)
|
const showAccountDialog = ref(false)
|
||||||
|
const showAccountsDialog = ref(false)
|
||||||
const editingOrg = ref<OrganizationalUnit | null>(null)
|
const editingOrg = ref<OrganizationalUnit | null>(null)
|
||||||
const parentOrg = ref<OrganizationalUnit | null>(null)
|
const parentOrg = ref<OrganizationalUnit | null>(null)
|
||||||
const selectedOrg = ref<OrganizationalUnit | null>(null)
|
const selectedOrg = ref<OrganizationalUnit | null>(null)
|
||||||
const saving = ref(false)
|
const saving = ref(false)
|
||||||
const savingAccount = ref(false)
|
const savingAccount = ref(false)
|
||||||
|
const loadingAccounts = ref(false)
|
||||||
|
const accounts = ref<any[]>([])
|
||||||
const formRef = ref<FormInstance>()
|
const formRef = ref<FormInstance>()
|
||||||
const accountFormRef = ref<FormInstance>()
|
const accountFormRef = ref<FormInstance>()
|
||||||
|
|
||||||
|
|
@ -204,6 +245,37 @@ function handleCreateAccount(org: OrganizationalUnit) {
|
||||||
showAccountDialog.value = true
|
showAccountDialog.value = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function handleViewAccounts(org: OrganizationalUnit) {
|
||||||
|
selectedOrg.value = org
|
||||||
|
showAccountsDialog.value = true
|
||||||
|
loadingAccounts.value = true
|
||||||
|
try {
|
||||||
|
accounts.value = await organizationsApi.getAccounts(org.id)
|
||||||
|
} catch {
|
||||||
|
ElMessage.error('加载账号列表失败')
|
||||||
|
} finally {
|
||||||
|
loadingAccounts.value = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleDeleteAccount(account: any) {
|
||||||
|
if (!selectedOrg.value) return
|
||||||
|
|
||||||
|
try {
|
||||||
|
await ElMessageBox.confirm(`确定要删除账号 "${account.username}" 吗?`, '确认删除', {
|
||||||
|
type: 'warning'
|
||||||
|
})
|
||||||
|
await organizationsApi.deleteAccount(selectedOrg.value.id, account.id)
|
||||||
|
ElMessage.success('账号删除成功')
|
||||||
|
// 重新加载账号列表
|
||||||
|
await handleViewAccounts(selectedOrg.value)
|
||||||
|
} catch (error: any) {
|
||||||
|
if (error !== 'cancel') {
|
||||||
|
ElMessage.error('删除账号失败')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function handleDelete(org: OrganizationalUnit) {
|
async function handleDelete(org: OrganizationalUnit) {
|
||||||
try {
|
try {
|
||||||
await ElMessageBox.confirm('确定要删除该组织吗?', '确认删除', {
|
await ElMessageBox.confirm('确定要删除该组织吗?', '确认删除', {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user