CommonHelper.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. <?php
  2. // app/Libraries/CommonHelper.php
  3. namespace App\Libraries;
  4. use Dcat\Admin\Admin;
  5. class CommonHelper
  6. {
  7. /*
  8. * $images 格式:['image.jpg','image2.jpg']
  9. * $ossSource 1:本地 2:相册
  10. * 返回显示的HTML显示图片
  11. */
  12. public static function displayImage($images,$boxSize=60,$imgSize=1024,$ossSource=1,$hasLink=true)
  13. {
  14. if (empty($images) || empty($images[0])) {
  15. $html = "";
  16. } else {
  17. // 默认显示 100x100 的图片
  18. $thumbnailImages = array_map(function ($imageUrl) use ($boxSize,$ossSource) {
  19. if ($ossSource == 1) {
  20. $imageUrl= CommonHelper::ossUrl($imageUrl);
  21. } else {
  22. $imageUrl= CommonHelper::albumUrl($imageUrl);
  23. }
  24. $extension = pathinfo($imageUrl, PATHINFO_EXTENSION);
  25. if ($extension == 'svg') {
  26. return $imageUrl;
  27. } else {
  28. return $imageUrl . "?x-oss-process=image/resize,m_pad,h_{$boxSize},w_{$boxSize},color_ffffff";
  29. }
  30. }, $images);
  31. // 生成点击查看大图的链接
  32. $largeImages = array_map(function ($imageUrl) use ($imgSize,$ossSource) {
  33. if ($ossSource == 1) {
  34. $imageUrl= CommonHelper::ossUrl($imageUrl);
  35. } else {
  36. $imageUrl= CommonHelper::albumUrl($imageUrl);
  37. }
  38. $extension = pathinfo($imageUrl, PATHINFO_EXTENSION);
  39. if ($extension == 'svg') {
  40. return $imageUrl;
  41. } else {
  42. return $imageUrl . "?x-oss-process=image/resize,m_lfit,w_{$imgSize},h_{$imgSize}";
  43. }
  44. }, $images);
  45. // 显示缩略图,并添加点击查看大图的功能
  46. $html = '';
  47. foreach ($thumbnailImages as $index => $thumbnailUrl) {
  48. $largeUrl = $largeImages[$index];
  49. if ($hasLink) {
  50. $html .= "<a href='$largeUrl' target='_blank'><img src='$thumbnailUrl' style='height:{$boxSize}px; margin-right:5px; border: 1px solid #ececf1;'></a>";
  51. } else {
  52. $html .= "<img src='$thumbnailUrl' style='height:{$boxSize}px; margin-right:5px; border: 1px solid #ececf1;'>";
  53. }
  54. }
  55. return $html;
  56. }
  57. return $html;
  58. }
  59. /*
  60. * 返回oss的url
  61. */
  62. public static function ossUrl($imageUrl)
  63. {
  64. if (strpos($imageUrl, 'http:') === 0 || strpos($imageUrl, 'https:') === 0) {
  65. return $imageUrl;
  66. }
  67. if (env('OSS_DOMAIN')) {
  68. return "http://".env('OSS_DOMAIN').'/'.$imageUrl;
  69. }
  70. return "http://".env('OSS_BUCKET').'.'.env('OSS_ENDPOINT').'/'.$imageUrl;
  71. }
  72. /*
  73. * 图库图片URL
  74. */
  75. public static function albumUrl($imageUrl)
  76. {
  77. if (strpos($imageUrl, 'http:') === 0 || strpos($imageUrl, 'https:') === 0) {
  78. return $imageUrl;
  79. }
  80. return env('ALBUM_OSS_URL').'/'.$imageUrl;
  81. }
  82. /*
  83. * 显示视频
  84. */
  85. public static function displayVideo($items,$videoCover,$videoSrc,$boxSize=150,$ossSource=1)
  86. {
  87. $html = '';
  88. if (is_array($items)) {
  89. foreach ($items as $item) {
  90. $item = (array) $item;
  91. $cover = $item[$videoCover];
  92. $src = $item[$videoSrc];
  93. if ($ossSource == 1) {
  94. $videoUrl = CommonHelper::ossUrl($src);
  95. } else {
  96. $videoUrl = CommonHelper::albumUrl($src);
  97. }
  98. if ($ossSource == 1) {
  99. $thumbnailUrl = CommonHelper::ossUrl($cover) . "?x-oss-process=image/resize,m_pad,h_{$boxSize},w_{$boxSize},color_ffffff";
  100. } else {
  101. $thumbnailUrl = CommonHelper::albumUrl($cover) . "?x-oss-process=image/resize,m_pad,h_{$boxSize},w_{$boxSize},color_ffffff";
  102. }
  103. $html .= '<div class="video-container"><a href="#" class="playVideo" videoUrl="'.$videoUrl.'")"><img src="'.$thumbnailUrl.'" alt="Video Thumbnail"><div class="play-button"></div></a></div>';
  104. }
  105. $html .= '<div class="video-popup" id="videoPopup"><span class="close-btn">&times;</span> <iframe src="" frameborder="0" allowfullscreen></iframe></div>';
  106. }
  107. //视频播放CSS
  108. Admin::style("
  109. .video-container {
  110. position: relative;
  111. display: inline-block;
  112. }
  113. .video-container img {
  114. height: 200px;
  115. margin-right: 5px;
  116. border: 1px solid #ececf1;
  117. }
  118. .play-button {
  119. position: absolute;
  120. top: 50%;
  121. left: 50%;
  122. transform: translate(-50%, -50%);
  123. width: 50px;
  124. height: 50px;
  125. background-color: rgba(0, 0, 0, 0.7);
  126. border-radius: 50%;
  127. cursor: pointer;
  128. }
  129. .play-button::after {
  130. content: '▶';
  131. font-size: 30px;
  132. color: white;
  133. position: absolute;
  134. top: 50%;
  135. left: 50%;
  136. transform: translate(-50%, -50%);
  137. }
  138. .video-popup {
  139. display: none;
  140. position: fixed;
  141. top: 50%;
  142. left: 50%;
  143. transform: translate(-50%, -50%);
  144. background-color: white;
  145. padding: 25px;
  146. box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
  147. z-index: 1000;
  148. }
  149. .video-popup iframe {
  150. width: 800px;
  151. height: 450px;
  152. }
  153. .close-btn {
  154. position: absolute;
  155. top: 0px;
  156. right: 5px;
  157. font-size: 24px;
  158. color: #000;
  159. cursor: pointer;
  160. }
  161. .close-btn:hover {
  162. color: #f00;
  163. }
  164. ");
  165. Admin::script("
  166. $('.playVideo').on('click', function(e) {
  167. e.preventDefault(); // 阻止默认跳转行为
  168. var videoUrl = $(this).attr('videoUrl'); // 获取 videoUrl 属性
  169. $('#videoPopup').css('display', 'block');
  170. $('#videoPopup iframe').attr('src', videoUrl); // 设置 iframe 的 src
  171. });
  172. // 点击关闭按钮关闭视频
  173. $('.close-btn').on('click', function() {
  174. $('#videoPopup').css('display', 'none');
  175. $('#videoPopup iframe').attr('src', ''); // 停止播放视频
  176. });
  177. ");
  178. return $html; // 当没有数组数据时
  179. }
  180. /*
  181. * 替换新增与编辑的url,在后边加上指定的参数 (适用于弹出框的新增与编辑)
  182. * $addButton = '.tree-quick-create';
  183. * $editButton = '.tree-quick-edit';
  184. * $paramsUrl = 'location=0';
  185. */
  186. public static function replaceAddEditerUrl($addButton,$editButton,$paramsUrl) {
  187. Admin::script(
  188. <<<JS
  189. var button = $('{$addButton}');
  190. var currentUrl = button.attr('data-url');
  191. if (currentUrl.indexOf('?') === -1) {
  192. button.attr('data-url', currentUrl + '?{$paramsUrl}');
  193. } else {
  194. button.attr('data-url', currentUrl + '&{$paramsUrl}');
  195. }
  196. $('{$editButton}').each(function() {
  197. var currentUrl = $(this).attr('data-url');
  198. if (currentUrl.indexOf('?') === -1) {
  199. $(this).attr('data-url', currentUrl + '?{$paramsUrl}');
  200. } else {
  201. // 如果已经有查询参数,添加 &id=123
  202. $(this).attr('data-url', currentUrl + '&{$paramsUrl}');
  203. }
  204. });
  205. JS
  206. );
  207. }
  208. public static function seoReplace($titleName = 'title',$modelName = 'pages')
  209. {
  210. if ($titleName) {
  211. Admin::script(
  212. <<<JS
  213. $('input[name="{$titleName}"]').on('input', function() {
  214. // 将 title 的值赋给 seo_title
  215. $('input[name="seo_title"]').val($(this).val());
  216. });
  217. JS
  218. );
  219. }
  220. //ajax 生成slug
  221. if ($modelName) {
  222. Admin::script(
  223. <<<JS
  224. $('input[name="{$titleName}"]').on('blur', function() {
  225. //通过ajax请求修改slug
  226. title = $(this).val();
  227. $.ajax({
  228. url: '/dist/api/generate-slug',
  229. type: 'GET',
  230. data: {
  231. model:'{$modelName}',
  232. title: title,
  233. },
  234. success: function(response) {
  235. $('input[name="slug"]').val(response.slug);
  236. }
  237. });
  238. });
  239. JS
  240. );
  241. }
  242. }
  243. public static function slug_fix($slug)
  244. {
  245. if ($slug == '') {
  246. return '';
  247. } else {
  248. $slug = preg_replace('/\s+/', '-', $slug);
  249. $slug = strtolower($slug);
  250. return $slug;
  251. }
  252. }
  253. /*
  254. * 用于详情页,图片下载与放大
  255. */
  256. public static function viewDownloadEnlarge() {
  257. //css
  258. Admin::style(
  259. <<<CSS
  260. .download-wrapper::after {
  261. content: "";
  262. position: absolute;
  263. top: 0;
  264. left: 0;
  265. width: 100%;
  266. height: 100%;
  267. background: rgba(58, 54, 54, 0.34); /* 浅灰色半透明 */
  268. opacity: 0;
  269. transition: opacity 0.3s ease;
  270. pointer-events: none;
  271. border-radius: 8px;
  272. }
  273. .download-wrapper {
  274. position: relative;
  275. display: inline-block;
  276. margin: 15px;
  277. overflow: hidden;
  278. border-radius: 8px;
  279. transition: transform 0.3s ease;
  280. }
  281. .download-hover {
  282. position: absolute;
  283. top: 50%;
  284. left: 50%;
  285. transform: translate(-50%, -50%);
  286. opacity: 0;
  287. transition: opacity 0.3s ease;
  288. display: flex;
  289. gap: 12px;
  290. z-index: 1; /* 确保按钮在遮罩层上方 */
  291. }
  292. .download-btn {
  293. background: rgba(255, 255, 255, 0.95);
  294. border: none;
  295. width: 42px;
  296. height: 42px;
  297. border-radius: 50%;
  298. cursor: pointer;
  299. box-shadow: 0 3px 6px rgba(0,0,0,0.16);
  300. transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  301. }
  302. .download-btn.disabled {
  303. background: rgba(189, 189, 189, 0.95);
  304. cursor: not-allowed;
  305. }
  306. .download-btn i {
  307. font-size: 18px;
  308. color: #2196F3;
  309. }
  310. .download-wrapper:hover::after {
  311. opacity: 1;
  312. }
  313. .download-wrapper:hover .download-hover {
  314. opacity: 1;
  315. }
  316. .auto-download-image {
  317. display: block;
  318. transition: transform 0.3s ease;
  319. max-width: 100%;
  320. height: auto;
  321. border-radius: 8px;
  322. }
  323. .download-wrapper:hover img {
  324. transform: scale(1.04);
  325. }
  326. /* 新增全屏样式 */
  327. .fullscreen-overlay {
  328. position: fixed;
  329. top: 0;
  330. left: 0;
  331. width: 100%;
  332. height: 100%;
  333. background: rgba(0,0,0,0.95);
  334. display: none;
  335. z-index: 1000;
  336. cursor: zoom-out;
  337. }
  338. .fullscreen-image {
  339. position: absolute;
  340. max-width: 90%;
  341. max-height: 90%;
  342. top: 50%;
  343. left: 50%;
  344. transform: translate(-50%, -50%);
  345. border-radius: 8px;
  346. animation: zoomIn 0.3s ease;
  347. }
  348. @keyframes zoomIn {
  349. from { transform: translate(-50%, -50%) scale(0.8); }
  350. to { transform: translate(-50%, -50%) scale(1); }
  351. }
  352. .download-wrapper:hover .download-hover {
  353. opacity: 1;
  354. }
  355. CSS );
  356. //js
  357. Admin::script(
  358. <<<JS
  359. const fullscreenOverlay = $('.fullscreen-overlay');
  360. const fullscreenImage = $('.fullscreen-image');
  361. function initDownloadButtons() {
  362. $('.box-body').find("img").each(function() {
  363. // 新增判断:如果父元素是<a>或已有download-wrapper则跳过
  364. if ($(this).parent().hasClass('download-wrapper') || $(this).parent().is('a')) return;
  365. if ($(this).hasClass('fullscreen-image')) {
  366. return;
  367. }
  368. const fileName = this.src.split('/').pop().split('?')[0];
  369. $(this).wrap('<div class="download-wrapper"></div>')
  370. .after(`<div class="download-hover">
  371. <button class="download-btn" title="下载">
  372. <i class="feather icon-download"></i>
  373. </button>
  374. <button class="download-btn zoom-btn" title="放大">
  375. <i class="feather icon-zoom-in"></i>
  376. </button>
  377. </div>`);
  378. const \$btn = $(this).parent().find('.download-btn').first();
  379. \$btn.click(function() {
  380. const btn = $(this);
  381. btn.addClass('disabled');
  382. downloadImage(
  383. btn.closest('.download-wrapper').find('img')[0].src,
  384. fileName
  385. ).finally(() => btn.removeClass('disabled'));
  386. });
  387. // 全屏放大功能
  388. $(this).next().find('.zoom-btn').click(function() {
  389. let imgSrc = $(this).closest('.download-wrapper').find('img').attr('src');
  390. imgSrc = imgSrc.replace(/\?.*$/, '');
  391. fullscreenImage.attr('src', imgSrc);
  392. fullscreenOverlay.fadeIn(200);
  393. });
  394. });
  395. }
  396. // 关闭全屏
  397. fullscreenOverlay.click(function(e) {
  398. if (e.target === this) $(this).fadeOut(200);
  399. });
  400. function downloadImage(url, filename) {
  401. return new Promise((resolve, reject) => {
  402. fetch(url)
  403. .then(response => {
  404. if (!response.ok) throw new Error('网络异常');
  405. return response.blob()
  406. })
  407. .then(blob => {
  408. const blobUrl = URL.createObjectURL(blob);
  409. console.log(blobUrl);
  410. const link = document.createElement('a');
  411. link.href = blobUrl;
  412. link.download = filename || 'image.jpg';
  413. document.body.appendChild(link);
  414. link.click();
  415. setTimeout(() => {
  416. document.body.removeChild(link);
  417. URL.revokeObjectURL(blobUrl);
  418. }, 100);
  419. resolve();
  420. })
  421. .catch(error => {
  422. console.error(error);
  423. alert('下载失败');
  424. reject(error);
  425. });
  426. });
  427. }
  428. initDownloadButtons();
  429. JS);
  430. }
  431. /*
  432. * 用于详情页,图片下载与放大 HTML
  433. */
  434. public static function viewDownloadEnlargeHtml() {
  435. return '<div class="fullscreen-overlay"><img src="" class="fullscreen-image"></div>';
  436. }
  437. }