mosen/mosen-api 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mosen/mosen-api

最新稳定版本:v1.4.5

Composer 安装命令:

composer require mosen/mosen-api

包简介

A comprehensive API plugin for ThinkPHP framework

README 文档

README

Mosen API 插件是一个功能丰富的 API 开发工具包,提供了多种常用的 API 开发功能。

功能特性

  • 统一的响应格式
  • 异常处理
  • 请求日志
  • 签名验证
  • 跨域支持
  • 队列任务
  • 文件处理
  • 文档生成

环境要求

  • PHP >= 8.0
  • ThinkPHP >= 8.0
  • Redis(用于队列功能)
  • Composer

安装

composer require mosen/api

配置

config/ms_plugin.php 中配置插件:

return [
    // 导出配置
    'export' => [
        'path' => runtime_path() . 'export/',
        'url' => '/export/',
    ],
    
    // 队列配置
    'queue' => [
        'default' => 'redis',
        'connections' => [
            'redis' => [
                'type' => 'redis',
                'host' => '127.0.0.1',
                'port' => 6379,
            ],
        ],
        'monitor' => [
            'enabled' => true,
            'timeout' => 3600,
        ],
    ],
    
    // 文件配置
    'file' => [
        'default' => 'local',
        'disks' => [
            'local' => [
                'type' => 'local',
                'root' => runtime_path() . 'storage',
            ],
        ],
    ],
    
    // 日志配置
    'log' => [
        'default' => 'file',
        'channels' => [
            'file' => [
                'type' => 'file',
                'path' => runtime_path() . 'log/',
            ],
        ],
    ],
    
    // 文档配置
    'doc' => [
        'path' => runtime_path() . 'doc/',
        'formats' => ['json', 'yaml'],
    ],
    
    // 签名配置
    'signature' => [
        'key' => 'your-signature-key',
        'expire' => 3600,
    ],
];

使用说明

响应服务

use mosen\api\Service\ResponseService;

class YourController
{
    protected ResponseService $responseService;

    public function __construct(ResponseService $responseService)
    {
        $this->responseService = $responseService;
    }

    public function index()
    {
        $data = ['name' => 'test'];
        return $this->responseService->json($data);
    }
}

异常处理

use mosen\api\Service\ErrorCodeService;

class YourController
{
    protected ErrorCodeService $errorCodeService;

    public function __construct(ErrorCodeService $errorCodeService)
    {
        $this->errorCodeService = $errorCodeService;
    }

    public function index()
    {
        try {
            // 你的代码
        } catch (\Exception $e) {
            $message = $this->errorCodeService->getMessage(500);
            return json(['code' => 500, 'msg' => $message]);
        }
    }
}

队列任务

use mosen\api\Service\QueueService;

class YourController
{
    protected QueueService $queueService;

    public function __construct(QueueService $queueService)
    {
        $this->queueService = $queueService;
    }

    public function index()
    {
        $job = new YourJob();
        $this->queueService->push($job);
    }
}

文件处理

use mosen\api\Service\FileService;

class YourController
{
    protected FileService $fileService;

    public function __construct(FileService $fileService)
    {
        $this->fileService = $fileService;
    }

    public function upload()
    {
        $file = request()->file('file');
        $result = $this->fileService->upload($file);
        return json($result);
    }
}

文档生成

use mosen\api\Service\DocService;

class YourController
{
    protected DocService $docService;

    public function __construct(DocService $docService)
    {
        $this->docService = $docService;
    }

    public function generate()
    {
        $result = $this->docService->generate();
        return json($result);
    }
}

中间件

异常处理中间件

// 在全局中间件中注册
'middleware' => [
    \mosen\api\middleware\ExceptionHandlerMiddleware::class,
],

请求日志中间件

// 在全局中间件中注册
'middleware' => [
    \mosen\api\middleware\RequestLogMiddleware::class,
],

签名验证中间件

// 在路由中间件中注册
Route::group('api', function () {
    Route::rule('test', 'test/index')->middleware(\mosen\api\middleware\SignatureMiddleware::class);
});

跨域中间件

// 在全局中间件中注册
'middleware' => [
    \mosen\api\middleware\CorsMiddleware::class,
],

命令行工具

队列监控

php think queue:monitor

注意事项

  1. 插件提供了核心功能,具体的业务逻辑需要自行实现
  2. 配置项可以根据项目需求进行调整
  3. 建议在生产环境中启用签名验证
  4. 文件上传建议使用云存储
  5. 队列任务建议使用 Redis 驱动
  6. 文档生成支持多种格式,建议使用 JSON 或 YAML

许可证

MIT

统计信息

  • 总下载量: 21
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-04-14

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固