263 lines
7.2 KiB
PHP
263 lines
7.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace app\admin\controller;
|
|
|
|
use app\admin\BaseController;
|
|
use think\exception\ValidateException;
|
|
use think\facade\Db;
|
|
use think\facade\Session;
|
|
use think\response\Json;
|
|
|
|
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()
|
|
{
|
|
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
|
|
]
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取租户用户
|
|
* @return Json
|
|
*/
|
|
public function getTenantUsers(int $tenantId)
|
|
{
|
|
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()
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 获取用户信息
|
|
* @return Json
|
|
*/
|
|
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]);
|
|
|
|
return json([
|
|
'code' => 200,
|
|
'msg' => '获取成功',
|
|
'data' => $user
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 修改密码
|
|
* @return Json
|
|
*/
|
|
public function changePassword(int $id, string $password)
|
|
{
|
|
try {
|
|
$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([
|
|
'code' => 200,
|
|
'msg' => '修改成功'
|
|
]);
|
|
} catch (\Exception $e) {
|
|
// 记录失败日志
|
|
$this->logFail('用户管理', '修改密码', $e->getMessage());
|
|
return json([
|
|
'code' => 500,
|
|
'msg' => '修改失败'
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 添加用户
|
|
* @return Json
|
|
*/
|
|
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;
|
|
$data['tid'] = $tid;
|
|
|
|
$id = AdminUser::insertGetId($data);
|
|
$this->logSuccess('用户管理', '添加用户', ['data' => $data]);
|
|
return json([
|
|
'code' => 200,
|
|
'msg' => '添加成功',
|
|
'data' => ['id' => $id]
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 编辑用户
|
|
* @return Json
|
|
*/
|
|
public function editUser(int $id)
|
|
{
|
|
$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');
|
|
$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,
|
|
'msg' => '编辑成功'
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 删除用户
|
|
* @return Json
|
|
*/
|
|
public function deleteUser(int $id)
|
|
{
|
|
$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' => '用户不存在、已删除或无权限操作'
|
|
]);
|
|
}
|
|
|
|
AdminUser::where('id', $id)
|
|
->where('tid', $tid)
|
|
->update([
|
|
'delete_time' => date('Y-m-d H:i:s')
|
|
]);
|
|
|
|
$this->logSuccess('用户管理', '删除用户', ['id' => $id]);
|
|
return json([
|
|
'code' => 200,
|
|
'msg' => '删除成功'
|
|
]);
|
|
}
|
|
}
|