78 lines
3.0 KiB
PHP
78 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Images;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\Articles as ModelArticles;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use App\Services\TomTool\Telegram\Slave as TeleSlave;
|
|
use App\Services\TomTool\Flow;
|
|
|
|
class GetController extends Controller
|
|
{
|
|
private $iStartNum = 0; // 改个名字,更有语义
|
|
private $iMaxTry = 5; // 最大尝试次数
|
|
|
|
public function urlbyNum(Request $oRequest)
|
|
{
|
|
$oFlow = Flow::start("urlByNum");
|
|
|
|
$sAuthorCode = $oRequest->input("sAuthorCode");
|
|
$iNum = (int)$oRequest->input("iNum", 1);
|
|
|
|
$this->iStartNum = $iNum;
|
|
|
|
return $this->findValidUrl($sAuthorCode, $iNum, $oFlow);
|
|
}
|
|
|
|
private function findValidUrl($sAuthorCode, $iNum, $oFlow)
|
|
{
|
|
try {
|
|
$oQuery = ModelArticles::where("sAuthorCode", $sAuthorCode)->orderBy("id", "asc");
|
|
$iTotal = $oQuery->count();
|
|
|
|
if ($iTotal === 0) throw new \Exception("作者 [{$sAuthorCode}] 旗下无任何记录");
|
|
|
|
// 1. 计算当前查库的偏移量,用 % 取模是最稳的,或者用 if 但不要改 iNum 本身
|
|
$iOffset = ($iNum - 1) % $iTotal;
|
|
$iTrueNum = $iOffset + 1; // 真正对应数据库的第几条
|
|
|
|
$oArticle = $oQuery->skip($iOffset)->first();
|
|
|
|
// 2. 拼文件名
|
|
$aThumbUse = $oArticle->aOpt["thumb_use"] ?? ["b"];
|
|
$sThumbUse = $aThumbUse[array_rand($aThumbUse)];
|
|
$sFileName = $oArticle->sCode . '-1-' . 'thumb_' . $sThumbUse . '.jpg';
|
|
|
|
// 3. 检查文件
|
|
if (!Storage::disk('upload_images')->exists($sFileName)) {
|
|
// 报警
|
|
TeleSlave::warn()->enableSlaveQueue(false)->enableAsync(false)->send("findValidUrl:图片文件不存在:{$sFileName}");
|
|
|
|
// 💡 这里的出口判定最重要!
|
|
// 如果尝试次数已经超过了最大限制,或者已经把库里所有的记录都扫了一遍还没找到
|
|
if ($iNum >= ($this->iStartNum + $this->iMaxTry) || $iNum >= ($this->iStartNum + $iTotal)) {
|
|
throw new \Exception("该作者连续查找多张图片均不存在,已停止尝试");
|
|
}
|
|
|
|
// 递归:iNum 必须一直往上涨,不能回头!
|
|
return $this->findValidUrl($sAuthorCode, $iNum + 1, $oFlow);
|
|
}
|
|
|
|
// 4. 成功
|
|
$aData = [
|
|
"sFileUrl" => Storage::disk('upload_images')->url($sFileName),
|
|
"iTrueNum" => $iTrueNum,
|
|
];
|
|
|
|
return response()->json($oFlow->setDataA($aData)->done()->toArray());
|
|
|
|
} catch (\Exception $e) {
|
|
$sMsg = $e->getMessage();
|
|
TeleSlave::warn()->enableSlaveQueue(false)->enableAsync(false)->send($sMsg);
|
|
return response()->json($oFlow->fail($sMsg)->toArray());
|
|
}
|
|
}
|
|
}
|