UserLogController.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. ->display(function ($value) {
  55. // 使用原始的、未截断的值
  56. $originalValue = $this->source_table;
  57. if (!$originalValue || !$this->source_id) {
  58. return $value;
  59. }
  60. // 获取对应的后台管理页面URL
  61. $url = self::getSourceDetailUrl($originalValue, $this->source_id);
  62. if ($url) {
  63. return "<a href=\"{$url}\" target=\"_blank\" class=\"text-primary\" title=\"查看来源详情\">" .
  64. "<i class=\"fa fa-external-link\"></i> {$value}</a>";
  65. }
  66. return $value;
  67. });
  68. $grid->column('created_at', '创建时间')
  69. ->sortable();
  70. // 筛选器
  71. $grid->filter(function (Grid\Filter $filter) {
  72. $filter->equal('user_id', '用户ID');
  73. $filter->like('message', '日志消息');
  74. $filter->equal('source_type', '来源类型')
  75. ->select([
  76. 'fund' => '资金',
  77. 'item' => '物品',
  78. 'farm' => '农场',
  79. 'pet' => '宠物',
  80. 'system' => '系统',
  81. ]);
  82. $filter->between('created_at', '创建时间')->datetime();
  83. });
  84. // 默认排序
  85. $grid->model()->orderBy('created_at', 'desc');
  86. // 禁用创建按钮
  87. $grid->disableCreateButton();
  88. // 禁用编辑
  89. $grid->disableEditButton();
  90. // 批量操作
  91. $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
  92. // 暂时注释掉批量删除功能,避免误删重要日志
  93. // $batch->add('清理选中日志', new \App\Module\Game\AdminControllers\Actions\BatchDeleteUserLogsAction());
  94. });
  95. // 工具栏
  96. $grid->tools(function (Grid\Tools $tools) {
  97. // 暂时注释掉工具按钮,避免类不存在错误
  98. // $tools->append(new \App\Module\Game\AdminControllers\Tools\CleanExpiredLogsButton());
  99. // $tools->append(new \App\Module\Game\AdminControllers\Tools\UserLogStatsButton());
  100. });
  101. });
  102. }
  103. /**
  104. * Make a show builder.
  105. *
  106. * @param mixed $id
  107. * @return Show
  108. */
  109. protected function detail($id)
  110. {
  111. return Show::make($id, UserLog::with(['user']), function (Show $show) {
  112. $show->field('id', 'ID');
  113. $show->field('user.username', '用户名');
  114. $show->field('user_id', '用户ID');
  115. $show->field('message', '日志消息')
  116. ->unescape();
  117. $show->field('source_type', '来源类型')
  118. ->using([
  119. 'fund' => '资金',
  120. 'item' => '物品',
  121. 'farm' => '农场',
  122. 'pet' => '宠物',
  123. 'system' => '系统',
  124. ]);
  125. $show->field('source_id', '来源ID');
  126. $show->field('source_table', '来源表名');
  127. $show->field('created_at', '创建时间');
  128. // 禁用编辑和删除按钮
  129. $show->disableEditButton();
  130. $show->disableDeleteButton();
  131. });
  132. }
  133. /**
  134. * Make a form builder.
  135. *
  136. * @return Form
  137. */
  138. protected function form()
  139. {
  140. return Form::make(UserLog::class, function (Form $form) {
  141. $form->display('id', 'ID');
  142. $form->number('user_id', '用户ID')
  143. ->required()
  144. ->help('关联的用户ID');
  145. $form->textarea('message', '日志消息')
  146. ->required()
  147. ->rows(3)
  148. ->help('用户操作的详细描述');
  149. $form->select('source_type', '来源类型')
  150. ->options([
  151. 'fund' => '资金',
  152. 'item' => '物品',
  153. 'farm' => '农场',
  154. 'pet' => '宠物',
  155. 'system' => '系统',
  156. ])
  157. ->help('日志来源的模块类型');
  158. $form->number('source_id', '来源ID')
  159. ->help('关联的业务记录ID');
  160. $form->text('source_table', '来源表名')
  161. ->help('关联的数据库表名');
  162. $form->display('created_at', '创建时间');
  163. // 禁用删除按钮
  164. $form->disableDeleteButton();
  165. });
  166. }
  167. /**
  168. * 获取来源详情页面URL
  169. *
  170. * @param string $sourceTable 来源表名
  171. * @param int $sourceId 来源记录ID
  172. * @return string|null 详情页面URL,null表示无对应页面
  173. */
  174. public static function getSourceDetailUrl(string $sourceTable, int $sourceId): ?string
  175. {
  176. // 来源表名到后台路由的映射关系
  177. $tableRouteMap = [
  178. 'fund_logs' => 'fund-logs',
  179. 'item_transaction_logs' => 'game-items-transaction-logs',
  180. 'farm_harvest_logs' => 'farm-harvest-logs',
  181. 'farm_upgrade_logs' => 'farm-upgrade-logs',
  182. 'pet_battle_logs' => 'pet-battle-logs',
  183. 'pet_skill_logs' => 'pet-skill-logs',
  184. 'task_reward_logs' => 'task-reward-logs',
  185. 'task_reset_logs' => 'task-reset-logs',
  186. 'game_items_dismantle_logs' => 'game-items-dismantle-logs',
  187. 'system_logs' => 'system-logs',
  188. ];
  189. // 检查是否有对应的路由
  190. if (!isset($tableRouteMap[$sourceTable])) {
  191. return null;
  192. }
  193. $route = $tableRouteMap[$sourceTable];
  194. // 生成详情页面URL
  195. return admin_url("{$route}/{$sourceId}");
  196. }
  197. /**
  198. * 清理过期日志
  199. *
  200. * @return \Illuminate\Http\JsonResponse
  201. */
  202. public function cleanExpiredLogs()
  203. {
  204. try {
  205. $days = request('days', 30);
  206. $deletedCount = UserLogService::cleanExpiredLogs($days);
  207. return response()->json([
  208. 'status' => true,
  209. 'message' => "成功清理 {$deletedCount} 条过期日志",
  210. ]);
  211. } catch (\Exception $e) {
  212. return response()->json([
  213. 'status' => false,
  214. 'message' => '清理过期日志失败:' . $e->getMessage(),
  215. ]);
  216. }
  217. }
  218. /**
  219. * 获取用户日志统计信息
  220. *
  221. * @return \Illuminate\Http\JsonResponse
  222. */
  223. public function getStats()
  224. {
  225. try {
  226. $userId = request('user_id');
  227. if (!$userId) {
  228. return response()->json([
  229. 'status' => false,
  230. 'message' => '请提供用户ID',
  231. ]);
  232. }
  233. $stats = UserLogService::getUserLogStats($userId);
  234. return response()->json([
  235. 'status' => true,
  236. 'data' => $stats,
  237. ]);
  238. } catch (\Exception $e) {
  239. return response()->json([
  240. 'status' => false,
  241. 'message' => '获取统计信息失败:' . $e->getMessage(),
  242. ]);
  243. }
  244. }
  245. }