ShowHelper.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace UCore\DcatAdmin;
  3. use App\Module\File\Services\ImgService;
  4. use UCore\DcatAdmin\Show\Divider;
  5. use UCore\DcatAdmin\Show\Expand;
  6. use UCore\DcatAdmin\Traits\Options;
  7. use App\Module\File\Img;
  8. use Dcat\Admin\Show;
  9. use Illuminate\Support\Arr;
  10. class ShowHelper
  11. {
  12. use Options;
  13. /**
  14. * @var Show
  15. */
  16. public $show;
  17. /**
  18. * @var AdminController
  19. */
  20. public $controller;
  21. public function __construct($show, $controller)
  22. {
  23. $this->show = $show;
  24. $this->controller = $controller;
  25. $this->disableButton();
  26. }
  27. public function disableButton()
  28. {
  29. $this->show->disableEditButton();
  30. $this->show->disableQuickEdit();
  31. $this->show->disableDeleteButton();
  32. }
  33. public function field($name,$label='')
  34. {
  35. return $this->show->field($name, $label);
  36. }
  37. public function divider($name)
  38. {
  39. return $this->show->fields()->push(new Divider($this->controller->_translation('divider.' . $name)));
  40. }
  41. /**
  42. * 图片字段
  43. *
  44. * @param $name
  45. * @return Show\Field
  46. */
  47. public function fieldImg($name,$label='')
  48. {
  49. return $this->show->field($name, $label)->as(function ($pp){
  50. // dump($pp);
  51. return ImgService::img2imgurl($pp);
  52. })->image();
  53. }
  54. public function fieldTimes($name,$label='')
  55. {
  56. return $this->show->field($name, $label)->as(function ($ts){
  57. // dump($pp);
  58. return date('Y-m-d H:i:s',$ts);
  59. });
  60. }
  61. /**
  62. * 用户Id 字段
  63. *
  64. * @param $name
  65. * @return Expand
  66. */
  67. public function fieldUserId($name='user_id',$label='')
  68. {
  69. return $this->show->field($name, $label)->expand(\App\Module\System\AdminLazyRenderable\UserInfo::make([
  70. 'user_id' => $this->show->model()->$name
  71. ]));
  72. }
  73. /**
  74. * 扩展使用
  75. * @param $name
  76. * @param $make
  77. * @return Expand
  78. */
  79. public function fieldExpland($name, $make,$label='')
  80. {
  81. return $this->show->field($name, $label)->expand($make);
  82. }
  83. public function fieldUseing($name, array $keys,$label='')
  84. {
  85. $this->show->field($name, $label)->using($this->useing($name, $keys));
  86. }
  87. public function fieldModelCats($name,$label='',$default=0)
  88. {
  89. // dd();
  90. $cates = $this->show->model()->getCasts();
  91. $enmu = $cates[$name]??"";
  92. if($enmu === ''){
  93. throw new \Exception("$name is not a model casts");
  94. }
  95. // dump($cates,$enmu::getValueDescription());
  96. $values = $enmu::getValueDescription();
  97. $this->show->field($name, $label)->as(function ($value) use ($values, $default) {
  98. if (is_null($value)) {
  99. return $default;
  100. }
  101. if($value instanceof \BackedEnum){
  102. $value = $value->value;
  103. }
  104. return Arr::get($values, $value, $default);
  105. });
  106. }
  107. /**
  108. * 显示使用Cast类型转换的字段,并使用友好的标签和HTML表格展示
  109. *
  110. * @param string $name 字段名称
  111. * @param string $label 字段标签
  112. * @return \Dcat\Admin\Show\Field
  113. * @throws \Exception 如果字段没有对应的Cast类
  114. */
  115. public function fieldModelCatsJson($name, $label='')
  116. {
  117. $casts = $this->show->model()->getCasts();
  118. $castClass = $casts[$name] ?? "";
  119. if ($castClass === '') {
  120. throw new \Exception("$name is not a model casts");
  121. }
  122. return $this->show->field($name, $label)->as(function ($attributes) use ($castClass) {
  123. if (empty($attributes)) {
  124. return '<div class="empty-data">(无数据)</div>';
  125. }
  126. // 使用反射获取Cast类的属性注释
  127. $reflectionClass = new \ReflectionClass($castClass);
  128. $rows = [];
  129. foreach ($attributes as $key => $value) {
  130. try {
  131. // 尝试获取属性
  132. $property = $reflectionClass->getProperty($key);
  133. $docComment = $property->getDocComment();
  134. // 从注释中提取属性描述
  135. $label = $key;
  136. if ($docComment && preg_match('/\*\s+([^@\n]+)/', $docComment, $matches)) {
  137. // 去除星号和前后空白
  138. $label = trim(str_replace('*', '', $matches[1]));
  139. }
  140. // 格式化值,处理不同类型
  141. if (is_bool($value)) {
  142. $displayValue = $value ? '<span class="text-success">\u662f</span>' : '<span class="text-danger">\u5426</span>';
  143. } elseif (is_array($value) || is_object($value)) {
  144. $displayValue = '<code>' . htmlspecialchars(json_encode($value, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)) . '</code>';
  145. } else {
  146. $displayValue = htmlspecialchars((string)$value);
  147. }
  148. $rows[] = [
  149. 'label' => $label,
  150. 'value' => $displayValue,
  151. ];
  152. } catch (\ReflectionException $e) {
  153. // 如果属性不存在,使用原始键名
  154. $rows[] = [
  155. 'label' => $key,
  156. 'value' => is_array($value) || is_object($value) ?
  157. '<code>' . htmlspecialchars(json_encode($value, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)) . '</code>' :
  158. htmlspecialchars((string)$value),
  159. ];
  160. }
  161. }
  162. // 生成HTML表格
  163. $html = '<table class="table table-bordered table-striped table-hover" style="width: 100%; max-width: 100%;">';
  164. $html .= '<thead><tr><th style="width: 30%">属性</th><th>值</th></tr></thead>';
  165. $html .= '<tbody>';
  166. foreach ($rows as $row) {
  167. $html .= '<tr>';
  168. $html .= '<td><strong>' . $row['label'] . '</strong></td>';
  169. $html .= '<td>' . $row['value'] . '</td>';
  170. $html .= '</tr>';
  171. }
  172. $html .= '</tbody></table>';
  173. return $html;
  174. })->unescape();
  175. }
  176. }