最近重构 Ajax请求的代码,由于系统模块较多 且大部分结构一样 定义如下接口
public interface IAjax { public function init($data); 初始化 请求 //本地 ajax public function doAjax(); //远程调用 ajax 执行 public function doRpcAction(); }
矩阵模块 Ajax 处理
class MatrixAjax implements IAjax{ public function init($data) { // TODO: Implement init() method. } public function doAjax() { // TODO: Implement doAjax() method. } public function doRpcAction() { // TODO: Implement doRpcAction() method. } }
离线模块 请求ajax处理
class OfflineAjax implements IAjax{ public function init($data) { // TODO: Implement init() method. } public function doAjax() { // TODO: Implement doAjax() method. } public function doRpcAction() { // TODO: Implement doRpcAction() method. } }
本地 Api
class Api { private $api; public function __construct() { } /** * 发送 ajax 请求数据 * @param string $products 产品模块 系统| 离线| 矩阵| 编码器| 多画面| 字幕| 导播 * @param array $data 数据 * @param false $sync 是否异步发送信息 */ public function sendMessage(string $products ,array $data,bool $sync = false){ switch ($products) { case 'matrix': $this->api = new MatrixAjax($data); break; case 'offline': $this->api = new OfflineAjax($data); break; default: } if(isset($data['rpc_ip']) && $data['rpc_ip']){ $this->api?->doRpcAction(); }else{ $this->api?->doAjax(); } } } ?>
Api 工厂
<?php /** * @name clsFactoryApi * @author yanyl * @time 2022-07-14 * API工厂 本地 api Ajax 以及 rpc api */ class clsFactoryApi { /** * @var clsRpcApi|Api * */ private clsRpcApi|Api $requestApi; /** * * @param $requestType string 请求类型 api | rpc api */ public function __construct(private string $requestType){ $this->createApi(); } /** * @return void */ private function createApi(): void { $this->requestApi = $this->requestType == 'api' ? new Api() : new clsRpcApi(); } /*** * 请求处理 * @param string $products * @param array $data */ public function handle(string $products = '', array $data = []){ $this->requestApi?->sendMessage($products,$data); } }
远程 clsApiRpc
<?php /** * @name clsRpcApi * @author yanyl * @time 2022-07-14 * @description rpc 入口 */ class clsRpcApi implements ISend { private $rpcApi ; public function __construct() { } /** * @param string $products * @param array $data * @param bool $sync * @return bool|void */ public function sendMessage(string $products ,array $data,bool $sync = false){ if(!$sync){ $param = [ 'product'=>$products, 'data' =>$data ]; (new Receive($this,$param))->handle(); return true; } $this->createApi($products,$data); $this->rpcApi?->init(); } /** * @param $type * @param $data */ private function createApi($type,$data){ switch ($type){ case 'matrix': $this->rpcApi = new clsMatrixRpc($data); break; case 'encoder_input': $this->rpcApi = new clsIngestRpc($data); break; case 'system': $this->rpcApi = new clsSystemRpc($data); break; case 'transcode': $this->rpcApi = new clsTranscodeRpc($data); break; default: } } }
PHP 使用
$param = strtoupper($_SERVER['REQUEST_METHOD']) == 'GET' ? $_GET : $_POST; (new clsFactoryApi('api'))->handle('offline',$param);
(new clsFactoryApi('rpc_api'))->handle('offline',$param);