CommonHelper.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. return $imageUrl . "?x-oss-process=image/resize,m_lfit,h_{$boxSize}";
  20. }, $images);
  21. // 生成点击查看大图的链接
  22. $largeImages = array_map(function ($imageUrl) use ($imgSize) {
  23. $imageUrl= CommonHelper::ossUrl($imageUrl);
  24. return $imageUrl . "?x-oss-process=image/resize,m_lfit,w_{$imgSize},h_{$imgSize}";
  25. }, $images);
  26. // 显示缩略图,并添加点击查看大图的功能
  27. $html = '';
  28. foreach ($thumbnailImages as $index => $thumbnailUrl) {
  29. $largeUrl = $largeImages[$index];
  30. $html .= "<a href='$largeUrl' target='_blank'><img src='$thumbnailUrl' style='height:{$boxSize}px; margin-right:5px;'></a>";
  31. }
  32. return $html;
  33. //
  34. // //默认用等比例缩放
  35. // $process = "?x-oss-process=image/resize,h_{$imgSize},m_lfit";
  36. // $html = '<div style="display: flex; flex-wrap: wrap; gap: 5px;">';
  37. // foreach ($images as $image) {
  38. // $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;'>
  39. // <img data-action='preview-img' src='" . self::ossUrl($image).$process . "' style='max-width: 100%; max-height: 100%; object-fit: contain;'>
  40. // </div>";
  41. // }
  42. // $html .= '</div>';
  43. }
  44. return $html;
  45. }
  46. /*
  47. * 返回oss的url
  48. */
  49. public static function ossUrl($image)
  50. {
  51. return "http://".env('OSS_BUCKET').'.'.env('OSS_ENDPOINT').'/'.$image;
  52. }
  53. /*
  54. * 替换新增与编辑的url,在后边加上指定的参数
  55. * $addButton = '.tree-quick-create';
  56. * $editButton = '.tree-quick-edit';
  57. * $paramsUrl = 'location=0';
  58. */
  59. public static function replaceAddEditerUrl($addButton,$editButton,$paramsUrl) {
  60. Admin::script(
  61. <<<JS
  62. var button = $('{$addButton}');
  63. var currentUrl = button.attr('data-url');
  64. if (currentUrl.indexOf('?') === -1) {
  65. button.attr('data-url', currentUrl + '?{$paramsUrl}');
  66. } else {
  67. button.attr('data-url', currentUrl + '&{$paramsUrl}');
  68. }
  69. $('{$editButton}').each(function() {
  70. var currentUrl = $(this).attr('data-url');
  71. if (currentUrl.indexOf('?') === -1) {
  72. $(this).attr('data-url', currentUrl + '?{$paramsUrl}');
  73. } else {
  74. // 如果已经有查询参数,添加 &id=123
  75. $(this).attr('data-url', currentUrl + '&{$paramsUrl}');
  76. }
  77. });
  78. JS
  79. );
  80. }
  81. public static function seoReplace($titleName = 'title',$modelName = 'pages')
  82. {
  83. Admin::script(
  84. <<<JS
  85. $('input[name="title"]').on('input', function() {
  86. // 将 title 的值赋给 seo_title
  87. $('input[name="seo_title"]').val($(this).val());
  88. });
  89. $('input[name="title"]').on('blur', function() {
  90. //通过ajax请求修改slug
  91. title = $(this).val();
  92. $.ajax({
  93. url: '/dist/api/generate-slug',
  94. type: 'GET',
  95. data: {
  96. model:'{$modelName}',
  97. title: title,
  98. },
  99. success: function(response) {
  100. $('input[name="slug"]').val(response.slug);
  101. }
  102. });
  103. });
  104. JS
  105. );
  106. }
  107. }