模型事件中可以扩展很多类似方法

/**
 * Created by PhpStorm.
 * Power by Mikkle
 * QQ:776329498
 * Date: 2017/4/14
 * Time: 16:23
 */

namespace app\base\model;


use think\Model;
use think\Request;
use think\Session;

class Base extends Model
{

    protected static function init()
    {

        self::event('after_insert', function ($model) {
        //获取执行的model名称
            $base_name = basename(str_replace('\\', '/', get_called_class()), '.php');
            //作者本人回复 $base_name = $this->name; 有待测试
            $request = Request::instance();
            $guid = $request->param('id');
            $data=[];
            if (method_exists($model, 'data')){
                if (empty($guid)&& isset($model->data['guid'])){
                    $guid = $model->guid;
                }
                if (empty($guid) && isset($model->data['uuid'])){
                    $guid = $model->uuid;
                }
            }
            if (empty($data["guid"]) && !empty($request->param('guid'))){
                $data["guid"] = $request->param('guid');
            }
            $data["uuid"] = Session::get('uuid', 'Global');
            $data['type']="after_insert";
            $data['url']=$request->url();
            $data['model_name']=$base_name;
            $data["guid"]=$guid;
            $data["param"]=json_encode($request->param());
            $data["ip"]=$request->ip();
            $data["content"]=json_encode($model->getData());
            $data["update_time"]=time();
            $model->table('mk_system_log')->insert($data);
        });
     }
 }