Modules_Manage_Controllers_BaseController.proxy.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. declare (strict_types=1);
  3. namespace Modules\Manage\Controllers;
  4. use App\Controller\CoreController;
  5. use App\Model\ManagerModel;
  6. use App\Repositories\ManagerLogRepository;
  7. use Hyperf\Utils\Context;
  8. use Modules\Manage\Services\AuthService;
  9. class BaseController extends CoreController
  10. {
  11. use \Hyperf\Di\Aop\ProxyTrait;
  12. use \Hyperf\Di\Aop\PropertyHandlerTrait;
  13. /**
  14. * 当前管理员ID.
  15. *
  16. * @var string
  17. */
  18. protected $ctManagerId = 'ct:Manage.BaseController.manager_id';
  19. /**
  20. * 当前管理员信息.
  21. *
  22. * @var string
  23. */
  24. protected $ctMnanagerInfo = 'ct:Manage.BaseController.manager_info';
  25. /**
  26. * 当前管理员角色.
  27. *
  28. * @var string
  29. */
  30. protected $ctRoleId = 'ct:Manage.BaseController.role_id';
  31. /**
  32. * 属性描述.
  33. *
  34. * @var ManagerLogRepository
  35. */
  36. protected $logRepo = null;
  37. public function __construct()
  38. {
  39. $this->__handlePropertyHandler(__CLASS__);
  40. // 初始化获取jwt管理员信息.
  41. $this->initJwtManager();
  42. }
  43. /**
  44. * 初始化获取jwt管理员信息.
  45. * @return void
  46. */
  47. protected function initJwtManager()
  48. {
  49. $managerInfo = $this->getCurrentManagerInfo();
  50. if (!empty($managerInfo)) {
  51. Context::set($this->ctMnanagerInfo, $managerInfo);
  52. Context::set($this->ctManagerId, $managerInfo['manager_id']);
  53. Context::set($this->ctRoleId, $managerInfo['role_id']);
  54. }
  55. }
  56. /**
  57. * 记录操作日志.
  58. * @param string $subject 主题.
  59. * @param string $svalue 关联数据.
  60. * @param array $dbData 此数组数据结构一定要是["after_items" => [0, 1, 2], "before_items" => [0, 1, 2]], after_items,before_items 是一个二维码数组.
  61. * @return bool
  62. */
  63. protected function log(string $subject, $svalue = '', $dbData = [])
  64. {
  65. $manager = $this->getCurrentManger();
  66. list($module, $action) = $this->getModuleAndAction();
  67. $inputs = ['manager_id' => !empty($manager->manager_id) ? (string) $manager->manager_id : '', 'username' => '', 'nick' => '', 'subject' => mb_strcut((string) $subject, 0, 700), 'module' => $module, 'action' => $action, 'svalue' => !empty($svalue) ? mb_strcut((string) $svalue, 0, 700) : '', 'db_data' => $dbData];
  68. if (!empty($manager)) {
  69. $inputs['username'] = $manager->username;
  70. $inputs['nick'] = $manager->nick;
  71. }
  72. $this->getLogRepo()->createOne($inputs);
  73. return true;
  74. }
  75. /**
  76. * 获取日志仓库.
  77. * @return ManagerLogRepository|null
  78. */
  79. protected function getLogRepo()
  80. {
  81. return make(ManagerLogRepository::class);
  82. }
  83. /**
  84. * 获取当前管理员对象信息.
  85. * @return \Hyperf\Database\Model\Builder|\Hyperf\Database\Model\Model|object|null
  86. */
  87. protected function getCurrentManger()
  88. {
  89. $managerInfo = $this->getCurrentManagerInfo();
  90. return ManagerModel::with(['role'])->where('manager_id', $managerInfo['manager_id'])->first();
  91. }
  92. /**
  93. * 获取当前管理员信息.
  94. * @return array
  95. */
  96. protected function getCurrentManagerInfo()
  97. {
  98. $managerInfo = Context::get($this->ctMnanagerInfo, []);
  99. if (empty($managerInfo)) {
  100. $authService = new AuthService();
  101. $managerInfo = $authService->getManagerInfo();
  102. }
  103. return $managerInfo;
  104. }
  105. }