UserLogController.php 11 KB

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