UserLogController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. <?php
  2. namespace App\Module\Game\AdminControllers;
  3. use App\Module\Game\Enums\REWARD_SOURCE_TYPE;
  4. use App\Module\Game\Models\UserLog;
  5. use App\Module\Game\Services\UserLogService;
  6. use Dcat\Admin\Form;
  7. use Dcat\Admin\Grid;
  8. use Dcat\Admin\Show;
  9. use Spatie\RouteAttributes\Attributes\Resource;
  10. use UCore\DcatAdmin\AdminController;
  11. /**
  12. * 用户日志管理控制器
  13. */
  14. #[Resource('game-user-logs', names: 'dcat.admin.game-user-logs')]
  15. class UserLogController extends AdminController
  16. {
  17. /**
  18. * 页面标题
  19. *
  20. * @var string
  21. */
  22. protected $title = '用户日志管理';
  23. /**
  24. * Make a grid builder.
  25. *
  26. * @return Grid
  27. */
  28. protected function grid()
  29. {
  30. return Grid::make(UserLog::with(['user']), function (Grid $grid) {
  31. $grid->column('id', 'ID')->sortable();
  32. $grid->column('user.username', '用户名');
  33. $grid->column('user_id', '用户ID');
  34. $grid->column('message', '日志消息')
  35. ->limit(50)
  36. ->help('用户操作的详细描述');
  37. $grid->column('source_type', '来源类型')
  38. ->display(function ($value) {
  39. if (!$value) {
  40. return '<span class="label label-default">未知</span>';
  41. }
  42. // 处理枚举实例或字符串
  43. $typeValue = $value instanceof REWARD_SOURCE_TYPE ? $value->value : $value;
  44. // 使用枚举获取名称和信息
  45. if (REWARD_SOURCE_TYPE::isValid($typeValue)) {
  46. $info = REWARD_SOURCE_TYPE::getTypeInfo($typeValue);
  47. $name = $info['name'];
  48. $icon = $info['icon'];
  49. $category = $info['category'];
  50. // 根据分类设置标签颜色
  51. $labelColors = [
  52. 'gameplay' => 'primary',
  53. 'event' => 'warning',
  54. 'daily' => 'info',
  55. 'achievement' => 'success',
  56. 'progression' => 'success',
  57. 'farm' => 'warning',
  58. 'social' => 'info',
  59. 'shop' => 'primary',
  60. 'promotion' => 'danger',
  61. 'competition' => 'success',
  62. 'special' => 'warning',
  63. 'system' => 'default',
  64. 'unknown' => 'default'
  65. ];
  66. $color = $labelColors[$category] ?? 'default';
  67. return "<span class=\"label label-{$color}\" title=\"{$info['description']}\">{$icon} {$name}</span>";
  68. }
  69. return "<span class=\"label label-default\">{$value}</span>";
  70. });
  71. $grid->column('source_id', '来源ID');
  72. $grid->column('source_table', '来源表名')
  73. ->limit(20)
  74. ->display(function ($value) {
  75. // 使用原始的、未截断的值
  76. $originalValue = $this->source_table;
  77. if (!$originalValue || !$this->source_id) {
  78. return $value;
  79. }
  80. // 获取对应的后台管理页面URL
  81. $url = self::getSourceDetailUrl($originalValue, $this->source_id);
  82. if ($url) {
  83. return "<a href=\"{$url}\" target=\"_blank\" class=\"text-primary\" title=\"查看来源详情\">" .
  84. "<i class=\"fa fa-external-link\"></i> {$value}</a>";
  85. }
  86. return $value;
  87. });
  88. $grid->column('original_time', '原始时间')
  89. ->display(function ($value) {
  90. return $value ? $value->format('Y-m-d H:i:s') : '-';
  91. })
  92. ->sortable()
  93. ->help('业务发生的原始时间');
  94. $grid->column('created_at', '收集时间')
  95. ->display(function ($value) {
  96. return $value ? $value->format('Y-m-d H:i:s') : '-';
  97. })
  98. ->sortable()
  99. ->help('日志收集时间');
  100. // 筛选器
  101. $grid->filter(function (Grid\Filter $filter) {
  102. $filter->equal('user_id', '用户ID');
  103. $filter->like('message', '日志消息');
  104. // 使用枚举生成来源类型选项
  105. $sourceTypeOptions = [];
  106. foreach (REWARD_SOURCE_TYPE::cases() as $case) {
  107. $info = REWARD_SOURCE_TYPE::getTypeInfo($case->value);
  108. $sourceTypeOptions[$case->value] = $info['icon'] . ' ' . $info['name'];
  109. }
  110. $filter->equal('source_type', '来源类型')
  111. ->select($sourceTypeOptions);
  112. $filter->between('original_time', '原始时间')->datetime();
  113. });
  114. // 默认排序 - 按原始日志创建时间排序
  115. $grid->model()->orderBy('original_time', 'desc');
  116. // 禁用创建按钮
  117. $grid->disableCreateButton();
  118. // 禁用编辑
  119. $grid->disableEditButton();
  120. // 批量操作
  121. $grid->batchActions(function (Grid\Tools\BatchActions $batch) {
  122. // 暂时注释掉批量删除功能,避免误删重要日志
  123. // $batch->add('清理选中日志', new \App\Module\Game\AdminControllers\Actions\BatchDeleteUserLogsAction());
  124. });
  125. // 工具栏
  126. $grid->tools(function (Grid\Tools $tools) {
  127. // 暂时注释掉工具按钮,避免类不存在错误
  128. // $tools->append(new \App\Module\Game\AdminControllers\Tools\CleanExpiredLogsButton());
  129. // $tools->append(new \App\Module\Game\AdminControllers\Tools\UserLogStatsButton());
  130. });
  131. });
  132. }
  133. /**
  134. * Make a show builder.
  135. *
  136. * @param mixed $id
  137. * @return Show
  138. */
  139. protected function detail($id)
  140. {
  141. return Show::make($id, UserLog::with(['user']), function (Show $show) {
  142. $show->field('id', 'ID');
  143. $show->field('user.username', '用户名');
  144. $show->field('user_id', '用户ID');
  145. $show->field('message', '日志消息')
  146. ->unescape();
  147. $show->field('source_type', '来源类型')
  148. ->as(function ($value) {
  149. if (!$value) {
  150. return '未知';
  151. }
  152. // 处理枚举实例或字符串
  153. $typeValue = $value instanceof REWARD_SOURCE_TYPE ? $value->value : $value;
  154. if (REWARD_SOURCE_TYPE::isValid($typeValue)) {
  155. $info = REWARD_SOURCE_TYPE::getTypeInfo($typeValue);
  156. return $info['icon'] . ' ' . $info['name'] . ' (' . $info['description'] . ')';
  157. }
  158. return $value;
  159. });
  160. $show->field('source_id', '来源ID');
  161. $show->field('source_table', '来源表名');
  162. $show->field('original_time', '原始时间')
  163. ->help('业务发生的原始时间');
  164. $show->field('created_at', '收集时间')
  165. ->help('日志收集时间');
  166. // 禁用编辑和删除按钮
  167. $show->disableEditButton();
  168. $show->disableDeleteButton();
  169. });
  170. }
  171. /**
  172. * Make a form builder.
  173. *
  174. * @return Form
  175. */
  176. protected function form()
  177. {
  178. return Form::make(UserLog::class, function (Form $form) {
  179. $form->display('id', 'ID');
  180. $form->number('user_id', '用户ID')
  181. ->required()
  182. ->help('关联的用户ID');
  183. $form->textarea('message', '日志消息')
  184. ->required()
  185. ->rows(3)
  186. ->help('用户操作的详细描述');
  187. // 使用枚举生成来源类型选项
  188. $sourceTypeOptions = [];
  189. foreach (REWARD_SOURCE_TYPE::cases() as $case) {
  190. $info = REWARD_SOURCE_TYPE::getTypeInfo($case->value);
  191. $sourceTypeOptions[$case->value] = $info['icon'] . ' ' . $info['name'];
  192. }
  193. $form->select('source_type', '来源类型')
  194. ->options($sourceTypeOptions)
  195. ->help('日志来源的模块类型,使用标准化的奖励来源枚举');
  196. $form->number('source_id', '来源ID')
  197. ->help('关联的业务记录ID');
  198. $form->text('source_table', '来源表名')
  199. ->help('关联的数据库表名');
  200. $form->display('created_at', '创建时间');
  201. // 禁用删除按钮
  202. $form->disableDeleteButton();
  203. });
  204. }
  205. /**
  206. * 获取来源详情页面URL
  207. *
  208. * @param string $sourceTable 来源表名
  209. * @param int $sourceId 来源记录ID
  210. * @return string|null 详情页面URL,null表示无对应页面
  211. */
  212. public static function getSourceDetailUrl(string $sourceTable, int $sourceId): ?string
  213. {
  214. // 来源表名到后台路由的映射关系
  215. $tableRouteMap = [
  216. 'fund_logs' => 'fund-logs',
  217. 'item_transaction_logs' => 'game-items-transaction-logs',
  218. 'farm_harvest_logs' => 'farm-harvest-logs',
  219. 'farm_upgrade_logs' => 'farm-upgrade-logs',
  220. 'pet_battle_logs' => 'pet-battle-logs',
  221. 'pet_skill_logs' => 'pet-skill-logs',
  222. 'task_reward_logs' => 'task-reward-logs',
  223. 'task_reset_logs' => 'task-reset-logs',
  224. 'game_items_dismantle_logs' => 'game-items-dismantle-logs',
  225. 'system_logs' => 'system-logs',
  226. ];
  227. // 检查是否有对应的路由
  228. if (!isset($tableRouteMap[$sourceTable])) {
  229. return null;
  230. }
  231. $route = $tableRouteMap[$sourceTable];
  232. // 生成详情页面URL
  233. return admin_url("{$route}/{$sourceId}");
  234. }
  235. /**
  236. * 清理过期日志
  237. *
  238. * @return \Illuminate\Http\JsonResponse
  239. */
  240. public function cleanExpiredLogs()
  241. {
  242. try {
  243. $days = request('days', 30);
  244. $deletedCount = UserLogService::cleanExpiredLogs($days);
  245. return response()->json([
  246. 'status' => true,
  247. 'message' => "成功清理 {$deletedCount} 条过期日志",
  248. ]);
  249. } catch (\Exception $e) {
  250. return response()->json([
  251. 'status' => false,
  252. 'message' => '清理过期日志失败:' . $e->getMessage(),
  253. ]);
  254. }
  255. }
  256. /**
  257. * 获取用户日志统计信息
  258. *
  259. * @return \Illuminate\Http\JsonResponse
  260. */
  261. public function getStats()
  262. {
  263. try {
  264. $userId = request('user_id');
  265. if (!$userId) {
  266. return response()->json([
  267. 'status' => false,
  268. 'message' => '请提供用户ID',
  269. ]);
  270. }
  271. $stats = UserLogService::getUserLogStats($userId);
  272. return response()->json([
  273. 'status' => true,
  274. 'data' => $stats,
  275. ]);
  276. } catch (\Exception $e) {
  277. return response()->json([
  278. 'status' => false,
  279. 'message' => '获取统计信息失败:' . $e->getMessage(),
  280. ]);
  281. }
  282. }
  283. }