content = $sContent; $oModelCacheTg->group_code = $sGroupCode; $r = $oModelCacheTg->save(); if (!$r) { return $oFlow->fail("add失败?"); } $r = ModelCacheTgTime::updateOrCreate( ['group_code' => $sGroupCode], // 查找条件:有没有这个组? ['time' => time()] // 更新或创建的数据:就改这个时间! ); if (!$r->exists) { return $oFlow->fail("cache写入成功但time更新失败?"); } return $oFlow->done(); } public static function cron_saveToSchema() { $sCronMsg = ''; $sCronMsg .= "\n\nCacheTg::saveToSchema() - 开始"; $bHaveTime = ModelCacheTgTime::where("time", ">", 0)->exists(); if (!$bHaveTime) { $sCronMsg .= "\n - 没有活跃time正常跳出"; return $sCronMsg; } $iTimeNow = time(); $oModelCacheTgTime = ModelCacheTgTime::where("time", ">", 0)->first(); $iTimeDiff = $iTimeNow - $oModelCacheTgTime->time; if ($iTimeDiff < $oModelCacheTgTime->delay) { $sCronMsg .= "\n - time活跃但未到delay时间"; return $sCronMsg; } $oFlow = (new self())->saveToSchema($oModelCacheTgTime->group_code); if ($oFlow->isFail()) { $sCronMsg .= "\n - ".$oFlow->toJson(); return $sCronMsg; } $oModelCacheTgTime->time = 0; $oModelCacheTgTime->save(); $sCronMsg .= "\n - ok"; return $sCronMsg; } public function saveToSchema($sGroupCode) { $oFlow = Flow::start(); $aSchema = []; $aSchema["type"] = "cache_tg"; // $aSchema["group"] = $sGroupCode; $aSchema["data"] = []; $oModelCacheTg = ModelCacheTg::where("status", 1)->where("group_code", $sGroupCode)->get(); if ($oModelCacheTg->isEmpty()) { return $oFlow->done(); } $sUlid = (string) Str::ulid(); $sCode = $sGroupCode."-".$sUlid; $sBotToken = ModelTelegramKey::botTokenByCode($sGroupCode); // $sDocumentRoot = $_SERVER['DOCUMENT_ROOT']; //// test // $sBotToken = ModelTelegramKey::botTokenByCode("test"); //// test end foreach ($oModelCacheTg as $oModelCacheTgRow) { $sCacheContent = $oModelCacheTgRow->content; $aCacheContent = json_decode($sCacheContent, true); if (isset($aCacheContent["message"]["photo"])) { $aSchemaRow = []; $aSchemaRow["type"] = "photo"; $aSchemaRow["photo_fullSizeKey"] = 0; $aSchemaRow["photo"] = []; // $aSchemaRow["title"] = "xxx"; 意思一下,这么扩展 $aTgPhoto = $aCacheContent["message"]["photo"]; $iFullSize = 0; foreach ($aTgPhoto as $k => $aTgPhotoRow) { $aPhotoRow = $aTgPhotoRow; $sFileId = $aTgPhotoRow["file_id"]; if ($aTgPhotoRow["file_size"] > $iFullSize) { $iFullSize = $aTgPhotoRow["file_size"]; $aSchemaRow["photo_fullSizeKey"] = $k; } $oResponse = Http::get("https://api.telegram.org/bot{$sBotToken}/getFile", [ 'file_id' => $sFileId ]); if (!$oResponse->successful()) { throw new \Exception("获取路径失败:" . $oResponse->body()); } $sFileName = $oResponse->json('result.file_path'); $sDownloadUrl = "https://api.telegram.org/file/bot{$sBotToken}/{$sFileName}"; $sFileContents = Http::get($sDownloadUrl)->body(); $sFileContents = Http::get($sDownloadUrl) ->throw() ->body(); $sFileNameBase = basename($sFileName); $sFileSaveName = $sCode."-".$sFileNameBase; $sLocalPath = public_path()."/upload/" . $sFileSaveName; $b = File::put($sLocalPath, $sFileContents); if (!$b) { throw new \Exception("文件保存失败:" . $sLocalPath); } $aPhotoRow["file_path"] = $sLocalPath; unset($aPhotoRow["file_id"]); unset($aPhotoRow["file_unique_id"]); $aSchemaRow["photo"][] = $aPhotoRow; } $aSchema["data"][] = $aSchemaRow; } else { // 不在预设里的,先不管 } } $oModelArticleSchema = new ModelArticleSchema(); $oModelArticleSchema->code = $sCode; $oModelArticleSchema->schema = json_encode($aSchema); $oModelArticleSchema->group_code = $sGroupCode; $oModelArticleSchema->save(); $oModelArticleSchema->_opt([ "thumb_use" => ['b','c'] ]); ServiceArticleCmd::startWithO($oModelArticleSchema)->syncOpen()->save("new"); $oModelCacheTg->each->update(['status' => 0, 'code' => $sCode]); // $oModelCacheTg->each->update(['status' => 1, 'code' => $sCode]); // ! test return $oFlow->done(); } }