123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- namespace App\Libraries;
- use Dcat\Admin\Admin;
- class CommonHelper
- {
-
- public static function displayImage($images,$boxSize=60,$imgSize=1024)
- {
- if (empty($images) || empty($images[0])) {
- $html = "";
- } else {
-
- $thumbnailImages = array_map(function ($imageUrl) use ($boxSize) {
- $imageUrl= CommonHelper::ossUrl($imageUrl);
- return $imageUrl . "?x-oss-process=image/resize,m_pad,h_{$boxSize},w_{$boxSize},color_ffffff";
- }, $images);
-
- $largeImages = array_map(function ($imageUrl) use ($imgSize) {
- $imageUrl= CommonHelper::ossUrl($imageUrl);
- return $imageUrl . "?x-oss-process=image/resize,m_lfit,w_{$imgSize},h_{$imgSize}";
- }, $images);
-
- $html = '';
- foreach ($thumbnailImages as $index => $thumbnailUrl) {
- $largeUrl = $largeImages[$index];
- $html .= "<a href='$largeUrl' target='_blank'><img src='$thumbnailUrl' style='height:{$boxSize}px; margin-right:5px; border: 1px solid #ececf1;'></a>";
- }
- return $html;
- }
- return $html;
- }
-
- public static function ossUrl($image)
- {
- if (env('OSS_DOMAIN')) {
- return "http://".env('OSS_DOMAIN').'/'.$image;
- }
- return "http://".env('OSS_BUCKET').'.'.env('OSS_ENDPOINT').'/'.$image;
- }
-
- public static function replaceAddEditerUrl($addButton,$editButton,$paramsUrl) {
- Admin::script(
- <<<JS
- var button = $('{$addButton}');
- var currentUrl = button.attr('data-url');
- if (currentUrl.indexOf('?') === -1) {
- button.attr('data-url', currentUrl + '?{$paramsUrl}');
- } else {
- button.attr('data-url', currentUrl + '&{$paramsUrl}');
- }
- $('{$editButton}').each(function() {
- var currentUrl = $(this).attr('data-url');
- if (currentUrl.indexOf('?') === -1) {
- $(this).attr('data-url', currentUrl + '?{$paramsUrl}');
- } else {
- // 如果已经有查询参数,添加 &id=123
- $(this).attr('data-url', currentUrl + '&{$paramsUrl}');
- }
- });
- JS
- );
- }
- public static function seoReplace($titleName = 'title',$modelName = 'pages')
- {
- if ($titleName) {
- Admin::script(
- <<<JS
- $('input[name="{$titleName}"]').on('input', function() {
- // 将 title 的值赋给 seo_title
- $('input[name="seo_title"]').val($(this).val());
- });
- JS
- );
- }
-
- if ($modelName) {
- Admin::script(
- <<<JS
- $('input[name="{$titleName}"]').on('blur', function() {
- //通过ajax请求修改slug
- title = $(this).val();
- $.ajax({
- url: '/dist/api/generate-slug',
- type: 'GET',
- data: {
- model:'{$modelName}',
- title: title,
- },
- success: function(response) {
- $('input[name="slug"]').val(response.slug);
- }
- });
- });
- JS
- );
- }
- }
- }
|