feat : admin driver name

This commit is contained in:
2024-11-11 13:06:07 +08:00
parent 3aba37d426
commit b64d2872ac
10 changed files with 153 additions and 6 deletions

View File

@@ -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();

View File

@@ -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;
}

View File

@@ -0,0 +1,11 @@
<?php
namespace App\Constants\Common;
class RoleCode
{
const int SUPER_ADMIN = 1;
const int ADMIN = 2;
const int CHEF = 3;
const int DRIVER = 4;
}

View File

@@ -52,4 +52,16 @@ class SiteController
{
return (new SiteService)->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();
}
}

View File

@@ -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

View File

@@ -0,0 +1,43 @@
<?php
namespace App\Middleware;
use App\Lib\AdminReturn;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class CoreMiddleware extends \Hyperf\HttpServer\CoreMiddleware
{
/**
* @var AdminReturn
*/
#[Inject]
protected AdminReturn $return;
/**
* 404重写
* @param ServerRequestInterface $request
* @return ResponseInterface
*/
protected function handleNotFound(ServerRequestInterface $request): ResponseInterface
{
return $this->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)));
}
}

View File

@@ -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();
}
}

View File

@@ -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);
}
}

View File

@@ -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('角色不存在');