ShowFieldTrait.php 11.2 KB
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
 * 自定义 show 字段
+-----------------------------------------------------------------------------------------------------------------------
 *
 * PHP version 7
 *
 * @category  App\Admin\Traits
 * @package   App\Admin\Traits
 * @author    Richer <yangzi1028@163.com>
 * @date      2020年11月13日 01:26:09
 * @copyright 2021-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
namespace App\Admin\Traits;

use Carbon\Carbon;
use Encore\Admin\Show;
use Encore\Admin\Widgets\Table;
use Illuminate\Support\Arr;

/**
 * Trait ShowFieldTrait
 *
 * @category  App\Admin\Traits
 * @package   App\Admin\Traits
 * @author    Richer <yangzi1028@163.com>
 * @date      2020年11月13日 01:26:09
 * @copyright 2021-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
trait ShowFieldTrait
{

    /**
     * 渲染生成通用字段类型
     *
     * @param string $field 字段名
     * @param string $comment 字段注释
     * @param integer $limit 截取字符串长度,默认为0,不截取
     * @return Show\Field
     */
    public function showCommonField($field = 'title', $comment = '名称', $limit = 0)
    {
        if (!$comment) {
            $comment = __($field);
        }
        return $this->show->field($field, $comment)->setWidth(10, 1)->setGroupClass('col-sm-6 clear-both');
    }

    /**
     * 渲染生成input 为text的字段类型
     *
     * @param string $field
     * @param string $comment
     * @param int $limit
     * @return Show\Field
     */
    public function showTextField($field = 'title', $comment = '', $limit = 0)
    {
        if (!$comment) {
            $comment = __($field);
        }
        return $this->showCommonField($field, $comment, $limit);
    }

    /**
     * 渲染生成number类型字段
     *
     * @param string $field 字段名
     * @param string $comment 字段注释
     * @return Show\Field
     */
    public function showNumberField($field, $comment)
    {
        if (!$comment) {
            $comment = __($field);
        }
        return $this->showCommonField($field, $comment);
    }

    /**
     * 渲染生成switch选择框
     *
     * @param string $field 字段名
     * @param string $comment 字段注释
     * @return Show
     */
    public function showColorField($field, $comment)
    {
        if (!$comment) {
            $comment = __($field);
        }
        $this->show->field($field, $comment)->as(function ($value) {
            if ($value) {
                return '<span style="background: '.$value .'!important;width: 100px;height: 50px;display: block"></span>';
            } else {
                return  '--';
            }
        });
    }

    /**
     * 渲染生成switch选择框
     *
     * @param string $field 字段名
     * @param string $comment 字段注释
     * @param array $states states
     * @return Show
     */
    public function showSwitchField($field, $comment, $states)
    {
        if (!$comment) {
            $comment = __($field);
        }
        // 设置text、color、和存储值
        $show = $this->show->$field($comment)->using($states);
        // 返回对象,方便继续链式调用
        return $show;
    }

    /**
     * 渲染生成select选择框
     *
     * @param string $field 字段名
     * @param string $comment 字段注释
     * @param array $options options
     * @param array $options_color
     * @return void
     */
    public function showSingleSelectField($field, $comment, $options = [], $options_color = [])
    {
        if (!$comment) {
            $comment = __($field);
        }
        // 获取代码集配置
        $options        = $options ?: config('constants.'.$field.'_options');
        // 获取颜色代码集配置
        $options_color  = $options_color ?: config('constants.'.$field.'_color_options');
        //$show->$field($comment)->using($options);
        $this->show->field($field, $comment)->as(function ($title) use ($options, $options_color) {
            if ($title !== null) {
                // 获取每个状态对应的颜色,默认为系统颜色
                $color = Arr::get($options_color, $title, '');
                $value = __(Arr::get($options, $title)) ? : '--';

                return '<span class="text text-'.$color. '" style="color:'.$color.';white-space:nowrap;">'.$value.'</span>';
            } else {
                return  '--';
            }
        });
    }

    /**
     * 渲染生成 select 多选框
     *
     * @param string $field 字段名
     * @param string $comment 字段注释
     * @param array $options options
     * @return Show
     */
    public function showMultipleSelectField($field, $comment, $options = [])
    {
        if (!$comment) {
            $comment = __($field);
        }
        $show = $this->show->$field($comment)->as(function ($data) use ($field, $options) {
            if ($data) {
                // 转换审核状态
                $array = [];

                // 转换审核状态
                $options = $options ?: \Config::get('constants.'.$field.'_options');
                if ($options) {
                    foreach ($options as $key => $option) {
                        if (in_array($key, $data)) {
                            $array[] = $option;
                        }
                    }
                } else {
                    $array = $data;
                }

                if (!empty($array)) {
                    return implode(',', $array);
                }
            }
        });

        // 返回对象,方便继续链式调用
        return $show;
    }

    /**
     * 渲染生成价格的字段类型
     *
     * @param string $field 字段名
     * @param string $comment 字段注释
     * @return Show\Field
     */
    public function showCurrencyField($field, $comment)
    {
        if (!$comment) {
            $comment = __($field);
        }
        return $this->show->field($field, $comment)->as(function ($data) {
            return '¥'.format_money($data);
        });
    }

    /**
     * 渲染生成价格的字段类型
     *
     * @param string $field 字段名
     * @param string $comment 字段注释
     * @return Show
     */
    public function showSpecialCurrencyField($field, $comment)
    {
        if (!$comment) {
            $comment = __($field);
        }
        // 不存在的`full_name`字段
        $show = $this->show->field($field, $comment)->display(function () {
            return '<span style="color:#e08e0b">'.rtrim(rtrim($this->purchase_price, '0'), '.')
                .'</span> / <span style="color: #999">' .rtrim(rtrim($this->price, '0'), '.').'</span>';
        });

        // 返回对象,方便继续链式调用
        return $show;
    }

    /**
     * 渲染生成单个上传图片字段
     *
     * @param string $field 字段名
     * @param string $comment 字段注释
     * @param string $server
     * @param int $width
     * @param int $height
     * @return Show
     */
    public function showSingleImageField($field = 'cover_image', $comment = '', $server = '', $width = 800, $height = 800)
    {
        if (!$comment) {
            $comment = __($field);
        }
        // 列表的图片可以点击放大,并设置大小
        //$show->$field($comment)->lightbox($options);
//        $show = $this->show->$field($comment)->image($options);
        $show = $this->show->$field($comment)->image($server, $width, $height)->setWidth(10, 1);
        //$show->column($field,$comment)->carousel($width = 300,  $height = 200);
        // 返回对象,方便继续链式调用
        return $show;
    }

    /**
     * 渲染生成单个上传图片字段
     *
     * @param string $field 字段名
     * @param string $comment 字段注释
     * @param array $options
     * @return Show
     */
    public function showMultipleImageField($field = 'images', $comment = '', $options = ['zooming' => true,'width' => 93,'height' => 52,'class' => 'rounded'])
    {
        if (!$comment) {
            $comment = __($field);
        }
        // 设置显示尺寸和图片服务器
        $show = $this->show->$field($comment)->carousel($width = 600, $height = 400)->setWidth(10, 1);

        // 列表的图片可以点击放大,并设置大小
        //$show->$field($comment)->lightbox($options);
        //$show->$field($comment)->gallery($options);
        //$show->column($field,$comment)->carousel($width = 300,  $height = 200);
        // 返回对象,方便继续链式调用
        return $show;
    }

    /**
     * 渲染详情视频字段
     *
     * @param string $field
     * @param string $comment
     * @return Show\Field
     */
    public function showVideoField($field = 'video', $comment = '视频')
    {
        if (!$comment) {
            $comment = __($field);
        }
        return $this->show->field($field, $comment)->video(['videoWidth' => '90%', 'videoHeight' => '90%'])->setWidth(10, 1);
    }

    /**
     * 渲染生成日期类型的字段
     *
     * @param string $field 字段名
     * @param string $comment 字段注释
     * @param string $format 日期转换格式
     * @return Show\Field
     */
    public function showDateField($field = 'created_at', $comment = '', $format = 'Y-m-d H:i:s')
    {
        if (!$comment) {
            $comment = __($field);
        }
        return $this->show->field($field, $comment)->as(function ($data) use ($format) {
            if ($data) {
                return '<span style="white-space: nowrap;">'.Carbon::parse($data) ->format($format).'</span>';
            } else {
                return '--';
            }
        })->setWidth(10, 1);
    }

    /**
     * 渲染生成富文本字段
     *
     * @param string $field 字段名
     * @param string $comment 字段注释
     * @return Show
     */
    public function showRichTextField($field = 'body', $comment = '详情')
    {
        if (!$comment) {
            $comment = __($field);
        }
        return $this->show->field($field, $comment)->setWidth(10, 1);
    }

    /**
     * 渲染生成创建用户字段
     *
     * @param string $field 字段名
     * @param string $comment 字段注释
     * @return Show
     */
    public function showCreatedByField($field, $comment)
    {
        if (!$comment) {
            $comment = __($field);
        }
        // 不存在的`full_name`字段
        return $this->show->field($field, $comment)->as(function ($data) {
            return  '<span class="username">'.Arr::get($data, 'username') .'<br/></span>'.Arr::get($data, 'nickname');
        })->setWidth(10, 1);
    }

    /**
     * 渲染生成创建用户字段
     *
     * @param string $field 字段名
     * @param string $comment 字段注释
     * @return Show\Field
     */
    public function showAuditedByField($field, $comment)
    {
        // 不存在的`full_name`字段
        return $this->show->field($field, $comment)->as(function ($data) {
            return  '<span class="username">'.Arr::get($data, 'username') .'<br/></span>'.Arr::get($data, 'name');
        })->setWidth(10, 1);
    }
}