CommonHelper.php 4.3 KB

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