代码拉取完成,页面将自动刷新
同步操作将从 MineAdmin/MineAdmin 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
<?php
/**
* MineAdmin is committed to providing solutions for quickly building web applications
* Please view the LICENSE file that was distributed with this source code,
* For the full copyright and license information.
* Thank you very much for using MineAdmin.
*
* @Author X.Mo<root@imoi.cn>
* @Link https://gitee.com/xmo/MineAdmin
*/
declare(strict_types=1);
namespace Api;
use App\System\Service\SystemAppService;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\PostMapping;
use Mine\Exception\NoPermissionException;
use Mine\Exception\NormalStatusException;
use Mine\Exception\TokenException;
use Mine\Helper\MineCode;
use Mine\MineApi;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Di\Annotation\AnnotationCollector;
use Hyperf\Di\Annotation\MultipleAnnotation;
use Hyperf\Di\ReflectionManager;
use Hyperf\Validation\Request\FormRequest;
use Hyperf\Validation\ValidationException;
use Psr\Http\Message\ResponseInterface;
use Api\Middleware\VerifyInterfaceMiddleware;
/**
* Class ApiController
* @package Api
*/
#[Controller(prefix: "api")]
class ApiController extends MineApi
{
public const SIGN_VERSION = '1.0';
/**
* 获取accessToken
* @return ResponseInterface
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
#[PostMapping("v1/getAccessToken")]
public function getAccessToken(): ResponseInterface
{
$service = container()->get(SystemAppService::class);
return $this->success($service->getAccessToken($this->request->all()));
}
/**
* v1 版本
* @return ResponseInterface
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
#[RequestMapping("v1/{method}")]
#[Middlewares([ VerifyInterfaceMiddleware::class ])]
public function v1(): ResponseInterface
{
$apiData = $this->__init();
try {
$class = make($apiData['class_name']);
// 反射拿参数
$reflectionMethod = ReflectionManager::reflectMethod($apiData['class_name'], $apiData['method_name']);
$parameters = $reflectionMethod->getParameters();
$args = [];
foreach ($parameters as $parameter) {
if ($parameter->getType() === null) {
continue;
}
$className = $parameter->getType()->getName();
$formRequest = container()->get($className);
$args[] = $formRequest;
if ($formRequest instanceof FormRequest) {
$this->handleSceneAnnotation($formRequest, $apiData['class_name'], $apiData['method_name'], $parameter->getName());
// 验证, 这里逻辑和 验证中间件一样 直接抛异常
$formRequest->validateResolved();
}
}
// 反射调用
return $reflectionMethod->invokeArgs($class, $args);
} catch (\Throwable $e) {
if ($e instanceof ValidationException) {
// 抛出的是验证异常 取一条错误信息返回
$errors = $e->errors();
$error = array_shift($errors);
if (is_array($error)) {
$error = array_shift($error);
}
throw new NormalStatusException(t('mineadmin.interface_exception') . $error, MineCode::INTERFACE_EXCEPTION);
}
if ($e instanceof NoPermissionException) {
throw new NormalstatusException( t( key: 'mineadmin.api_auth_fail') . $e->getMessage(), code: MineCode::NO_PERMISSION);
}
if ($e instanceof TokenException) {
throw new NormalstatusException( t( key: 'mineadmin.api_auth_exception') . $e->getMessage(), code: MineCode::TOKEN_EXPIRED);
}
throw new NormalstatusException( t( key: 'mineadmin.interface_exception') . $e->getMessage(), code: MineCode::INTERFACE_EXCEPTION);
}
}
/**
* 初始化
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
protected function __init()
{
if (empty($this->request->input('apiData'))) {
throw new NormalStatusException(t('mineadmin.access_denied'), MineCode::NORMAL_STATUS);
}
return $this->request->input('apiData');
}
protected function handleSceneAnnotation(FormRequest $request, string $class, string $method, string $argument): void
{
/** @var null|MultipleAnnotation $scene */
$scene = AnnotationCollector::getClassMethodAnnotation($class, $method)[Scene::class] ?? null;
if (! $scene) {
return;
}
$annotations = $scene->toAnnotations();
if (empty($annotations)) {
return;
}
/** @var Scene $annotation */
foreach ($annotations as $annotation) {
if ($annotation->argument === null || $annotation->argument === $argument) {
$request->scene($annotation->scene ?? $method);
return;
}
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。