123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?php
- namespace app\common\controller;
- use Firebase\JWT\JWT;
- use Firebase\JWT\Key;
- use app\BaseController;
- use app\common\util\Result;
- use EasyWeChatComposer\EasyWeChat;
- use app\common\exception\CatchException;
- class JwtBaseController extends BaseController
- {
- protected $failException = true;
-
- protected $key;
-
- protected $table;
-
- protected $model;
-
- private static $_wechat;
-
- protected function setKey($key) {
- $this->key = $key;
- }
-
- public function valid()
- {
-
- $jwt = $this->request->header('Authorization');
- if (empty($jwt)) {
- throw new CatchException("未授权用户", 600);
- }
- $jwt = str_replace('Bearer ', '', $jwt);
-
- $this->decodeJwt($jwt);
- return Result::rest(true);
- }
-
- protected function params($validator = null, $name = '')
- {
- $params = $this->request->param($name, null, 'trim');
-
- if (!is_null($validator)) {
- $this->validate($params, $validator);
- }
- return $params;
- }
- protected function only(array $names)
- {
- return $this->request->only($names);
- }
-
- protected function encodeJwt($payload)
- {
- return JWT::encode($payload, $this->key, 'HS256');
- }
-
- protected function decodeJwt($jwt)
- {
- return JWT::decode($jwt, new Key($this->key, 'HS256'));
- }
- }
|