$classes
$classes :
Class SaveAspect
$loginUser : \Mine\Helper\LoginUser
__construct(\Mine\Helper\LoginUser $loginUser)
\Mine\Helper\LoginUser | $loginUser |
<?php
/**
* MineAdmin is committed to providing solutions for quickly building web applications
* Please view the LICENSE file that was distributed with this source code,
* For the full copyright and license information.
* Thank you very much for using MineAdmin.
*
* @Author X.Mo<root@imoi.cn>
* @Link https://gitee.com/xmo/MineAdmin
*/
declare(strict_types=1);
namespace Mine\Aspect;
use Hyperf\Di\Annotation\Aspect;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
use Hyperf\Di\Exception\Exception;
use Mine\Helper\LoginUser;
use Mine\MineModel;
/**
* Class SaveAspect
* @package Mine\Aspect
* @Aspect
*/
class SaveAspect extends AbstractAspect
{
public $classes = [
'Mine\MineModel::save'
];
/**
* @var LoginUser
*/
protected $loginUser;
public function __construct(LoginUser $loginUser)
{
$this->loginUser = $loginUser;
}
/**
* @param ProceedingJoinPoint $proceedingJoinPoint
* @return mixed
* @throws Exception
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws \Exception
*/
public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
$instance = $proceedingJoinPoint->getInstance();
// 设置创建人
try {
$this->loginUser->check();
if ($instance instanceof MineModel && in_array('created_by', $instance->getFillable())) {
$instance->created_by = $this->loginUser->getId();
}
} catch (\Throwable $e) {}
// 生成ID
if ($instance instanceof MineModel &&
!$instance->incrementing &&
$instance->getPrimaryKeyType() === 'int' &&
empty($instance->{$instance->getKeyName()})
) {
$instance->setPrimaryKeyValue(snowflake_id());
}
return $proceedingJoinPoint->process();
}
}