fixed
This commit is contained in:
72
front/application/api/controller/BaseCommonController.php
Normal file
72
front/application/api/controller/BaseCommonController.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
/*
|
||||
* description:
|
||||
* author:wh
|
||||
* email:
|
||||
* createTime:{2023/1/21} {16:22}
|
||||
*/
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
|
||||
use app\common\model\TabConf;
|
||||
use think\Controller;
|
||||
use think\Db;
|
||||
use think\Request;
|
||||
use wanghua\general_utility_tools_php\framework\BaseController;
|
||||
use wanghua\general_utility_tools_php\tool\Tools;
|
||||
|
||||
class BaseCommonController extends BaseController
|
||||
{
|
||||
function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* desc:检查路径维护状态
|
||||
*
|
||||
* “===”完全匹配,不能使用like
|
||||
*
|
||||
* author:wh
|
||||
*/
|
||||
protected function checkMaintain(){
|
||||
|
||||
$configs = Db::table(TabConf::$fa_sys_maintain_config)
|
||||
->where('status','1')
|
||||
->cache()
|
||||
->select();
|
||||
|
||||
//模块
|
||||
$strmodule = request()->module();
|
||||
foreach ($configs as $config){
|
||||
if($strmodule == $config['url']){
|
||||
//模块维护中
|
||||
return ['is_maintain'=>true,'msg'=>$config['msg'],'openid'=>$config['openid']];
|
||||
}
|
||||
}
|
||||
|
||||
//模块/控制器
|
||||
$strcontroller = strtolower(request()->module().'/'.request()->controller());
|
||||
foreach ($configs as $config){
|
||||
if($strcontroller == $config['url']){
|
||||
//模块维护中
|
||||
return ['is_maintain'=>true,'msg'=>$config['msg'],'openid'=>$config['openid']];
|
||||
}
|
||||
}
|
||||
|
||||
//模块/控制器/方法
|
||||
$straction = strtolower(request()->module().'/'.request()->controller().'/'.request()->action());
|
||||
foreach ($configs as $config){
|
||||
if($straction == $config['url']){
|
||||
//模块维护中
|
||||
return ['is_maintain'=>true,'msg'=>$config['msg'],'openid'=>$config['openid']];
|
||||
}
|
||||
}
|
||||
|
||||
//未维护
|
||||
return ['is_maintain'=>false,'msg'=>'服务运行中'];
|
||||
}
|
||||
}
|
||||
34
front/application/api/controller/BaseHttpApi.php
Normal file
34
front/application/api/controller/BaseHttpApi.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
/*
|
||||
* description:
|
||||
* author:wh
|
||||
* email:
|
||||
* createTime:{2024/4/22} {11:52}
|
||||
*/
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
|
||||
use app\index\controller\BaseCommonController;
|
||||
use think\App;
|
||||
use think\Controller;
|
||||
use think\Exception;
|
||||
use wanghua\general_utility_tools_php\tool\Ip;
|
||||
|
||||
class BaseHttpApi extends BaseCommonController
|
||||
{
|
||||
/**
|
||||
http请求专用
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
|
||||
//ip校验
|
||||
//$ip = request()->ip();
|
||||
//$res = Ip::ip_is_china($ip,false);
|
||||
//if(!$res){
|
||||
// throw new Exception('ip不合法');
|
||||
//}
|
||||
}
|
||||
}
|
||||
34
front/application/api/controller/BaseWssApi.php
Normal file
34
front/application/api/controller/BaseWssApi.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
/*
|
||||
* description:
|
||||
* author:wh
|
||||
* email:
|
||||
* createTime:{2024/4/5} {19:55}
|
||||
*/
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
|
||||
class BaseWssApi extends BaseCommonController
|
||||
{
|
||||
|
||||
/**
|
||||
* desc:socket专用json格式
|
||||
*
|
||||
* author:wh
|
||||
* @param $action
|
||||
* @param string $msg
|
||||
* @param array $items
|
||||
* @param string $method
|
||||
* @return false|string
|
||||
*/
|
||||
static function json_wss($action,$msg='', $items=[], $method='response'){
|
||||
$json = [
|
||||
'action'=>$action,
|
||||
'method'=>$method,
|
||||
'msg'=>$msg,
|
||||
'items'=>$items
|
||||
];
|
||||
return json_encode($json, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
}
|
||||
49
front/application/api/controller/Common.php
Normal file
49
front/application/api/controller/Common.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
/*
|
||||
* description:
|
||||
* author:wh
|
||||
* email:
|
||||
* createTime:{2024/8/13} {17:27}
|
||||
*/
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
|
||||
use wanghua\general_utility_tools_php\Mmodel;
|
||||
use wanghua\general_utility_tools_php\tool\Tools;
|
||||
|
||||
class Common extends BaseHttpApi
|
||||
{
|
||||
|
||||
/**
|
||||
* desc:唯一id
|
||||
* 参数:无
|
||||
*
|
||||
* api/Common/getUniqueId
|
||||
*
|
||||
* author:wh
|
||||
*/
|
||||
function getUniqueId(){
|
||||
return Mmodel::catchJson(function (){
|
||||
$uniqueid = md5(Tools::getMillisecond());
|
||||
return Tools::set_ok('ok',['uniqueid'=>$uniqueid]);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* desc:获取对话token,在听译对话开始之前调用,用于隔离聊天记录
|
||||
* 参数:无
|
||||
*
|
||||
* api/Common/getSayToken
|
||||
*
|
||||
* author:wh
|
||||
*/
|
||||
function getSayToken(){
|
||||
return Mmodel::catchJson(function (){
|
||||
$uniqueid = md5(Tools::getMillisecond());
|
||||
return Tools::set_ok('ok',['uniqueid'=>$uniqueid]);
|
||||
});
|
||||
}
|
||||
}
|
||||
80
front/application/api/controller/Sms.php
Normal file
80
front/application/api/controller/Sms.php
Normal file
@@ -0,0 +1,80 @@
|
||||
<?php
|
||||
/*
|
||||
* description:
|
||||
* author:wh
|
||||
* email:
|
||||
* createTime:{2024/8/13} {11:22}
|
||||
*/
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
|
||||
use app\common\model\TabConf;
|
||||
use think\Db;
|
||||
use wanghua\general_utility_tools_php\Mmodel;
|
||||
use wanghua\general_utility_tools_php\sms\AliSms;
|
||||
use wanghua\general_utility_tools_php\tool\Tools;
|
||||
use wanghua\general_utility_tools_php\Validate;
|
||||
|
||||
class Sms extends BaseHttpApi
|
||||
{
|
||||
|
||||
/**
|
||||
* desc:发送短信验证码
|
||||
* 参数:
|
||||
* mobile 手机号
|
||||
* event 发送场景
|
||||
*
|
||||
* api/sms/send
|
||||
* author:wh
|
||||
*/
|
||||
function send(){
|
||||
return Mmodel::catchJson(function (){
|
||||
$mobile = input('mobile');
|
||||
if(empty($mobile)){
|
||||
return Tools::set_fail('手机号错误');
|
||||
}
|
||||
if(!Validate::is_mobile($mobile)){
|
||||
return Tools::set_fail('手机号错误');
|
||||
}
|
||||
//发送间隔不能低于60秒
|
||||
$sms_record = Db::table(TabConf::$fa_sms)
|
||||
->where('create_time','>',date('Y-m-d H:i:s',time()-60))
|
||||
->where('mobile',$mobile)
|
||||
->order('id desc')
|
||||
->find();
|
||||
if($sms_record){
|
||||
return Tools::set_fail('发送太频繁,请稍后再试');
|
||||
}
|
||||
|
||||
|
||||
//查询记录,10分钟内超过10次禁止发送
|
||||
$count = Db::table(TabConf::$fa_sms)
|
||||
->where('create_time','>',date('Y-m-d H:i:s',time()-600))
|
||||
->where('mobile',$mobile)
|
||||
->count();
|
||||
if($count>=10){
|
||||
return Tools::set_fail('发送次数过多,请稍后再试');
|
||||
}
|
||||
$config = config('sms_config');
|
||||
$code = Tools::rand_str(6,3);
|
||||
Tools::log_to_write_txt(['发送短信,开始','mobile'=>$mobile,'code'=>$code,'$config'=>$config]);
|
||||
$res = (new AliSms($config,$config['sms_sign_name'],$config['sms_template_code']))
|
||||
->send($mobile,json_encode(['code'=>$code]));
|
||||
Tools::log_to_write_txt(['发送短信,结束',$res]);
|
||||
|
||||
|
||||
//写入发送记录
|
||||
$sms_data = [
|
||||
'event'=>input('event'),
|
||||
'mobile'=>$mobile,
|
||||
'code'=>$code,
|
||||
'times'=>0,
|
||||
'ip'=>request()->ip(),
|
||||
];
|
||||
Db::table(TabConf::$fa_sms)->insert($sms_data);
|
||||
|
||||
return Tools::set_ok('操作成功',$res);
|
||||
});
|
||||
}
|
||||
}
|
||||
98
front/application/api/controller/Wsspush.php
Normal file
98
front/application/api/controller/Wsspush.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
/*
|
||||
* description:
|
||||
* author:wh
|
||||
* email:
|
||||
* createTime:{2024/4/5} {19:51}
|
||||
*/
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
|
||||
use app\api\logic\StatisticsLogic;
|
||||
use app\common\model\TabConf;
|
||||
use GatewayWorker\Lib\Gateway;
|
||||
use think\Db;
|
||||
use wanghua\general_utility_tools_php\Date;
|
||||
use wanghua\general_utility_tools_php\tool\Tools;
|
||||
|
||||
|
||||
/**
|
||||
* socket 主动推送
|
||||
*
|
||||
* Class Wsspush
|
||||
* @package app\api\controller
|
||||
*/
|
||||
class Wsspush extends BaseWssApi
|
||||
{
|
||||
|
||||
/**
|
||||
* desc:向指定客户端发送信息
|
||||
*
|
||||
* api/wsspush/index/socketTaskId/xxxx
|
||||
*
|
||||
* author:wh
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
|
||||
$socketTaskId = input('socketTaskId');
|
||||
Gateway::sendToClient($socketTaskId,json_encode(Tools::set_ok('向指定客户端发送信息。',$socketTaskId),JSON_UNESCAPED_UNICODE));
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* desc:向所有客户端发消息
|
||||
* api/wsspush/pushToAll
|
||||
* author:wh
|
||||
* @throws \Exception
|
||||
*/
|
||||
function pushToAll(){
|
||||
//$json = Tools::wss_json_ok('Reportty/createReport','ok',['flow_code'=>'create_report_end']);
|
||||
//Gateway::sendToAll($json);
|
||||
}
|
||||
public function hello($name = 'ThinkPHP5')
|
||||
{
|
||||
return 'hello,' . $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* desc:向客户端推送消息
|
||||
*
|
||||
* 接收一维数组表单
|
||||
*
|
||||
* /api/wsspush/pushMessageToClient
|
||||
*
|
||||
* author:wh
|
||||
*/
|
||||
function pushMessageToClient(){
|
||||
$socketTaskId = input('clientid');
|
||||
if(empty($socketTaskId)){
|
||||
return json(Tools::set_fail('客户端id必须'));
|
||||
}
|
||||
|
||||
$action = input('action');
|
||||
if(empty($action)){
|
||||
return json(Tools::set_fail('ACTION MUST'));
|
||||
}
|
||||
|
||||
$msg = input('msg');
|
||||
if(empty($msg)){
|
||||
return json(Tools::set_fail('MSG MUST'));
|
||||
}
|
||||
$all_params = [];
|
||||
foreach (input() as $key=>$item){
|
||||
if(in_array($key, ['clientid','action','msg'])){
|
||||
continue;
|
||||
}
|
||||
$all_params[$key] = $item;
|
||||
}
|
||||
|
||||
//json_encode(Tools::set_ok('向指定客户端发送信息。',$socketTaskId),JSON_UNESCAPED_UNICODE);
|
||||
$json = self::json_wss($action,$msg,$all_params);
|
||||
|
||||
Gateway::sendToClient($socketTaskId, $json);
|
||||
return json(Tools::set_ok('向指定客户端发送信息成功。'));
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
19
front/application/api/logic/WssMessageLogic.php
Normal file
19
front/application/api/logic/WssMessageLogic.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
/*
|
||||
* description:
|
||||
* author:wh
|
||||
* email:
|
||||
* createTime:{2024/5/23} {17:34}
|
||||
*/
|
||||
|
||||
namespace app\api\logic;
|
||||
|
||||
|
||||
use app\api\controller\BaseWssApi;
|
||||
use GatewayWorker\Lib\Gateway;
|
||||
use wanghua\general_utility_tools_php\tool\Tools;
|
||||
|
||||
class WssMessageLogic extends BaseLogic
|
||||
{
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user