diff --git a/app/Http/Controllers/Api/TG/HookController.php b/app/Http/Controllers/Api/TG/HookController.php index 664afd0e..111e0eba 100755 --- a/app/Http/Controllers/Api/TG/HookController.php +++ b/app/Http/Controllers/Api/TG/HookController.php @@ -106,28 +106,28 @@ final class HookController // else { // throw new \Exception("没有这个方法:".$sClass.'#'.$sFunc); // } - - if (isset($r)) { - // 返回可以是数组 - if (is_array($r)) { - foreach ($r as $row) { - $oTGHttp->sendToTG($row, $aUpdate); - } - } else { - $oTGHttp->sendToTG($r, $aUpdate); - } - - Http::response(200); - } else { - throw new \Exception("?!?!?!?!"); - } + $oTGHttp->send($r, $aUpdate); +// if (isset($r)) { +// // 返回可以是数组 +// if (is_array($r)) { +// foreach ($r as $row) { +// $oTGHttp->sendToTG($row, $aUpdate); +// } +// } else { +// $oTGHttp->sendToTG($r, $aUpdate); +// } +// +// Http::response(200); +// } else { +// throw new \Exception("?!?!?!?!"); +// } } 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'); } } diff --git a/app/Models/Option.php b/app/Models/DogOption.php similarity index 55% rename from app/Models/Option.php rename to app/Models/DogOption.php index 3cc7d429..147070c8 100755 --- a/app/Models/Option.php +++ b/app/Models/DogOption.php @@ -4,28 +4,28 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; -class Option extends Model +class DogOption extends Model { - protected $table = 'option'; + protected $table = 'dog_option'; public $timestamps = false; - public static function _hit($sThisName, $k) + public static function _hit($sDogName, $k) { - $v = self::where("this_name", $sThisName)->where("k", $k)->first()?->v; + $v = self::where("dog_name", $sDogName)->where("k", $k)->first()?->v; return $v; } - public static function _list($sThisName) + public static function _list($sDogName) { - $oSelf = self::where("this_name", $sThisName)->get(); + $oSelf = self::where("dog_name", $sDogName)->get(); return self::_filterList($oSelf); } -// public static function _limit($sThisName, $iSkip, $iTake) +// public static function _limit($sDogName, $iSkip, $iTake) // { -// $oSelf = self::where("this_name", $sThisName)->skip($iSkip)->take($iTake)->get(); +// $oSelf = self::where("dog_name", $sDogName)->skip($iSkip)->take($iTake)->get(); // // return self::__filterList($oSelf); // } @@ -33,7 +33,7 @@ class Option 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; } diff --git a/app/Services/TG/Hook/Mod/DDT.php b/app/Services/TG/Hook/Mod/DDT.php deleted file mode 100644 index ea731f06..00000000 --- a/app/Services/TG/Hook/Mod/DDT.php +++ /dev/null @@ -1,583 +0,0 @@ -_setModel($oModelSelf); - - } - - //// 定义系列 - public function _setStrMaxLen($iStrMaxLen) - { - $this->iStrMaxLen = $iStrMaxLen; - } - - public function _setStrField($aStrField) - { - $this->aStrField = $aStrField; - } - - public function _setThisName($sThisName) - { - $this->sThisName = $sThisName; - } - - - public function _setModel($oModelSelf) - { - $this->oModelSelf = $oModelSelf; - } - - public function _setPrimaryKey($key) - { - $this->sPrimaryKey = $key; - } - //// 定义系列 end - - //// option 系列 - public function option($aParam) - { - - $aOption = ModelOption::_list($this->sThisName); - - return $this->toJson($aOption); - } - - public function optionAdd($aParam) - { - $k = $aParam["aArg"][1] ?? false; - $v = $aParam["aArg"][2] ?? false; - - if ($k == "?") { - return "#optionAdd {k} {v}"; - } - - if (!$k) { - return "需要k"; - } - - if (!$v) { - return "需要v"; - } - - $oOption = new ModelOption(); - $oOption->this_name = $this->sThisName; - $oOption->k = $k; - $oOption->v = $v; - $oOption->save(); - - return $this->toJson(ModelOption::_filter($oOption)); - } - - public function optionSet($aParam) - { - $k = $aParam["aOption"][1] ?? false; - $v = $aParam["aArg"][1] ?? false; - - if ($v == "?") { - return "#optionSet__{k} {v}"; - } - - if (!$k) { - return "需要k"; - } - - if (!$v) { - return "需要v"; - } - - $oOption = ModelOption::where("this_name", $this->sThisName)->where("k", $k)->first(); - - if (!$oOption) { - return "没找到这个"; - } - - $oOption->v = $v; - $oOption->save(); - - return $this->toJson(ModelOption::_filter($oOption)); - } - - public function optionDel($aParam) - { - $k = $aParam["aArg"][1] ?? false; - $is_done = $aParam["is_done"] ?? false; - - if ($k == "?") { - return "#optionDel {k}"; - } - - if (!$k) { - return "需要k"; - } - - $oOption = ModelOption::where("this_name", $this->sThisName)->where("k", $k)->first(); - - if (!$oOption) { - return "没找到这个"; - } - - if ($is_done === false) { - - $sMsg = "匹配以下,确认用optionDelDone()。"; - $sOption = $this->toJson($oOption); - - return $sMsg."\n\n".$sOption; - - } else if ($is_done === true) { - - $r = $oOption->delete(); - - return $r; - } - } - - public function optionDelDone($aParam) - { - $aParam["is_done"] = true; - - return $this->optionDel($aParam); - } - //// option 系列 end - - //// 辅助系列 - private function filterList($oSelfList) - { - foreach ($oSelfList as &$oSelfListRow) { - $oSelfListRow = $this->filterRow($oSelfListRow); - } - - return $oSelfList; - } - - private function meToStr_list($oMe) - { - $str = ""; - $i = 0; - foreach ($oMe as $oMeRow) { - if ($i !== 0) { - $str .= "\n\n"; - } - $str .= $this->meToStr($oMeRow); - $i++; - } - - return $str; - } - - private function meToStr($oMe) - { - $str = $oMe->id.":".$oMe->rag; - - return $str; - } - //// 辅助系列 end - - //// me系列 - public function me() - { - $oMe = ModelMe::where("class", $this->sThisName)->first(); - - if (!$oMe) { - return "空"; - } - - return $oMe->rag; - } - - public function meEdit($aParam) - { - $aValue = $aParam["aArg"] ?? false; - - $sValue = implode(" ", $aValue); - - if ($sValue == "?") { - return "/meEdit {value}"; - } - - $aWhere = [ - "class" => $this->sThisName, - ]; - - $aValue = [ - "rag" => $sValue, - ]; - - $oMe = ModelMe::updateOrCreate($aWhere, $aValue); - - return $this->me(); - } - -// public function meAdd($aParam) -// { -// $aRag = $aParam["aArg"] ?? false; -// $sRag = implode(" ", $aRag); -// -// if ($sRag == "?") { -// return "meAdd {value}"; -// } -// -// if (!$sRag) { -// return "需要value"; -// } -// -// $oMe = new ModelMe(); -// $oMe->class = $this->sThisName; -// $oMe->rag = $sRag; -// $oMe->save(); -// -// return $this->toJson($oMe); -// } - -// public function meLike($aParam) -// { -// $sValue = $aParam["aArg"][1] ?? false; -// -// if ($sValue == "?") { -// return "meLike {value}"; -// } -// -// if (!$sValue) { -// return "需要value"; -// } -// -// $oMe = ModelMe::where("rag", "like", "%".$sValue."%")->get(); -// -// return $this->meToStr_list($oMe); -// } - -// public function meSet($aParam) -// { -// $iId = $aParam["aOption"][1] ?? false; -// $sValue = $aParam["aArg"][1] ?? false; -// -// if ($sValue == "?") { -// return "#meSet__{id} {value}"; -// } -// -// if (!$sValue) { -// return "需要value"; -// } -// -// $oMe = ModelMe::find($iId); -// -// if (!$oMe) { -// return "没找到这个"; -// } -// -// $oMe->rag = $sValue; -// $oMe->save(); -// -// return $this->toJson($oMe); -// } - -// public function meDelDone($aParam) -// { -// $aParam["isDone"] = true; -// -// return $this->meDel($aParam); -// } - -// public function meDel($aParam) -// { -// if ($aParam["aArg"][1] == "?") { -// return "#meDel {id多个}"; -// } -// -// $aId = $aParam["aArg"] ?? []; -// $isDone = $aParam["isDone"] ?? false; -// -// if (!$aId) { -// return "需要id"; -// } -// -// $oMe = ModelMe::whereIn("id", $aId)->get(); -// -// if ($oMe->isEmpty()) { -// return "没找到这个"; -// } -// -// if ($isDone === false) { -// -// $sMsg = "删除这几个,确认用meDelDone。\n\n"; -// -// $sMe = $this->meToStr_list($oMe); -// -// return $sMsg.$sMe; -// } -// -// if ($isDone === true) { -// -// $i = 0; -// foreach ($oMe as $oMeRow) { -// $r = $oMeRow->delete(); -// $i = $i+$r; -// } -// -// return "删除".$i."条数据"; -// } -// } - //// me系列 end - - private function filterRow($oSelfRow) - { - - foreach ($this->aStrField as $v) { - $oSelfRow->{$v} = $this->subStr($oSelfRow->{$v}); - } - - return $oSelfRow; - } - - private function subStr($input) { - - if (mb_strlen($input, 'UTF-8') > $this->iStrMaxLen) { - - return mb_substr($input, 0, $this->iStrMaxLen, 'UTF-8')."……"; - - } - - return $input; - } - - public function list() - { - - $oSelf = $this->oModelSelf->all(); - - $oSelf = $this->filterList($oSelf); - - return $this->toJson($oSelf); - } - - public function add($aParam) - { - $sField = $aParam["aOption"][1] ?? false; - $sValue = $aParam["aArg"][1] ?? false; - - if ($sValue == "?") { - - return "#add__{field} {value}"; - - } - - if (!$sValue) { - return "需要value"; - } - - if (!$sField) { - return "需要field"; - } - - $this->oModelSelf->$sField = $sValue; - - $this->oModelSelf->save(); - - $aParam["aOption"][1] = "id"; - $aParam["aArg"][1] = $this->oModelSelf->id; - - return $this->find($aParam); - } - - public function like($aParam) - { - - $aParam["is_like"] = true; - - return $this->find($aParam); - - } - - public function find($aParam) - { - $sField = $aParam["aOption"][1] ?? $this->sPrimaryKey; - $sValue = $aParam["aArg"][1] ?? false; - $isLike = $aParam["is_like"] ?? false; - $sHitField = $aParam["sHitField"] ?? false; - - if ($sValue == "?") { - return "#find__{field?primaryKey} {value}"; - } - - if (!$sValue) { - return "需要value"; - } - - if (!$sField) { - return "需要field"; - } - - if ($isLike === true) { - $oSelf = $this->oModelSelf->where($sField, "like", "%".$sValue."%")->get(); - } else { - $oSelf = $this->oModelSelf->where($sField, $sValue)->get(); - } - - if ($oSelf->isEmpty()) { - return "没找到这个"; - } - - // hit必然单体 - if ($sHitField) { - return $oSelf[0]->$sHitField; - } - - return $this->toJson($this->filterList($oSelf)); - } - - public function set($aParam) - { - $sSetValue = $aParam["aArg"] ?? false; - $sSetValue = implode(" ", $sSetValue); - - if ($sSetValue == "?") { - $tmp = "#set__{whereField}__{WhereValue}__{setField} {setValue}"; - $tmp .= "\n\n"; - $tmp .= "#set__{primaryValue主键值,默认id}__{setField} {serValue}"; - return $tmp; - } - - // 有第三参数说明是findSet模式 - if (isset($aParam["aOption"][3])) { - $sWhereField = $aParam["aOption"][1] ?? false; - $sWhereValue = $aParam["aOption"][2] ?? false; - $sSetField = $aParam["aOption"][3] ?? false; - $sMod = "findSet"; - } else { - $sWhereField = $this->sPrimaryKey; - $sWhereValue = $aParam["aOption"][1] ?? false; - $sSetField = $aParam["aOption"][2] ?? false; - $sMod = "set"; - } - - if (!$sWhereField) { - return "需要whereField"; - } - - if (!$sWhereValue) { - return "需要whereValue"; - } - - if (!$sSetField) { - return "需要setValue"; - } - - $oSelf = $this->oModelSelf->where($sWhereField, $sWhereValue)->get(); - - if ($oSelf->isEmpty()) { - return "没找到这个"; - } - - $i = 0; - foreach ($oSelf as $oSelfRow) { - $oSelfRow->$sSetField = $sSetValue; - $oSelfRow->save(); - $i++; - } - - if ($i == 1) { - $aParam["aOption"][1] = $this->sPrimaryKey; - $aParam["aArg"][1] = $oSelfRow->{$this->sPrimaryKey}; - return $this->find($aParam); - } - - return "影响".$i."条数据"; - } - - public function delDone($aParam) - { - $aParam["is_done"] = true; - - return $this->del($aParam); - } - - public function del($aParam) - { - $sField = $aParam["aOption"][1] ?? $this->sPrimaryKey; - $sValue = $aParam["aArg"][1] ?? false; - $bIsDone = $aParam["is_done"] ?? false; - - if ($sValue == "?") { - return "#del__{field?默认primaryKey} {value}"; - } - - $oSelf = $this->oModelSelf->where($sField, $sValue)->get(); - - if ($oSelf->isEmpty()) { - return "没找到这个"; - } - - $sMsg = "匹配以下数据,确认删除使用delDone()。\n\n"; - $sStr = $this->toJson($oSelf); - - return $sMsg.$sStr; - } - - public function hit($aParam) - { - $sHitField = $aParam["aOption"][1] ?? false; - $sWhereField = $aParam["aOption"][2] ?? $this->sPrimaryKey; - $sWhereValue = $aParam["aArg"][1] ?? false; - - if ($sWhereValue == "?") { - return "#hit__{hitField}__{whereField?primaryKey} {whereValue}"; - } - - if (!$sWhereValue) { - return "需要value"; - } - - $aParam["sHitField"] = $sHitField; - $aParam["aOption"][1] = $sWhereField; - - return $this->find($aParam); - } - - public function content($aParam) - { - $sWhereField = $aParam["aOption"][1] ?? $this->sPrimaryKey; - $sValue = $aParam["aArg"][1] ?? false; - - if ($sValue == "?") { - return "#content__{whereField?primaryKey} {whereValue}"; - } - - if (!$sValue) { - return "需要value"; - } - - $aParam["aOption"][1] = "content"; - $aParam["aOption"][2] = $sWhereField; - - return $this->hit($aParam); - } - - public function table() - { - $sTable = $this->oModelSelf->getTable(); -// tomd($sTable, 1); -// $columns = \DB::select("SHOW FULL COLUMNS FROM blade"); - $columns = \DB::select("SHOW CREATE TABLE `{$sTable}`"); - - return $this->toJson($columns[0]->{"Create Table"}); -// tomd($columns[0]->{"Create Table"}, 1); - } - -} diff --git a/app/Services/TG/Hook/Mod/DOG.php b/app/Services/TG/Hook/Mod/DOG.php new file mode 100644 index 00000000..da059e4e --- /dev/null +++ b/app/Services/TG/Hook/Mod/DOG.php @@ -0,0 +1,868 @@ +oDog->_setDogName("hub"); // 必须,me用,option用 +// $this->oDog->_setPrimaryKey("id"); // 可选,默认id +// $this->oDog->_setStrField(["content"]); // 可选,指定长文本字段 +// $this->oDog->_setInFormat(["type"]); // 可选,zero^int,^date,^dateFull +// $this->oDog->_setOutFormat(["time" => "date"]); // 可选,date,dateFull + +class DOG extends Base { + + public $oModelSelf; + public $sPrimaryKey = "id"; + public $iStrMaxLen = 50; + public $aStrField = []; + public $inFormat = []; + public $outFormat = []; + public $sDogName; + + public function __construct($oModelSelf = false) + { + $this->_setModel($oModelSelf); + } + + //// 定义系列 + public function _setStrMaxLen($iStrMaxLen) + { + $this->iStrMaxLen = $iStrMaxLen; + } + + public function _setStrField($aStrField) + { + $this->aStrField = $aStrField; + } + + public function _setDogName($sDogName) + { + $this->sDogName = $sDogName; + } + + + public function _setModel($oModelSelf) + { + $this->oModelSelf = $oModelSelf; + } + + public function _setPrimaryKey($key) + { + $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 系列 + public function option($aParam) + { + + $aOption = ModelDogOption::_list($this->sDogName); + + return $this->toJson($aOption); + } + + public function optionAdd($aParam) + { + $k = $aParam["aArg"][1] ?? false; + $v = $aParam["aArg"][2] ?? false; + + if ($k == "?") { + return "#optionAdd {k} {v}"; + } + + if (!$k) { + return "需要k"; + } + + if (!$v) { + return "需要v"; + } + + $oOption = new ModelDogOption(); + $oOption->dog_name = $this->sDogName; + $oOption->k = $k; + $oOption->v = $v; + $oOption->save(); + + return $this->toJson(ModelDogOption::_filter($oOption)); + } + + public function optionSet($aParam) + { + $k = $aParam["aOption"][1] ?? false; + $v = $aParam["aArg"][1] ?? false; + + if ($v == "?") { + return "#optionSet__{k} {v}"; + } + + if (!$k) { + return "需要k"; + } + + if (!$v) { + return "需要v"; + } + + $oOption = ModelDogOption::where("dog_name", $this->sDogName)->where("k", $k)->first(); + + if (!$oOption) { + return "没找到这个"; + } + + $oOption->v = $v; + $oOption->save(); + + return $this->toJson(ModelDogOption::_filter($oOption)); + } + + public function optionDel($aParam) + { + $k = $aParam["aArg"][1] ?? false; + $is_done = $aParam["is_done"] ?? false; + + if ($k == "?") { + return "#optionDel {k}"; + } + + if (!$k) { + return "需要k"; + } + + $oOption = ModelDogOption::where("dog_name", $this->sDogName)->where("k", $k)->first(); + + if (!$oOption) { + return "没找到这个"; + } + + if ($is_done === false) { + + $sMsg = "匹配以下,确认用optionDelDone()。"; + $sOption = $this->toJson($oOption); + + return $sMsg."\n\n".$sOption; + + } else if ($is_done === true) { + + $r = $oOption->delete(); + + return $r; + } + } + + public function optionDelDone($aParam) + { + $aParam["is_done"] = true; + + return $this->optionDel($aParam); + } + //// option 系列 end + + //// 辅助系列 + private function filterList($oSelfList) + { + foreach ($oSelfList as &$oSelfListRow) { + $oSelfListRow = $this->filterRow($oSelfListRow); + } + + return $oSelfList; + } + + private function meToStr_list($oMe) + { + $str = ""; + $i = 0; + foreach ($oMe as $oMeRow) { + if ($i !== 0) { + $str .= "\n\n"; + } + $str .= $this->meToStr($oMeRow); + $i++; + } + + return $str; + } + + private function meToStr($oMe) + { + $str = $oMe->id.":".$oMe->rag; + + return $str; + } + //// 辅助系列 end + + //// json系列 +// private function + //// json系列end + + //// me系列 + public function me() + { + $oMe = ModelMe::where("class", $this->sDogName)->first(); + + if (!$oMe) { + return "空"; + } + + return $oMe->rag; + } + + public function meEdit($aParam) + { + $aValue = $aParam["aArg"] ?? false; + + $sValue = implode(" ", $aValue); + + if ($sValue == "?") { + return "/meEdit {value}"; + } + + $aWhere = [ + "class" => $this->sDogName, + ]; + + $aValue = [ + "rag" => $sValue, + ]; + + $oMe = ModelMe::updateOrCreate($aWhere, $aValue); + + return $this->me(); + } + //// me系列 end + + private function filterRow($oSelfRow) + { + + foreach ($this->aStrField as $v) { + $oSelfRow->{$v} = $this->subStr($oSelfRow->{$v}); + } + + return $oSelfRow; + } + + private function subStr($input) { + + if (mb_strlen($input, 'UTF-8') > $this->iStrMaxLen) { + + return mb_substr($input, 0, $this->iStrMaxLen, 'UTF-8')."……"; + + } + + return $input; + } + + public function list($aParam) + { + return $this->page($aParam); + } + + public function limit($aParam) + { + if (($aParam["aArg"][1] ?? false) == "?") { + return "#limit {skip} {take}"; + } + + $iSkip = $aParam["aArg"][1] ?? 0; + $iTake = $aParam["aArg"][2] ?? false; + + 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) + { + $sField = $aParam["aOption"][1] ?? false; + $sValue = $aParam["aArg"][1] ?? false; + + if ($sValue == "?") { + + return "#add__{field} {value}"; + + } + + if (!$sValue) { + return "需要value"; + } + + if (!$sField) { + return "需要field"; + } + + $this->oModelSelf->$sField = $sValue; + + $this->oModelSelf->save(); + + $aParam["aOption"][1] = $this->sPrimaryKey; + $aParam["aArg"][1] = $this->oModelSelf->{$this->sPrimaryKey}; + + return $this->find($aParam); + } + + public function like($aParam) + { + + $aParam["is_like"] = true; + + return $this->find($aParam); + + } + + public function clone($aParam) + { + $iId = $aParam['aArg'][1] ?? false; + + if ($iId == "?") { + return "#clone {主键值}"; + } + + if (!$iId) { + return "需要arg[1]"; + } + + // 查找文章 + $oModelSelf = $this->oModelSelf->find($iId); + + if (!$oModelSelf) { + return "没找到这个"; + } + + $oModelSelfNew = $oModelSelf->replicate(); + $oModelSelfNew->save(); + + $aParam = []; + $aParam["aArg"][1] = $oModelSelfNew->{$this->sPrimaryKey}; + + 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}__{page?1} {value?all}"; + } + + $iLimit = $this->_getLimit(); + + if ($sValue === false ) { + + $oSelf = $this->oModelSelf; + + } else if ($isLike === true) { + + $oSelf = $this->oModelSelf->where($sField, "like", "%".$sValue."%"); + + } else { + + $oSelf = $this->oModelSelf->where($sField, $sValue); + + } + + $oSelf = $oSelf->paginate($iLimit, ['*'], 'page', $iPage); + + if ($oSelf->isEmpty()) { + return "没找到这个"; + } + + // hit必然单体 + if ($sHitField) { + return $oSelf->items()[0]->$sHitField; + } + + $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) + { + $k = $aParam["aOption"][1] ?? false; + $v = $aParam["aArg"][1] ?? false; + + if ($v == "?") { + return "#kv__{k} {v}"; + } + + $aParam = []; + $aParam["aOption"][1] = $k; + $aParam["aOption"][2] = "v"; + $aParam["aArg"][1] = $v; + + return $this->set($aParam); + } + + public function jsonAdd($aParam) + { + if (($aParam["aArg"][1] ?? false) == "?") { + return "#jsonAdd {json}"; + } + + $aJson = $aParam["aArg"] ?? false; + $sJson = implode(" ", $aJson); + + if (!$sJson) { + return "需要json"; + } + + $aJson = json_decode($sJson, true); + + foreach ($aJson as $k => $v) { + $this->oModelSelf->$k = $v; + } + + $this->oModelSelf->save(); + + $aParam = []; + $aParam["aArg"][1] = $this->oModelSelf->{$this->sPrimaryKey}; + + return $this->find($aParam); + } + + public function jsonSet($aParam) + { + $sWhereValue = $aParam["aOption"][1] ?? false; + $aJson = $aParam["aArg"] ?? false; + $sJson = implode(" ", $aJson); + + if ($sJson == "?") { + return "#setJson__{whereValue} {json} | 绑定主键 单体"; + } + + if (!$sWhereValue) { + return "需要whereValue"; + } + + if (!$sJson) { + return "需要json"; + } + + $oModelSelf = $this->oModelSelf->where($this->sPrimaryKey, $sWhereValue)->first(); + + if (!$oModelSelf) { + return "没找到这个"; + } + + $aJson = json_decode($sJson, true); + + foreach ($aJson as $k => $v) { + $v = $this->inFormat($k, $v); + $oModelSelf->$k = $v; + } + + $oModelSelf->save(); + + $aParam = []; + $aParam["aArg"][1] = $oModelSelf->{$this->sPrimaryKey}; + + 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) == "?") { + return "原set走like匹配,慎用"; + } + + $aParam["is_like"] = true; + + return $this->set($aParam); + } + + public function likeSetDone($aParam) + { + if (($aParam["aArg"][1] ?? false) == "?") { + return "原set走like匹配,慎用"; + } + + $aParam["is_like"] = true; + $aParam["is_like_done"] = true; + + return $this->set($aParam); + } + + public function set($aParam) + { + $sSetValue = $aParam["aArg"] ?? false; + $sSetValue = implode(" ", $sSetValue); + $bIsLike = $aParam["is_like"] ?? false; + $bIsLikeDone = $aParam["is_like_done"] ?? false; + + if ($sSetValue == "?") { + $tmp = "#set__{whereField}__{WhereValue}__{setField} {setValue} | 多条"; + $tmp .= "\n\n"; + $tmp .= "#set__{whereValue}__{setField} {setValue} | 主键"; + return $tmp; + } + + // 有第三参数说明是findSet模式 + if (isset($aParam["aOption"][3])) { + $sWhereField = $aParam["aOption"][1] ?? false; + $sWhereValue = $aParam["aOption"][2] ?? false; + $sSetField = $aParam["aOption"][3] ?? false; + $sMod = "findSet"; + } else { + $sWhereField = $this->sPrimaryKey; + $sWhereValue = $aParam["aOption"][1] ?? false; + $sSetField = $aParam["aOption"][2] ?? false; + $sMod = "set"; + } + + if (!$sWhereField) { + return "需要whereField"; + } + + if (!$sWhereValue) { + return "需要whereValue"; + } + + if (!$sSetField) { + return "需要setField"; + } + + if ($bIsLike === false) { + $oSelf = $this->oModelSelf->where($sWhereField, $sWhereValue)->get(); + } else if ($bIsLike === true) { + + $oSelf = $this->oModelSelf->where($sWhereField, "like", "%".$sWhereValue."%")->get(); + + if ($bIsLikeDone === false) { + + $sMsg = "匹配以下数据,确认修改用likeSetDone()。"; + + $sSelfStr = $this->toJson($oSelf); + + return $sMsg."\n\n".$sSelfStr; + } + + } else { + return "??????????????"; + } + + if ($oSelf->isEmpty()) { + return "没找到这个"; + } + + $sSetValue = $this->inFormat($sSetField, $sSetValue); + $i = 0; + foreach ($oSelf as $oSelfRow) { + $oSelfRow->$sSetField = $sSetValue; + $oSelfRow->save(); + $i++; + } + + if ($i == 1) { + $aParam = []; + $aParam["aOption"][1] = $this->sPrimaryKey; + $aParam["aArg"][1] = $oSelfRow->{$this->sPrimaryKey}; + return $this->find($aParam); + } + + 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; + + return $this->del($aParam); + } + + public function del($aParam) + { + $sField = $aParam["aOption"][1] ?? $this->sPrimaryKey; + $sValue = $aParam["aArg"][1] ?? false; + $bIsDone = $aParam["is_done"] ?? false; + + if ($sValue == "?") { + return "#del__{field?默认primaryKey} {value}"; + } + + $oSelf = $this->oModelSelf->where($sField, $sValue)->get(); + + if ($oSelf->isEmpty()) { + return "没找到这个"; + } + + if ($bIsDone == true) { + + $i = 0; + foreach ($oSelf as $oSelfRow) { + $ii = $oSelfRow->delete(); + $i = $i + $ii; + } + + return "删除".$i."条数据"; + } + + $sMsg = "匹配以下数据,确认删除使用delDone()。\n\n"; + $sStr = $this->toJson($oSelf); + + return $sMsg.$sStr; + } + + public function hit($aParam) + { + $sHitField = $aParam["aOption"][1] ?? false; + $sWhereField = $aParam["aOption"][2] ?? $this->sPrimaryKey; + $sWhereValue = $aParam["aArg"][1] ?? false; + + if ($sWhereValue == "?") { + return "#hit__{hitField}__{whereField?primaryKey} {whereValue}"; + } + + if (!$sWhereValue) { + return "需要value"; + } + + $aParam["sHitField"] = $sHitField; + $aParam["aOption"][1] = $sWhereField; + + return $this->find($aParam); + } + + public function content($aParam) + { + $sWhereField = $aParam["aOption"][1] ?? $this->sPrimaryKey; + $sValue = $aParam["aArg"][1] ?? false; + + if ($sValue == "?") { + return "#content__{whereField?primaryKey} {whereValue}"; + } + + if (!$sValue) { + return "需要value"; + } + + $aParam["aOption"][1] = "content"; + $aParam["aOption"][2] = $sWhereField; + + return $this->hit($aParam); + } + + public function table() + { + $sTable = $this->oModelSelf->getTable(); + + $columns = \DB::select("SHOW CREATE TABLE `{$sTable}`"); + + return $columns[0]->{"Create Table"}; + } + +} diff --git a/app/Services/TG/Http.php b/app/Services/TG/Http.php index d55eb210..46247a6d 100644 --- a/app/Services/TG/Http.php +++ b/app/Services/TG/Http.php @@ -14,11 +14,62 @@ class Http { $this->sApiChatId = env('telegram_chatid'); } - // 原来的命名不合理,包装一下 - public function send($sMsg, $aUpdate = []) { + public function send($sMsg = "msg空", $aUpdate = []) + { - return $this->sendToTG($sMsg, $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) + { + $oHttp = new TomHttp(); + $oHttp->sMethod = "post"; + $oHttp->sToUrl = "https://api.telegram.org/bot$this->sApiToken/sendMessage"; +// $oHttp->bIsJson = true; + $oHttp->aData = [ + "chat_id" => $this->sApiChatId, + "text" => $sMsg + ]; + + $oHttp->send(); } public function sendToTG($sMsg, $aUpdate = []) diff --git a/resources/views/cmd.blade.php b/resources/views/cmd.blade.php index 85879232..b69feae1 100644 --- a/resources/views/cmd.blade.php +++ b/resources/views/cmd.blade.php @@ -373,12 +373,13 @@ $(document).ready(function(){ function bind() { - $('.cmd_enter').keypress(function(e){ + $('.cmd_enter').off('keypress').keypress(function(e){ if(e.which == 13){ // 检测回车键 //toggleArea($("#cmd_option").val()); submit($(this), "input"); + return false; } });