GridHelperTrait.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. * 添加用户名列
  16. *
  17. * 复用价值:高 - 在多个控制器中使用,提供统一的用户名显示
  18. *
  19. * @param string $field 字段名
  20. * @param string $label 标签名
  21. * @return Column
  22. */
  23. public function columnUsername(string $field = 'username', string $label = '用户名'): Column
  24. {
  25. return $this->grid->column($field, $label);
  26. }
  27. /**
  28. * 添加用户信息组合列
  29. *
  30. * 复用价值:高 - 将用户ID、用户名和头像组合显示,提高信息密度
  31. *
  32. * @param string $idField 用户ID字段名
  33. * @param string $usernameField 用户名字段名
  34. * @param string $avatarField 头像字段名
  35. * @param string $label 标签名
  36. * @return Column
  37. */
  38. public function columnUserInfo(string $idField = 'id', string $usernameField = 'username', string $avatarField = 'avatar', string $label = '用户信息'): Column
  39. {
  40. return $this->grid->column($idField, $label)->display(function ($userId) use ($usernameField, $avatarField) {
  41. $username = $this->{$usernameField} ?? '';
  42. $avatar = $this->{$avatarField} ?? '';
  43. $avatarHtml = $avatar ? "<img src='{$avatar}' width='30' height='30' style='border-radius: 50%; margin-right: 5px;'>" : '';
  44. return <<<HTML
  45. <div style="display: flex; align-items: center;">
  46. {$avatarHtml}
  47. <div>
  48. <div>ID: {$userId}</div>
  49. <div>{$username}</div>
  50. </div>
  51. </div>
  52. HTML;
  53. });
  54. }
  55. /**
  56. * 添加用户联系信息组合列
  57. *
  58. * 复用价值:高 - 将用户手机号、邮箱和微信号组合显示,提高信息密度
  59. *
  60. * @param string $phoneField 手机号字段名
  61. * @param string $emailField 邮箱字段名
  62. * @param string $wxIdField 微信号字段名
  63. * @param string $label 标签名
  64. * @return Column
  65. */
  66. public function columnUserContact(string $phoneField = 'phone', string $emailField = 'email', string $wxIdField = 'wx_id', string $label = '联系方式'): Column
  67. {
  68. return $this->grid->column($phoneField, $label)->display(function ($phone) use ($emailField, $wxIdField) {
  69. $email = $this->{$emailField} ?? '';
  70. $wxId = $this->{$wxIdField} ?? '';
  71. $phoneHtml = $phone ? "<div>手机: {$phone}</div>" : '';
  72. $emailHtml = $email ? "<div>邮箱: {$email}</div>" : '';
  73. $wxIdHtml = $wxId ? "<div>微信: {$wxId}</div>" : '';
  74. return $phoneHtml . $emailHtml . $wxIdHtml;
  75. });
  76. }
  77. /**
  78. * 添加用户安全信息组合列
  79. *
  80. * 复用价值:高 - 将用户安全相关信息组合显示,提高信息密度
  81. *
  82. * @param string $secretPasswordField 安全密码字段名
  83. * @param string $lastCheckAtField 最后验证时间字段名
  84. * @param string $label 标签名
  85. * @return Column
  86. */
  87. public function columnUserSecurity(string $secretPasswordField = 'secret_password', string $lastCheckAtField = 'last_check_at', string $label = '安全信息'): Column
  88. {
  89. return $this->grid->column($secretPasswordField, $label)->display(function ($secretPassword) use ($lastCheckAtField) {
  90. $lastCheckAt = $this->{$lastCheckAtField} ?? '';
  91. $secretPasswordHtml = "<div>安全密码: " . ($secretPassword ? '已设置' : '未设置') . "</div>";
  92. $lastCheckAtHtml = $lastCheckAt ? "<div>最后验证: {$lastCheckAt}</div>" : '';
  93. return $secretPasswordHtml . $lastCheckAtHtml;
  94. });
  95. }
  96. /**
  97. * 添加时间信息组合列 - 使用统一的时间格式化
  98. *
  99. * 复用价值:高 - 将创建时间和更新时间组合显示,提高信息密度
  100. *
  101. * @param string $createdAtField 创建时间字段名
  102. * @param string $updatedAtField 更新时间字段名
  103. * @param string $label 标签名
  104. * @return Column
  105. */
  106. public function columnTimes($createdAtField = 'created_at', $updatedAtField = 'updated_at', $label = '时间信息'): Column
  107. {
  108. return $this->grid->column($createdAtField, $label)->display(function ($createdAt) use ($updatedAtField) {
  109. $updatedAt = $this->{$updatedAtField} ?? '';
  110. // 使用静态方法进行时间格式化
  111. $createdAtFormatted = self::formatDateTimeStatic($createdAt);
  112. $createdAtHtml = "<div><small class='text-muted'>创建:</small> {$createdAtFormatted}</div>";
  113. $updatedAtHtml = '';
  114. if ($updatedAt) {
  115. $updatedAtFormatted = self::formatDateTimeStatic($updatedAt);
  116. $updatedAtHtml = "<div><small class='text-muted'>更新:</small> {$updatedAtFormatted}</div>";
  117. }
  118. return $createdAtHtml . $updatedAtHtml;
  119. })->sortable();
  120. }
  121. /**
  122. * 格式化时间的静态方法
  123. *
  124. * @param mixed $value 时间值
  125. * @return string 格式化后的时间字符串
  126. */
  127. private static function formatDateTimeStatic($value)
  128. {
  129. // 检查空值(但不包括0,因为0是有效的时间戳)
  130. if (is_null($value) || $value === '') {
  131. return '-';
  132. }
  133. // 如果是时间戳,转换为日期时间字符串
  134. if (is_numeric($value)) {
  135. return date('Y-m-d H:i:s', $value);
  136. }
  137. // 如果是Carbon实例或DateTime对象
  138. if ($value instanceof \Carbon\Carbon || $value instanceof \DateTime) {
  139. return $value->format('Y-m-d H:i:s');
  140. }
  141. // 如果是字符串,尝试转换为标准格式
  142. if (is_string($value)) {
  143. try {
  144. $date = new \DateTime($value);
  145. return $date->format('Y-m-d H:i:s');
  146. } catch (\Exception $e) {
  147. return $value; // 如果转换失败,返回原值
  148. }
  149. }
  150. return $value;
  151. }
  152. /**
  153. * 添加用户活动时间组合列
  154. *
  155. * 复用价值:高 - 将最后登录时间和最后活动时间组合显示,提高信息密度
  156. *
  157. * @param string $label 标签名
  158. * @return Column
  159. */
  160. public function columnUserActivityTimes(string $label = '活动时间'): Column
  161. {
  162. return $this->grid->column('info.last_login_time', $label)->display(function ($lastLoginTime) {
  163. $lastActivityTime = $this->info->last_activity_time ?? '';
  164. $loginTimeHtml = $lastLoginTime ? "<div>最后登录: {$lastLoginTime}</div>" : "<div>最后登录: 未登录</div>";
  165. $activityTimeHtml = $lastActivityTime ? "<div>最后活动: {$lastActivityTime}</div>" : "<div>最后活动: 无活动</div>";
  166. return $loginTimeHtml . $activityTimeHtml;
  167. })->sortable();
  168. }
  169. /**
  170. * 添加最后登录时间列
  171. *
  172. * 复用价值:中 - 单独显示最后登录时间
  173. *
  174. * @param string $label 标签名
  175. * @return Column
  176. */
  177. public function columnLastLoginTime(string $label = '最后登录时间'): Column
  178. {
  179. return $this->grid->column('info.last_login_time', $label)->display(function ($value) {
  180. return $value ?: '未登录';
  181. })->sortable();
  182. }
  183. /**
  184. * 添加最后活动时间列
  185. *
  186. * 复用价值:中 - 单独显示最后活动时间
  187. *
  188. * @param string $label 标签名
  189. * @return Column
  190. */
  191. public function columnLastActivityTime(string $label = '最后活动时间'): Column
  192. {
  193. return $this->grid->column('info.last_activity_time', $label)->display(function ($value) {
  194. return $value ?: '无活动';
  195. })->sortable();
  196. }
  197. }