You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
2.2 KiB
75 lines
2.2 KiB
<?php
|
|
declare (strict_types=1);
|
|
/**
|
|
* 工程基类
|
|
* @since 2017/02/28 创建
|
|
* @author zhaoxiang <zhaoxiang051405@gmail.com>
|
|
*/
|
|
|
|
namespace app\controller\wechat;
|
|
|
|
use app\BaseController;
|
|
use app\util\ReturnCode;
|
|
use Firebase\JWT\JWT;
|
|
use think\facade\Env;
|
|
use think\Response;
|
|
|
|
class Base extends BaseController {
|
|
|
|
private $debug = [];
|
|
protected $noNeedLogin = [];
|
|
protected $user = [];
|
|
public $user_id = '';
|
|
|
|
public function buildSuccess(array $data = [], string $msg = '操作成功', int $code = ReturnCode::SUCCESS): Response {
|
|
$return = [
|
|
'code' => $code,
|
|
'msg' => $msg,
|
|
'data' => $data
|
|
];
|
|
if (Env::get('APP_DEBUG') && $this->debug) {
|
|
$return['debug'] = $this->debug;
|
|
}
|
|
|
|
return json($return);
|
|
}
|
|
|
|
public function buildFailed(int $code, string $msg = '操作失败', array $data = []): Response {
|
|
$return = [
|
|
'code' => $code,
|
|
'msg' => $msg,
|
|
'data' => $data
|
|
];
|
|
if (Env::get('APP_DEBUG') && $this->debug) {
|
|
$return['debug'] = $this->debug;
|
|
}
|
|
|
|
return json($return);
|
|
}
|
|
|
|
protected function debug($data): void {
|
|
if ($data) {
|
|
$this->debug[] = $data;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 生成验签
|
|
* @param $data
|
|
* @return string
|
|
*/
|
|
protected function signToken($data): string
|
|
{
|
|
$key = config('jwt.key'); //这里是自定义的一个随机字串,应该写在config文件中的,解密时也会用,相当于加密中常用的 盐-salt
|
|
$token = array(
|
|
"iss" => $key, //签发者 可以为空
|
|
"aud" => '', //面象的用户,可以为空
|
|
"iat" => time(), //签发时间
|
|
"nbf" => time() + 3, //在什么时候jwt开始生效 (这里表示生成100秒后才生效)
|
|
"exp" => time() + 7200, //token 过期时间
|
|
"data" => $data //记录的userid的信息,这里是自已添加上去的,如果有其它信息,可以再添加数组的键值对
|
|
);
|
|
return JWT::encode($token, $key, "HS384"); //根据参数生成了token,可选:HS256、HS384、HS512、RS256、ES256等
|
|
}
|
|
}
|
|
|