lumen7.x 使用笔记(七)Api返回处理中间件

作者: 太阳上的雨天 分类: Lumen7.x使用教程,PHP 发布时间: 2020-10-01 15:45

lumen7.x 使用笔记(七)Api返回处理中间件

<?php
namespace App\Http\Middleware;

use Closure;
use App\Enums\WopCode;

/**
* API返回封装中间件
*/
class CustomFormat
{
    /**
     * Notes: API返回封装
     * User: clj
     * Date: 2020/9/17
     * Time: 9:02 下午
     * @param $request
     * @param Closure $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $response = $next($request);
        $original = $response->getOriginalContent();
        switch (gettype($original)) {
            case 'object':
                $original = $original->toArray();
                $response->setContent([
                    'code' => WopCode::SUCCESS,
                    'msg'  => 'success',
                    'nowTime' => time(),
                    'data' => $original,
                ]);
                break;
            case 'boolean':
                if ($original == false) {
                    $response->setContent([
                        'code' => WopCode::FIELD,
                        'msg'  => 'failed',
                        'nowTime' => time(),
                        'data' => '失败',
                    ]);
                    break;
                } else {
                    $response->setContent([
                        'code' => WopCode::SUCCESS,
                        'msg'  => 'success',
                        'nowTime' => time(),
                        'data' => '成功',
                    ]);
                    break;
                }
            case 'array':
                if (array_key_exists('code', $original)) {
                    switch ($original['code']) {
                        case WopCode::VERIFY_FIELD:
                            $response->setContent([
                                'code' => WopCode::VERIFY_FIELD,
                                'msg'  => 'failed',
                                'nowTime' => time(),
                                'data' => $original['data'],
                            ]);
                            break;
                        case WopCode::MISS_REQUIRED_PARAMS:
                            $response->setContent([
                                'code' => WopCode::MISS_REQUIRED_PARAMS,
                                'msg'  => 'failed',
                                'nowTime' => time(),
                                'data' => $original['data'],
                            ]);
                            break;
                        default:
                            $response->setContent([
                                'code' => 0,
                                'msg'  => 'failed',
                                'nowTime' => time(),
                                'data' => $original['data'],
                            ]);
                            break;
                    }
                } else {
                    $response->setContent([
                        'code' => WopCode::SUCCESS,
                        'msg' => 'success',
                        'nowTime' => time(),
                        'data' => $original,
                    ]);
                }
            case 'NULL':
                $response->setContent([
                    'code' => WopCode::SUCCESS,
                    'msg' => 'success',
                    'nowTime' => time(),
                    'data' => [],
                ]);
            default:
                break;
        }
        return $response;
    }
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注