generated from liutk/owl-admin-base
115 lines
2.5 KiB
PHP
115 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Traits;
|
|
|
|
use App\Enums\CheckStatus;
|
|
use App\Models\WorkflowLog;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Support\Str;
|
|
|
|
trait HasCheckable
|
|
{
|
|
public function checkApply()
|
|
{
|
|
$this->update([
|
|
'check_status' => CheckStatus::Processing,
|
|
]);
|
|
}
|
|
|
|
public function checkSuccess()
|
|
{
|
|
$this->update([
|
|
'check_status' => CheckStatus::Success,
|
|
'checked_at' => now(),
|
|
]);
|
|
}
|
|
|
|
public function checkFail()
|
|
{
|
|
$this->update([
|
|
'check_status' => CheckStatus::Fail,
|
|
]);
|
|
}
|
|
|
|
public function checkCancel()
|
|
{
|
|
$this->update([
|
|
'check_status' => CheckStatus::Cancel,
|
|
]);
|
|
}
|
|
|
|
public function getCheckKey()
|
|
{
|
|
return Str::snake(class_basename(__CLASS__));
|
|
}
|
|
|
|
public function workflows()
|
|
{
|
|
return $this->morphMany(WorkflowLog::class, 'subject');
|
|
}
|
|
|
|
/**
|
|
* 是否允许修改
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function canEdit(): bool
|
|
{
|
|
return !($this->check_status === CheckStatus::Processing);
|
|
}
|
|
|
|
/**
|
|
* 查询审核通过的记录
|
|
*
|
|
* @param Builder $q
|
|
*/
|
|
public function scopeChecked(Builder $q): Builder
|
|
{
|
|
return $q->where('check_status', CheckStatus::Success);
|
|
}
|
|
|
|
/**
|
|
* 审核成功前, 会调用该方法
|
|
* 返回错误信息 阻止审核通过
|
|
*/
|
|
public function checkSuccessPre()
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 审核申请前, 会调用此方法
|
|
* 返回错误信息 阻止申请
|
|
*/
|
|
public function checkApplyPre()
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public static function boot()
|
|
{
|
|
parent::boot();
|
|
static::updating(function ($model) {
|
|
// 修改审核通过的内容, 需要重新审核
|
|
if (isset($model->checkListen) && count($model->checkListen) > 0 && $model->isDirty($model->checkListen) && $model->check_status === CheckStatus::Success) {
|
|
$model->check_status = CheckStatus::None;
|
|
$model->checked_at = null;
|
|
}
|
|
});
|
|
|
|
static::deleting(function ($model) {
|
|
// 删除审核记录
|
|
$model->workflows()->delete();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 是否允许删除,已取消或已拒绝的
|
|
* @return bool
|
|
*/
|
|
public function canDel(): bool
|
|
{
|
|
return $this->check_status === CheckStatus::Cancel || $this->check_status === CheckStatus::Fail || $this->check_status === CheckStatus::None;
|
|
}
|
|
}
|