UserLogController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace App\Module\Game\AdminControllers;
  3. use App\Module\Game\Models\UserLog;
  4. use App\Module\Game\Services\UserLogService;
  5. use Dcat\Admin\Form;
  6. use Dcat\Admin\Grid;
  7. use Dcat\Admin\Show;
  8. use Spatie\RouteAttributes\Attributes\Resource;
  9. use UCore\DcatAdmin\AdminController;
  10. /**
  11. * 用户日志管理控制器
  12. */
  13. #[Resource('game-user-logs', names: 'dcat.admin.game-user-logs')]
  14. class UserLogController extends AdminController
  15. {
  16. /**
  17. * 页面标题
  18. *
  19. * @var string
  20. */
  21. protected $title = '用户日志管理';
  22. /**
  23. * Make a grid builder.
  24. *
  25. * @return Grid
  26. */
  27. protected function grid()
  28. {
  29. return Grid::make(UserLog::with(['user']), function (Grid $grid) {
  30. $grid->column('id', 'ID')->sortable();
  31. $grid->column('user.username', '用户名');
  32. $grid->column('user_id', '用户ID');
  33. $grid->column('message', '日志消息')
  34. ->limit(50)
  35. ->help('用户操作的详细描述');
  36. $grid->column('source_type', '来源类型')
  37. ->using([
  38. 'fund' => '资金',
  39. 'item' => '物品',
  40. 'farm' => '农场',
  41. 'pet' => '宠物',
  42. 'system' => '系统',
  43. ])
  44. ->label([
  45. 'fund' => 'success',
  46. 'item' => 'primary',
  47. 'farm' => 'warning',
  48. 'pet' => 'info',
  49. 'system' => 'default',
  50. ]);
  51. $grid->column('source_id', '来源ID');
  52. $grid->column('source_table', '来源表名')
  53. ->limit(20);
  54. $grid->column('created_at', '创建时间')
  55. ->sortable();
  56. // 筛选器
  57. $grid->filter(function (Grid\Filter $filter) {
  58. $filter->equal('user_id', '用户ID');
  59. $filter->like('message', '日志消息');
  60. $filter->equal('source_type', '来源类型')
  61. ->select([
  62. 'fund' => '资金',
  63. 'item' => '物品',
  64. 'farm' => '农场',
  65. 'pet' => '宠物',
  66. 'system' => '系统',
  67. ]);
  68. $filter->between('created_at', '创建时间')->datetime();
  69. });
  70. // 默认排序
  71. $grid->model()->orderBy('created_at', 'desc');
  72. // 禁用创建按钮
  73. $grid->disableCreateButton();
  74. // 禁用编辑
  75. $grid->disableEditButton();
  76. // 批量操作
  77. $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
  78. // 暂时注释掉批量删除功能,避免误删重要日志
  79. // $batch->add('清理选中日志', new \App\Module\Game\AdminControllers\Actions\BatchDeleteUserLogsAction());
  80. });
  81. // 工具栏
  82. $grid->tools(function (Grid\Tools $tools) {
  83. // 暂时注释掉工具按钮,避免类不存在错误
  84. // $tools->append(new \App\Module\Game\AdminControllers\Tools\CleanExpiredLogsButton());
  85. // $tools->append(new \App\Module\Game\AdminControllers\Tools\UserLogStatsButton());
  86. });
  87. });
  88. }
  89. /**
  90. * Make a show builder.
  91. *
  92. * @param mixed $id
  93. * @return Show
  94. */
  95. protected function detail($id)
  96. {
  97. return Show::make($id, UserLog::with(['user']), function (Show $show) {
  98. $show->field('id', 'ID');
  99. $show->field('user.username', '用户名');
  100. $show->field('user_id', '用户ID');
  101. $show->field('message', '日志消息')
  102. ->unescape();
  103. $show->field('source_type', '来源类型')
  104. ->using([
  105. 'fund' => '资金',
  106. 'item' => '物品',
  107. 'farm' => '农场',
  108. 'pet' => '宠物',
  109. 'system' => '系统',
  110. ]);
  111. $show->field('source_id', '来源ID');
  112. $show->field('source_table', '来源表名');
  113. $show->field('created_at', '创建时间');
  114. // 禁用编辑和删除按钮
  115. $show->disableEditButton();
  116. $show->disableDeleteButton();
  117. });
  118. }
  119. /**
  120. * Make a form builder.
  121. *
  122. * @return Form
  123. */
  124. protected function form()
  125. {
  126. return Form::make(UserLog::class, function (Form $form) {
  127. $form->display('id', 'ID');
  128. $form->number('user_id', '用户ID')
  129. ->required()
  130. ->help('关联的用户ID');
  131. $form->textarea('message', '日志消息')
  132. ->required()
  133. ->rows(3)
  134. ->help('用户操作的详细描述');
  135. $form->select('source_type', '来源类型')
  136. ->options([
  137. 'fund' => '资金',
  138. 'item' => '物品',
  139. 'farm' => '农场',
  140. 'pet' => '宠物',
  141. 'system' => '系统',
  142. ])
  143. ->help('日志来源的模块类型');
  144. $form->number('source_id', '来源ID')
  145. ->help('关联的业务记录ID');
  146. $form->text('source_table', '来源表名')
  147. ->help('关联的数据库表名');
  148. $form->display('created_at', '创建时间');
  149. // 禁用删除按钮
  150. $form->disableDeleteButton();
  151. });
  152. }
  153. /**
  154. * 清理过期日志
  155. *
  156. * @return \Illuminate\Http\JsonResponse
  157. */
  158. public function cleanExpiredLogs()
  159. {
  160. try {
  161. $days = request('days', 30);
  162. $deletedCount = UserLogService::cleanExpiredLogs($days);
  163. return response()->json([
  164. 'status' => true,
  165. 'message' => "成功清理 {$deletedCount} 条过期日志",
  166. ]);
  167. } catch (\Exception $e) {
  168. return response()->json([
  169. 'status' => false,
  170. 'message' => '清理过期日志失败:' . $e->getMessage(),
  171. ]);
  172. }
  173. }
  174. /**
  175. * 获取用户日志统计信息
  176. *
  177. * @return \Illuminate\Http\JsonResponse
  178. */
  179. public function getStats()
  180. {
  181. try {
  182. $userId = request('user_id');
  183. if (!$userId) {
  184. return response()->json([
  185. 'status' => false,
  186. 'message' => '请提供用户ID',
  187. ]);
  188. }
  189. $stats = UserLogService::getUserLogStats($userId);
  190. return response()->json([
  191. 'status' => true,
  192. 'data' => $stats,
  193. ]);
  194. } catch (\Exception $e) {
  195. return response()->json([
  196. 'status' => false,
  197. 'message' => '获取统计信息失败:' . $e->getMessage(),
  198. ]);
  199. }
  200. }
  201. }