service = new UserService();
}
/**
* 列表页面
*
* @return Grid
*/
protected function grid()
{
return Grid::make(new UserRepository(['info', 'primaryPhone', 'farmUser', 'fundAccounts', 'fundAccounts.fund_config','items.item', 'lands.landType', 'crops.seed']), function (Grid $grid) {
$helper = new GridHelper($grid, $this);
$grid->model()->where('id','>',10000);
// 基础列
$grid->column('id', '用户ID');
$grid->column('username', '用户名');
// 用户活动时间
$helper->columnUserActivityTimes(); // 组合列,显示最后登录时间和最后活动时间
// 添加联系方式列
$grid->column('contact', '联系方式')->display(function ($value) {
// 获取当前行的模型数据
$model = $this;
$phone = $model->primaryPhone ? $model->primaryPhone->phone : '';
$email = $model->email ?? '';
$contacts = [];
if ($phone) {
$contacts[] = '' . $phone . '';
}
if ($email) {
$contacts[] = '' . $email . '';
}
return $contacts ? implode(' ', $contacts) : '无';
});
// 添加游戏状态列
$grid->column('game_status', '游戏状态')->display(function () {
$model = $this;
$farmUser = $model->farmUser;
$status = [];
if ($farmUser) {
$status[] = '农场等级: ' . $farmUser->house_level . '';
} else {
$status[] = '未开通农场';
}
return implode('
', $status);
});
// 添加资金账户列
$grid->column('fund_accounts', '资金账户')->display(function () {
$model = $this;
$fundAccounts = $model->fundAccounts;
if ($fundAccounts->isEmpty()) {
return '无账户';
}
$accounts = [];
foreach ($fundAccounts as $account) {
// dd($account->fund_config);
$fundName = $account->fund_config->name;
$balance = NumberWan::formatToWan($account->balance);
$accounts[] = '' . $fundName . ': ' . $balance . '';
}
return implode('
', $accounts);
});
// 添加最后活跃时间列
$grid->column('last_active', '最后活跃')->display(function () {
$model = $this;
$lastActive = $model->getAttribute('updated_at');
if ($lastActive) {
$diffInDays = intval($lastActive->diffInDays(now()));
if ($diffInDays == 0) {
return '今天';
} elseif ($diffInDays <= 7) {
return '' . $diffInDays . '天前';
} elseif ($diffInDays <= 30) {
return '' . $diffInDays . '天前';
} else {
return '' . $diffInDays . '天前';
}
} else {
return '未知';
}
});
// 添加物品背包列
$grid->column('items_summary', '物品背包')->display(function () {
$model = $this;
$items = $model->items;
if ($items->isEmpty()) {
return '无物品';
}
$totalItems = $items->count();
$totalQuantity = $items->sum('quantity');
return '物品种类: ' . $totalItems . '
' .
'总数量: ' . $totalQuantity . '';
});
// 添加土地状态列
$grid->column('land_status', '土地状态')->display(function () {
$model = $this;
$lands = $model->lands;
if ($lands->isEmpty()) {
return '无土地';
}
$totalLands = $lands->count();
$plantingLands = $lands->where('status', 1)->count(); // 种植中
$harvestableLands = $lands->where('status', 3)->count(); // 可收获
$status = [];
$status[] = '总土地: ' . $totalLands . '';
if ($plantingLands > 0) {
$status[] = '种植中: ' . $plantingLands . '';
}
if ($harvestableLands > 0) {
$status[] = '可收获: ' . $harvestableLands . '';
}
return implode('
', $status);
});
$grid->column('created_at', '创建时间');
$grid->column('updated_at', '更新时间');
// 行操作
$grid->actions(function (Grid\Displayers\Actions $actions){
// 禁用删除按钮
$actions->disableDelete();
// 添加修改密码操作
$actions->append(new ChangePasswordAction());
$actions->append(ResetLogin::make());
// 添加相关页面链接操作
$actions->append(new UserRelatedPagesAction());
});
// 筛选器
$grid->filter(function (Grid\Filter $filter) {
$helper = new FilterHelper($filter, $this);
$helper->equalUserId();
$helper->likeUsername(); // 用户名筛选
$helper->likeEmail(); // 邮箱筛选
});
$grid->paginate(10);
});
}
/**
* 详情页面
*
* @param mixed $id
* @return Show
*/
protected function detail($id)
{
return Show::make($id, new UserRepository(), function (Show $show) {
// 确保加载用户信息关联
$show->model()->load('info');
$helper = new ShowHelper($show, $this);
// 使用高复用价值的面板方法
$helper->show->divider('基本信息');
$helper->fieldUserId();
$helper->fieldUsername();
$helper->show->field('nickname', '昵称');
$helper->fieldAvatar();
$helper->fieldStatus();
$helper->show->divider('联系信息');
$helper->show->field('phone', '手机号');
$helper->show->field('email', '邮箱');
$helper->show->field('wx_id', '微信号');
$helper->show->divider('安全信息');
$helper->fieldSecretPassword();
$helper->show->field('last_check_at', '最后验证时间');
$helper->show->divider('活动信息');
$helper->fieldLastLoginTime();
$helper->fieldLastActivityTime();
$helper->show->divider('时间信息');
$helper->show->field('created_at', '创建时间');
$helper->show->field('updated_at', '更新时间');
$helper->show->field('deleted_at', '删除时间');
// 显示其他特殊字段
$show->field('google2fa_secret', 'Google双因素密钥');
});
}
/**
* 表单页面
*
* @return Form
*/
protected function form()
{
return Form::make(new UserRepository(), function (Form $form) {
$form->display('id', 'ID');
// 用户基本信息
$form->text('username', '用户名')
->required()
->rules('required|max:100');
$form->password('password', '密码')
->help('不修改请留空')
->saving(function ($value) {
if ($value) {
return \Illuminate\Support\Facades\Hash::make($value);
}
});
$form->radio('status2', '状态')
->options([
STATUS2::Normal->value => '正常',
STATUS2::Restrict->value => '限制登录',
STATUS2::Ban->value => '封禁',
STATUS2::Hidden->value => '隐藏账户',
STATUS2::Deleteing->value => '删除中',
])
->default(STATUS2::Normal->value);
// 添加其他特殊字段
$form->text('google2fa_secret', 'Google双因素密钥');
$form->display('created_at', '创建时间');
$form->display('updated_at', '更新时间');
});
}
}