no message

This commit is contained in:
hellcat 2025-07-18 17:34:39 +08:00
parent 61328ce2b6
commit 60f5ebb997
7 changed files with 332 additions and 42 deletions

View File

@ -70,23 +70,24 @@ final class HookController
$sClassPath = "App\\Services\\TG\\Hook\\Mod\\".$sClass;
// if (method_exists($sClassPath, $sFunc)) {
$oInstans = new $sClassPath();
$r = $oInstans->$sFunc($aParams);
$oInstans = new $sClassPath();
$r = $oInstans->$sFunc($aParams);
$oTGHttp->send($r, $aUpdate);
// 可以不返回里面直接send
if (isset($r)) {
if (empty($r)) { $r = 'hook的mod返回空'; }
// 返回可以是数组
if (is_array($r)) {
foreach ($r as $row) {
$oTGHttp->sendToTG($row, $aUpdate);
}
} else {
$oTGHttp->sendToTG($r, $aUpdate);
}
Http::response(200);
}
// if (isset($r)) {
// if (empty($r)) { $r = 'hook的mod返回空'; }
// // 返回可以是数组
// if (is_array($r)) {
// foreach ($r as $row) {
// $oTGHttp->sendToTG($row, $aUpdate);
// }
// } else {
// $oTGHttp->sendToTG($r, $aUpdate);
// }
//
// Http::response(200);
// }
// } else {
// throw new \Exception("没有这个方法:".$sClass.'#'.$sFunc);
@ -95,9 +96,9 @@ final class HookController
} catch (\Throwable $e) {
$sMessage = "错误(文件: " . $e->getFile() . ",行: " . $e->getLine() . ": " . $e->getMessage();
$oTGHttp->sendToTG($sMessage, $aUpdate);
$oTGHttp->send($sMessage, $aUpdate);
Http::response(200, 'error');
// Http::response(200, 'error');
}
}

View File

@ -7,4 +7,5 @@ use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
protected $table = 'article';
protected $hidden = ['updated_at', 'created_at'];
}

View File

@ -26,7 +26,7 @@ class DogOption extends Model
public static function _filter($oOption)
{
$arr = [];
$arr[$oOption->this_name."#".$oOption->k] = $oOption->v;
$arr[$oOption->dog_name."#".$oOption->k] = $oOption->v;
return $arr;
}

View File

@ -42,7 +42,16 @@ class Base {
public function toJson($aData)
{
return json_encode($aData, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
$json = json_encode($aData, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
// if ($sMsg) {
// $sMsg = $sMsg."\n\n";
// }
$str = $json;
return $str;
// return json_encode($aData, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}
public function urlFormat($sUrl)

View File

@ -6,10 +6,13 @@ namespace App\Services\TG\Hook\Mod;
use App\Models\Me as ModelMe;
use App\Models\DogOption as ModelDogOption;
use App\Models\Option as ModelOption;
// $this->oDog->_setDogName("hub"); // 必须me用option用
// $this->oDog->_setPrimaryKey("id"); // 可选默认id
// $this->oDog->_setStrField(["content"]); // 可选,指定长文本字段
// $this->oDog->_setDogName("hub"); // 必须me用option用
// $this->oDog->_setInFormat(["type"]); // 可选zero^int,^date,^dateFull
// $this->oDog->_setOutFormat(["time" => "date"]); // 可选datedateFull
class DOG extends Base {
@ -17,13 +20,13 @@ class DOG extends Base {
public $sPrimaryKey = "id";
public $iStrMaxLen = 50;
public $aStrField = [];
public $inFormat = [];
public $outFormat = [];
public $sDogName;
public function __construct($oModelSelf = false)
{
$this->_setModel($oModelSelf);
}
//// 定义系列
@ -52,6 +55,20 @@ class DOG extends Base {
{
$this->sPrimaryKey = $key;
}
public function _setInFormat($aInFormat)
{
$this->aInFormat = $aInFormat;
}
public function _setOutFormat($aOutFormat)
{
$this->aOutFormat = $aOutFormat;
}
// public function _setFormatField($aFormatField)
// {
// $this->aFormatField = $aFormatField;
// }
//// 定义系列 end
//// option 系列
@ -81,7 +98,7 @@ class DOG extends Base {
}
$oOption = new ModelDogOption();
$oOption->this_name = $this->sDogName;
$oOption->dog_name = $this->sDogName;
$oOption->k = $k;
$oOption->v = $v;
$oOption->save();
@ -106,7 +123,7 @@ class DOG extends Base {
return "需要v";
}
$oOption = ModelDogOption::where("this_name", $this->sDogName)->where("k", $k)->first();
$oOption = ModelDogOption::where("dog_name", $this->sDogName)->where("k", $k)->first();
if (!$oOption) {
return "没找到这个";
@ -131,7 +148,7 @@ class DOG extends Base {
return "需要k";
}
$oOption = ModelDogOption::where("this_name", $this->sDogName)->where("k", $k)->first();
$oOption = ModelDogOption::where("dog_name", $this->sDogName)->where("k", $k)->first();
if (!$oOption) {
return "没找到这个";
@ -254,15 +271,42 @@ class DOG extends Base {
return $input;
}
public function list()
public function list($aParam)
{
return $this->page($aParam);
}
public function limit($aParam)
{
if (($aParam["aArg"][1] ?? false) == "?") {
return "#limit {skip} {take}";
}
$oSelf = $this->oModelSelf->all();
$iSkip = $aParam["aArg"][1] ?? 0;
$iTake = $aParam["aArg"][2] ?? false;
$oSelf = $this->filterList($oSelf);
if (!$iTake) {
$iTake = ModelOption::_hit("dog_limit");
}
if (!$iTake) {
return "需要option表里的dog_limit";
}
$oSelf = $this->oModelSelf->skip($iSkip)->take($iTake)->get();
return $this->toJson($oSelf);
}
public function page($aParam)
{
$iPage = $aParam["aArg"][1] ?? 1;
$aParam = [];
$aParam["aOption"][2] = $iPage;
return $this->find($aParam);
}
public function add($aParam)
{
@ -330,30 +374,46 @@ class DOG extends Base {
return $this->find($aParam);
}
private function _getLimit()
{
$iLimit = ModelDogOption::_hit($this->sDogName, "limit");
if (!$iLimit) {
$iLimit = ModelOption::_hit("dog_limit");
}
return $iLimit;
}
public function find($aParam)
{
$sField = $aParam["aOption"][1] ?? $this->sPrimaryKey;
$sValue = $aParam["aArg"][1] ?? false;
$iPage = $aParam["aOption"][2] ?? 1;
$isLike = $aParam["is_like"] ?? false;
$sHitField = $aParam["sHitField"] ?? false;
if ($sValue == "?") {
return "#find__{field?primaryKey} {value}";
return "#find__{field?primaryKey}__{page?1} {value?all}";
}
if (!$sValue) {
return "需要value";
}
$iLimit = $this->_getLimit();
if (!$sField) {
return "需要field";
}
if ($isLike === true) {
$oSelf = $this->oModelSelf->where($sField, "like", "%".$sValue."%")->get();
if ($sValue === false ) {
$oSelf = $this->oModelSelf;
} else if ($isLike === true) {
$oSelf = $this->oModelSelf->where($sField, "like", "%".$sValue."%");
} else {
$oSelf = $this->oModelSelf->where($sField, $sValue)->get();
$oSelf = $this->oModelSelf->where($sField, $sValue);
}
$oSelf = $oSelf->paginate($iLimit, ['*'], 'page', $iPage);
if ($oSelf->isEmpty()) {
return "没找到这个";
@ -361,10 +421,117 @@ class DOG extends Base {
// hit必然单体
if ($sHitField) {
return $oSelf[0]->$sHitField;
return $oSelf->items()[0]->$sHitField;
}
return $this->toJson($this->filterList($oSelf));
$aData = $this->toDataList($oSelf);
return $this->toJson($aData);
}
private function toDataList($oSelf)
{
$aSelf = $oSelf->toArray();
$aMsg = [];
// 说明是分页
if (isset($aSelf["data"])) {
$aData = $aSelf["data"];
} else {
$aData = $aSelf;
}
$iCount = $aSelf["total"] ?? 0;
$iPagePer = $aSelf["per_page"] ?? 0;
$iPage = $aSelf["current_page"] ?? 0;
$iPageTotal = $aSelf["last_page"] ?? 0;
if ($iPageTotal > 1) {
if ($iPage) {
$aMsg[""]["页当前"] = $iPage;
}
if ($iPageTotal) {
$aMsg[""]["页总数"] = $iPageTotal;
}
if ($iPagePer) {
$aMsg[""]["行每页"] = $iPagePer;
}
if ($iCount) {
$aMsg[""]["行总数"] = $iCount;
}
}
$aData = $this->filterDataList($aData);
$aData = $this->dataInsterMsgList($aData, $aMsg);
return $aData;
}
private function dataInsterMsgList($aData, $aMsg)
{
if ($aMsg) {
array_unshift($aData, $aMsg);
}
return $aData;
}
private function dataInsterMsg($aData, $aMsg)
{
$aDataNew = [];
if ($aMsg) {
$aDataNew[] = $aMsg;
$aDataNew[] = $aData;
} else {
$aDataNew = $aData;
}
return $aDataNew;
}
private function toData($oSelf)
{
if (!is_array($oSelf)) {
$aData = $oSelf->toArray();
} else {
$aData = $oSelf;
}
$aData = $this->filterData($aData);
return $aData;
}
private function filterDataList($aData)
{
foreach ($aData as &$aDataRow) {
$aDataRow = $this->filterData($aDataRow);
}
return $aData;
}
private function filterData($aData)
{
foreach ($this->aStrField as $sStrField) {
$aData[$sStrField] = $this->subStr($aData[$sStrField]);
}
foreach ($this->aOutFormat as $sFormatField => $sFormatType) {
if ($sFormatType == "date") {
$aData[$sFormatField] = date("Y-m-d", (int) $aData[$sFormatField]);
} else if ($sFormatType == "dateFull") {
$aData[$sFormatField] = date("Y-m-d", (int) $aData[$sFormatField]);
}
}
return $aData;
}
public function kv($aParam)
@ -438,6 +605,7 @@ class DOG extends Base {
$aJson = json_decode($sJson, true);
foreach ($aJson as $k => $v) {
$v = $this->inFormat($k, $v);
$oModelSelf->$k = $v;
}
@ -449,6 +617,28 @@ class DOG extends Base {
return $this->find($aParam);
}
public function jsonGet($aParam)
{
$sWhereValue = $aParam["aArg"][1] ?? false;
$sWhereField = $aParam["aOption"][1] ?? $this->sPrimaryKey;
if ($sWhereValue == "?") {
return "#jsonGet__{whereFile?primary} {whereValue}";
}
if (!$sWhereValue) {
return "需要whereValue";
}
$oSelf = $this->oModelSelf->where($sWhereField, $sWhereValue)->first();
if (!$oSelf) {
return "没找到这个";
}
return $this->toJson($oSelf);
}
public function likeSet($aParam)
{
if (($aParam["aArg"][1] ?? false) == "?") {
@ -523,7 +713,7 @@ class DOG extends Base {
$sSelfStr = $this->toJson($oSelf);
return $sMsg.'\n\n'.$sSelfStr;
return $sMsg."\n\n".$sSelfStr;
}
} else {
@ -534,6 +724,7 @@ class DOG extends Base {
return "没找到这个";
}
$sSetValue = $this->inFormat($sSetField, $sSetValue);
$i = 0;
foreach ($oSelf as $oSelfRow) {
$oSelfRow->$sSetField = $sSetValue;
@ -551,6 +742,41 @@ class DOG extends Base {
return "影响".$i."条数据";
}
private function inFormat($sSetField, $sSetValue) {
if (in_array($sSetField, $this->aInFormat)) {
$sSetValue = $this->mapInFormat($sSetValue);
}
return $sSetValue;
}
private function mapInFormat($sValue)
{
$aValue = explode("^", $sValue, 2);
if (!isset($aValue[1])) {
return $sValue;
}
$sValueQian = $aValue[0];
$sValueHou = $aValue[1];
if ($sValueHou == "int") {
if ($sValueQian == "zero") {
$sValueQian = 0;
}
} else if ($sValueHou == "dayToDate") {
$sValueQian = date("Y-m-d H:i:s", strtotime("+{$sValueQian} days"));
}
return $sValueQian;
}
public function delDone($aParam)
{
$aParam["is_done"] = true;

View File

@ -15,6 +15,8 @@ class Hub extends Base {
// $this->oDDT->_setPrimaryKey("id"); // 可选默认id
// $this->oDDT->_setStrField(["content"]); // 可选,指定长文本字段
$this->oDog->_setDogName("hub"); // 必须
$this->oDog->_setInFormat(["user_name"]);
$this->oDog->_setOutFormat(["user_name" => "date"]);
}

View File

@ -1,6 +1,7 @@
<?php
namespace App\Services\TG;
use App\Services\TomTool\Http as TomHttp;
class Http {
@ -13,6 +14,56 @@ class Http {
$this->sApiChatId = env('telegram_chatid');
}
public function send($sMsg = "msg空", $aUpdate = [])
{
$bSendTg = true;
$sShell = $aUpdate["shell"] ?? false;
$bIsDove = $aUpdate["is_dove"] ?? false;
if ($sShell == "cmd") {
$bSendTg = false;
}
if ($bIsDove == true) {
$bSendTg = false;
}
if (env("is_loc") == true) {
$bSendTg = false;
}
if (is_array($sMsg)) {
foreach ($sMsg as $sMsgRow) {
if ($bSendTg) {
$this->sendTg($sMsgRow);
} else {
echo $sMsgRow;
}
}
} else {
if ($bSendTg) {
$this->sendTg($sMsg);
} else {
echo $sMsg;
}
}
if ($bSendTg) {
Http::response(200);
} else {
exit;
}
}
private function sendTg($sMsg)
{
$url = "https://api.telegram.org/bot$this->sApiToken/sendMessage?chat_id=$this->sApiChatId&text=" . urlencode($sMsg);
file_get_contents($url);
}
public function sendToTG($sMsg, $aUpdate = [])
{