From b64d2872ac635d4e8527002c9c1421148bf852dc Mon Sep 17 00:00:00 2001 From: ctexthuang Date: Mon, 11 Nov 2024 13:06:07 +0800 Subject: [PATCH] feat : admin driver name --- app/Cache/Redis/Admin/RoleCache.php | 3 +- app/Constants/Admin/UserCode.php | 14 +++++- app/Constants/Common/RoleCode.php | 11 ++++ app/Controller/Admin/SiteController.php | 12 +++++ app/Exception/Handler/AppExceptionHandler.php | 9 +++- app/Middleware/CoreMiddleware.php | 43 ++++++++++++++++ app/Model/AdminSection.php | 13 +++++ app/Service/Admin/System/SiteService.php | 50 +++++++++++++++++++ app/Service/Admin/User/RoleService.php | 3 +- config/autoload/dependencies.php | 1 + 10 files changed, 153 insertions(+), 6 deletions(-) create mode 100644 app/Constants/Common/RoleCode.php create mode 100644 app/Middleware/CoreMiddleware.php diff --git a/app/Cache/Redis/Admin/RoleCache.php b/app/Cache/Redis/Admin/RoleCache.php index 42a67db..f5ad6d9 100644 --- a/app/Cache/Redis/Admin/RoleCache.php +++ b/app/Cache/Redis/Admin/RoleCache.php @@ -4,6 +4,7 @@ namespace App\Cache\Redis\Admin; use App\Cache\Redis\RedisCache; use App\Constants\Admin\AuthCode; +use App\Constants\Common\RoleCode; use App\Model\AdminMenu; use App\Model\AdminRoleMenu; use App\Service\ServiceTrait\Admin\AdminRoleMenuTrait; @@ -81,7 +82,7 @@ class RoleCache ]; } - if ($this->roleId == AuthCode::SUPERADMIN) { + if ($this->roleId == RoleCode::SUPER_ADMIN) { $menuIds = $this->adminMenuModel->getAllIds(); $data = (new MenuCache)->getMenu(); // $menuList = $this->adminMenuModel->getAllMenu(); diff --git a/app/Constants/Admin/UserCode.php b/app/Constants/Admin/UserCode.php index 88d70aa..769875f 100644 --- a/app/Constants/Admin/UserCode.php +++ b/app/Constants/Admin/UserCode.php @@ -12,10 +12,20 @@ class UserCode extends AbstractConstants * 禁用 * @Message("该用户已被禁用") */ - const DISABLE = 2; + const int DISABLE = 2; /** * 启用 */ - const ENABLE = 1; + const int ENABLE = 1; + + /** + * 未删除 + */ + const int IS_NO_DEL = 1; + + /** + * @Message("该用户已被删除") + */ + const int IS_DEL = 2; } \ No newline at end of file diff --git a/app/Constants/Common/RoleCode.php b/app/Constants/Common/RoleCode.php new file mode 100644 index 0000000..7c17363 --- /dev/null +++ b/app/Constants/Common/RoleCode.php @@ -0,0 +1,11 @@ +info(); } + + /** + * 司机列表 + * @param SiteRequest $request + * @return array + */ + #[RequestMapping(path: "driver_list", methods: "GET")] + #[Scene(scene: "driver_list")] + public function driverList(SiteRequest $request) + { + return (new SiteService)->driverList(); + } } diff --git a/app/Exception/Handler/AppExceptionHandler.php b/app/Exception/Handler/AppExceptionHandler.php index 1138f75..df5faca 100644 --- a/app/Exception/Handler/AppExceptionHandler.php +++ b/app/Exception/Handler/AppExceptionHandler.php @@ -12,6 +12,7 @@ declare(strict_types=1); namespace App\Exception\Handler; +use App\Lib\AdminReturn; use App\Lib\Log; use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\ExceptionHandler\ExceptionHandler; @@ -21,7 +22,7 @@ use Throwable; class AppExceptionHandler extends ExceptionHandler { - public function __construct(protected StdoutLoggerInterface $logger,protected Log $log) + public function __construct(protected StdoutLoggerInterface $logger,protected Log $log,protected AdminReturn $return) { } @@ -33,7 +34,11 @@ class AppExceptionHandler extends ExceptionHandler $this->log->error(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile())); $this->log->error($throwable->getTraceAsString()); - return $response->withHeader('Server', 'Hyperf')->withStatus(500)->withBody(new SwooleStream('Internal Server Error.')); + $result = $this->return->error('Internal Server Error.'); + return $response->withHeader("Content-Type", "application/json") + ->withStatus(200) + ->withBody(new SwooleStream(json_encode($result, JSON_UNESCAPED_UNICODE))); +// return $response->withHeader('Server', 'Hyperf')->withStatus(500)->withBody(new SwooleStream('Internal Server Error.')); } public function isValid(Throwable $throwable): bool diff --git a/app/Middleware/CoreMiddleware.php b/app/Middleware/CoreMiddleware.php new file mode 100644 index 0000000..2d1c097 --- /dev/null +++ b/app/Middleware/CoreMiddleware.php @@ -0,0 +1,43 @@ +response()->withHeader("Content-Type", "application/json") + ->withStatus(200) + ->withBody(new SwooleStream(json_encode($this->return->error('路由不存在'), JSON_UNESCAPED_UNICODE))); + } + + /** + * 405重写 + * @param array $methods + * @param ServerRequestInterface $request + * @return ResponseInterface + */ + protected function handleMethodNotAllowed(array $methods, ServerRequestInterface $request): ResponseInterface + { + return $this->response()->withHeader("Content-Type", "application/json") + ->withStatus(200) + ->withBody(new SwooleStream(json_encode($this->return->error('路由请求方法不正确'), JSON_UNESCAPED_UNICODE))); + } +} \ No newline at end of file diff --git a/app/Model/AdminSection.php b/app/Model/AdminSection.php index 54c8b8e..0e97162 100644 --- a/app/Model/AdminSection.php +++ b/app/Model/AdminSection.php @@ -81,4 +81,17 @@ class AdminSection extends Model { return $this->where('id',$id)->value('city_id') ?? 0; } + + /** + * @param int $cityId + * @return array + */ + public function getIdsByCityId(int $cityId): array + { + return $this + ->where('city_id',$cityId) + ->where('status',AuthCode::SECTION_STATUS_ENABLE) + ->pluck('id') + ->toArray(); + } } diff --git a/app/Service/Admin/System/SiteService.php b/app/Service/Admin/System/SiteService.php index 641e7a2..f6241a1 100644 --- a/app/Service/Admin/System/SiteService.php +++ b/app/Service/Admin/System/SiteService.php @@ -10,7 +10,12 @@ declare(strict_types=1); namespace App\Service\Admin\System; +use App\Constants\Admin\UserCode; +use App\Constants\Common\RoleCode; +use App\Model\AdminSection; +use App\Model\AdminUser; use App\Service\Admin\BaseService; +use Hyperf\Di\Annotation\Inject; class SiteService extends BaseService { @@ -36,6 +41,51 @@ class SiteService extends BaseService public function info() { + return $this->return->success(); + } + /** + * 注入用户类 + * @var AdminUser + */ + #[Inject] + protected AdminUser $adminUserModel; + + /** + * 注入部门类 + * @var AdminSection + */ + #[Inject] + protected AdminSection $adminSectionModel; + + /** + * @return array + */ + public function driverList(): array + { + $limit = $this->request->input('limit', 10); + $cityId = (int)$this->request->input('city_id',0); + $name = $this->request->input('name'); + +// $where[] = [ +// ['is_del', '=', UserCode::IS_NO_DEL], +// ['status','=',UserCode::ENABLE], +// ['role_id','=',RoleCode::DRIVER] +// ]; + + $list = $this + ->adminUserModel + ->where('is_del',UserCode::IS_NO_DEL) + ->where('status',UserCode::ENABLE) + ->where('role_id',RoleCode::DRIVER) + ->when($name, function ($query) use ($name) { + $query->where('name', 'like', "$name%"); + }) + ->when($cityId > 0, function ($query) use ($cityId) { + $query->whereIn('section_id', $this->adminSectionModel->getIdsByCityId($cityId)); + }) + ->paginate($limit,['chinese_name','id','mobile','status'])->toArray(); + + return $this->return->success('success',$list); } } \ No newline at end of file diff --git a/app/Service/Admin/User/RoleService.php b/app/Service/Admin/User/RoleService.php index 8112559..d201a3e 100644 --- a/app/Service/Admin/User/RoleService.php +++ b/app/Service/Admin/User/RoleService.php @@ -13,6 +13,7 @@ namespace App\Service\Admin\User; use App\Cache\Redis\Admin\MenuCache; use App\Cache\Redis\Admin\RoleCache; use App\Constants\Admin\AuthCode; +use App\Constants\Common\RoleCode; use App\Exception\AdminException; use App\Model\AdminRole; use App\Model\AdminRoleMenu; @@ -178,7 +179,7 @@ class RoleService extends BaseService public function changeStatus(): array { $id = (int)$this->request->input('role_id'); - if ($id == AuthCode::SUPERADMIN) throw new AdminException('超级管理员不可关闭'); + if ($id == RoleCode::SUPER_ADMIN) throw new AdminException('超级管理员不可关闭'); if (!$info = $this->adminRoleModel->getInfoById($id)) throw new AdminException('角色不存在'); diff --git a/config/autoload/dependencies.php b/config/autoload/dependencies.php index f46bd96..434781b 100644 --- a/config/autoload/dependencies.php +++ b/config/autoload/dependencies.php @@ -10,4 +10,5 @@ declare(strict_types=1); * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ return [ + Hyperf\HttpServer\CoreMiddleware::class => App\Middleware\CoreMiddleware::class, ];