| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <?php
- namespace App\Module\User\AdminControllers\Helper;
- use App\Module\User\Enums\STATUS2;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Grid\Column;
- /**
- * 列表页辅助特性
- *
- * 提供用户模块后台控制器的列表页构建功能的具体实现
- * 只保留具有复用价值的方法
- */
- trait GridHelperTrait
- {
- /**
- * 添加用户名列
- *
- * 复用价值:高 - 在多个控制器中使用,提供统一的用户名显示
- *
- * @param string $field 字段名
- * @param string $label 标签名
- * @return Column
- */
- public function columnUsername(string $field = 'username', string $label = '用户名'): Column
- {
- return $this->grid->column($field, $label);
- }
- /**
- * 添加用户信息组合列
- *
- * 复用价值:高 - 将用户ID、用户名和头像组合显示,提高信息密度
- *
- * @param string $idField 用户ID字段名
- * @param string $usernameField 用户名字段名
- * @param string $avatarField 头像字段名
- * @param string $label 标签名
- * @return Column
- */
- public function columnUserInfo(string $idField = 'id', string $usernameField = 'username', string $avatarField = 'avatar', string $label = '用户信息'): Column
- {
- return $this->grid->column($idField, $label)->display(function ($userId) use ($usernameField, $avatarField) {
- $username = $this->{$usernameField} ?? '';
- $avatar = $this->{$avatarField} ?? '';
- $avatarHtml = $avatar ? "<img src='{$avatar}' width='30' height='30' style='border-radius: 50%; margin-right: 5px;'>" : '';
- return <<<HTML
- <div style="display: flex; align-items: center;">
- {$avatarHtml}
- <div>
- <div>ID: {$userId}</div>
- <div>{$username}</div>
- </div>
- </div>
- HTML;
- });
- }
- /**
- * 添加用户联系信息组合列
- *
- * 复用价值:高 - 将用户手机号、邮箱和微信号组合显示,提高信息密度
- *
- * @param string $phoneField 手机号字段名
- * @param string $emailField 邮箱字段名
- * @param string $wxIdField 微信号字段名
- * @param string $label 标签名
- * @return Column
- */
- public function columnUserContact(string $phoneField = 'phone', string $emailField = 'email', string $wxIdField = 'wx_id', string $label = '联系方式'): Column
- {
- return $this->grid->column($phoneField, $label)->display(function ($phone) use ($emailField, $wxIdField) {
- $email = $this->{$emailField} ?? '';
- $wxId = $this->{$wxIdField} ?? '';
- $phoneHtml = $phone ? "<div>手机: {$phone}</div>" : '';
- $emailHtml = $email ? "<div>邮箱: {$email}</div>" : '';
- $wxIdHtml = $wxId ? "<div>微信: {$wxId}</div>" : '';
- return $phoneHtml . $emailHtml . $wxIdHtml;
- });
- }
- /**
- * 添加用户安全信息组合列
- *
- * 复用价值:高 - 将用户安全相关信息组合显示,提高信息密度
- *
- * @param string $secretPasswordField 安全密码字段名
- * @param string $lastCheckAtField 最后验证时间字段名
- * @param string $label 标签名
- * @return Column
- */
- public function columnUserSecurity(string $secretPasswordField = 'secret_password', string $lastCheckAtField = 'last_check_at', string $label = '安全信息'): Column
- {
- return $this->grid->column($secretPasswordField, $label)->display(function ($secretPassword) use ($lastCheckAtField) {
- $lastCheckAt = $this->{$lastCheckAtField} ?? '';
- $secretPasswordHtml = "<div>安全密码: " . ($secretPassword ? '已设置' : '未设置') . "</div>";
- $lastCheckAtHtml = $lastCheckAt ? "<div>最后验证: {$lastCheckAt}</div>" : '';
- return $secretPasswordHtml . $lastCheckAtHtml;
- });
- }
- /**
- * 添加时间信息组合列 - 使用统一的时间格式化
- *
- * 复用价值:高 - 将创建时间和更新时间组合显示,提高信息密度
- *
- * @param string $createdAtField 创建时间字段名
- * @param string $updatedAtField 更新时间字段名
- * @param string $label 标签名
- * @return Column
- */
- public function columnTimes($createdAtField = 'created_at', $updatedAtField = 'updated_at', $label = '时间信息'): Column
- {
- return $this->grid->column($createdAtField, $label)->display(function ($createdAt) use ($updatedAtField) {
- $updatedAt = $this->{$updatedAtField} ?? '';
- // 使用静态方法进行时间格式化
- $createdAtFormatted = self::formatDateTimeStatic($createdAt);
- $createdAtHtml = "<div><small class='text-muted'>创建:</small> {$createdAtFormatted}</div>";
- $updatedAtHtml = '';
- if ($updatedAt) {
- $updatedAtFormatted = self::formatDateTimeStatic($updatedAt);
- $updatedAtHtml = "<div><small class='text-muted'>更新:</small> {$updatedAtFormatted}</div>";
- }
- return $createdAtHtml . $updatedAtHtml;
- })->sortable();
- }
- /**
- * 格式化时间的静态方法
- *
- * @param mixed $value 时间值
- * @return string 格式化后的时间字符串
- */
- private static function formatDateTimeStatic($value)
- {
- // 检查空值(但不包括0,因为0是有效的时间戳)
- if (is_null($value) || $value === '') {
- return '-';
- }
- // 如果是时间戳,转换为日期时间字符串
- if (is_numeric($value)) {
- return date('Y-m-d H:i:s', $value);
- }
- // 如果是Carbon实例或DateTime对象
- if ($value instanceof \Carbon\Carbon || $value instanceof \DateTime) {
- return $value->format('Y-m-d H:i:s');
- }
- // 如果是字符串,尝试转换为标准格式
- if (is_string($value)) {
- try {
- $date = new \DateTime($value);
- return $date->format('Y-m-d H:i:s');
- } catch (\Exception $e) {
- return $value; // 如果转换失败,返回原值
- }
- }
- return $value;
- }
- /**
- * 添加用户活动时间组合列
- *
- * 复用价值:高 - 将最后登录时间和最后活动时间组合显示,提高信息密度
- *
- * @param string $label 标签名
- * @return Column
- */
- public function columnUserActivityTimes(string $label = '活动时间'): Column
- {
- return $this->grid->column('info.last_login_time', $label)->display(function ($lastLoginTime) {
- $lastActivityTime = $this->info->last_activity_time ?? '';
- $loginTimeHtml = $lastLoginTime ? "<div>最后登录: {$lastLoginTime}</div>" : "<div>最后登录: 未登录</div>";
- $activityTimeHtml = $lastActivityTime ? "<div>最后活动: {$lastActivityTime}</div>" : "<div>最后活动: 无活动</div>";
- return $loginTimeHtml . $activityTimeHtml;
- })->sortable();
- }
- /**
- * 添加最后登录时间列
- *
- * 复用价值:中 - 单独显示最后登录时间
- *
- * @param string $label 标签名
- * @return Column
- */
- public function columnLastLoginTime(string $label = '最后登录时间'): Column
- {
- return $this->grid->column('info.last_login_time', $label)->display(function ($value) {
- return $value ?: '未登录';
- })->sortable();
- }
- /**
- * 添加最后活动时间列
- *
- * 复用价值:中 - 单独显示最后活动时间
- *
- * @param string $label 标签名
- * @return Column
- */
- public function columnLastActivityTime(string $label = '最后活动时间'): Column
- {
- return $this->grid->column('info.last_activity_time', $label)->display(function ($value) {
- return $value ?: '无活动';
- })->sortable();
- }
- }
|