ShowHelper.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace UCore\DcatAdmin;
  3. use UCore\DcatAdmin\Show\Divider;
  4. use UCore\DcatAdmin\Show\Expand;
  5. use UCore\DcatAdmin\Traits\Options;
  6. use App\Module\File\Img;
  7. use Dcat\Admin\Show;
  8. use Illuminate\Support\Arr;
  9. class ShowHelper
  10. {
  11. use Options;
  12. /**
  13. * @var Show
  14. */
  15. public $show;
  16. /**
  17. * @var AdminController
  18. */
  19. public $controller;
  20. public function __construct($show, $controller)
  21. {
  22. $this->show = $show;
  23. $this->controller = $controller;
  24. $this->disableButton();
  25. }
  26. public function disableButton()
  27. {
  28. $this->show->disableEditButton();
  29. $this->show->disableQuickEdit();
  30. $this->show->disableDeleteButton();
  31. }
  32. public function field($name,$label='')
  33. {
  34. return $this->show->field($name, $label);
  35. }
  36. public function divider($name)
  37. {
  38. return $this->show->fields()->push(new Divider($this->controller->_translation('divider.' . $name)));
  39. }
  40. /**
  41. * 图片字段
  42. *
  43. * @param $name
  44. * @return Show\Field
  45. */
  46. public function fieldImg($name,$label='')
  47. {
  48. return $this->show->field($name, $label)->as(function ($pp){
  49. // dump($pp);
  50. return Img::img2imgurl($pp);
  51. })->image();
  52. }
  53. public function fieldTimes($name,$label='')
  54. {
  55. return $this->show->field($name, $label)->as(function ($ts){
  56. // dump($pp);
  57. return date('Y-m-d H:i:s',$ts);
  58. });
  59. }
  60. /**
  61. * 用户Id 字段
  62. *
  63. * @param $name
  64. * @return Expand
  65. */
  66. public function fieldUserId($name,$label='')
  67. {
  68. return $this->show->field($name, $label)->expand(\App\Module\System\AdminLazyRenderable\UserInfo::make([
  69. 'user_id' => $this->show->model()->$name
  70. ]));
  71. }
  72. /**
  73. * 扩展使用
  74. * @param $name
  75. * @param $make
  76. * @return Expand
  77. */
  78. public function fieldExpland($name, $make,$label='')
  79. {
  80. return $this->show->field($name, $label)->expand($make);
  81. }
  82. public function fieldUseing($name, array $keys,$label='')
  83. {
  84. $this->show->field($name, $label)->using($this->useing($name, $keys));
  85. }
  86. public function fieldModelCats($name,$label='',$default=0)
  87. {
  88. // dd();
  89. $cates = $this->show->model()->getCasts();
  90. $enmu = $cates[$name]??"";
  91. if($enmu === ''){
  92. throw new \Exception("$name is not a model casts");
  93. }
  94. // dump($cates,$enmu::getValueDescription());
  95. $values = $enmu::getValueDescription();
  96. $this->show->field($name, $label)->as(function ($value) use ($values, $default) {
  97. if (is_null($value)) {
  98. return $default;
  99. }
  100. if($value instanceof \BackedEnum){
  101. $value = $value->value();
  102. }
  103. return Arr::get($values, $value, $default);
  104. });
  105. }
  106. }