| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289 |
- <?php
- namespace App\Module\Game\AdminControllers;
- use App\Module\AppGame\AdminControllers\Actions\ResetLogin;
- use App\Module\User\AdminControllers\Actions\ChangePasswordAction;
- use App\Module\User\AdminControllers\Actions\UserRelatedPagesAction;
- use App\Module\Game\AdminControllers\Helper\FilterHelper;
- use App\Module\Game\AdminControllers\Helper\GridHelper;
- use App\Module\Game\AdminControllers\Helper\ShowHelper;
- use App\Module\User\Enums\STATUS2;
- use App\Module\User\Repositorys\UserRepository;
- use App\Module\User\Services\UserService;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Spatie\RouteAttributes\Attributes\Resource;
- use UCore\DcatAdmin\AdminController;
- /**
- * 游戏用户管理控制器
- *
- * 专门用于管理游戏相关的用户信息,包括资金、物品、土地、神像buff、作物等游戏数据
- */
- #[Resource('game-users', names: 'dcat.admin.game-users')]
- class UserController extends AdminController
- {
- /**
- * 页面标题
- *
- * @var string
- */
- protected $title = '游戏用户管理';
- /**
- * 用户服务
- *
- * @var UserService
- */
- protected $service;
- /**
- * 构造函数
- */
- public function __construct()
- {
- $this->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', '用户名');
- // 添加联系方式列
- $grid->column('contact', '联系方式')->display(function ($value) {
- // 获取当前行的模型数据
- $model = $this;
- $phone = $model->primaryPhone ? $model->primaryPhone->phone : '';
- $email = $model->email ?? '';
- $contacts = [];
- if ($phone) {
- $contacts[] = '<span class="badge badge-primary">' . $phone . '</span>';
- }
- if ($email) {
- $contacts[] = '<span class="badge badge-info">' . $email . '</span>';
- }
- return $contacts ? implode(' ', $contacts) : '<span class="text-muted">无</span>';
- });
- // 添加游戏状态列
- $grid->column('game_status', '游戏状态')->display(function () {
- $model = $this;
- $farmUser = $model->farmUser;
- $status = [];
- if ($farmUser) {
- $status[] = '<span class="badge badge-success">农场等级: ' . $farmUser->house_level . '</span>';
- } else {
- $status[] = '<span class="badge badge-secondary">未开通农场</span>';
- }
- return implode('<br>', $status);
- });
- // 添加资金账户列
- $grid->column('fund_accounts', '资金账户')->display(function () {
- $model = $this;
- $fundAccounts = $model->fundAccounts;
- if ($fundAccounts->isEmpty()) {
- return '<span class="text-muted">无账户</span>';
- }
- $accounts = [];
- foreach ($fundAccounts as $account) {
- // dd($account->fund_config);
- $fundName = $account->fund_config->name;
- $balance = number_format($account->balance);
- $accounts[] = '<span class="badge badge-primary">' . $fundName . ': ' . $balance . '</span>';
- }
- return implode('<br>', $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 '<span class="badge badge-success">今天</span>';
- } elseif ($diffInDays <= 7) {
- return '<span class="badge badge-warning">' . $diffInDays . '天前</span>';
- } elseif ($diffInDays <= 30) {
- return '<span class="badge badge-info">' . $diffInDays . '天前</span>';
- } else {
- return '<span class="badge badge-secondary">' . $diffInDays . '天前</span>';
- }
- } else {
- return '<span class="text-muted">未知</span>';
- }
- });
- // 添加物品背包列
- $grid->column('items_summary', '物品背包')->display(function () {
- $model = $this;
- $items = $model->items;
- if ($items->isEmpty()) {
- return '<span class="text-muted">无物品</span>';
- }
- $totalItems = $items->count();
- $totalQuantity = $items->sum('quantity');
- return '<span class="badge badge-info">物品种类: ' . $totalItems . '</span><br>' .
- '<span class="badge badge-success">总数量: ' . $totalQuantity . '</span>';
- });
- // 添加土地状态列
- $grid->column('land_status', '土地状态')->display(function () {
- $model = $this;
- $lands = $model->lands;
- if ($lands->isEmpty()) {
- return '<span class="text-muted">无土地</span>';
- }
- $totalLands = $lands->count();
- $plantingLands = $lands->where('status', 1)->count(); // 种植中
- $harvestableLands = $lands->where('status', 3)->count(); // 可收获
- $status = [];
- $status[] = '<span class="badge badge-primary">总土地: ' . $totalLands . '</span>';
- if ($plantingLands > 0) {
- $status[] = '<span class="badge badge-warning">种植中: ' . $plantingLands . '</span>';
- }
- if ($harvestableLands > 0) {
- $status[] = '<span class="badge badge-success">可收获: ' . $harvestableLands . '</span>';
- }
- return implode('<br>', $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) {
- $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->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', '更新时间');
- });
- }
- }
|