This commit is contained in:
2026-04-01 10:12:37 +08:00
parent 2a6b14f698
commit e4eab9ad89
9 changed files with 1444 additions and 39 deletions
+125 -31
View File
@@ -14,21 +14,46 @@ use app\model\System\AdminUser;
class UserController extends BaseController
{
private function getCurrentTenantIdOrFail(): int
{
$tid = $this->getTenantId();
if ($tid <= 0) {
throw new \RuntimeException('未获取到有效租户信息');
}
return $tid;
}
/**
* 获取所有用户信息
* @return Json
*/
public function getAllUsers()
{
$users = AdminUser::where('delete_time', null)->field('id, account, name, phone, birth, email, qq, sex, group_id, status, last_login_ip, login_count, create_time, update_time')->select()->toArray();
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'list' => $users,
'total' => count($users)
]
]);
try {
$tid = $this->getCurrentTenantIdOrFail();
$users = AdminUser::where('delete_time', null)
->where('tid', $tid)
->field('id, tid, account, name, phone, birth, email, qq, sex, group_id, status, last_login_ip, login_count, create_time, update_time')
->select()
->toArray();
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'list' => $users,
'total' => count($users)
]
]);
} catch (\Throwable $e) {
return json([
'code' => 401,
'msg' => '获取用户失败:' . $e->getMessage(),
'data' => [
'list' => [],
'total' => 0
]
]);
}
}
/**
@@ -37,15 +62,33 @@ class UserController extends BaseController
*/
public function getTenantUsers(int $tenantId)
{
$users = AdminUser::where('delete_time', null)->where('tid', $tenantId)->field('id, account, name, phone, birth, email, qq, sex, group_id, status, last_login_ip, login_count, create_time, update_time')->select()->toArray();
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'list' => $users,
'total' => count($users)
]
]);
try {
$tid = $this->getCurrentTenantIdOrFail();
if ($tenantId !== $tid) {
return json([
'code' => 403,
'msg' => '禁止跨租户查看用户'
]);
}
$users = AdminUser::where('delete_time', null)
->where('tid', $tid)
->field('id, tid, account, name, phone, birth, email, qq, sex, group_id, status, last_login_ip, login_count, create_time, update_time')
->select()
->toArray();
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'list' => $users,
'total' => count($users)
]
]);
} catch (\Throwable $e) {
return json([
'code' => 401,
'msg' => '获取用户失败:' . $e->getMessage()
]);
}
}
/**
@@ -54,9 +97,18 @@ class UserController extends BaseController
*/
public function getUserInfo(int $id)
{
$tid = $this->getTenantId();
$user = AdminUser::where('id', $id)
->where('tid', $tid)
->where('delete_time', null)
->field('id, account, name, phone, email, birth, qq, sex, group_id, status, create_time, last_login_ip')
->find();
if (!$user) {
return json([
'code' => 404,
'msg' => '用户不存在或无权限访问'
]);
}
// 记录操作日志
$this->logSuccess('用户管理', '获取用户信息', ['id' => $id]);
@@ -75,10 +127,20 @@ class UserController extends BaseController
public function changePassword(int $id, string $password)
{
try {
AdminUser::where('id', $id)->update([
$tid = $this->getCurrentTenantIdOrFail();
$affected = AdminUser::where('id', $id)
->where('tid', $tid)
->where('delete_time', null)
->update([
'password' => md5($password),
'update_time' => date('Y-m-d H:i:s')
]);
if (!$affected) {
return json([
'code' => 404,
'msg' => '用户不存在或无权限修改'
]);
}
// 记录操作日志
$this->logSuccess('用户管理', '修改密码', ['id' => $id]);
return json([
@@ -102,18 +164,20 @@ class UserController extends BaseController
public function addUser()
{
$data = request()->param();
$tid = $this->getTenantId();
if ($tid <= 0) {
return json([
'code' => 401,
'msg' => '未获取到有效租户信息'
]);
}
$data['password'] = md5($data['password']);
$data['create_time'] = date('Y-m-d H:i:s');
$data['update_time'] = $data['create_time'];
$data['group_id'] = 2;
if (!isset($data['tid']) || empty($data['tid'])) {
return json([
'code' => 400,
'msg' => '租户ID不能为空'
]);
}
$data['tid'] = $tid;
$id = AdminUser::insertGetId($data);
$this->logSuccess('用户管理', '添加用户', ['data' => $data]);
return json([
@@ -131,8 +195,25 @@ class UserController extends BaseController
{
$data = request()->param();
unset($data['_t'], $data['id']);
unset($data['tid']);
$tid = $this->getTenantId();
if ($tid <= 0) {
return json([
'code' => 401,
'msg' => '未获取到有效租户信息'
]);
}
$data['update_time'] = date('Y-m-d H:i:s');
AdminUser::where('id', $id)->update($data);
$affected = AdminUser::where('id', $id)
->where('tid', $tid)
->where('delete_time', null)
->update($data);
if (!$affected) {
return json([
'code' => 404,
'msg' => '用户不存在或无权限编辑'
]);
}
$this->logSuccess('用户管理', '编辑用户', ['id' => $id]);
return json([
'code' => 200,
@@ -146,16 +227,29 @@ class UserController extends BaseController
*/
public function deleteUser(int $id)
{
$user = AdminUser::where('id', $id)->where('delete_time', null)->find();
$tid = $this->getTenantId();
if ($tid <= 0) {
return json([
'code' => 401,
'msg' => '未获取到有效租户信息'
]);
}
$user = AdminUser::where('id', $id)
->where('tid', $tid)
->where('delete_time', null)
->find();
if (!$user) {
return json([
'code' => 404,
'msg' => '用户不存在已删除'
'msg' => '用户不存在已删除或无权限操作'
]);
}
AdminUser::where('id', $id)->update([
AdminUser::where('id', $id)
->where('tid', $tid)
->update([
'delete_time' => date('Y-m-d H:i:s')
]);