<?php
// application/middleware/ApiEncrypt.php

declare (strict_types=1);

namespace app\common\middleware;

class ApiEncrypt
{
    public function handle($request, \Closure $next)
    {
        // 解密逻辑
        $data = $this->decrypt($request->param());
        $request->replace($data);

        $response = $next($request);

        // 加密逻辑
        $response->content(json_encode($this->encrypt($response->getData())));

        return $response;
    }

    private function decrypt($data)
    {
        // 实现解密逻辑
        // ...
        return $data;
    }

    private function encrypt($data)
    {
        // 实现加密逻辑
        // ...
        return $data;
    }
}