1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
namespace Meibuyu\Micro\Model;
use Hyperf\Utils\Context;
use Meibuyu\Micro\Exceptions\HttpResponseException;
class Auth
{
/**
* @return bool|mixed|string|null
* @throws HttpResponseException
*/
private static function init()
{
if (Context::has('auth')) {
return Context::get('auth');
} else {
$token = token();
if (!$token) throw new HttpResponseException('Token不存在');
$auth = redis()->get($token);
if ($auth) {
$auth = json_decode($auth, true);
Context::set('auth', $auth);
return $auth;
} else {
throw new HttpResponseException('用户不存在');
}
}
}
/**
* @return object
* @throws HttpResponseException
*/
public static function user()
{
return self::init();
}
/**
* @return integer
* @throws HttpResponseException
*/
public static function id()
{
return self::init()['id'];
}
}