| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
- /**
- * 描述
- * @auth nj911<nj-x@gmail.com>
- * @date: 2021-04-18 12:24
- */
- namespace Modules\ManageApi\Controllers;
- use App\Models\Mysql\ManagerModel;
- use App\Repositories\ManagerLogRepository;
- use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
- use Illuminate\Foundation\Bus\DispatchesJobs;
- use Illuminate\Foundation\Validation\ValidatesRequests;
- use Illuminate\Routing\Controller as RoutingController;
- use Modules\ManageApi\Services\ManageAuthService;
- use Route;
- class BaseController extends RoutingController
- {
- use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
- /**
- * 当前管理员UID.
- *
- * @var string
- */
- protected $ctManagerUid = '';
- /**
- * 当前管理员信息.
- *
- * @var string
- */
- protected $ctManagerInfo = [];
- /**
- * 当前管理员角色.
- *
- * @var string
- */
- protected $ctRoleId = '';
- /**
- * 获取
- * @return array|mixed
- */
- protected function getJsonData()
- {
- $arrJson = request()->all();
- return $arrJson['data'] ?? [];
- }
- /**
- * 获取日志需要的当前模块名和动作名.
- * @return array
- */
- protected function getModuleAndAction()
- {
- $routeAction = $this->getCurrentRouteHandler();
- $controller_suffix = 'Controller';
- preg_match_all('/([a-z0-9A-Z]+\\\)?(\w+)@(\w+)/', $routeAction, $matchs);
- $controller_full = !empty($matchs[2][0]) ? $matchs[2][0] : ''; //类名.
- $controller = str_replace($controller_suffix, '', $controller_full); // 控制器名.
- $action = !empty($matchs[3][0]) ? $matchs[3][0] : ''; //方法名.
- return [$controller, $action];
- }
- /**
- * 获取当前路由话柄.
- * @return string
- */
- protected function getCurrentRouteHandler()
- {
- return Route::currentRouteAction();
- }
- /**
- * 获取当前管理员对象信息.
- * @return mixed
- */
- protected function getCurrentManger()
- {
- $managerInfo = $this->getCurrentManagerInfo();
- return ManagerModel::with(['role'])->where('manager_uid', $managerInfo['manager_uid'])->first();
- }
- /**
- * 获取当前管理员信息.
- * @return array
- */
- protected function getCurrentManagerInfo()
- {
- if (empty($this->ctManagerInfo)) {
- $authService = new ManageAuthService();
- $this->ctManagerInfo = $authService->getManagerInfo();
- $this->ctManagerUid = $this->ctManagerInfo['manager_uid'];
- }
- return $this->ctManagerInfo;
- }
- /**
- * 获取日志仓库.
- * @return ManagerLogRepository
- */
- protected function getLogRepo()
- {
- return make(ManagerLogRepository::class);
- }
- /**
- * 记录操作日志.
- * @param string $subject 主题.
- * @param string $svalue 关联数据.
- * @param array $dbData 此数组数据结构一定要是["after_items" => [0, 1, 2], "before_items" => [0, 1, 2]], after_items,before_items 是一个二维码数组.
- * @return bool
- */
- protected function log(string $subject, $svalue = '', $dbData = [])
- {
- $manager = $this->getCurrentManger();
- list($module, $action) = $this->getModuleAndAction();
- $inputs = [
- 'manager_uid' => !empty($manager->manager_uid) ? (string)$manager->manager_uid : '',
- '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,
- ];
- if (!empty($manager)) {
- $inputs['username'] = $manager->username;
- $inputs['nick'] = $manager->nick;
- }
- $this->getLogRepo()->createOne($inputs);
- return true;
- }
- }
|