GridHelperTrait.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. namespace App\Module\User\AdminControllers\Helper;
  3. use App\Module\User\Enums\STATUS2;
  4. use Dcat\Admin\Grid;
  5. use Dcat\Admin\Grid\Column;
  6. /**
  7. * 列表页辅助特性
  8. *
  9. * 提供用户模块后台控制器的列表页构建功能的具体实现
  10. * 只保留具有复用价值的方法
  11. */
  12. trait GridHelperTrait
  13. {
  14. /**
  15. * 添加用户ID列
  16. *
  17. * 复用价值:高 - 在多个控制器中使用,提供统一的用户ID显示
  18. *
  19. * @param string $field 字段名
  20. * @param string $label 标签名
  21. * @return Column
  22. */
  23. public function columnUserId(string $field = 'id', string $label = '用户ID'): Column
  24. {
  25. return $this->grid->column($field, $label)->sortable();
  26. }
  27. /**
  28. * 添加用户名列
  29. *
  30. * 复用价值:高 - 在多个控制器中使用,提供统一的用户名显示
  31. *
  32. * @param string $field 字段名
  33. * @param string $label 标签名
  34. * @return Column
  35. */
  36. public function columnUsername(string $field = 'username', string $label = '用户名'): Column
  37. {
  38. return $this->grid->column($field, $label);
  39. }
  40. /**
  41. * 添加用户状态列
  42. *
  43. * 复用价值:高 - 统一处理用户状态的显示,包括状态名称和标签样式
  44. *
  45. * @param string $field 字段名
  46. * @param string $label 标签名
  47. * @return Column
  48. */
  49. public function columnStatus(string $field = 'status2', string $label = '状态'): Column
  50. {
  51. return $this->grid->column($field, $label)
  52. ->using(STATUS2::getNames())
  53. ->label([
  54. STATUS2::Normal->value => 'success',
  55. STATUS2::Disabled->value => 'danger',
  56. STATUS2::Ban->value => 'warning',
  57. ]);
  58. }
  59. /**
  60. * 添加用户头像列
  61. *
  62. * 复用价值:高 - 统一处理用户头像的显示,包括图片大小和样式
  63. *
  64. * @param string $field 字段名
  65. * @param string $label 标签名
  66. * @param int $width 宽度
  67. * @param int $height 高度
  68. * @return Column
  69. */
  70. public function columnAvatar(string $field = 'avatar', string $label = '头像', int $width = 50, int $height = 50): Column
  71. {
  72. return $this->grid->column($field, $label)->image('', $width, $height);
  73. }
  74. /**
  75. * 添加用户信息组合列
  76. *
  77. * 复用价值:高 - 将用户ID、用户名和头像组合显示,提高信息密度
  78. *
  79. * @param string $idField 用户ID字段名
  80. * @param string $usernameField 用户名字段名
  81. * @param string $avatarField 头像字段名
  82. * @param string $label 标签名
  83. * @return Column
  84. */
  85. public function columnUserInfo(string $idField = 'id', string $usernameField = 'username', string $avatarField = 'avatar', string $label = '用户信息'): Column
  86. {
  87. return $this->grid->column($idField, $label)->display(function ($userId) use ($usernameField, $avatarField) {
  88. $username = $this->{$usernameField} ?? '';
  89. $avatar = $this->{$avatarField} ?? '';
  90. $avatarHtml = $avatar ? "<img src='{$avatar}' width='30' height='30' style='border-radius: 50%; margin-right: 5px;'>" : '';
  91. return <<<HTML
  92. <div style="display: flex; align-items: center;">
  93. {$avatarHtml}
  94. <div>
  95. <div>ID: {$userId}</div>
  96. <div>{$username}</div>
  97. </div>
  98. </div>
  99. HTML;
  100. });
  101. }
  102. /**
  103. * 添加用户联系信息组合列
  104. *
  105. * 复用价值:高 - 将用户手机号、邮箱和微信号组合显示,提高信息密度
  106. *
  107. * @param string $phoneField 手机号字段名
  108. * @param string $emailField 邮箱字段名
  109. * @param string $wxIdField 微信号字段名
  110. * @param string $label 标签名
  111. * @return Column
  112. */
  113. public function columnUserContact(string $phoneField = 'phone', string $emailField = 'email', string $wxIdField = 'wx_id', string $label = '联系方式'): Column
  114. {
  115. return $this->grid->column($phoneField, $label)->display(function ($phone) use ($emailField, $wxIdField) {
  116. $email = $this->{$emailField} ?? '';
  117. $wxId = $this->{$wxIdField} ?? '';
  118. $phoneHtml = $phone ? "<div>手机: {$phone}</div>" : '';
  119. $emailHtml = $email ? "<div>邮箱: {$email}</div>" : '';
  120. $wxIdHtml = $wxId ? "<div>微信: {$wxId}</div>" : '';
  121. return $phoneHtml . $emailHtml . $wxIdHtml;
  122. });
  123. }
  124. /**
  125. * 添加用户安全信息组合列
  126. *
  127. * 复用价值:高 - 将用户安全相关信息组合显示,提高信息密度
  128. *
  129. * @param string $secretPasswordField 安全密码字段名
  130. * @param string $lastCheckAtField 最后验证时间字段名
  131. * @param string $label 标签名
  132. * @return Column
  133. */
  134. public function columnUserSecurity(string $secretPasswordField = 'secret_password', string $lastCheckAtField = 'last_check_at', string $label = '安全信息'): Column
  135. {
  136. return $this->grid->column($secretPasswordField, $label)->display(function ($secretPassword) use ($lastCheckAtField) {
  137. $lastCheckAt = $this->{$lastCheckAtField} ?? '';
  138. $secretPasswordHtml = "<div>安全密码: " . ($secretPassword ? '已设置' : '未设置') . "</div>";
  139. $lastCheckAtHtml = $lastCheckAt ? "<div>最后验证: {$lastCheckAt}</div>" : '';
  140. return $secretPasswordHtml . $lastCheckAtHtml;
  141. });
  142. }
  143. /**
  144. * 添加时间信息组合列
  145. *
  146. * 复用价值:高 - 将创建时间和更新时间组合显示,提高信息密度
  147. *
  148. * @param string $createdAtField 创建时间字段名
  149. * @param string $updatedAtField 更新时间字段名
  150. * @param string $label 标签名
  151. * @return Column
  152. */
  153. public function columnTimes(string $createdAtField = 'created_at', string $updatedAtField = 'updated_at', string $label = '时间信息'): Column
  154. {
  155. return $this->grid->column($createdAtField, $label)->display(function ($createdAt) use ($updatedAtField) {
  156. $updatedAt = $this->{$updatedAtField} ?? '';
  157. $createdAtHtml = "<div>创建: {$createdAt}</div>";
  158. $updatedAtHtml = $updatedAt ? "<div>更新: {$updatedAt}</div>" : '';
  159. return $createdAtHtml . $updatedAtHtml;
  160. });
  161. }
  162. }