From 71d6b6128bc9cd20a7d745f9fffe25ab3377a8d6 Mon Sep 17 00:00:00 2001 From: wanghongjun <1445693971@qq,com> Date: Thu, 25 Jul 2024 18:09:31 +0800 Subject: [PATCH] =?UTF-8?q?=E9=AA=8C=E8=AF=81xml=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E3=80=81=E5=BE=AE=E4=BF=A1=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E8=B4=A6=E5=8F=B7=E8=AE=A4=E8=AF=813?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/wechat/Base.php | 80 +++++++++------------------------ app/controller/wechat/Login.php | 10 +---- config/jwt.php | 16 +++++++ 3 files changed, 39 insertions(+), 67 deletions(-) create mode 100644 config/jwt.php diff --git a/app/controller/wechat/Base.php b/app/controller/wechat/Base.php index 9047062..ebcfea9 100644 --- a/app/controller/wechat/Base.php +++ b/app/controller/wechat/Base.php @@ -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等 + } } diff --git a/app/controller/wechat/Login.php b/app/controller/wechat/Login.php index db5b546..2f00102 100644 --- a/app/controller/wechat/Login.php +++ b/app/controller/wechat/Login.php @@ -3,7 +3,6 @@ namespace app\controller\wechat; use app\service\user\LoginService; -use think\App; use think\facade\Request; class Login extends Base @@ -14,13 +13,6 @@ class Login extends Base public $auth; protected $valid = \app\validate\Login::class; - public function __construct() - { - $app = new App(); - parent::__construct($app); - } - - /** * @title 登录凭证校验 * @return \think\Response|void @@ -28,6 +20,7 @@ class Login extends Base public function code2session(){ try { + validate($this->valid)->scene('code2session')->check(Request::post()); $code = $this->request->param('code'); $iv = $this->request->param('iv'); $encryptedData = $this->request->param('encryptedData'); @@ -51,6 +44,7 @@ class Login extends Base $unionid = $this->request->post('unionid'); $loginService = new LoginService(); $user = $loginService->userLogin($phone, $openid, $unionid); + $user['token'] = $this->signToken($user); return $this->buildSuccess($user); } catch (\Exception $e) { //错误消息 $e->getMessage() return $this->buildFailed($e->getCode() ?: 400,$e->getMessage()); diff --git a/config/jwt.php b/config/jwt.php new file mode 100644 index 0000000..381c99b --- /dev/null +++ b/config/jwt.php @@ -0,0 +1,16 @@ + "invoice_jwt@key", + // 签发时间 + 'lat' => time(), + // 生效时间 + 'nbf' => time(), + // 过期时间 +// 'exp' => time() + (3600 * 5), + +];