PHP code example of david-wang / apinote

1. Go to this page and download the library: Download david-wang/apinote library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

david-wang / apinote example snippets


// config/autoload/middlewares.php

DavidWang\ApiNote\Middleware\ApiValidationMiddleware::class;

/**
 * @ApiVersion(version="v1")
 * @ApiServer(name="http")
 */
class UserController {} 

@ApiResponse(code="0", description="删除成功", schema={"id|这里是ID":1})
@ApiResponse(code="0", description="删除成功", schema={"$ref": "ExampleResponse"})


declare(strict_types=1);
namespace App\Controller;

use DavidWang\ApiNote\Annotation\ApiController;
use DavidWang\ApiNote\Annotation\ApiResponse;
use DavidWang\ApiNote\Annotation\ApiVersion;
use DavidWang\ApiNote\Annotation\Body;
use DavidWang\ApiNote\Annotation\DeleteApi;
use DavidWang\ApiNote\Annotation\FormData;
use DavidWang\ApiNote\Annotation\GetApi;
use DavidWang\ApiNote\Annotation\Header;
use DavidWang\ApiNote\Annotation\PostApi;
use DavidWang\ApiNote\Annotation\Query;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Utils\ApplicationContext;

/**
 * @ApiVersion(version="v1")
 * @ApiController(tag="demo管理", description="demo的新增/修改/删除接口")
 * @ApiDefinitions({
 *  @ApiDefinition(name="DemoOkResponse", properties={
 *     "code|响应码": 200,
 *     "msg|响应信息": "ok",
 *     "data|响应数据": {"$ref": "DemoInfoData"}
 *  }),
 *  @ApiDefinition(name="DemoInfoData", properties={
 *     "userInfo|用户数据": {"$ref": "DemoInfoDetail"}
 *  }),
 *  @ApiDefinition(name="DemoInfoDetail", properties={
 *     "id|用户ID": 1,
 *     "mobile|用户手机号": { "default": "13545321231", "type": "string" },
 *     "nickname|用户昵称": "nickname",
 *     "avatar": { "default": "avatar", "type": "string", "description": "用户头像" },
 *  })
 * })
 */
class DemoController extends AuthController
{

    /**
     * @PostApi(path="/demo", description="添加一个用户")
     * @Header(key="token|接口访问凭证", rule="dy()->getContents();
        return [
            'code'  => 0,
            'query' => $this->request->getQueryParams(),
            'body'  => json_decode($body, true),
        ];
    }

    /**
     * @GetApi(path="/demo", description="获取用户详情")
     * @Query(key="id", rule="
bash
php bin/hyperf.php vendor:publish DavidWang/apinote

# hyperf/validation 的依赖发布

php bin/hyperf.php vendor:publish hyperf/translation

php bin/hyperf.php vendor:publish hyperf/validation

# 视图发布
php bin/hyperf.php vendor:publish hyperf/view-engine



/**
 * @Aspect
 * Class AuthAspect
 */
class AuthAspect extends AbstractAspect
{
    // 要切入的注解,具体切入的还是使用了这些注解的类,仅可切入类注解和类方法注解
    public $annotations = [
        ApiController::class,
    ];

    public function process(ProceedingJoinPoint $proceedingJoinPoint)
    {
        if (config('apinote.auth.enable')) {
            /** @var ApiController $class */
            $class = $proceedingJoinPoint->getAnnotationMetadata()->class[ApiController::class];
            $auth = $class->auth;
            $methods = $proceedingJoinPoint->getAnnotationMetadata()->method;
            foreach ($methods as $method) {
                if ($method instanceof Mapping) {
                    $auth = is_bool($method->auth) ? $method->auth : $auth;
                }
            }
            if ($auth) {
                #TODO 登录判断
                echo '需要登录';
            } else {
                echo '无需登录';
            }
        }
        return $proceedingJoinPoint->process();
        // 在调用后进行某些处理
        // TODO: Implement process() method.
    }
}