request->param(); $service = new ImageService(); $service->handleImage($params); $this->success('任务成功提交至队列'); } /** * 查询队列列表 * 统计文件对应的队列情况 */ public function get_queue_logs() { $params = $this->request->param('old_image_file', ''); $queue_logs = Db::name('queue_logs') ->where('old_image_file', $params) ->order('id desc') ->select(); $result = []; //初始化变量,避免未定义错误 foreach ($queue_logs as &$log) { $taskId = $log['id']; $statusCount = Db::name('image_task_log') ->field('status, COUNT(*) as count') ->where('task_id', $taskId) ->where('mod_rq', null) ->group('status') ->select(); $log['已完成数量'] = 0; $log['处理中数量'] = 0; $log['排队中的数量'] = 0; $log['失败数量'] = 0; foreach ($statusCount as $item) { switch ($item['status']) { case 0: $log['排队中的数量'] = $item['count']; break; case 1: $log['处理中数量'] = $item['count']; break; case 2: $log['已完成数量'] = $item['count']; break; case -1: $log['失败数量'] = $item['count']; break; } } // if ($log['排队中的数量'] >$log['已完成数量']) { // $result[] = $log; // } if ($log['排队中的数量']) { $result[] = $log; } // if ($log['处理中数量'] >= 0) { // $result[] = $log; // } } return json([ 'code' => 0, 'msg' => '查询成功', 'data' => $result, 'count' => count($result) ]); } /** * 查询总队列状态(统计当前处理的数据量) */ public function queueStats() { $statusList = Db::name('image_task_log') ->field('status, COUNT(*) as total') ->where('mod_rq', null) ->where('create_time', '>=', date('Y-m-d 00:00:00')) ->group('status') ->select(); $statusCount = []; foreach ($statusList as $item) { $statusCount[$item['status']] = $item['total']; } // 总数为所有状态和 $total = array_sum($statusCount); //获取队列当前状态 $statusText = Db::name('queue_logs')->order('id desc')->value('status'); return json([ 'code' => 0, 'msg' => '获取成功', 'data' => [ '总任务数' => $total, '待处理' => $statusCount[0] ?? 0, '处理中' => $statusCount[1] ?? 0, '成功' => $statusCount[2] ?? 0, '失败' => $statusCount[-1] ?? 0, '当前状态' => $statusText ] ]); } /** * 显示当前运行中的队列监听进程 */ public function viewQueueStatus() { $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); $redis->auth('123456'); $redis->select(15); $key = 'queues:imgtotxt'; // 判断 key 是否存在,避免报错 if (!$redis->exists($key)) { return json([ 'code' => 0, 'msg' => '查询成功,队列为空', 'count' => 0, 'tasks_preview' => [] ]); } $count = $redis->lLen($key); $list = $redis->lRange($key, 0, 9); // 解码 JSON 内容,确保每一项都有效 $parsed = array_filter(array_map(function ($item) { return json_decode($item, true); }, $list), function ($item) { return !is_null($item); }); return json([ 'code' => 0, 'msg' => '查询成功', 'count' => $count, 'tasks_preview' => $parsed ]); } /** * 清空队列并删除队列日志记录 */ public function stopQueueProcesses() { Db::name('image_task_log') ->where('log', '队列中') ->whereOr('status', 1) ->where('create_time', '>=', date('Y-m-d 00:00:00')) ->update([ 'status' => "-1", 'log' => '清空取消队列', 'mod_rq' => date('Y-m-d H:i:s') ]); Db::name('image_task_log') ->whereLike('log', '%处理中%') ->where('create_time', '>=', date('Y-m-d 00:00:00')) ->update([ 'status' => "-1", 'log' => '清空取消队列', 'mod_rq' => date('Y-m-d H:i:s') ]); $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); $redis->auth('123456'); $redis->select(15); $key_txttoimg = 'queues:txttoimg:reserved'; $key_txttotxt = 'queues:txttotxt:reserved'; $key_imgtotxt = 'queues:imgtotxt:reserved'; $key_imgtoimg = 'queues:imgtoimg:reserved'; // 清空 Redis 队列 $redis->del($key_txttoimg); $redis->del($key_txttotxt); $redis->del($key_imgtotxt); $redis->del($key_imgtoimg); return json([ 'code' => 0, 'msg' => '成功停止队列任务' ]); } public function getPreviewSubDirs() { $baseDir = rtrim(str_replace('\\', '/', ROOT_PATH), '/') . '/public/uploads/operate/ai/Preview'; $baseRelativePath = 'uploads/operate/ai/Preview'; if (!is_dir($baseDir)) { return json(['code' => 1, 'msg' => '目录不存在']); } // 包含 DB 时间戳扰动的缓存键 $version = $this->generateFlexibleDirectoryHash($baseDir); $cacheKey = 'preview_flexible_dirs_' . $version; $dirList = cache($cacheKey); if (!$dirList) { $dirList = $this->scanFlexibleDirectories($baseDir, $baseRelativePath); cache($cacheKey, $dirList, 3600); // 默认300 缓存 5 分钟(可调) } else { // 实时刷新 new_image_count(避免缓存值过期) foreach ($dirList as &$dir) { $dir['new_image_count'] = Db::name('text_to_image') ->where('status', 1) ->where('new_image_url', '<>', '') ->where('img_name', '<>', '') ->whereLike('old_image_url', $dir['old_image_url'] . '/%') ->count(); } } return json([ 'code' => 0, 'msg' => '获取成功', 'data' => $dirList ]); } private function scanFlexibleDirectories($baseDir, $baseRelativePath) { $dirs = []; $index = 1; $firstLevelDirs = glob($baseDir . '/*', GLOB_ONLYDIR); foreach ($firstLevelDirs as $level1Path) { $secondLevelDirs = glob($level1Path . '/*', GLOB_ONLYDIR); if ($secondLevelDirs) { foreach ($secondLevelDirs as $level2Path) { $dirs = array_merge($dirs, $this->processDir($level2Path, $baseDir, $baseRelativePath, $index)); } } else { $dirs = array_merge($dirs, $this->processDir($level1Path, $baseDir, $baseRelativePath, $index)); } } usort($dirs, function ($a, $b) { return $b['id'] - $a['id']; }); return $dirs; } private function processDir($fullPath, $baseDir, $baseRelativePath, &$index) { $result = []; $relativeDir = ltrim(str_replace($baseDir, '', $fullPath), '/'); $ctime = @filectime($fullPath) ?: time(); $imageFiles = glob($fullPath . '/*.{jpg,jpeg,png}', GLOB_BRACE); $originalImageCount = $imageFiles ? count($imageFiles) : 0; $img_count = Db::name('text_to_image') ->where('status', 1) ->where('new_image_url', '<>', '') ->where('img_name', '<>', '') ->whereLike('old_image_url', $baseRelativePath . '/' . $relativeDir . '/%') ->count(); $queueLog = Db::name('image_task_log') ->whereLike('file_name', $baseRelativePath . '/' . $relativeDir . '/%') ->whereLike('log', '%处理中%') ->order('id', 'desc') ->find(); if ($img_count === 0 && !$queueLog && $originalImageCount === 0) { return []; } $result[] = [ 'id' => $index++, 'name' => basename($fullPath), 'ctime' => $ctime, 'ctime_text' => date('Y-m-d H:i:s', $ctime), 'old_img_count' => $originalImageCount, 'new_image_count' => $img_count, 'old_image_url' => $baseRelativePath . '/' . $relativeDir, 'new_image_url' => '/uploads/operate/ai/dall-e/', 'queueLog_id' => $queueLog['id'] ?? '', 'queueLog_task_id' => $queueLog['task_id'] ?? '', 'queueLog_model_name' => $queueLog['model_name'] ?? '', 'queueLog_model_name_status' => $queueLog ? 1 : 0, ]; return $result; } private function generateFlexibleDirectoryHash($baseDir) { $hash = ''; $dirPaths = []; $firstDirs = glob($baseDir . '/*', GLOB_ONLYDIR); foreach ($firstDirs as $dir1) { $subDirs = glob($dir1 . '/*', GLOB_ONLYDIR); if ($subDirs) { foreach ($subDirs as $sub) { $dirPaths[] = $sub; $hash .= basename($dir1) . '/' . basename($sub) . filemtime($sub); } } else { $dirPaths[] = $dir1; $hash .= basename($dir1) . filemtime($dir1); } } $baseRelativePath = 'uploads/operate/ai/Preview'; $queueStatusBits = []; foreach ($dirPaths as $fullPath) { $relativeDir = ltrim(str_replace($baseDir, '', $fullPath), '/'); $fileNameLike = $baseRelativePath . '/' . $relativeDir . '/%'; // 查询是否存在任何“处理中”的记录 $logs = Db::name('image_task_log') ->whereLike('file_name', $fileNameLike) ->whereLike('log', '%处理中%') ->select(); // 转换为布尔状态再转成位标记(0 或 1) $queueStatusBits[] = count($logs) > 0 ? '1' : '0'; // 可选:调试打印 // echo "
路径:{$fileNameLike} => 状态:" . (count($logs) > 0 ? '有处理中' : '无') . "";
}
// 队列状态位图拼接
$queueStatusHash = implode('', $queueStatusBits); // 如:'01001'
$hash .= '_QS_' . md5($queueStatusHash); // 状态稳定扰动,无需 time()
return md5($hash);
}
/**
* 获取指定目录所有图片(完全实时版本)
*/
public function getPreviewimg()
{
$page = (int)$this->request->param('page', 1);
$limit = (int)$this->request->param('limit', 50);
$status = $this->request->param('status', '');
$status_name = $this->request->param('status_name', '');
$relativePath = $this->request->param('path', '');
$sys_id = $this->request->param('sys_id', '');
$basePath = ROOT_PATH . 'public/';
$fullPath = $basePath . $relativePath;
if (!is_dir($fullPath)) {
return json(['code' => 1, 'msg' => '原图目录不存在']);
}
// 构建缓存键与构建锁键(仅缓存文件系统信息),包含sys_id以确保不同sys_id的数据有各自的缓存
// 更新缓存键前缀,确保旧缓存失效
$hash = md5($relativePath . $sys_id);
$cacheKey = "previewimg_fileinfo_v2_{$hash}";
$lockKey = "previewimg_building_v2_{$hash}";
// $cacheExpire = 600; // 10分钟
$cacheExpire = 3600; // 60分钟
$cachedFileInfo = cache($cacheKey);
if (!$cachedFileInfo) {
// 防止缓存"惊群效应"
if (!cache($lockKey)) {
cache($lockKey, 1, 60); // 1分钟构建锁
// 获取所有图片文件信息
$allImages = glob($fullPath . '/*.{jpg,jpeg,png}', GLOB_BRACE);
$fileInfoMap = [];
foreach ($allImages as $imgPath) {
$relative = str_replace('\\', '/', trim(str_replace($basePath, '', $imgPath), '/'));
$info = @getimagesize($imgPath);
$fileInfoMap[$relative] = [
'width' => $info[0] ?? 0,
'height' => $info[1] ?? 0,
'size_kb' => round(filesize($imgPath) / 1024, 2),
'created_time' => date('Y-m-d H:i:s', filectime($imgPath))
];
}
// 构建缓存数据(仅文件系统信息)
$cachedFileInfo = [];
foreach (array_keys($fileInfoMap) as $path) {
$cachedFileInfo[] = [
'path' => $path,
'info' => $fileInfoMap[$path]
];
}
// 设置缓存 + 删除构建锁
cache($cacheKey, $cachedFileInfo, $cacheExpire);
cache($lockKey, null);
} else {
// 等待缓存生成
$waitTime = 0;
while (!$cachedFileInfo && $waitTime < 10) {
sleep(1);
$waitTime++;
$cachedFileInfo = cache($cacheKey);
}
if (!$cachedFileInfo) {
return json(['code' => 2, 'msg' => '系统正忙,请稍后重试']);
}
}
}
// 获取所有需要实时查询的路径
$paths = array_column($cachedFileInfo, 'path');
// 实时查询数据库状态信息(单次批量查询)
$dbQuery = Db::name('text_to_image')
->whereIn('old_image_url', $paths)
->field('id as img_id, old_image_url, new_image_url, custom_image_url,imgtoimg_url,taskId,chinese_description, english_description, img_name, status, status_name,sys_id');
// 仅在sys_id不为空时添加sys_id条件
if (!empty($sys_id)) {
$dbQuery->where('sys_id', $sys_id);
}
$dbRecords = $dbQuery->select();
// 实时查询队列状态(单次批量查询)
$queueRecords = Db::name('image_task_log')
->where('mod_rq', null)
->whereIn('file_name', $paths)
->field('file_name, log')
->select();
// 实时查询same_count(稍后按需查询)
// 构建映射关系
$processedMap = [];
foreach ($dbRecords as $item) {
$key = str_replace('\\', '/', trim($item['old_image_url'], '/'));
$processedMap[$key] = $item;
}
$queueMap = [];
foreach ($queueRecords as $q) {
$key = str_replace('\\', '/', trim($q['file_name'], '/'));
$queueMap[$key] = $q['log'];
}
// 合并数据
$mergedData = [];
foreach ($cachedFileInfo as $data) {
$path = $data['path'];
$item = $processedMap[$path] ?? [];
$mergedData[] = [
'path' => $path,
'item' => $item,
'info' => $data['info'],
'dbStatus' => isset($item['status']) ? (int)$item['status'] : 0,
'dbStatusName' => $item['status_name'] ?? '',
'isProcessed' => !empty($item['img_name']) && !empty($item['custom_image_url']),
'queueStatus' => $queueMap[$path] ?? ''
];
}
// 筛选状态字段
$filtered = array_filter($mergedData, function ($data) use ($status, $status_name) {
// 状态码筛选
if ($status !== '' && (int)$status !== $data['dbStatus']) return false;
// 状态名称筛选
if ($status_name !== '') {
if ($status_name === '未图生文') {
// 当状态名为"未图生文"时,匹配空值或null
if (!empty($data['dbStatusName'])) return false;
} elseif ($status_name === '未文生文') {
if ($data['dbStatusName'] !== '图生文') return false;
} elseif ($status_name === '未文生图') {
if ($data['dbStatusName'] !== '图生文') return false;
} elseif ($status_name === '未图生图') {
if ($data['dbStatusName'] !== '文生图') return false;
} else {
// 其他状态名需要精确匹配
if ($status_name !== $data['dbStatusName']) return false;
}
}
return true;
});
// 分页处理
$total = count($filtered);
$paged = array_slice(array_values($filtered), ($page - 1) * $limit, $limit);
// 实时查询当前页的same_count(优化性能)
$pagedPaths = array_column($paged, 'path');
$sameCountMap = [];
if ($pagedPaths) {
$sameCountMap = Db::name('text_to_image')
->whereIn('old_image_url', $pagedPaths)
// ->where('new_image_url', '<>', '')
->where('new_image_url', '<>', '')
->where('taskId', '<>', '')
->group('old_image_url')
->column('count(*) as cnt', 'old_image_url');
}
// 构建最终结果
$result = [];
foreach ($paged as $i => $data) {
$path = $data['path'];
$item = $data['item'];
$info = $data['info'];
$result[] = [
'id' => ($page - 1) * $limit + $i + 1,
'ids' => $item['img_id'],
'path' => $path,
// 实时数据
'status' => $data['dbStatus'],
'status_name' => $data['dbStatusName'],
'same_count' => $sameCountMap[$path] ?? 0,
'is_processed' => $data['isProcessed'] ? 1 : 0,
'queue_status' => $data['queueStatus'],
'taskId' => $item['taskId'] ?? '',
'sys_id' => $item['sys_id'] ?? '',
'new_image_url' => $item['new_image_url'] ?? '',
'custom_image_url' => $item['custom_image_url'] ?? '',
'imgtoimg_url' => $item['imgtoimg_url'] ?? '',
'chinese_description' => $item['chinese_description'] ?? '',
'english_description' => $item['english_description'] ?? '',
'img_name' => $item['img_name'] ?? '',
// 来自缓存
'width' => $info['width'],
'height' => $info['height'],
'created_time' => $info['created_time']
];
}
return json([
'code' => 0,
'msg' => '获取成功',
'data' => $result,
'total' => $total,
'page' => $page,
'limit' => $limit
]);
}
/**
* 通过服务器中获取对应目录
*/
public function getlsit()
{
// 获取前端传入的图片路径参数
$params = $this->request->param('path', '');
$sys_id = $this->request->param('sys_id', '');
// 查询数据库
$res = Db::name('text_to_image')
->field('id,chinese_description,english_description,new_image_url,custom_image_url,size,old_image_url,img_name,model,imgtoimg_url')
->where('old_image_url', $params)
->where('img_name', '<>', '')
->where('sys_id',$sys_id)
->order('id desc')
->select();
if($res){
return json(['code' => 0, 'msg' => '查询成功', 'data' => $res,'count'=>count($res)]);
}else{
return json(['code' => 0, 'msg' => '查询成功', 'data' => [],'count'=>0]);
}
}
/**
* 图片上传
*/
public function ImgUpload()
{
// 处理 CORS OPTIONS 预检请求
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
header('Access-Control-Max-Age: 86400');
exit(204);
}
// 实际请求必须返回 CORS 头
header('Access-Control-Allow-Origin: *');
// 获取上传的文件
$file = request()->file('image');
if ($file) {
// 生成日期格式的文件夹名 image_YYYYMMDD
$dateFolder = 'image_' . date('Ymd');
// 指定目标目录(包含日期文件夹)
$targetPath = ROOT_PATH . 'public' . DS . 'uploads' . DS . 'operate' . DS . 'ai' . DS . 'Preview' . DS . $dateFolder;
// 若目录不存在则创建
if (!is_dir($targetPath)) {
mkdir($targetPath, 0755, true);
}
// 获取原始文件名(或自定义新文件名)
$originalName = $file->getInfo('name'); // 原始文件名
$extension = pathinfo($originalName, PATHINFO_EXTENSION); // 文件扩展名
$newFileName = uniqid() . '.' . $extension; // 生成唯一文件名(避免冲突)
// 移动文件到指定目录,并验证大小/格式,同时指定自定义文件名
$info = $file->validate([
'size' => 10485760, // 最大10MB
'ext' => 'jpg,png'
])->move($targetPath, $newFileName); // 关键:手动指定文件名,避免自动生成日期目录
if ($info) {
// 直接拼接路径,不依赖 getSaveName() 的返回值
$imageUrl = '/uploads/operate/ai/Preview/' . $dateFolder . '/' . $newFileName;
return json(['code' => 0, 'msg' => '成功', 'data' => ['url' => $imageUrl]]);
} else {
$res = $file->getError();
return json(['code' => 1, 'msg' => '失败', 'data' => $res]);
}
}
return json(['code' => 1, 'msg' => '没有文件上传', 'data' => null]);
}
/**
* 打包图片(支持对象结构中的 path 字段)
*/
public function packImagess()
{
try {
$params = $this->request->post();
$paths = $params['paths'] ?? [];
if (empty($paths) || !is_array($paths)) {
return json(['code' => 1, 'msg' => '路径参数不能为空或格式不正确']);
}
// 提取所有合法的路径(支持字符串或对象中带 path 字段)
$validPaths = [];
foreach ($paths as $item) {
if (is_string($item)) {
$validPaths[] = $item;
} elseif (is_array($item) && isset($item['path']) && is_string($item['path'])) {
$validPaths[] = $item['path'];
}
}
if (empty($validPaths)) {
return json(['code' => 1, 'msg' => '没有有效的图片路径']);
}
// 设置基本路径和 zip 目录
$basePath = ROOT_PATH . 'public/';
$zipDir = $basePath . 'uploads/operate/ai/zip/';
if (!is_dir($zipDir)) {
mkdir($zipDir, 0755, true);
}
// 生成压缩文件路径
$fileName = 'images_' . date('Ymd_His') . '.zip';
$zipPath = $zipDir . $fileName;
$zip = new \ZipArchive();
if ($zip->open($zipPath, \ZipArchive::CREATE) !== TRUE) {
return json(['code' => 1, 'msg' => '无法创建压缩包']);
}
$addCount = 0;
foreach ($validPaths as $relativePath) {
$relativePath = ltrim($relativePath, '/'); // 去除前导斜杠
$fullPath = $basePath . $relativePath;
if (file_exists($fullPath)) {
// 使用 basename 作为压缩包内的文件名(不保留路径结构)
$zip->addFile($fullPath, basename($fullPath));
$addCount++;
}
}
$zip->close();
if ($addCount === 0) {
@unlink($zipPath);
return json(['code' => 1, 'msg' => '未找到有效图片文件,未生成压缩包']);
}
$downloadUrl = request()->domain() . '/uploads/operate/ai/zip/' . $fileName;
return json([
'code' => 0,
'msg' => '打包成功',
'download_url' => $downloadUrl
]);
} catch (\Exception $e) {
return json([
'code' => 1,
'msg' => '异常错误:' . $e->getMessage()
]);
}
}
/**
* 获取所有模版列表,并返回当前使用模版 ID
*/
public function TemplateList()
{
$params = Request::instance()->param();
$path = $params['path'] ?? '';
if (empty($path)) {
return json([
'code' => 400,
'msg' => '缺少 path 参数',
'data' => []
]);
}
// 先查询是否已有数据
$list = Db::name("template")
->where('path', $path)
->order('ids', 'desc')
->select();
// 如果有记录,直接返回
if ($list) {
return json([
'code' => 0,
'msg' => '模版列表',
'data' => [
'list' => $list,
'usedId' => Db::name("template")->where('path', $path)->where('ids', 1)->value('id')
]
]);
}
$res_ids = Db::name("template")->where('ids', 99)->find();
// 否则插入默认模板
$default = [
'path' => $path,
'english_content' => '',
'content' => $res_ids['content'],
'width' => '1024',
'height' => '1303',
'ids' => 1
];
$insertId = Db::name("template")->insertGetId($default);
if ($insertId) {
$list = Db::name("template")
->where('path', $path)
->order('ids', 'desc')
->select();
return json([
'code' => 0,
'msg' => '模版列表(已创建默认)',
'data' => [
'list' => $list,
'usedId' => Db::name("template")->where('path', $path)->where('ids', 1)->value('id')
]
]);
}
// 插入失败
return json([
'code' => 500,
'msg' => '创建默认模版失败',
'data' => []
]);
}
/**
* 查询使用模版
*/
public function Template_ids(){
$params = Request::instance()->param();
$Template = Db::name("template")->where('path',$params['path'])->where('ids',1)->find();
return json([
'code' => 0,
'msg' => '模版',
'data' => $Template
]);
}
/**
* 查询模版
*/
public function Template()
{
$id = Request::instance()->param('id');
if (!$id) {
return json(['code' => 1, 'msg' => '参数错误']);
}
$template = Db::name("template")->where('id', $id)->find();
return json([
'code' => 0,
'msg' => '模版详情',
'data' => $template
]);
}
/**
* 更新模版
*/
public function updatetemplate()
{
if (!Request::instance()->isPost()) {
return json(['code' => 1, 'msg' => '非法请求']);
}
$params = Request::instance()->post();
if (empty($params['textareaContent']) || empty($params['width']) || empty($params['height'])) {
return json(['code' => 1, 'msg' => '参数缺失']);
}
$data = [
'path' => $params['path'],
'english_content' => $params['english_content'],
'content' => $params['textareaContent'],
'width' => $params['width'],
'height' => $params['height'],
];
if (!empty($params['id'])) {
// 更新已有模版
Db::name("template")->where('id', $params['id'])->update($data);
} else {
// 新增模版,默认设为备用
$data['ids'] = 0;
Db::name("template")->insert($data);
}
return json(['code' => 0, 'msg' => '保存成功']);
}
/**
* 设置默认使用模版
*/
public function setActiveTemplate()
{
$id = Request::instance()->param('id');
$params = Request::instance()->param();
if (!$id) {
return json(['code' => 1, 'msg' => '参数错误']);
}
Db::name("template")->where('path',$params['path'])->where('ids', 1)->update(['ids' => 0]); // 清除当前使用
Db::name("template")->where('path',$params['path'])->where('id', $id)->update(['ids' => 1]); // 设置新的使用模版
return json(['code' => 0, 'msg' => '模版已设为当前使用']);
}
public function txttoimg_moxing()
{
// 获取所有模型数据
$list = Db::name("moxing")->order('id asc')->select();
// 初始化分类数组
$classifiedData = [
'wenshengwen' => [], // 文生文模型 (txttotxt)
'tushengwen' => [], // 图生文模型 (imgtotxt)
'wenshengtu' => [] // 文生图模型 (txttoimg)
];
// 当前使用的模型ID
$usedIds = [
'wenshengwen' => null,
'tushengwen' => null,
'wenshengtu' => null
];
// 分类处理数据
foreach ($list as $model) {
// 文生文模型 (txttotxt)
if (!empty($model['txttotxt'])) {
$classifiedData['wenshengwen'][] = [
'id' => $model['id'],
'txttotxt' => $model['txttotxt'],
'txttotxt_val' => $model['txttotxt_val']
];
if ($model['txttotxt_val'] == 1) {
$usedIds['wenshengwen'] = $model['id'];
}
}
// 图生文模型 (imgtotxt)
if (!empty($model['imgtotxt'])) {
$classifiedData['tushengwen'][] = [
'id' => $model['id'],
'imgtotxt' => $model['imgtotxt'],
'imgtotxt_val' => $model['imgtotxt_val']
];
if ($model['imgtotxt_val'] == 1) {
$usedIds['tushengwen'] = $model['id'];
}
}
// 文生图模型 (txttoimg)
if (!empty($model['txttoimg'])) {
$classifiedData['wenshengtu'][] = [
'id' => $model['id'],
'txttoimg' => $model['txttoimg'],
'txttoimg_val' => $model['txttoimg_val']
];
if ($model['txttoimg_val'] == 1) {
$usedIds['wenshengtu'] = $model['id'];
}
}
}
return json([
'code' => 0,
'msg' => '模型分类列表',
'data' => [
'models' => $classifiedData,
'used_ids' => $usedIds
]
]);
}
public function txttoimg_update()
{
// 获取并验证参数
$id = input('id/d', 0); // 强制转换为整数
$type = input('type/s', ''); // 强制转换为字符串
if ($id <= 0) {
return json(['code' => 1, 'msg' => '无效的模型ID']);
}
if (!in_array($type, ['tushengwen', 'wenshengwen', 'wenshengtu'])) {
return json(['code' => 1, 'msg' => '无效的模型类型']);
}
// 定义模型类型与字段的映射关系
$fieldMap = [
'tushengwen' => 'imgtotxt_val',
'wenshengwen' => 'txttotxt_val',
'wenshengtu' => 'txttoimg_val'
];
$field = $fieldMap[$type];
try {
// 开启事务确保数据一致性
Db::startTrans();
// 1. 重置该类型下所有模型的激活状态
Db::name("moxing")
->where($field, 1)
->update([$field => 0]);
// 2. 设置指定模型为激活状态
$result = Db::name("moxing")
->where('id', $id)
->update([$field => 1]);
Db::commit();
if ($result) {
return json([
'code' => 0,
'msg' => '设置成功',
'data' => [
'id' => $id,
'type' => $type
]
]);
}
return json(['code' => 1, 'msg' => '设置失败,模型不存在或未变更']);
} catch (\Exception $e) {
Db::rollback();
return json([
'code' => 2,
'msg' => '设置失败: ' . $e->getMessage()
]);
}
}
//获取原文件夹数据
public function getPreviewFolders()
{
$baseDir = rtrim(str_replace('\\', '/', ROOT_PATH), '/') . '/public/uploads/operate/ai/Preview';
$cacheDir = ROOT_PATH . '/runtime/cache/';
$cacheFile = $cacheDir . 'folder_list.json';
$cacheTTL = 86400; // 缓存有效期:1天
$forceRefresh = input('get.refresh', 0); // 是否强制刷新缓存
if (!is_dir($baseDir)) {
return json([
'code' => 404,
'msg' => '目录不存在',
'data' => []
]);
}
$folders = [];
$useCache = file_exists($cacheFile) && (time() - filemtime($cacheFile)) < $cacheTTL;
if ($useCache && !$forceRefresh) {
$folders = json_decode(file_get_contents($cacheFile), true);
} else {
$folders = [];
// 使用高效方式递归遍历所有子目录
$directory = new RecursiveDirectoryIterator($baseDir, RecursiveDirectoryIterator::SKIP_DOTS);
$iterator = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $file) {
if ($file->isDir()) {
$fullPath = str_replace('\\', '/', $file->getPathname());
$relativePath = str_replace(ROOT_PATH . 'public/', '', $fullPath);
$folders[] = [
'name' => basename($fullPath),
'path' => $relativePath,
'full_path' => $fullPath
];
}
}
// 写入缓存
if (!is_dir($cacheDir)) {
mkdir($cacheDir, 0777, true);
}
file_put_contents($cacheFile, json_encode($folders));
}
return json([
'code' => 0,
'msg' => '获取所有预览文件夹成功',
'data' => [
'folders' => $folders,
'total' => count($folders),
'from_cache' => $useCache && !$forceRefresh
]
]);
}
/**
* 修改
*/
public function getTab()
{
if (!$this->request->isPost()) {
return json(['code' => 400, 'msg' => '请求方式错误'], 400);
}
$param = $this->request->post([
'id',
'chinese_description',
'english_description',
'img_name'
]);
if (empty($param['id'])) {
return json(['code' => 422, 'msg' => '缺少必要参数'], 422);
}
$result = Db::name('text_to_image')
->where('id', $param['id'])
->update([
'chinese_description' => $param['chinese_description'] ?? '',
'english_description' => $param['english_description'] ?? '',
'img_name' => $param['img_name'] ?? ''
]);
if ($result !== false) {
$this->success('修改成功');
} else {
$this->success('修改失败');
}
}
/**
*
*/
public function getList()
{
$params = Request::instance()->param();
// 分页参数(默认第1页,每页10条)
$page = isset($params['page']) ? max((int)$params['page'], 1) : 1;
$limit = isset($params['limit']) ? max((int)$params['limit'], 10) : 10;
// 搜索关键词
$search = isset($params['search']) ? trim($params['search']) : '';
$folder = isset($params['folder']) ? trim($params['folder']) : '';
$where = [];
$wheres = [];
if (isset($search)) {
$where['id|img_name|chinese_description|english_description'] = ['like', '%'. $search . '%'];
}
if (isset($folder)) {
$wheres['old_image_url'] = ['like', '%'. $folder . '%'];
}
$count = Db::name('text_to_image')
->where('new_image_url', '<>', '')
->where($where)
->where($wheres)
->where('img_name', '<>', '')
->select();
$list = Db::name('text_to_image')
->where('new_image_url', '<>', '')
->where('img_name', '<>', '')
->where($where)
->where($wheres)
->order('update_time desc')
->page($page, $limit)
->select();
$folder = Db::name('text_to_image')
->field('old_image_url')
->where('new_image_url', '<>', '')
->where('img_name', '<>', '')
->order('update_time desc')
->select();
// 处理路径并去重
$processedFolders = [];
foreach ($folder as $item) {
$url = $item['old_image_url'];
$lastSlashPos = strrpos($url, '/');
if ($lastSlashPos !== false) {
$folderPath = substr($url, 0, $lastSlashPos);
if (!in_array($folderPath, $processedFolders)) {
$processedFolders[] = $folderPath;
}
}
}
$this->success('获取成功', [
'total' => count($count),
'list' => $list,
'folder' => $processedFolders
]);
}
}