| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- /**
- * 描述
- * @auth nj911<nj-x@gmail.com>
- * @date: 2021-04-18 12:24
- */
- namespace Modules\AgentApi\Controllers;
- use App\Models\Mysql\AgentModel;
- use App\Repositories\AgentLogRepository;
- use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
- use Illuminate\Foundation\Bus\DispatchesJobs;
- use Illuminate\Foundation\Validation\ValidatesRequests;
- use Illuminate\Routing\Controller as RoutingController;
- use Modules\AgentApi\Services\AgentAuthService;
- use Route;
- class BaseController extends RoutingController
- {
- use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
- /**
- * 当前代理UID.
- *
- * @var string
- */
- protected $ctAgentUid = '';
- /**
- * 当前管理员信息.
- *
- * @var string
- */
- protected $ctAgentInfo = [];
- /**
- * 登陆账户信息
- * @var array
- */
- public $accountInfo = [];
- /**
- * 获取
- * @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 getCurrentAgent()
- {
- $agentInfo = $this->getCurrentAgentInfo();
- return AgentModel::where('uid', $agentInfo['agent_uid'])->first();
- }
- /**
- * 获取当前代理信息.
- * @return array
- */
- protected function getCurrentAgentInfo()
- {
- if (empty($this->ctAgentInfo)) {
- $authService = new AgentAuthService();
- $this->ctAgentInfo = $authService->getAgentInfo();
- $this->ctAgentUid = $this->ctAgentInfo['agent_uid'];
- }
- return $this->ctAgentInfo;
- }
- /**
- * 获取日志仓库.
- * @return AgentLogRepository
- */
- protected function getLogRepo()
- {
- return make(AgentLogRepository::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 = [])
- {
- $agent = $this->getCurrentAgent();
- list($module, $action) = $this->getModuleAndAction();
- $inputs = [
- 'agent_uid' => !empty($agent->uid) ? (string)$agent->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($agent)) {
- $inputs['username'] = $agent->username;
- $inputs['nick'] = $agent->nick;
- }
- $this->getLogRepo()->createOne($inputs);
- return true;
- }
- }
|