<?php
/**
* Created by PhpStorm.
* User: talen
* Date: 2018/12/22
* Time: 10:52
*/
namespace App\Controller;
use App\Common\SeasLog;
use ReflectionClass;
use Swoolefy\Core\Swfy;
class IndexController extends BaseController {
private $param_cfg;
private $func_name;
private $func_data;
private $func_model;
/**
* @return string
* @throws \ReflectionException
*/
public function index() {
$this->init();
$res = $this->getRequestParams();
foreach ($res as $key=>$value){
$res = $key;
}
$this->checkData($res);
$class = new ReflectionClass($this->func_model); // 建立 这个类的反射类
$instance = $class->newInstance($this->param_cfg, $this->func_data);
if (!$class->hasMethod($this->func_name)) {
$this->return_error(99998, "暂未提供");
}
$method = $class->getMethod($this->func_name);
if (!$method->isPublic()) {
$this->return_error(99998, "暂未提供");
}
$result = $method->invoke($instance, $this->func_data);
if ($result === false){
$result = ['code' => $class->getMethod('getErrorCode')->invoke($instance), 'msg' => $class->getMethod('getErrorMsg')->invoke($instance)];
$this->returnJson($result);
}
$this->returnJson($result);
}
public function init(){
$conf = Swfy::getConf();
$log_file = $conf['setting']['log_file'];
if (!empty($log_file)) {
\SeasLog::setBasePath(LOG_PATH);
}
// 加载公共文件
$commFile = __DIR__ . '/../Common/Common.php';
if (is_file($commFile)) {
include_once $commFile;
}
}
public function checkData($strBody){
$data = json_decode($strBody, true);
if (!is_array($data)) return false;
if (!array_key_exists(self::FUNCTION_NAME, $data) || !array_key_exists(self::FUNCTION_DATA, $data)) return false;
$this->func_name = $data[self::FUNCTION_NAME];
$this->func_data = $data[self::FUNCTION_DATA];
$this->param_cfg = $this->cfgDecode($data[self::PARAM_CFG], $this->func_name, $this->func_data);
$this->func_model = "App\Model\\".$data['func_model'];
SeasLog::info("http($this->func_name) => " . $this->func_name, ['func_data' => $this->func_data, 'param_cfg' => $this->param_cfg], $data['func_model']);
}
private function cfgDecode($strCfg, $func_name, $func_data)
{
if (is_array($func_data)) $strData = json_encode($func_data, JSON_UNESCAPED_UNICODE);
else $strData = $func_data;
$strKey = base64_encode(substr(md5($func_name . '-' . $strData), 8, 16));
$strJson = \decrypt($strCfg, $strKey);
return json_decode($strJson, true);
}
}
php 反射调用
最新推荐文章于 2023-03-02 10:17:30 发布