UserLogController.php 11 KB

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