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.
103 lines
2.7 KiB
103 lines
2.7 KiB
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Services\AdminMenuService;
|
|
use App\Services\ApiResponseService;
|
|
use Exception;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Psr\SimpleCache\InvalidArgumentException;
|
|
|
|
class BaseController extends Controller
|
|
{
|
|
// 用户信息
|
|
public array $adminUser;
|
|
// 用户id
|
|
public int $adminUserId;
|
|
// 菜单uri
|
|
protected string $menuUri;
|
|
|
|
/**
|
|
* @var ApiResponseService
|
|
*/
|
|
protected ApiResponseService $responseService;
|
|
|
|
/**
|
|
* 构造函数
|
|
* @param ApiResponseService $responseService
|
|
*/
|
|
public function __construct(
|
|
ApiResponseService $responseService
|
|
) {
|
|
$this->responseService = $responseService;
|
|
}
|
|
|
|
/**
|
|
* 验证ID
|
|
* @param $id
|
|
* @param $model
|
|
* @return void
|
|
* @throws ValidationException|Exception
|
|
*/
|
|
protected function validateId($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'));
|
|
};
|
|
}
|
|
|
|
/**
|
|
* 功能显示权限
|
|
* @return int[]
|
|
* @throws InvalidArgumentException
|
|
*/
|
|
protected function methodShow(): array
|
|
{
|
|
$methodAuthArr = AdminMenuService::auth($this->adminUserId);
|
|
$roleMenuArr = AdminMenuService::getRoleMenuList($this->menuUri);
|
|
$authArr = [];
|
|
foreach ($roleMenuArr as $value) {
|
|
$authArr[$value] = 0;
|
|
}
|
|
$newArr = [];
|
|
foreach ($authArr as $authKey => $value) {
|
|
if (in_array($authKey, $methodAuthArr)) {
|
|
$authArr[$authKey] = 1;
|
|
}
|
|
$key = explode('.',$authKey);
|
|
$newArr[$key[1]] = $authArr[$authKey];
|
|
}
|
|
return $newArr;
|
|
}
|
|
|
|
/**
|
|
* @return JsonResponse
|
|
* @throws InvalidArgumentException
|
|
*/
|
|
public function rule(): JsonResponse
|
|
{
|
|
try {
|
|
if (!$this->menuUri) {
|
|
throw new Exception('');
|
|
}
|
|
return $this->responseService->success($this->methodShow());
|
|
} catch (Exception $e) {
|
|
return $this->responseService->systemError(
|
|
__('exception.get_data_failed') . ':' . $e->getMessage()
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|