class DemoAPIController extends Controller { /** * 接口配置参数 * * @var array */ protected $anoleConfig; /** * 必传参数列表 * * @var array */ protected $requireParam = array( 'cuid'=>'', 'from'=>'', 'device'=>'', 'timestamp'=>'', 'client_version'=>'', 'api_sign'=>'', ); /** * 初始化应用 */ public function __construct() { parent::__construct(); $this->anoleConfig = $GLOBALS['anole_cofnig']; } /** * 运行核心逻辑,并进行前期的参数判断,后期的结果输出和异常处理 * * @param $method_name 主要执行的业务逻辑方法 * */ final public function run($method_name) { try { //检查方法是否存在 if (!method_exists($this,$method_name)) { $this->_response(StatusCode::METHOD_NOT_EXISTS,'请求的方法不存在'); } //检查参数是否合法,并获取最终请求数据 $request_data = $this->_check($method_name); //获取方法的参数列表和顺序 $method_obj = new ReflectionMethod($this,$method_name); $method_params_list = $method_obj->getParameters(); //对请求数据进行排序 $invoke_data = array(); foreach($method_params_list as $node){ $param_key = strval($node->name); $invoke_data[] = $request_data[$param_key]; } //执行方法 $method_obj->invokeArgs($this,$invoke_data); exit; } catch (ReflectionException $e) { $this->_response(StatusCode::FAIL,$e->getCode().':'.$e->getMessage()); } catch (Exception $e) { $this->_response($e->getCode(),$e->getMessage()); } } /** * 检查请求访问方式,参数,校验签名合法性 * @param $method_name 将要执行的方法名称 */ private function _check($method_name) { //判断访问方式是否正确,并舍去系统的mod,act,do参数 $request_data = $this->_checkMethod(); //判断剩余参数是否匹配 $this->_checkParams($request_data,$method_name); //判断签名是否正确 $this->_checkSignData($request_data); return $request_data; } /** * 获取参数 * * 新版本客户端接口获取参数 */ private function _checkMethod() { /*if ('POST' !== $_SERVER['REQUEST_METHOD']) { $this->_response(StatusCode::HTTP_REQUEST_METHOD_ERROR,'接口请求方式错误'); }*/ $params = $this->input->request(); return $params; } /** * 参数检验 * * 校验请求是否传递必传参数且不为空 * @param array $data 待校验数据 * @param $method_name 待校验方法名 * @return void */ private function _checkParams(array $request_data,$method_name) { //获取方法所需参数 $method_obj = new ReflectionMethod($this,$method_name); $method_params_list = $method_obj->getParameters(); $method_params_array = array(); foreach ($method_params_list as $node) { $method_params_array[] = strval($node->name); } $total_params_array = array_merge(array_keys($this->requireParam),$method_params_array); //逐个校验参数是否存在,包括必选参数和方法参数 foreach ($total_params_array as $v) { if (!isset($request_data[$v]) || isRealEmpty($request_data[$v])) { $this->_response(StatusCode::PARAM_ERROR,$v.'为空或者未传递'); } } } /** * 验签响应 * * @param $real_params 真实请求参数 */ private function _checkSignData(&$request_data) { //删除真实参数当中的必选参数,并赋予给requireParam留备子类使用,真实参数只保留方法参数 foreach ($this->requireParam as $key=>$node) { if (array_key_exists($key,$request_data)) { $this->requireParam[$key] = $request_data[$key]; unset($request_data[$key]); } } /* if (!checkSign ($request_data,$this->requireParam['api_sign'], $this->anoleConfig['private_key'][$this->requireParam['from']])) { $this->_response(StatusCode::SIGN_ERROR,'签名失败'); }*/ } /** * 结果输出 * @param $code 状态码 * @param $message 消息内容 * @param $data 数据内容 */ protected function _response($code,$message,$data = null) { $data = (array)$data; if ($code == StatusCode::SUCCESS && !empty($data)) { $data = api_tools::camelArray2LowerArray($data); } jsonResponse($code, $message,$data,false); exit; } }
APIController原型
最新推荐文章于 2024-05-13 14:23:51 发布