DevLogic.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace App\Module\Dev\Logics;
  3. use App\Module\Dev\Events\DevLogCreatedEvent;
  4. use App\Module\Dev\Models\Dev;
  5. use App\Module\Dev\Models\DevLog;
  6. use Illuminate\Support\Facades\Log;
  7. use Illuminate\Support\Facades\Request;
  8. /**
  9. * 开发工具逻辑类
  10. *
  11. * 处理开发工具相关的业务逻辑
  12. */
  13. class DevLogic
  14. {
  15. /**
  16. * 获取开发工具列表
  17. *
  18. * @param array $params 查询参数
  19. * @return \Illuminate\Pagination\LengthAwarePaginator
  20. */
  21. public static function getDevTools(array $params = [])
  22. {
  23. $query = Dev::query();
  24. if (!empty($params['name'])) {
  25. $query->where('name', 'like', '%' . $params['name'] . '%');
  26. }
  27. if (isset($params['status'])) {
  28. $query->where('status', $params['status']);
  29. }
  30. return $query->paginate($params['per_page'] ?? 15);
  31. }
  32. /**
  33. * 创建开发工具
  34. *
  35. * @param array $data 开发工具数据
  36. * @return Dev
  37. */
  38. public static function createDevTool(array $data): Dev
  39. {
  40. // 检查事务是否已开启
  41. \UCore\Db\Helper::check_tr();
  42. try {
  43. return Dev::create($data);
  44. } catch (\Exception $e) {
  45. Log::error('创建开发工具失败', [
  46. 'error' => $e->getMessage(),
  47. 'data' => $data
  48. ]);
  49. throw $e;
  50. }
  51. }
  52. /**
  53. * 更新开发工具
  54. *
  55. * @param int $id 开发工具ID
  56. * @param array $data 开发工具数据
  57. * @return bool
  58. */
  59. public static function updateDevTool(int $id, array $data): bool
  60. {
  61. // 检查事务是否已开启
  62. \UCore\Db\Helper::check_tr();
  63. try {
  64. $devTool = Dev::find($id);
  65. if (!$devTool) {
  66. return false;
  67. }
  68. return $devTool->update($data);
  69. } catch (\Exception $e) {
  70. Log::error('更新开发工具失败', [
  71. 'error' => $e->getMessage(),
  72. 'id' => $id,
  73. 'data' => $data
  74. ]);
  75. return false;
  76. }
  77. }
  78. /**
  79. * 删除开发工具
  80. *
  81. * @param int $id 开发工具ID
  82. * @return bool
  83. */
  84. public static function deleteDevTool(int $id): bool
  85. {
  86. // 检查事务是否已开启
  87. \UCore\Db\Helper::check_tr();
  88. try {
  89. $devTool = Dev::find($id);
  90. if (!$devTool) {
  91. return false;
  92. }
  93. return $devTool->delete();
  94. } catch (\Exception $e) {
  95. Log::error('删除开发工具失败', [
  96. 'error' => $e->getMessage(),
  97. 'id' => $id
  98. ]);
  99. return false;
  100. }
  101. }
  102. /**
  103. * 记录开发日志
  104. *
  105. * @param string $type 日志类型
  106. * @param string $content 日志内容
  107. * @param array $extraData 额外数据
  108. * @return DevLog
  109. */
  110. public static function log(string $type, string $content, array $extraData = []): DevLog
  111. {
  112. try {
  113. $data = [
  114. 'type' => $type,
  115. 'content' => $content,
  116. 'ip' => Request::ip(),
  117. 'user_id' => auth()->id() ?? 0,
  118. 'user_agent' => Request::userAgent(),
  119. 'extra_data' => $extraData,
  120. ];
  121. $log = DevLog::create($data);
  122. // 触发日志创建事件
  123. event(new DevLogCreatedEvent($log));
  124. return $log;
  125. } catch (\Exception $e) {
  126. \UCore\Helper\Logger::exception('记录开发日志失败', $e, [
  127. 'type' => $type,
  128. 'content' => $content,
  129. 'extra_data' => $extraData
  130. ]);
  131. // 创建失败时,返回一个空的日志对象
  132. return new DevLog();
  133. }
  134. }
  135. }