BaseController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * 描述
  4. * @auth nj911<nj-x@gmail.com>
  5. * @date: 2021-04-18 12:24
  6. */
  7. namespace Modules\AgentApi\Controllers;
  8. use App\Models\Mysql\AgentModel;
  9. use App\Repositories\AgentLogRepository;
  10. use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
  11. use Illuminate\Foundation\Bus\DispatchesJobs;
  12. use Illuminate\Foundation\Validation\ValidatesRequests;
  13. use Illuminate\Routing\Controller as RoutingController;
  14. use Modules\AgentApi\Services\AgentAuthService;
  15. use Route;
  16. class BaseController extends RoutingController
  17. {
  18. use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
  19. /**
  20. * 当前代理UID.
  21. *
  22. * @var string
  23. */
  24. protected $ctAgentUid = '';
  25. /**
  26. * 当前管理员信息.
  27. *
  28. * @var string
  29. */
  30. protected $ctAgentInfo = [];
  31. /**
  32. * 登陆账户信息
  33. * @var array
  34. */
  35. public $accountInfo = [];
  36. /**
  37. * 获取
  38. * @return array|mixed
  39. */
  40. protected function getJsonData()
  41. {
  42. $arrJson = request()->all();
  43. return $arrJson['data'] ?? [];
  44. }
  45. /**
  46. * 获取日志需要的当前模块名和动作名.
  47. * @return array
  48. */
  49. protected function getModuleAndAction()
  50. {
  51. $routeAction = $this->getCurrentRouteHandler();
  52. $controller_suffix = 'Controller';
  53. preg_match_all('/([a-z0-9A-Z]+\\\)?(\w+)@(\w+)/', $routeAction, $matchs);
  54. $controller_full = !empty($matchs[2][0]) ? $matchs[2][0] : ''; //类名.
  55. $controller = str_replace($controller_suffix, '', $controller_full); // 控制器名.
  56. $action = !empty($matchs[3][0]) ? $matchs[3][0] : ''; //方法名.
  57. return [$controller, $action];
  58. }
  59. /**
  60. * 获取当前路由话柄.
  61. * @return string
  62. */
  63. protected function getCurrentRouteHandler()
  64. {
  65. return Route::currentRouteAction();
  66. }
  67. /**
  68. * 获取当前代理对象信息.
  69. * @return mixed
  70. */
  71. protected function getCurrentAgent()
  72. {
  73. $agentInfo = $this->getCurrentAgentInfo();
  74. return AgentModel::where('uid', $agentInfo['agent_uid'])->first();
  75. }
  76. /**
  77. * 获取当前代理信息.
  78. * @return array
  79. */
  80. protected function getCurrentAgentInfo()
  81. {
  82. if (empty($this->ctAgentInfo)) {
  83. $authService = new AgentAuthService();
  84. $this->ctAgentInfo = $authService->getAgentInfo();
  85. $this->ctAgentUid = $this->ctAgentInfo['agent_uid'];
  86. }
  87. return $this->ctAgentInfo;
  88. }
  89. /**
  90. * 获取日志仓库.
  91. * @return AgentLogRepository
  92. */
  93. protected function getLogRepo()
  94. {
  95. return make(AgentLogRepository::class);
  96. }
  97. /**
  98. * 记录操作日志.
  99. * @param string $subject 主题.
  100. * @param string $svalue 关联数据.
  101. * @param array $dbData 此数组数据结构一定要是["after_items" => [0, 1, 2], "before_items" => [0, 1, 2]], after_items,before_items 是一个二维码数组.
  102. * @return bool
  103. */
  104. protected function log(string $subject, $svalue = '', $dbData = [])
  105. {
  106. $agent = $this->getCurrentAgent();
  107. list($module, $action) = $this->getModuleAndAction();
  108. $inputs = [
  109. 'agent_uid' => !empty($agent->uid) ? (string)$agent->uid : '',
  110. 'username' => '',
  111. 'nick' => '',
  112. 'subject' => mb_strcut((string)$subject, 0, 700),
  113. 'module' => $module,
  114. 'action' => $action,
  115. 'svalue' => !empty($svalue) ? mb_strcut((string)$svalue, 0, 700) : '',
  116. 'db_data' => $dbData,
  117. ];
  118. if (!empty($agent)) {
  119. $inputs['username'] = $agent->username;
  120. $inputs['nick'] = $agent->nick;
  121. }
  122. $this->getLogRepo()->createOne($inputs);
  123. return true;
  124. }
  125. }