fixed
This commit is contained in:
103
front/application/api/logic/BaseLogic.php
Normal file
103
front/application/api/logic/BaseLogic.php
Normal file
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
/*
|
||||
* description:
|
||||
* author:wh
|
||||
* email:
|
||||
* createTime:{2024/5/23} {18:01}
|
||||
*/
|
||||
|
||||
namespace app\api\logic;
|
||||
|
||||
|
||||
use app\api\controller\BaseWssApi;
|
||||
use GatewayWorker\Lib\Gateway;
|
||||
use think\Db;
|
||||
use wanghua\general_utility_tools_php\tool\Tools;
|
||||
|
||||
class BaseLogic
|
||||
{
|
||||
function domsg($client_id, $data){
|
||||
try {
|
||||
$res = @json_decode($data, true);
|
||||
Tools::log_to_write_txt(['json_decode:', $res]);
|
||||
if (!$res) {
|
||||
$json = BaseWssApi::json_wss('error', '消息格式错误');
|
||||
Gateway::sendToClient($client_id, json_encode($json, JSON_UNESCAPED_UNICODE));
|
||||
return;
|
||||
}
|
||||
if (empty($res['action'])) {
|
||||
$json = BaseWssApi::json_wss('error', '消息格式错误,action必须');
|
||||
Gateway::sendToClient($client_id, json_encode($json, JSON_UNESCAPED_UNICODE));
|
||||
return;
|
||||
}
|
||||
$act_arr = explode('/', $res['action']);
|
||||
if (empty($act_arr[0])) {
|
||||
$json = BaseWssApi::json_wss('error', '错误的action格式');
|
||||
Gateway::sendToClient($client_id, json_encode($json, JSON_UNESCAPED_UNICODE));
|
||||
return;
|
||||
}
|
||||
if (empty($act_arr[1])) {
|
||||
$json = BaseWssApi::json_wss('error', '错误的action格式!');
|
||||
Gateway::sendToClient($client_id, json_encode($json, JSON_UNESCAPED_UNICODE));
|
||||
return;
|
||||
}
|
||||
//根据action执行业务逻辑
|
||||
$controller = ucfirst($act_arr[0]);
|
||||
$function = $act_arr[1];
|
||||
$object = '\\app\\api\\logic\\' . $controller . 'Logic';
|
||||
$obj = $this->getinstance($object);
|
||||
$obj->$function($client_id, $res);
|
||||
} catch (\Exception $e) {
|
||||
Tools::error_txt_log($e);
|
||||
$json = BaseWssApi::json_wss('error', '服务繁忙');
|
||||
Gateway::sendToClient($client_id, json_encode($json, JSON_UNESCAPED_UNICODE));
|
||||
}
|
||||
}
|
||||
|
||||
function getinstance($className)
|
||||
{
|
||||
// 类名字符串
|
||||
// 参数数组
|
||||
|
||||
// 确保类存在
|
||||
if (!class_exists($className)) {
|
||||
throw new \InvalidArgumentException("Class {$className} does not exist.");
|
||||
}
|
||||
// 创建反射类实例
|
||||
$reflection = new \ReflectionClass($className);
|
||||
|
||||
// 检查构造函数是否存在
|
||||
//if (!$reflection->hasMethod('__construct')) {
|
||||
// throw new \LogicException("Class {$className} has no constructor.");
|
||||
//}
|
||||
//$constructor = $reflection->getConstructor();
|
||||
|
||||
// 如果构造函数有参数,我们需要匹配参数
|
||||
//if ($constructor !== null) {
|
||||
// $constructorParams = $constructor->getParameters();
|
||||
//
|
||||
// // 确保参数数量匹配
|
||||
// if (count($constructorParams) !== count($params)) {
|
||||
// throw new \InvalidArgumentException("Number of constructor parameters does not match provided arguments.");
|
||||
//
|
||||
// }
|
||||
// // 创建参数数组,将参数类型与值匹配
|
||||
// $matchedParams = [];
|
||||
// foreach ($constructorParams as $index => $param) {
|
||||
// // 如果参数允许null,或者参数类型与传递的值兼容,添加到匹配参数数组
|
||||
// if ($param->allowsNull() || $param->getClass() === null || $param->getClass()->isInstance($params[$index])) {
|
||||
// $matchedParams[] = $params[$index];
|
||||
// } else {
|
||||
// throw new \InvalidArgumentException("Provided argument does not match constructor parameter type at position {$index}.");
|
||||
// }
|
||||
// }
|
||||
// // 使用反射类创建并初始化类实例
|
||||
// $instance = $reflection->newInstanceArgs($matchedParams);
|
||||
//} else {
|
||||
//
|
||||
//}
|
||||
// 构造函数无参数,直接创建实例
|
||||
$instance = $reflection->newInstanceWithoutConstructor();
|
||||
return $instance;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user