CommonHelper.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. * 返回显示的HTML显示图片
  10. */
  11. public static function displayImage($images,$boxSize=60,$imgSize=1024)
  12. {
  13. if (empty($images) || empty($images[0])) {
  14. $html = "";
  15. } else {
  16. // 默认显示 100x100 的图片
  17. $thumbnailImages = array_map(function ($imageUrl) use ($boxSize) {
  18. $imageUrl= CommonHelper::ossUrl($imageUrl);
  19. $extension = pathinfo($imageUrl, PATHINFO_EXTENSION);
  20. if ($extension == 'svg') {
  21. return $imageUrl;
  22. } else {
  23. return $imageUrl . "?x-oss-process=image/resize,m_pad,h_{$boxSize},w_{$boxSize},color_ffffff";
  24. }
  25. }, $images);
  26. // 生成点击查看大图的链接
  27. $largeImages = array_map(function ($imageUrl) use ($imgSize) {
  28. $imageUrl= CommonHelper::ossUrl($imageUrl);
  29. $extension = pathinfo($imageUrl, PATHINFO_EXTENSION);
  30. if ($extension == 'svg') {
  31. return $imageUrl;
  32. } else {
  33. return $imageUrl . "?x-oss-process=image/resize,m_lfit,w_{$imgSize},h_{$imgSize}";
  34. }
  35. }, $images);
  36. // 显示缩略图,并添加点击查看大图的功能
  37. $html = '';
  38. foreach ($thumbnailImages as $index => $thumbnailUrl) {
  39. $largeUrl = $largeImages[$index];
  40. $html .= "<a href='$largeUrl' target='_blank'><img src='$thumbnailUrl' style='height:{$boxSize}px; margin-right:5px; border: 1px solid #ececf1;'></a>";
  41. }
  42. return $html;
  43. //
  44. // //默认用等比例缩放
  45. // $process = "?x-oss-process=image/resize,h_{$imgSize},m_lfit";
  46. // $html = '<div style="display: flex; flex-wrap: wrap; gap: 5px;">';
  47. // foreach ($images as $image) {
  48. // $html .= "<div style='width: {$boxSize}px; height: {$boxSize}px; padding: 3px; border: 1px solid #ddd; border-radius: 3px; background-color: #f9f9f9; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1); display: flex; align-items: center; justify-content: center;'>
  49. // <img data-action='preview-img' src='" . self::ossUrl($image).$process . "' style='max-width: 100%; max-height: 100%; object-fit: contain;'>
  50. // </div>";
  51. // }
  52. // $html .= '</div>';
  53. }
  54. return $html;
  55. }
  56. /*
  57. * 返回oss的url
  58. */
  59. public static function ossUrl($imageUrl)
  60. {
  61. if (strpos($imageUrl, 'http:') === 0 || strpos($imageUrl, 'https:') === 0) {
  62. return $imageUrl;
  63. }
  64. if (env('OSS_DOMAIN')) {
  65. return "http://".env('OSS_DOMAIN').'/'.$imageUrl;
  66. }
  67. return "http://".env('OSS_BUCKET').'.'.env('OSS_ENDPOINT').'/'.$imageUrl;
  68. }
  69. /*
  70. * 替换新增与编辑的url,在后边加上指定的参数 (适用于弹出框的新增与编辑)
  71. * $addButton = '.tree-quick-create';
  72. * $editButton = '.tree-quick-edit';
  73. * $paramsUrl = 'location=0';
  74. */
  75. public static function replaceAddEditerUrl($addButton,$editButton,$paramsUrl) {
  76. Admin::script(
  77. <<<JS
  78. var button = $('{$addButton}');
  79. var currentUrl = button.attr('data-url');
  80. if (currentUrl.indexOf('?') === -1) {
  81. button.attr('data-url', currentUrl + '?{$paramsUrl}');
  82. } else {
  83. button.attr('data-url', currentUrl + '&{$paramsUrl}');
  84. }
  85. $('{$editButton}').each(function() {
  86. var currentUrl = $(this).attr('data-url');
  87. if (currentUrl.indexOf('?') === -1) {
  88. $(this).attr('data-url', currentUrl + '?{$paramsUrl}');
  89. } else {
  90. // 如果已经有查询参数,添加 &id=123
  91. $(this).attr('data-url', currentUrl + '&{$paramsUrl}');
  92. }
  93. });
  94. JS
  95. );
  96. }
  97. public static function seoReplace($titleName = 'title',$modelName = 'pages')
  98. {
  99. if ($titleName) {
  100. Admin::script(
  101. <<<JS
  102. $('input[name="{$titleName}"]').on('input', function() {
  103. // 将 title 的值赋给 seo_title
  104. $('input[name="seo_title"]').val($(this).val());
  105. });
  106. JS
  107. );
  108. }
  109. //ajax 生成slug
  110. if ($modelName) {
  111. Admin::script(
  112. <<<JS
  113. $('input[name="{$titleName}"]').on('blur', function() {
  114. //通过ajax请求修改slug
  115. title = $(this).val();
  116. $.ajax({
  117. url: '/dist/api/generate-slug',
  118. type: 'GET',
  119. data: {
  120. model:'{$modelName}',
  121. title: title,
  122. },
  123. success: function(response) {
  124. $('input[name="slug"]').val(response.slug);
  125. }
  126. });
  127. });
  128. JS
  129. );
  130. }
  131. }
  132. }