CommonHelper.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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)
  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. $html .= "<a href='$largeUrl' target='_blank'><img src='$thumbnailUrl' style='height:{$boxSize}px; margin-right:5px; border: 1px solid #ececf1;'></a>";
  50. }
  51. return $html;
  52. }
  53. return $html;
  54. }
  55. /*
  56. * 返回oss的url
  57. */
  58. public static function ossUrl($imageUrl)
  59. {
  60. if (strpos($imageUrl, 'http:') === 0 || strpos($imageUrl, 'https:') === 0) {
  61. return $imageUrl;
  62. }
  63. if (env('OSS_DOMAIN')) {
  64. return "http://".env('OSS_DOMAIN').'/'.$imageUrl;
  65. }
  66. return "http://".env('OSS_BUCKET').'.'.env('OSS_ENDPOINT').'/'.$imageUrl;
  67. }
  68. /*
  69. * 图库图片URL
  70. */
  71. public static function albumUrl($imageUrl)
  72. {
  73. if (strpos($imageUrl, 'http:') === 0 || strpos($imageUrl, 'https:') === 0) {
  74. return $imageUrl;
  75. }
  76. return env('ALBUM_OSS_URL').'/'.$imageUrl;
  77. }
  78. /*
  79. * 显示视频
  80. */
  81. public static function displayVideo($items,$videoCover,$videoSrc,$boxSize=150,$ossSource=1)
  82. {
  83. $html = '';
  84. if (is_array($items)) {
  85. foreach ($items as $item) {
  86. $item = (array) $item;
  87. $cover = $item[$videoCover];
  88. $src = $item[$videoSrc];
  89. if ($ossSource == 1) {
  90. $videoUrl = CommonHelper::ossUrl($src);
  91. } else {
  92. $videoUrl = CommonHelper::albumUrl($src);
  93. }
  94. if ($ossSource == 1) {
  95. $thumbnailUrl = CommonHelper::ossUrl($cover) . "?x-oss-process=image/resize,m_pad,h_{$boxSize},w_{$boxSize},color_ffffff";
  96. } else {
  97. $thumbnailUrl = CommonHelper::albumUrl($cover) . "?x-oss-process=image/resize,m_pad,h_{$boxSize},w_{$boxSize},color_ffffff";
  98. }
  99. $html .= '<div class="video-container"><a href="#" class="playVideo" videoUrl="'.$videoUrl.'")"><img src="'.$thumbnailUrl.'" alt="Video Thumbnail"><div class="play-button"></div></a></div>';
  100. }
  101. $html .= '<div class="video-popup" id="videoPopup"><span class="close-btn">&times;</span> <iframe src="" frameborder="0" allowfullscreen></iframe></div>';
  102. }
  103. //视频播放CSS
  104. Admin::style("
  105. .video-container {
  106. position: relative;
  107. display: inline-block;
  108. }
  109. .video-container img {
  110. height: 200px;
  111. margin-right: 5px;
  112. border: 1px solid #ececf1;
  113. }
  114. .play-button {
  115. position: absolute;
  116. top: 50%;
  117. left: 50%;
  118. transform: translate(-50%, -50%);
  119. width: 50px;
  120. height: 50px;
  121. background-color: rgba(0, 0, 0, 0.7);
  122. border-radius: 50%;
  123. cursor: pointer;
  124. }
  125. .play-button::after {
  126. content: '▶';
  127. font-size: 30px;
  128. color: white;
  129. position: absolute;
  130. top: 50%;
  131. left: 50%;
  132. transform: translate(-50%, -50%);
  133. }
  134. .video-popup {
  135. display: none;
  136. position: fixed;
  137. top: 50%;
  138. left: 50%;
  139. transform: translate(-50%, -50%);
  140. background-color: white;
  141. padding: 25px;
  142. box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
  143. z-index: 1000;
  144. }
  145. .video-popup iframe {
  146. width: 800px;
  147. height: 450px;
  148. }
  149. .close-btn {
  150. position: absolute;
  151. top: 0px;
  152. right: 5px;
  153. font-size: 24px;
  154. color: #000;
  155. cursor: pointer;
  156. }
  157. .close-btn:hover {
  158. color: #f00;
  159. }
  160. ");
  161. Admin::script("
  162. $('.playVideo').on('click', function(e) {
  163. e.preventDefault(); // 阻止默认跳转行为
  164. var videoUrl = $(this).attr('videoUrl'); // 获取 videoUrl 属性
  165. $('#videoPopup').css('display', 'block');
  166. $('#videoPopup iframe').attr('src', videoUrl); // 设置 iframe 的 src
  167. });
  168. // 点击关闭按钮关闭视频
  169. $('.close-btn').on('click', function() {
  170. $('#videoPopup').css('display', 'none');
  171. $('#videoPopup iframe').attr('src', ''); // 停止播放视频
  172. });
  173. ");
  174. return $html; // 当没有数组数据时
  175. }
  176. /*
  177. * 替换新增与编辑的url,在后边加上指定的参数 (适用于弹出框的新增与编辑)
  178. * $addButton = '.tree-quick-create';
  179. * $editButton = '.tree-quick-edit';
  180. * $paramsUrl = 'location=0';
  181. */
  182. public static function replaceAddEditerUrl($addButton,$editButton,$paramsUrl) {
  183. Admin::script(
  184. <<<JS
  185. var button = $('{$addButton}');
  186. var currentUrl = button.attr('data-url');
  187. if (currentUrl.indexOf('?') === -1) {
  188. button.attr('data-url', currentUrl + '?{$paramsUrl}');
  189. } else {
  190. button.attr('data-url', currentUrl + '&{$paramsUrl}');
  191. }
  192. $('{$editButton}').each(function() {
  193. var currentUrl = $(this).attr('data-url');
  194. if (currentUrl.indexOf('?') === -1) {
  195. $(this).attr('data-url', currentUrl + '?{$paramsUrl}');
  196. } else {
  197. // 如果已经有查询参数,添加 &id=123
  198. $(this).attr('data-url', currentUrl + '&{$paramsUrl}');
  199. }
  200. });
  201. JS
  202. );
  203. }
  204. public static function seoReplace($titleName = 'title',$modelName = 'pages')
  205. {
  206. if ($titleName) {
  207. Admin::script(
  208. <<<JS
  209. $('input[name="{$titleName}"]').on('input', function() {
  210. // 将 title 的值赋给 seo_title
  211. $('input[name="seo_title"]').val($(this).val());
  212. });
  213. JS
  214. );
  215. }
  216. //ajax 生成slug
  217. if ($modelName) {
  218. Admin::script(
  219. <<<JS
  220. $('input[name="{$titleName}"]').on('blur', function() {
  221. //通过ajax请求修改slug
  222. title = $(this).val();
  223. $.ajax({
  224. url: '/dist/api/generate-slug',
  225. type: 'GET',
  226. data: {
  227. model:'{$modelName}',
  228. title: title,
  229. },
  230. success: function(response) {
  231. $('input[name="slug"]').val(response.slug);
  232. }
  233. });
  234. });
  235. JS
  236. );
  237. }
  238. }
  239. }