<?php namespace app\common\controller; use app\common\exception\CatchException; use think\facade\Log; trait JwtAuthorizedTrait { /** * $payload Jwt 载荷,stdClass * * @var \stdClass */ protected $payload; public function initialize() { parent::initialize(); // 允许cros通过jwt所在的header $jwt = $this->request->header('Authorization'); if (!$jwt) { throw new CatchException("请登录", 600); } // 解析jwt $jwt = str_replace('Bearer ', '', $jwt); Log::debug("尝试解析Jwt: " . var_export($jwt, true)); $this->payload = $this->decodeJwt($jwt); // 调用用户自定义的初始化语句 if (method_exists($this, 'init')) { $this->init(); } } }