'黄牛群', '高山牛肉' => '高山黄牛群', '牦牛肉' => '高原牦牛群', '羊肉' => '羊群', '黄羊肉' => '黄羊群', '黑山羊' => '黑山羊群', '生鲜猪肉' => '生态猪群', '散养土鸡' => '散养土鸡群', '麻鸭' => '绿草水塘边的麻鸭群', '鹅肉' => '农家大白鹅群', '鸽子肉' => '白色信鸽群', '驴肉' => '健康驴群', '马肉' => '草原骏马群' ]; $eggs = ['鲜土鸡蛋', '土鸭蛋', '土鹅蛋', '鹌鹑蛋']; $processedMeat = ['腊肉腊肠', '毛肚黄喉', '牛蹄筋', '牛杂', '羊肉卷']; /** * 获取 PDO 连接 (支持自动重连) * * @return PDO */ function getPdo() { static $pdo = null; try { if ($pdo) { $pdo->query("SELECT 1"); return $pdo; } } catch (\Exception $e) { $pdo = null; } $pdo = new PDO('mysql:host=127.0.0.1;port=3307;dbname=fenleixinxi;charset=utf8mb4', 'fenleixinxi', 'fenleixinxi', [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC ]); return $pdo; } /** * 构建中文 Prompt * * @param array $cat 分类数据 * @return string */ function buildChinesePrompt($cat) { global $liveAnimals, $eggs, $processedMeat; $name = $cat['name']; $kind = $cat['kind']; $noText = ',画面绝对干净,1:1正方形,单反写实摄影,不要出现任何文字、不要水印、不要LOGO、不要AI标识、不要边框角标'; if ($kind === 'farm') { return "一只质朴的手工编制竹筐装满新鲜刚采摘的{$name},放置在农田果园的松软泥土田埂上,背景是自然生长的{$name}植株田园风光,自然柔和日光,通透清新质感,特写实拍{$noText}"; } if (isset($liveAnimals[$name])) { $animal = $liveAnimals[$name]; return "在蓝天白云和青山绿草的广阔草场牧场上,一群自然站立吃草活动的健康{$animal},自然阳光照射,生态农场实景,开阔全景构图,真实单反写实摄影{$noText}"; } if (in_array($name, $eggs)) { return "阳光明媚的农家小院质朴木桌上,一只编织竹筐装满新鲜农家{$name},点缀有干燥麦秆和新鲜绿叶,温馨田园氛围,特写写实摄影{$noText}"; } if ($name === '鲜活水产') { return "清澈透亮的自然生态鱼塘水池,多种活鱼水产在水草与芦苇间自由游弋,波光粼粼,自然日光,真实生态摄影{$noText}"; } if (in_array($name, $processedMeat)) { return "农家质朴的木质台面上整齐摆放的优质鲜美{$name}实物,农家厨房市集生活氛围,自然日光,特写微距写实单反摄影{$noText}"; } return "一只质朴的手工编制竹筐装满新鲜的{$name},放置在农田泥土上,背景是自然生长植株,自然柔和日光,通透清新质感,特写实拍{$noText}"; } /** * 提交单个生图任务到百炼 (带单次重试) * * @param string $prompt 提示词 * @param string $apiKey API密钥 * @param string $submitUrl 提交地址 * @return string|false */ function submitWanxTask($prompt, $apiKey, $submitUrl) { $body = [ 'model' => 'wanx2.1-t2i-turbo', 'input' => [ 'prompt' => $prompt ], 'parameters' => [ 'size' => '1024*1024', 'n' => 1 ] ]; for ($attempt = 0; $attempt < 2; $attempt++) { $ch = curl_init($submitUrl); curl_setopt_array($ch, [ CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode($body, JSON_UNESCAPED_UNICODE), CURLOPT_HTTPHEADER => [ 'Authorization: Bearer ' . $apiKey, 'Content-Type: application/json', 'X-DashScope-Async: enable' ], CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 25 ]); $resp = curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($code === 200) { $json = json_decode($resp, true); $taskId = $json['output']['task_id'] ?? false; if ($taskId) return $taskId; } usleep(300000); // 重试前等待 300ms } return false; } /** * 轮询任务状态 * * @param string $taskId 任务ID * @param string $apiKey API密钥 * @param string $taskBaseUrl 任务基础URL * @return string|false 图片URL */ function pollWanxTask($taskId, $apiKey, $taskBaseUrl) { $queryUrl = $taskBaseUrl . $taskId; for ($i = 0; $i < 40; $i++) { sleep(2); $ch = curl_init($queryUrl); curl_setopt_array($ch, [ CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . $apiKey], CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 15 ]); $resp = curl_exec($ch); curl_close($ch); $json = json_decode($resp, true); $status = $json['output']['task_status'] ?? 'UNKNOWN'; if ($status === 'SUCCEEDED') { return $json['output']['results'][0]['url'] ?? false; } elseif ($status === 'FAILED' || $status === 'CANCELED') { return false; } } return false; } /** * 下载图片落盘到本地 * * @param string $url 图片网络地址 * @param string $dest 本地路径 * @return bool */ function downloadFile($url, $dest) { $fp = fopen($dest, 'wb'); if (!$fp) return false; $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_FILE => $fp, CURLOPT_TIMEOUT => 60, CURLOPT_FOLLOWLOCATION => true ]); $ok = curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); fclose($fp); return ($ok && $code === 200 && filesize($dest) > 50000); } // ======================== 主执行循环 ======================== echo "========================================================\n"; echo " 阿里百炼 (通义万相) 全量分类配图与 OSS 入库流水线 (续跑版)\n"; echo "========================================================\n"; $phpBin = 'D:\Softwares\phpstudy_pro\Extensions\php\php7.4.3nts\php.exe'; $batchSize = 8; $round = 1; while (true) { $pdo = getPdo(); $stmt = $pdo->query(" SELECT c.id, c.pid, c.name, c.kind FROM fa_fc_category c LEFT JOIN fa_attachment a ON a.storage = 'alioss' AND a.url = c.image AND c.image != '' WHERE c.pid > 0 AND (c.image = '' OR a.id IS NULL) ORDER BY c.pid ASC, c.sort DESC, c.id ASC "); $pending = $stmt->fetchAll(PDO::FETCH_ASSOC); $totalPending = count($pending); if ($totalPending === 0) { echo "\n🎉 恭喜!所有二级分类已全部完成配图并入库阿里云 OSS!\n"; break; } echo sprintf("\n--- [轮次 %d] 剩余待处理: %d 项,本次选取前 %d 项 ---\n", $round++, $totalPending, min($batchSize, $totalPending)); $currentBatch = array_slice($pending, 0, $batchSize); // 1. 并发提交生图任务 $tasks = []; foreach ($currentBatch as $cat) { $prompt = buildChinesePrompt($cat); echo " 提交 #{$cat['id']} {$cat['name']}..."; $taskId = submitWanxTask($prompt, $apiKey, $submitUrl); if ($taskId) { echo " [OK: $taskId]\n"; $tasks[] = [ 'cat' => $cat, 'task_id' => $taskId ]; } else { echo " [FAIL]\n"; } usleep(200000); // 200ms 间隔防突发 } // 2. 轮询并下载图片 $downloadedCount = 0; foreach ($tasks as $item) { $cat = $item['cat']; $taskId = $item['task_id']; echo " 轮询 #{$cat['id']} {$cat['name']}..."; $imgUrl = pollWanxTask($taskId, $apiKey, $taskBaseUrl); if ($imgUrl) { $destFile = $downloadDir . '/' . $cat['id'] . '.jpg'; if (downloadFile($imgUrl, $destFile)) { echo " [下载成功: " . round(filesize($destFile) / 1024) . "KB]\n"; $downloadedCount++; } else { echo " [下载失败]\n"; } } else { echo " [生图超时或失败]\n"; } } // 3. 本批次如果有新下载图片,立刻触发 think fc:category-images 入库 OSS if ($downloadedCount > 0) { echo " ==> 触发 think fc:category-images 推送阿里云 OSS 并写入附件表...\n"; $output = []; $returnVar = 0; exec("\"$phpBin\" think fc:category-images", $output, $returnVar); foreach ($output as $line) { if (strpos($line, '完成') !== false || strpos($line, '校验') !== false) { echo " $line\n"; } } } sleep(1); } echo "\n流水线全流程执行结束!\n";