Loading src/Exceptions/ObjectNotExistException.php 0 → 100644 +19 −0 Original line number Original line Diff line number Diff line <?php /** * Created by PhpStorm. * User: zero * Date: 2020/3/17 */ namespace Meibuyu\Micro\Exceptions; use Throwable; class ObjectNotExistException extends \Exception { public function __construct($message, $code = 0, Throwable $previous = null) { parent::__construct($message . ' Not Exist!', $code, $previous); } } No newline at end of file src/Model/Auth.php 0 → 100644 +50 −0 Original line number Original line Diff line number Diff line <?php namespace Meibuyu\Micro\Model; use Hyperf\Utils\Context; use Meibuyu\Micro\Exceptions\ObjectNotExistException; class Auth { /** * @return bool|mixed|string|null * @throws ObjectNotExistException */ private static function init() { if (Context::has('auth')) { return Context::get('auth'); } else { $token = token(); if (!$token) throw new ObjectNotExistException('Token'); $auth = redis()->get($token); if ($auth) { $auth = json_decode($auth, true); Context::set('auth', $auth); return $auth; } else { throw new ObjectNotExistException('User'); } } } /** * @return object * @throws ObjectNotExistException */ public static function user() { return self::init(); } /** * @return integer * @throws ObjectNotExistException */ public static function id() { return self::init()['id']; } } src/functions.php +23 −3 Original line number Original line Diff line number Diff line Loading @@ -18,16 +18,36 @@ if (!function_exists('container')) { } } } } if (!function_exists('redis')) { /** /** * redis 客户端实例 * 获取redis客户端实例 * @return Redis|mixed */ */ if (!function_exists('redis')) { function redis() function redis() { { return container(Redis::class); return container(Redis::class); } } } } /** * token */ if (!function_exists('token')) { function token() { $token = request()->getHeader('Authorization')[0] ?? ''; if (strlen($token) > 0) { $token = ucfirst($token); $arr = explode('Bearer ', $token); $token = $arr[1] ?? ''; if (strlen($token) > 0) { return $token; } } return false; } } if (!function_exists('request')) { if (!function_exists('request')) { /** /** * 请求实例 * 请求实例 Loading Loading
src/Exceptions/ObjectNotExistException.php 0 → 100644 +19 −0 Original line number Original line Diff line number Diff line <?php /** * Created by PhpStorm. * User: zero * Date: 2020/3/17 */ namespace Meibuyu\Micro\Exceptions; use Throwable; class ObjectNotExistException extends \Exception { public function __construct($message, $code = 0, Throwable $previous = null) { parent::__construct($message . ' Not Exist!', $code, $previous); } } No newline at end of file
src/Model/Auth.php 0 → 100644 +50 −0 Original line number Original line Diff line number Diff line <?php namespace Meibuyu\Micro\Model; use Hyperf\Utils\Context; use Meibuyu\Micro\Exceptions\ObjectNotExistException; class Auth { /** * @return bool|mixed|string|null * @throws ObjectNotExistException */ private static function init() { if (Context::has('auth')) { return Context::get('auth'); } else { $token = token(); if (!$token) throw new ObjectNotExistException('Token'); $auth = redis()->get($token); if ($auth) { $auth = json_decode($auth, true); Context::set('auth', $auth); return $auth; } else { throw new ObjectNotExistException('User'); } } } /** * @return object * @throws ObjectNotExistException */ public static function user() { return self::init(); } /** * @return integer * @throws ObjectNotExistException */ public static function id() { return self::init()['id']; } }
src/functions.php +23 −3 Original line number Original line Diff line number Diff line Loading @@ -18,16 +18,36 @@ if (!function_exists('container')) { } } } } if (!function_exists('redis')) { /** /** * redis 客户端实例 * 获取redis客户端实例 * @return Redis|mixed */ */ if (!function_exists('redis')) { function redis() function redis() { { return container(Redis::class); return container(Redis::class); } } } } /** * token */ if (!function_exists('token')) { function token() { $token = request()->getHeader('Authorization')[0] ?? ''; if (strlen($token) > 0) { $token = ucfirst($token); $arr = explode('Bearer ', $token); $token = $arr[1] ?? ''; if (strlen($token) > 0) { return $token; } } return false; } } if (!function_exists('request')) { if (!function_exists('request')) { /** /** * 请求实例 * 请求实例 Loading