OperationLog.php 2.9 KB
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
 * 模型层:系统-操作日志 模型类
+-----------------------------------------------------------------------------------------------------------------------
 *
 * PHP version 7
 *
 * @category  App\Models\System
 * @package   App\Models\System
 * @author    Richer <yangzi1028@163.com>
 * @date      2022年8月3日11:23:05
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
namespace App\Models\System;

use App\Models\BaseModel;
use App\Models\Traits\MorphMapTrait;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Arr;

/**
 * Class OperationLog
 *
 * @property mixed user_id
 * @property mixed type
 * @property mixed type_show
 * @property mixed unshelved_opinion
 * @property mixed input
 * @category  App\Models\System
 * @package   App\Models\System
 * @author    Richer <yangzi1028@163.com>
 * @date      2022年8月3日11:23:05
 * @copyright 2020-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
class OperationLog extends BaseModel
{
    use MorphMapTrait;

    // 指定数据库表
    const TABLE = 'system_operation_logs';
    protected $table = self::TABLE ;

    // 指定对象显示名称:方便系统统一查询和做其他处理
    const OBJ_NAME      = 'system-operation-log';
    const OBJ_NAME_ZH   = '系统操作日志';

    const CREATE   = 1; // 创建
    const UPDATE   = 2; // 更新
    const DELETE   = 3; // 删除
    const SHELVE   = 4; // 上架
    const UNSHELVE = 5; // 下架
    const TYPE_OPTIONS = [
        self::CREATE   => '创建',
        self::UPDATE   => '更新',
        self::DELETE   => '删除',
        self::SHELVE   => '上架',
        self::UNSHELVE => '下架',
    ];

    /**
     * 访问器被附加到模型数组的形式。
     *
     * @var array
     */
    protected $appends = ['type_show','unshelved_opinion'];

    /**
     * 多态关联
     *
     * @return MorphTo
     */
    public function loggable(): MorphTo
    {
        return $this->morphTo()->withTrashed();
    }

    /**
     * @param $value
     */
    public function setInputAttribute($value)
    {
        $this->attributes['input'] = my_json_encode($value);
    }

    /**
     * @param $value
     * @return mixed|null
     */
    public function getInputAttribute($value)
    {
        return json_decode($value, true) ?: null;
    }

    /**
     * @return string
     */
    public function getTypeShowAttribute()
    {
        return Arr::get(self::TYPE_OPTIONS, $this->type ? : self::CREATE);
    }

    /**
     * @return string
     */
    public function getUnshelvedOpinionAttribute()
    {
        return Arr::get($this->input, 'unshelved_opinion');
    }
}