feat: auth
This commit is contained in:
71
app/Controller/Admin/AuthController.php
Normal file
71
app/Controller/Admin/AuthController.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller\Admin;
|
||||
|
||||
use App\Controller\AbstractController;
|
||||
use App\Request\Admin\AuthRequest;
|
||||
use App\Service\Admin\User\RoleMenuService;
|
||||
use App\Service\Admin\User\RoleService;
|
||||
use Hyperf\HttpServer\Annotation\Controller;
|
||||
use Hyperf\HttpServer\Annotation\RequestMapping;
|
||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
use Hyperf\HttpServer\Contract\ResponseInterface;
|
||||
use Hyperf\Validation\Annotation\Scene;
|
||||
|
||||
#[Controller(prefix: "admin/auth")]
|
||||
class AuthController extends AbstractController
|
||||
{
|
||||
public function menu_add(AuthRequest $request)
|
||||
{
|
||||
return (new RoleMenuService)->add();
|
||||
}
|
||||
|
||||
public function menu_edit()
|
||||
{
|
||||
return (new RoleMenuService)->edit();
|
||||
}
|
||||
|
||||
public function menu_del()
|
||||
{
|
||||
return (new RoleMenuService)->del();
|
||||
}
|
||||
|
||||
public function menu_list()
|
||||
{
|
||||
return (new RoleMenuService)->handle();
|
||||
}
|
||||
|
||||
public function mean()
|
||||
{
|
||||
return (new RoleMenuService)->details();
|
||||
}
|
||||
|
||||
public function role_add(AuthRequest $request)
|
||||
{
|
||||
return (new RoleService)->add();
|
||||
}
|
||||
|
||||
public function role_edit()
|
||||
{
|
||||
return (new RoleService)->edit();
|
||||
}
|
||||
|
||||
public function role_status()
|
||||
{
|
||||
return (new RoleService)->changeStatus();
|
||||
}
|
||||
|
||||
public function role_list()
|
||||
{
|
||||
return (new RoleService)->handle();
|
||||
}
|
||||
|
||||
#[RequestMapping(path: "role", methods: "GET")]
|
||||
#[Scene(scene: "role_info")]
|
||||
public function role(AuthRequest $request)
|
||||
{
|
||||
return (new RoleService)->details();
|
||||
}
|
||||
}
|
||||
45
app/Model/AdminMenu.php
Normal file
45
app/Model/AdminMenu.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
use Hyperf\DbConnection\Model\Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property int $parent_id
|
||||
* @property string $title
|
||||
* @property string $icon
|
||||
* @property int $type
|
||||
* @property string $url
|
||||
* @property string $value
|
||||
* @property int $status
|
||||
* @property int $sort
|
||||
* @property string $create_time
|
||||
* @property string $update_time
|
||||
*/
|
||||
class AdminMenu extends Model
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*/
|
||||
protected ?string $table = 'admin_menu';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected array $fillable = [];
|
||||
|
||||
protected array $guarded = [];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*/
|
||||
protected array $casts = ['id' => 'integer', 'parent_id' => 'integer', 'type' => 'integer', 'status' => 'integer', 'sort' => 'integer'];
|
||||
|
||||
const CREATED_AT = 'create_time';
|
||||
|
||||
const UPDATED_AT = 'update_time';
|
||||
|
||||
}
|
||||
58
app/Model/AdminRole.php
Normal file
58
app/Model/AdminRole.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
use Hyperf\Database\Model\Builder;
|
||||
use Hyperf\DbConnection\Model\Model;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $name
|
||||
* @property string $remark
|
||||
* @property int $status
|
||||
* @property string $create_time
|
||||
*/
|
||||
class AdminRole extends Model
|
||||
{
|
||||
/**
|
||||
* The table associated with the model.
|
||||
*/
|
||||
protected ?string $table = 'admin_role';
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected array $fillable = [];
|
||||
|
||||
protected array $guarded = [];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*/
|
||||
protected array $casts = ['id' => 'integer', 'status' => 'integer'];
|
||||
|
||||
const CREATED_AT = 'create_time';
|
||||
|
||||
const UPDATED_AT = 'update_time';
|
||||
|
||||
|
||||
/**
|
||||
* @param int $id
|
||||
* @return Builder|\Hyperf\Database\Model\Model|object|null
|
||||
*/
|
||||
public function getInfoById(int $id)
|
||||
{
|
||||
return $this->where('id', $id)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return Builder|\Hyperf\Database\Model\Model|object|null
|
||||
*/
|
||||
public function getInfoByName(string $name)
|
||||
{
|
||||
return $this->where('name', $name)->first();
|
||||
}
|
||||
}
|
||||
39
app/Request/Admin/AuthRequest.php
Normal file
39
app/Request/Admin/AuthRequest.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Request\Admin;
|
||||
|
||||
use Hyperf\Validation\Request\FormRequest;
|
||||
|
||||
class AuthRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'role_id' => 'required|integer',
|
||||
];
|
||||
}
|
||||
|
||||
public function messages(): array
|
||||
{
|
||||
return [
|
||||
''
|
||||
];
|
||||
}
|
||||
|
||||
protected array $scenes = [
|
||||
'role_info' => 'role_id',
|
||||
];
|
||||
}
|
||||
41
app/Service/Admin/User/RoleMenuService.php
Normal file
41
app/Service/Admin/User/RoleMenuService.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Admin\User;
|
||||
|
||||
use App\Service\Admin\BaseService;
|
||||
|
||||
class RoleMenuService extends BaseService
|
||||
{
|
||||
public function handle()
|
||||
{
|
||||
return $this->return->success();
|
||||
}
|
||||
|
||||
public function add()
|
||||
{
|
||||
return $this->return->success();
|
||||
}
|
||||
|
||||
public function edit()
|
||||
{
|
||||
return $this->return->success();
|
||||
}
|
||||
|
||||
public function del()
|
||||
{
|
||||
return $this->return->success();
|
||||
}
|
||||
|
||||
public function details()
|
||||
{
|
||||
return $this->return->success();
|
||||
}
|
||||
}
|
||||
135
app/Service/Admin/User/RoleService.php
Normal file
135
app/Service/Admin/User/RoleService.php
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Admin\User;
|
||||
|
||||
use App\Exception\AdminException;
|
||||
use App\Model\AdminRole;
|
||||
use App\Service\Admin\BaseService;
|
||||
use Exception;
|
||||
use Hyperf\DbConnection\Db;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class RoleService extends BaseService
|
||||
{
|
||||
/**
|
||||
* @var AdminRole $adminRoleModel
|
||||
*/
|
||||
#[Inject]
|
||||
protected AdminRole $adminRoleModel;
|
||||
|
||||
private string $field = 'id as role_id, name, remark, status, created_at, updated_at';
|
||||
|
||||
/**
|
||||
* 列表
|
||||
* @return array
|
||||
*/
|
||||
public function handle(): array
|
||||
{
|
||||
$limit = $this->request->input('limit', 10);
|
||||
|
||||
$list = $this->adminRoleModel->paginate($limit,$this->field)->toArray();
|
||||
|
||||
return $this->return->success('success',['list' => $list]);
|
||||
}
|
||||
|
||||
|
||||
public function add()
|
||||
{
|
||||
$name = $this->request->input('name');
|
||||
|
||||
if ($this->adminRoleModel->getInfoByName($name)) throw new AdminException('角色已存在');
|
||||
|
||||
Db::beginTransaction();
|
||||
try {
|
||||
$model = new AdminRole();
|
||||
|
||||
$model->name = $name;
|
||||
$model->status = $this->request->input('status', 1);
|
||||
$model->remark = $this->request->input('remark', '');
|
||||
|
||||
if (!$model->save()) throw new Exception('添加失败');
|
||||
|
||||
//todo 添加角色权限
|
||||
$menuIdArr = explode(',', $this->request->input('menu_ids'));
|
||||
|
||||
|
||||
|
||||
Db::commit();
|
||||
} catch (Exception $e) {
|
||||
Db::rollBack();
|
||||
throw new AdminException($e->getMessage());
|
||||
}
|
||||
|
||||
return $this->return->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改
|
||||
* @return array
|
||||
*/
|
||||
public function edit(): array
|
||||
{
|
||||
$id = $this->request->input('id');
|
||||
|
||||
if (!$info = $this->adminRoleModel->getInfoById($id)) throw new AdminException('角色不存在');
|
||||
|
||||
Db::beginTransaction();
|
||||
try {
|
||||
$info->name = $this->request->input('name');
|
||||
$info->status = $this->request->input('status', 1);
|
||||
$info->remark = $this->request->input('remark', '');
|
||||
|
||||
if (!$info->save()) throw new Exception('修改失败');
|
||||
|
||||
//todo 删除权限 添加角色权限
|
||||
|
||||
Db::commit();
|
||||
} catch (Exception $e) {
|
||||
Db::rollBack();
|
||||
throw new AdminException($e->getMessage());
|
||||
}
|
||||
|
||||
return $this->return->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改状态
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function changeStatus(): array
|
||||
{
|
||||
$id = $this->request->input('id');
|
||||
|
||||
if (!$info = $this->adminRoleModel->getInfoById($id)) throw new AdminException('角色不存在');
|
||||
|
||||
$info->status = $this->request->input('status', 0);
|
||||
|
||||
if (!$info->save()) throw new Exception('修改失败');
|
||||
|
||||
return $this->return->success();
|
||||
}
|
||||
|
||||
/**
|
||||
* 详情
|
||||
* @return array
|
||||
*/
|
||||
public function details(): array
|
||||
{
|
||||
$roleId = $this->request->input('role_id');
|
||||
$res = $this->adminRoleModel->where('id',$roleId)->first($this->field);
|
||||
if (!$res) throw new AdminException('角色不存在');
|
||||
|
||||
return $this->return->success('success',[
|
||||
'info' => $res->toArray()
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -22,4 +22,32 @@ CREATE TABLE `app_admin_user` (
|
||||
`role_id` tinyint(1) NOT NULL DEFAULT '0' COMMENT '角色',
|
||||
`create_time` datetime NOT NULL COMMENT '创建时间',
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC COMMENT='后台用户表';
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ROW_FORMAT=DYNAMIC COMMENT='后台用户表';
|
||||
|
||||
-- 后台用户角色表
|
||||
DROP TABLE IF EXISTS `app_admin_role`;
|
||||
CREATE TABLE `app_admin_role` (
|
||||
`id` int NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '角色名',
|
||||
`remark` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '备注',
|
||||
`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1=正常 2=禁用',
|
||||
`create_time` datetime DEFAULT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||
|
||||
-- 后台用户权限表
|
||||
DROP TABLE IF EXISTS `app_admin_menu`;
|
||||
CREATE TABLE `app_admin_menu` (
|
||||
`id` int NOT NULL AUTO_INCREMENT,
|
||||
`parent_id` int NOT NULL DEFAULT '0' COMMENT '父级权限ID',
|
||||
`title` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '权限名',
|
||||
`icon` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '图标',
|
||||
`type` tinyint(1) NOT NULL DEFAULT '1' COMMENT '权限类型 1=菜单 2=按钮',
|
||||
`url` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '权限URL',
|
||||
`value` varchar(10) COLLATE utf8mb4_unicode_ci DEFAULT '' COMMENT '权限方法',
|
||||
`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '权限状态 1=正常 2=禁用',
|
||||
`sort` int NOT NULL DEFAULT '0' COMMENT '排序',
|
||||
`create_time` datetime DEFAULT NULL,
|
||||
`update_time` datetime DEFAULT NULL,
|
||||
PRIMARY KEY (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||
|
||||
5
sync/http/admin/auth.http
Normal file
5
sync/http/admin/auth.http
Normal file
@@ -0,0 +1,5 @@
|
||||
### 角色详情
|
||||
GET {{host}}/admin/auth/role?role_id=1
|
||||
Content-Type: application/x-www-form-urlencoded
|
||||
|
||||
###
|
||||
5
sync/http/admin/http-client.env.json
Normal file
5
sync/http/admin/http-client.env.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"dev": {
|
||||
"host": "http://api.hhl.top"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user