停车场管理系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

85 lines
2.3 KiB

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Services\AdminMenuService;
use App\Services\ApiResponseService;
use Exception;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
use Psr\SimpleCache\InvalidArgumentException;
class BaseController extends Controller
{
/**
* @var ApiResponseService
*/
protected ApiResponseService $responseService;
/**
* 构造函数
* @param ApiResponseService $responseService
*/
public function __construct(
ApiResponseService $responseService
) {
$this->responseService = $responseService;
}
/**
* 验证ID
* @param int $id
* @param $model
* @return void
* @throws ValidationException|Exception
*/
protected function validateId(int $id, $model): void
{
$data = ['id' => $id];
$validator = Validator::make($data, [
'id' => 'required|numeric'
], [
'id.required' => __('validation.id_empty'),
'id.numeric' => __('validation.id_numeric')
]);
if ($validator->fails()) {
throw new ValidationException($validator);
}
if (!$model::query()->where('id', $id)->exists()) {
throw new Exception(__('exception.exception_handler.resource'));
};
}
/**
* 功能显示权限
* @param string $className
* @param array $auth
* @return int[]
* @throws InvalidArgumentException
*/
protected function methodShow(string $className, array $auth = []): array
{
$user = Auth::guard('sanctum')->user();
$methodAuthArr = AdminMenuService::auth($user->id);
$authArr = [
$className . '/show' => 0,
$className . '/store' => 0,
$className . '/update' => 0,
$className . '/destroy' => 0,
];
$newArr = [];
foreach ($authArr as $authKey => $value) {
if (in_array($authKey, $methodAuthArr)) {
$authArr[$authKey] = 1;
}
$key = explode('/',$authKey);
$newArr[$key[1]] = $authArr[$authKey];
}
if ($auth) {
$newArr = array_merge($newArr, $auth);
}
return $newArr;
}
}