CommonHelper.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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($image)
  60. {
  61. if (env('OSS_DOMAIN')) {
  62. return "http://".env('OSS_DOMAIN').'/'.$image;
  63. }
  64. return "http://".env('OSS_BUCKET').'.'.env('OSS_ENDPOINT').'/'.$image;
  65. }
  66. /*
  67. * 替换新增与编辑的url,在后边加上指定的参数 (适用于弹出框的新增与编辑)
  68. * $addButton = '.tree-quick-create';
  69. * $editButton = '.tree-quick-edit';
  70. * $paramsUrl = 'location=0';
  71. */
  72. public static function replaceAddEditerUrl($addButton,$editButton,$paramsUrl) {
  73. Admin::script(
  74. <<<JS
  75. var button = $('{$addButton}');
  76. var currentUrl = button.attr('data-url');
  77. if (currentUrl.indexOf('?') === -1) {
  78. button.attr('data-url', currentUrl + '?{$paramsUrl}');
  79. } else {
  80. button.attr('data-url', currentUrl + '&{$paramsUrl}');
  81. }
  82. $('{$editButton}').each(function() {
  83. var currentUrl = $(this).attr('data-url');
  84. if (currentUrl.indexOf('?') === -1) {
  85. $(this).attr('data-url', currentUrl + '?{$paramsUrl}');
  86. } else {
  87. // 如果已经有查询参数,添加 &id=123
  88. $(this).attr('data-url', currentUrl + '&{$paramsUrl}');
  89. }
  90. });
  91. JS
  92. );
  93. }
  94. public static function seoReplace($titleName = 'title',$modelName = 'pages')
  95. {
  96. if ($titleName) {
  97. Admin::script(
  98. <<<JS
  99. $('input[name="{$titleName}"]').on('input', function() {
  100. // 将 title 的值赋给 seo_title
  101. $('input[name="seo_title"]').val($(this).val());
  102. });
  103. JS
  104. );
  105. }
  106. //ajax 生成slug
  107. if ($modelName) {
  108. Admin::script(
  109. <<<JS
  110. $('input[name="{$titleName}"]').on('blur', function() {
  111. //通过ajax请求修改slug
  112. title = $(this).val();
  113. $.ajax({
  114. url: '/dist/api/generate-slug',
  115. type: 'GET',
  116. data: {
  117. model:'{$modelName}',
  118. title: title,
  119. },
  120. success: function(response) {
  121. $('input[name="slug"]').val(response.slug);
  122. }
  123. });
  124. });
  125. JS
  126. );
  127. }
  128. }
  129. }