<?php
namespace
app\hladmin\controller;
use
think\Controller;
use
\Firebase\JWT\JWT;
class
InterCommonController
extends
Controller {
private
$key
=
"123456789"
;
public
function
_getJwtToken(){
try
{
$string
= input(
"string"
);
if
(
empty
(
$string
)) {
throw
new
\Exception(
"请传入需要加密string"
, -105);
}
$jwt
=
$this
->_setJwtToken(
$string
);
throw
new
\Exception(
$jwt
, 200);
}
catch
(\Exception
$e
) {
return
json(
array
(
"code"
=>
$e
->getCode(),
"msg"
=>
$e
->getMessage()));
}
}
private
function
_setJwtToken(
$string
=
""
){
$key
=
$this
->key;
$time
= time();
$token
=
array
(
"iss"
=>
"http://ml.cn"
,
"aud"
=>
"http://ml.cn"
,
'iat' =>
$time
,
'nbf' =>
$time
+ 10,
'
exp
' =>
$time
+ 10,
"string"
=>
$string
);
$jwt
= JWT::encode(
$token
,
$key
);
return
$jwt
;
}
protected
function
_readJwtToken(
$jwt
){
$key
=
$this
->key;
try
{
JWT::
$leeway
= 60;
$decoded
= JWT::decode(
$jwt
,
$key
, ['HS256']);
$arr
= (
array
)
$decoded
;
return
json_msg(200,
"success"
,
$arr
);
}
catch
(\Firebase\JWT\SignatureInvalidException
$e
) {
return
json_msg(-101,
$e
->getMessage());
}
catch
(\Firebase\JWT\BeforeValidException
$e
) {
return
json_msg(-102,
$e
->getMessage());
}
catch
(\Firebase\JWT\ExpiredException
$e
) {
return
json_msg(-103,
$e
->getMessage());
}
catch
(Exception
$e
) {
return
json_msg(-104,
$e
->getMessage());
}
}
public
function
_writeJwtToken(
$token
){
halt(
$this
->_readJwtToken(
$token
));
}
}
?>