|
|
|
@ -9,13 +9,9 @@ declare (strict_types=1); |
|
|
|
namespace app\controller\wechat; |
|
|
|
|
|
|
|
use app\BaseController; |
|
|
|
use app\service\user\LoginService; |
|
|
|
use app\util\ReturnCode; |
|
|
|
use fast\FuncException; |
|
|
|
use think\App; |
|
|
|
use Firebase\JWT\JWT; |
|
|
|
use think\facade\Env; |
|
|
|
use think\facade\Request; |
|
|
|
use think\facade\Session; |
|
|
|
use think\Response; |
|
|
|
|
|
|
|
class Base extends BaseController { |
|
|
|
@ -25,60 +21,6 @@ class Base extends BaseController { |
|
|
|
protected $user = []; |
|
|
|
public $user_id = ''; |
|
|
|
|
|
|
|
// public function __construct() |
|
|
|
// { |
|
|
|
// $app = new App(); |
|
|
|
// parent::__construct($app); |
|
|
|
// |
|
|
|
// try { |
|
|
|
// if(!$this->user){ |
|
|
|
// $bool = (new LoginService())->userAutologin(); |
|
|
|
// if($bool){ |
|
|
|
// $this->user = session('user'); |
|
|
|
// } |
|
|
|
// } |
|
|
|
// if ($this->user){ |
|
|
|
// $this->user_id = $this->user['id']; |
|
|
|
// } |
|
|
|
//// //需要登录接口进行校验 |
|
|
|
// if (!$this->match($this->noNeedLogin)){ |
|
|
|
// $this->checklogin(); |
|
|
|
// } |
|
|
|
// } catch (\Exception $e) { |
|
|
|
// return $this->buildFailed($e->getCode() ?: 400,$e->getMessage()); |
|
|
|
// } |
|
|
|
// } |
|
|
|
|
|
|
|
/** |
|
|
|
* 关联检测是否包含该请求是否包含该方法 |
|
|
|
* @param $arr |
|
|
|
* @return bool |
|
|
|
*/ |
|
|
|
public function match($arr = []) { |
|
|
|
$request = Request::instance(); |
|
|
|
$arr = is_array($arr) ? $arr : explode(',', $arr); |
|
|
|
if (! $arr) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
$arr = array_map('strtolower', $arr); |
|
|
|
// 是否存在 |
|
|
|
if (in_array(strtolower($request->action()), $arr) || in_array('*', $arr)) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
// 没找到匹配 |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 用户登录检测 |
|
|
|
*/ |
|
|
|
public function checkLogin() { |
|
|
|
$login = new LoginService(); |
|
|
|
if (!$login->isLogin()){ |
|
|
|
throw new FuncException('用户未登录',302); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public function buildSuccess(array $data = [], string $msg = '操作成功', int $code = ReturnCode::SUCCESS): Response { |
|
|
|
$return = [ |
|
|
|
'code' => $code, |
|
|
|
@ -110,4 +52,24 @@ class Base extends BaseController { |
|
|
|
$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等 |
|
|
|
} |
|
|
|
} |
|
|
|
|