up
This commit is contained in:
114
app/controller/User.php
Normal file
114
app/controller/User.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\controller;
|
||||
|
||||
use app\BaseController;
|
||||
use app\middleware\Auth;
|
||||
use app\service\UserService;
|
||||
use think\facade\Session;
|
||||
use think\facade\View;
|
||||
|
||||
/**
|
||||
* 用户管理控制器
|
||||
*/
|
||||
class User extends BaseController
|
||||
{
|
||||
protected $middleware = [Auth::class];
|
||||
|
||||
/**
|
||||
* 显示账号管理页面
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
// 检查是否为管理员
|
||||
if (!Session::get('is_admin', false)) {
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '无权限访问',
|
||||
]);
|
||||
}
|
||||
|
||||
return View::fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有账号列表
|
||||
*/
|
||||
public function getUsers()
|
||||
{
|
||||
// 检查是否为管理员
|
||||
if (!Session::get('is_admin', false)) {
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '无权限访问',
|
||||
]);
|
||||
}
|
||||
|
||||
$service = new UserService();
|
||||
$users = $service->getAllUsers();
|
||||
|
||||
// 隐藏密码
|
||||
foreach ($users as &$user) {
|
||||
if (isset($user['password'])) {
|
||||
$user['password'] = '******';
|
||||
}
|
||||
}
|
||||
|
||||
return json([
|
||||
'code' => 1,
|
||||
'data' => $users,
|
||||
'msg' => '获取成功',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加账号
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
// 检查是否为管理员
|
||||
if (!Session::get('is_admin', false)) {
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '无权限访问',
|
||||
]);
|
||||
}
|
||||
|
||||
$username = $this->request->param('username', '');
|
||||
$password = $this->request->param('password', '');
|
||||
|
||||
$service = new UserService();
|
||||
$result = $service->addUser($username, $password);
|
||||
|
||||
return json($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除账号
|
||||
*/
|
||||
public function delete()
|
||||
{
|
||||
// 检查是否为管理员
|
||||
if (!Session::get('is_admin', false)) {
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '无权限访问',
|
||||
]);
|
||||
}
|
||||
|
||||
$username = $this->request->param('username', '');
|
||||
|
||||
if (empty($username)) {
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '用户名不能为空',
|
||||
]);
|
||||
}
|
||||
|
||||
$service = new UserService();
|
||||
$result = $service->deleteUser($username);
|
||||
|
||||
return json($result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user