BaseController.php 3.8 KB

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