HomeController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. <?php
  2. namespace App\Http\Controllers;
  3. /*
  4. * 用户认证控制器
  5. */
  6. use App\Models\SiteAlbum;
  7. use App\Models\SiteAlbumFolder;
  8. use App\Models\SiteAlbumLog;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\Auth;
  11. use Illuminate\Support\Facades\Validator;
  12. use ZipArchive;
  13. use Illuminate\Support\Facades\Session;
  14. use Illuminate\Support\Str;
  15. class HomeController extends Controller
  16. {
  17. /*
  18. * 首页
  19. */
  20. public function index(Request $request)
  21. {
  22. return view('index',['foldersTree' => $this->foldersTree]);
  23. }
  24. public function main(Request $request)
  25. {
  26. return view('main',['foldersTree' => $this->foldersTree]);
  27. }
  28. /*
  29. * 分类列表
  30. */
  31. public function gallery(Request $request)
  32. {
  33. $search = $request->input('search', '');
  34. if ($search) {
  35. $search = trim($search);
  36. $search = '%' . $search . '%';
  37. $albums = SiteAlbum::where('model', 'like', $search)->orderBy('id', 'desc')->get();
  38. $title = '搜索结果';
  39. $folder_id = 0;
  40. $father_id = 0;
  41. if ($albums) {
  42. $albums = $albums->toArray();
  43. }
  44. } else {
  45. $folder_id = $request->input('fid',$this->getFoldersTreeFirstId());
  46. $father_id = $this->getFoldersTreeFatherId($folder_id);
  47. $folder = SiteAlbumFolder::find($folder_id);
  48. $albums = SiteAlbum::where('folder_id', $folder_id)->orderBy('id', 'desc')->get();
  49. $albums = $albums->toArray();
  50. $title = $folder->title;
  51. }
  52. foreach ($albums as $key => $album) {
  53. $cover = json_decode($album['cover']);
  54. foreach ($cover as $k => $v) {
  55. if (strpos($v, 'http') === false && strpos($v, 'https') === false) {
  56. unset($cover[$k]);
  57. }
  58. }
  59. $cover = empty($cover) ? ['/static/images/noimg.jpg'] : $cover;
  60. $albums[$key]['cover'] = $cover;
  61. }
  62. // 面包屑导航
  63. $breadcrumb = getBreadcrumb($folder_id, $this->foldersTree);
  64. $breadcrumb = implode(' / ', $breadcrumb);
  65. return view('gallery',
  66. [
  67. 'foldersTree' => $this->foldersTree,
  68. 'folderName'=> $title,
  69. 'folder_id' => $folder_id,
  70. 'father_id' => $father_id,
  71. 'albums' => $albums,
  72. 'breadcrumb' => $breadcrumb,
  73. ]
  74. );
  75. }
  76. public function detail(Request $request) {
  77. $id = $request->input('id',0);
  78. $album = SiteAlbum::find($id);
  79. $bumFolder = SiteAlbumFolder::where('id', $album->folder_id)->first();
  80. $showTabs = [];
  81. if ($bumFolder) {
  82. $row = json_decode($bumFolder->show_tabs);
  83. $row[7] = '7';
  84. foreach ($row as $key => $value) {
  85. $column = '';
  86. $title = '';
  87. switch ($value) {
  88. case '0':
  89. $column = 'cover';
  90. $title = '主图';
  91. break;
  92. case '1':
  93. $column = 'en_detail';
  94. $title = '英文详情';
  95. break;
  96. case '2':
  97. $column = 'cn_detail';
  98. $title = '中文详情';
  99. break;
  100. case '3':
  101. $column = 'video';
  102. $title = '视频';
  103. break;
  104. case '4':
  105. $column = 'poster';
  106. $title = '海报';
  107. break;
  108. case '5':
  109. $column = 'cert';
  110. $title = '证书';
  111. break;
  112. case '6':
  113. $column = 'pdf';
  114. $title = 'PDF';
  115. break;
  116. case '7':
  117. $column = 'parameters';
  118. $title = '属性';
  119. break;
  120. }
  121. $content = json_decode($album[$column], true);
  122. if (empty($content) == false) {
  123. $showTabs[$key] = ['value'=>$value,'column'=>$column, 'title'=>$title];
  124. }
  125. }
  126. }
  127. // dd(json_decode($album['parameters'], true));
  128. return view('gallery-detail',[
  129. 'foldersTree' => $this->foldersTree,
  130. 'album' => $album->toArray(),
  131. 'showTabs' => $showTabs
  132. ]);
  133. }
  134. public function updateLog(Request $request)
  135. {
  136. $losgs = SiteAlbumLog::getFormattedLogs();
  137. return view('update_log',[
  138. 'foldersTree' => $this->foldersTree,
  139. 'logs' => $losgs
  140. ]);
  141. }
  142. /*
  143. * 下载全部
  144. */
  145. public function downloadAll(Request $request)
  146. {
  147. Session::put('downloadAllStatus', '1');
  148. $id = $request->input('id', 0);
  149. $tab = $request->input('tab', 'cover');
  150. $album = SiteAlbum::find($id);
  151. if ($album) {
  152. $album = $album->toArray();
  153. if (isset($album[$tab])) {
  154. $files = json_decode($album[$tab]);
  155. $fileUrls = [];
  156. // 获取所有图片的 URL
  157. foreach ($files as $key => $value) {
  158. $fileUrl = ossUrl($value); // 获取完整的图片 URL
  159. $fileUrls[] = $fileUrl; // 将图片 URL 存入数组
  160. }
  161. // 创建一个临时 ZIP 文件
  162. $zipFileName = 'album_' . $id . '_' . $tab . '.zip';
  163. $zipPath = storage_path('app/' . $zipFileName);
  164. $zip = new ZipArchive;
  165. if ($zip->open($zipPath, ZipArchive::CREATE) === TRUE) {
  166. foreach ($fileUrls as $fileUrl) {
  167. $fileContent = file_get_contents($fileUrl); // 下载图片内容
  168. $fileName = basename($fileUrl); // 获取文件名
  169. $zip->addFromString($fileName, $fileContent); // 将图片添加到 ZIP 文件中
  170. }
  171. $zip->close();
  172. // 提供 ZIP 文件下载
  173. Session::put('downloadAllStatus', '2');
  174. return response()->download($zipPath, $zipFileName)->deleteFileAfterSend(true);
  175. } else {
  176. Session::put('downloadAllStatus', '2');
  177. return response()->json(['error' => '系统正在下载中,请稍后再试','status'=> 500]);
  178. }
  179. } else {
  180. Session::put('downloadAllStatus', '2');
  181. return response()->json(['error' => '未找到指定的 Tab 数据','status'=> 404]);
  182. }
  183. } else {
  184. Session::put('downloadAllStatus', '2');
  185. return response()->json(['error' => '未找到相册','status'=> 404]);
  186. }
  187. }
  188. public function downloadImage(Request $request)
  189. {
  190. Session::put('downloadAllStatus', '1');
  191. // Get the URL from the request parameter
  192. $imageUrl = $request->input('url');
  193. // Validate the URL
  194. if (filter_var($imageUrl, FILTER_VALIDATE_URL) === false) {
  195. Session::put('downloadAllStatus', '2');
  196. return response()->json(['error' => 'Invalid URL'], 400);
  197. }
  198. try {
  199. // Initialize cURL
  200. $ch = curl_init($imageUrl);
  201. // Set cURL options
  202. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  203. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // Allow HTTPS (disable SSL peer verification)
  204. curl_setopt($ch, CURLOPT_TIMEOUT, 30); // Set timeout to prevent hanging
  205. // Execute cURL request
  206. $imageContent = curl_exec($ch);
  207. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  208. if ($httpCode !== 200) {
  209. Session::put('downloadAllStatus', '2');
  210. return response()->json(['error' => 'Failed to download image'], 500);
  211. }
  212. curl_close($ch);
  213. // Generate a random filename
  214. $fileName = Str::random(32) . '.jpg';
  215. Session::put('downloadAllStatus', '2');
  216. // Return the image as a download
  217. return response($imageContent)
  218. ->header('Content-Type', 'image/jpeg')
  219. ->header('Content-Disposition', 'attachment; filename="' . $fileName . '"');
  220. } catch (\Exception $e) {
  221. Session::put('downloadAllStatus', '2');
  222. return response()->json(['error' => 'Error downloading image: ' . $e->getMessage()], 500);
  223. }
  224. }
  225. public function getDownloadAllStatus(Request $request)
  226. {
  227. $reset = $request->input('reset', 0);
  228. if ($reset) {
  229. Session::put('downloadAllStatus', '0');
  230. }
  231. $status = Session::get('downloadAllStatus',0);
  232. return response()->json(['status' => $status]);
  233. }
  234. /*
  235. * 更新日志
  236. */
  237. public function nextLog(Request $request)
  238. {
  239. $id = $request->input('id', 0);
  240. $log = SiteAlbumLog::getNextLogEntry($id);
  241. if ($log) {
  242. return response()->json(['status' => 'success', 'data' => $log]);
  243. } else {
  244. return response()->json(['status' => 'error', 'data' => []]);
  245. }
  246. }
  247. //获取分类树父ID
  248. private function getFoldersTreeFatherId($folder_id)
  249. {
  250. foreach ($this->foldersTree as $key => $value) {
  251. $father_id = $value['id'];
  252. if ($folder_id == $value['id']) {
  253. return $father_id;
  254. }
  255. foreach ($value['children'] as $k => $v) {
  256. if ($folder_id == $v['id']) {
  257. return $father_id;
  258. }
  259. foreach ($v['children'] as $kk => $vv) {
  260. if ($folder_id == $vv['id']) {
  261. return $father_id;
  262. }
  263. }
  264. }
  265. }
  266. return 0;
  267. }
  268. //返回分类树第一个无子节点的ID
  269. private function getFoldersTreeFirstId()
  270. {
  271. foreach ($this->foldersTree as $key => $value) {
  272. if (isset($value['children'])) {
  273. foreach ($value['children'] as $k => $v) {
  274. return $v['id'];
  275. }
  276. } else {
  277. return $value['id'];
  278. }
  279. }
  280. return 0;
  281. }
  282. }