feat: auth
This commit is contained in:
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()
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user