| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- <?php
- namespace App\Module\User\AdminControllers;
- use App\Module\User\AdminControllers\Helper\FilterHelper;
- use App\Module\User\AdminControllers\Helper\FormHelper;
- use App\Module\User\AdminControllers\Helper\GridHelper;
- use App\Module\User\AdminControllers\Helper\ShowHelper;
- use App\Module\User\Enums\ACTION_STATUS;
- use App\Module\User\Enums\ACTION_TYPE;
- use App\Module\User\Models\UserAction;
- use App\Module\User\Repositorys\UserActionRepository;
- 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;
- /**
- * 用户操作记录管理控制器
- */
- #[Resource('user-actions', names: 'dcat.admin.user-actions')]
- class UserActionController 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 UserActionRepository(), function (Grid $grid) {
- $helper = new GridHelper($grid, $this);
- $grid->column('id', 'ID')->sortable();
- $grid->column('user_id', '用户ID')->sortable();
- $grid->column('admin_id', '管理员ID');
- $grid->column('type', '操作类型')->display(function ($value) {
- $typeMap = [
- 'Ban' => '封禁',
- 'Restrict' => '禁止登录',
- 'Delete' => '删除',
- 'Normal' => '正常化',
- ];
- // $value 是 ACTION_TYPE 枚举对象,需要获取其 name 属性
- $typeName = $value instanceof ACTION_TYPE ? $value->name : $value;
- return $typeMap[$typeName] ?? $typeName;
- });
- $grid->column('desc', '描述')->limit(30);
- $grid->column('exp_time', '有效期')->display(function ($value) {
- return $value ? date('Y-m-d H:i:s', $value) : '永久';
- });
- $grid->column('status', '状态')->display(function ($value) {
- $statusMap = [
- ACTION_STATUS::ING->value => '生效中',
- ACTION_STATUS::OK->value => '已生效',
- ACTION_STATUS::OUT_TIME->value => '已过期',
- ACTION_STATUS::END->value => '已结束',
- ];
- // $value 是 ACTION_STATUS 枚举对象,需要获取其 value 属性
- $statusValue = $value instanceof ACTION_STATUS ? $value->value : $value;
- return $statusMap[$statusValue] ?? '未知';
- })->label([
- ACTION_STATUS::ING->value => 'success',
- ACTION_STATUS::OK->value => 'info',
- ACTION_STATUS::OUT_TIME->value => 'warning',
- ACTION_STATUS::END->value => 'default',
- ]);
- $grid->column('re_type', '关联类型');
- $grid->column('re_id', '关联ID');
- $helper->columnCreatedAt();
- // 筛选器
- $grid->filter(function (Grid\Filter $filter) {
- $helper = new FilterHelper($filter, $this);
- $filter->equal('id', 'ID');
- $filter->equal('user_id', '用户ID');
- $filter->equal('admin_id', '管理员ID');
- $filter->equal('type', '操作类型')->select([
- 'Ban' => '封禁',
- 'Restrict' => '禁止登录',
- 'Delete' => '删除',
- 'Normal' => '正常化',
- ]);
- $filter->like('desc', '描述');
- $filter->equal('status', '状态')->select([
- ACTION_STATUS::ING->value => '生效中',
- ACTION_STATUS::OK->value => '已生效',
- ACTION_STATUS::OUT_TIME->value => '已过期',
- ACTION_STATUS::END->value => '已结束',
- ]);
- $filter->equal('re_type', '关联类型');
- $filter->equal('re_id', '关联ID');
- $helper->betweenCreatedAt();
- });
- });
- }
- /**
- * 详情页面
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make($id, new UserActionRepository(), function (Show $show) {
- $helper = new ShowHelper($show, $this);
- $show->field('id', 'ID');
- $show->field('user_id', '用户ID');
- $show->field('admin_id', '管理员ID');
- $show->field('type', '操作类型')->as(function ($value) {
- $typeMap = [
- 'Ban' => '封禁',
- 'Restrict' => '禁止登录',
- 'Delete' => '删除',
- 'Normal' => '正常化',
- ];
- // $value 是 ACTION_TYPE 枚举对象,需要获取其 name 属性
- $typeName = $value instanceof ACTION_TYPE ? $value->name : $value;
- return $typeMap[$typeName] ?? $typeName;
- });
- $show->field('desc', '描述');
- $show->field('exp_time', '有效期')->as(function ($value) {
- return $value ? date('Y-m-d H:i:s', $value) : '永久';
- });
- $show->field('status', '状态')->as(function ($value) {
- $statusMap = [
- ACTION_STATUS::ING->value => '生效中',
- ACTION_STATUS::OK->value => '已生效',
- ACTION_STATUS::OUT_TIME->value => '已过期',
- ACTION_STATUS::END->value => '已结束',
- ];
- // $value 是 ACTION_STATUS 枚举对象,需要获取其 value 属性
- $statusValue = $value instanceof ACTION_STATUS ? $value->value : $value;
- return $statusMap[$statusValue] ?? '未知';
- });
- $show->field('re_type', '关联类型');
- $show->field('re_id', '关联ID');
- });
- }
- /**
- * 表单页面
- *
- * @return Form
- */
- protected function form()
- {
- return Form::make(new UserActionRepository(), function (Form $form) {
- $helper = new FormHelper($form, $this);
- $form->display('id', 'ID');
- $form->number('user_id', '用户ID')
- ->required()
- ->min(1)
- ->help('用户ID,必须是有效的用户');
- $form->number('admin_id', '管理员ID')
- ->required()
- ->min(1)
- ->help('管理员ID,必须是有效的管理员');
- $form->select('type', '操作类型')
- ->options([
- 'Ban' => '封禁',
- 'Restrict' => '禁止登录',
- 'Delete' => '删除',
- 'Normal' => '正常化',
- ])
- ->required();
- $form->textarea('desc', '描述')
- ->required()
- ->rows(3)
- ->help('操作描述,说明操作原因等');
- $form->datetime('exp_time', '有效期')
- ->help('操作有效期,留空表示永久有效')
- ->saving(function ($value) {
- return $value ? strtotime($value) : null;
- });
- $form->radio('status', '状态')
- ->options([
- ACTION_STATUS::ING->value => '生效中',
- ACTION_STATUS::OK->value => '已生效',
- ACTION_STATUS::OUT_TIME->value => '已过期',
- ACTION_STATUS::END->value => '已结束',
- ])
- ->default(ACTION_STATUS::ING->value);
- $form->text('re_type', '关联类型')
- ->help('关联的业务类型,如order, recharge等');
- $form->number('re_id', '关联ID')
- ->default(0)
- ->help('关联的业务ID,如订单ID等');
- $form->display('created_at', '创建时间');
- $form->display('updated_at', '更新时间');
- // 保存前回调
- $form->saving(function (Form $form) {
- // 如果是新建记录,需要检查用户ID是否存在
- if ($form->isCreating()) {
- $userId = $form->user_id;
- $user = \App\Module\User\Models\User::find($userId);
- if (!$user) {
- return $form->response()->error('用户ID不存在');
- }
- }
- });
- });
- }
- }
|