Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
meibuyu-rpc
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
without authentication
meibuyu-rpc
Commits
e43b86d7
Commit
e43b86d7
authored
Mar 28, 2020
by
王源
🎧
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
添加当前用户相关方法
parent
8ce6d0b6
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
92 additions
and
3 deletions
+92
-3
ObjectNotExistException.php
src/Exceptions/ObjectNotExistException.php
+19
-0
Auth.php
src/Model/Auth.php
+50
-0
functions.php
src/functions.php
+23
-3
No files found.
src/Exceptions/ObjectNotExistException.php
0 → 100644
View file @
e43b86d7
<?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
View file @
e43b86d7
<?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
View file @
e43b86d7
...
...
@@ -18,16 +18,36 @@ if (!function_exists('container')) {
}
}
/**
* redis 客户端实例
*/
if
(
!
function_exists
(
'redis'
))
{
/**
* 获取redis客户端实例
* @return Redis|mixed
*/
function
redis
()
{
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'
))
{
/**
* 请求实例
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment