From 525cd849c64280742f3c4cbf4f5780f580650d24 Mon Sep 17 00:00:00 2001 From: qinger7520-home-linux Date: Mon, 30 Jan 2023 17:38:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0composer=E3=80=81=E9=AA=8C?= =?UTF-8?q?=E8=AF=81=E7=A0=81=E6=89=A9=E5=B1=95=E3=80=81=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E5=A2=9E=E5=8A=A0=E7=BC=A9=E7=95=A5=E5=9B=BE?= =?UTF-8?q?=E7=AD=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .example.env | 9 +- app/admin/controller/Ajax.php | 3 +- app/admin/controller/Passport.php | 2 +- app/admin/controller/system/Admin.php | 4 +- composer.json | 9 +- config/captcha.php | 4 + config/logic.php | 24 +++ extend/util/Token.php | 237 +++++++++++++------------- extend/util/Upload.php | 50 +++++- 9 files changed, 208 insertions(+), 134 deletions(-) create mode 100644 config/logic.php diff --git a/.example.env b/.example.env index 33c96d8..865abe8 100644 --- a/.example.env +++ b/.example.env @@ -7,7 +7,7 @@ PASSWORD_STR = 'alkdaneoisaGJKLsgsdfga31f' [DATABASE] TYPE = mysql HOSTNAME = 127.0.0.1 -DATABASE = quickadmin +DATABASE = quickadmins USERNAME = root PASSWORD = root HOSTPORT = 3306 @@ -27,5 +27,8 @@ TAG_PREFIX = '' default_lang = zh-cn [FRONT] -PROJECT_NAME = quickadmin-front -API_URL = 'http://quickadmin-back.io' +PROJECT_NAME = quickadmins-front +API_URL = 'http://quickadmins-back.io' + +[LOGIC] +SQUEEZE = 0 diff --git a/app/admin/controller/Ajax.php b/app/admin/controller/Ajax.php index 9f70d89..6886933 100644 --- a/app/admin/controller/Ajax.php +++ b/app/admin/controller/Ajax.php @@ -75,7 +75,6 @@ class Ajax extends AdminBase public function getCaptcha() { - return captchaimg(); + return imgcaptcha(); } - } diff --git a/app/admin/controller/Passport.php b/app/admin/controller/Passport.php index 7115971..1d395a8 100644 --- a/app/admin/controller/Passport.php +++ b/app/admin/controller/Passport.php @@ -36,7 +36,7 @@ class Passport extends AdminBase $key = isset($post['key']) ? $post['key'] : ''; $code = isset($post['code']) ? $post['code'] : ''; if ($key && $code) { - if (!captchaimg_check($code, $key)) { + if (!imgcaptcha_check($code, $key)) { return error('验证码不正确'); } } else { diff --git a/app/admin/controller/system/Admin.php b/app/admin/controller/system/Admin.php index 48c05a7..7816f6a 100644 --- a/app/admin/controller/system/Admin.php +++ b/app/admin/controller/system/Admin.php @@ -93,7 +93,7 @@ class Admin extends AdminBase Db::startTrans(); try { validate(SystemAdmin::class)->check($post); - $post['password'] = md5($post['password']); + $post['password'] = encrypt_pwd($post['password']); $post['admin_id']=$adminId; $this->model->save($post); $row = $this->model->where('username', $post['username'])->find(); @@ -148,7 +148,7 @@ class Admin extends AdminBase try { validate(SystemAdmin::class)->scene('edit')->check($post); if (isset($post['password']) && $post['password'] != "") { - $post['password'] = md5($post['password']); + $post['password'] = encrypt_pwd($post['password']); } else { unset($post['password']); } diff --git a/composer.json b/composer.json index 3cacf46..c3dc5a6 100644 --- a/composer.json +++ b/composer.json @@ -21,18 +21,19 @@ ], "require": { "php": ">=7.1.0", - "topthink/framework": "^6.0.0", + "topthink/framework": "^6.1.1", "topthink/think-orm": "^2.0", "topthink/think-multi-app": "^1.0", - "firebase/php-jwt": "^5.2", "topthink/think-captcha": "^3.0", "topthink/think-view": "^1.0", "phpoffice/phpspreadsheet": "^1.20", "zzstudio/think-addons": "^2.0", "symfony/var-exporter": "^5.4", - "zhusai/captcha-img": "^1.0", "zhusai/onlinecurd": "^1.0", - "topthink/think-filesystem": "^2.0" + "topthink/think-filesystem": "^2.0", + "hck/img-captcha": "^1.0", + "topthink/think-image": "^1.0", + "firebase/php-jwt": "6.3" }, "require-dev": { "symfony/var-dumper": "^4.2", diff --git a/config/captcha.php b/config/captcha.php index fbcfb09..6f5607f 100644 --- a/config/captcha.php +++ b/config/captcha.php @@ -31,6 +31,10 @@ return [ 'imageH' => Env::get('captcha.imageH', 0), // 验证码图片宽度 'imageW' => Env::get('captcha.imageW', 0), + // 验证码缓存head返回key + 'head_key' => Env::get('captcha.head_key', 'key'), + // 验证码缓存key前缀 + 'cache_key' => Env::get('captcha.cache_key', 'captcha_'), // 添加额外的验证码设置 // verify => [ diff --git a/config/logic.php b/config/logic.php new file mode 100644 index 0000000..6a45b7d --- /dev/null +++ b/config/logic.php @@ -0,0 +1,24 @@ + 'storage/', + //上传图片压缩的大小 长/宽 + 'file_thumb' => ['300', '300'], + //上传图片是否压缩 0 不压缩, 1 压缩 + 'squeeze' => 0, + //压缩图片前缀 + 'sq_prefix' => "it_", +]; diff --git a/extend/util/Token.php b/extend/util/Token.php index f53eb7e..39f7638 100644 --- a/extend/util/Token.php +++ b/extend/util/Token.php @@ -3,45 +3,24 @@ namespace util; use Exception; use Firebase\JWT\JWT; -use Symfony\Component\VarDumper\Caster\Caster; -use think\facade\Cache; +use Firebase\JWT\Key; use think\facade\Log; +use think\facade\Cache; class Token { // token类型 0=自定义生成, 1=jwt模式 public static $token_type = 0; + // 登陆渠道web,h5,mini小程序 + public static $login_type = "web"; + // jwt-加密方式 public static $jwt_alg = 'RS256'; // 刷新token前缀 public static $fresh_key = 'fresh_'; - /** - * 生成一个token - */ - // public static function create($admin, $is_keeplogin = false) - // { - // $jwt_config = config('auth.jwt'); - // $key = $jwt_config['key']; - // $expire = $jwt_config['expire']; - // $keeplogin_expire = $jwt_config['keeplogin_expire']; - // $time = time(); - // if ($is_keeplogin) { - // $expire = $keeplogin_expire; - // } - // $token = array( - // "iss" => $key, //签发者 可以为空 - // "aud" => '', //面象的用户,可以为空 - // "iat" => $time, //签发时间 - // "exp" => $time + $expire, //token 过期时间 - // "data" => $admin, - // ); - // $jwt = JWT::encode($token, $key, "HS256"); //根据参数生成了 token - // return $jwt; - // } - /** * 创建token * @param array $admin 待缓存的数据 @@ -51,20 +30,33 @@ class Token public static function create($admin, $is_keeplogin = false) { $jwt_config = config('auth')['jwt']; + $expire = $jwt_config['expire']; + if ($is_keeplogin) { + $expire = $jwt_config['keeplogin_expire']; + } // jwt if (self::$token_type == 1) { - $token = JWT::encode($admin, $jwt_config['key'], self::$jwt_alg); + $aud = $jwt_config['aud'][0] ?? 'mini'; + $encode = [ + 'data' => $admin, + 'aud' => $aud, + 'exp' => time() + $expire, // token过期时间, + 'iat' => time(), // token创建时间, + 'jti' => $aud . mt_rand(10000, 99999), // 对当前token设置唯一标示, + ]; + // 小程序不过期 + if (self::$login_type == 'mini') { + unset($encode['exp']); + $encode['sub_type'] = 'mini'; + } + $token = JWT::encode($encode, $jwt_config['key'], self::$jwt_alg); } else { $token = CommonTool::uuid(); } $fresh_token = self::$fresh_key . $token; - $expire = $jwt_config['expire']; - if ($is_keeplogin) { - $expire = $jwt_config['keeplogin_expire']; - } + $admin['exp'] = $expire; // token过期时间 - $admin['expire'] = $expire; Cache::set($token, $admin, $expire); Cache::set($fresh_token, $admin, $expire * 2); return $token; @@ -73,26 +65,54 @@ class Token /** * 验证token * @param string $token 请求的token - * @return bool + * @param int $return 0=返回bool 1=返回解析后的数据 + * @return bool|mixed */ - public static function verify($token) + public static function verify($token, $return = 0) { if (!$token) { return false; } - $admin = Cache::get($token); - if (!$admin) { - $fresh_token = "fresh_".$token; - $admin = Cache::get($fresh_token); + if (self::$token_type == 1) { + try { + $token = str_replace("Bearer ", "", $token); + if (empty($token)) { + return false; + } + $jwt_config = config('auth')['jwt']; + $token = (array)JWT::decode($token, new Key($jwt_config['key'], self::$jwt_alg)); + // 过期 + if (!isset($token['sub_type'])) { + if (!isset($token['exp']) || $token['exp'] < time()) { + return false; + } + } + if ($return) { + if ($token['data'] ?? []) { + $token['data'] = (array)$token['data']; + } + return $token; + } + return true; + } catch (Exception $e) { + Log::error("--verify token error--" . $e->getMessage()); + return false; + } + } else { + $admin = Cache::get($token); + if (!$admin) { + $fresh_token = "fresh_".$token; + $admin = Cache::get($fresh_token); + if ($admin['expire'] ?? "") { + Cache::set($token, $admin ,$admin['expire']); + Cache::set($fresh_token, $admin,$admin['expire'] * 2); + } + } if ($admin) { - Cache::set($token, $admin ,$admin['expire']); - Cache::set($fresh_token, $admin,$admin['expire'] * 2); + return true; } + return false; } - if ($admin) { - return true; - } - return false; } /** @@ -105,19 +125,32 @@ class Token if (!$token) { return false; } - $admin = Cache::get($token); - if (!$admin) { - $fresh_token = self::$fresh_key . $token; - $admin = Cache::get($fresh_token); + if (self::$token_type == 1) { + try { + $token = self::verify($token, 1); + if (!$token) { + return false; + } + return $token['data']['id'] ?? false; + } catch (Exception $e) { + Log::error("--token userId error--" . $e); + return false; + } + } else { + $admin = Cache::get($token); + if (!$admin) { + $fresh_token = self::$fresh_key . $token; + $admin = Cache::get($fresh_token); + if ($admin['expire'] ?? "") { + Cache::set($token, $admin, $admin['expire']); + Cache::set($fresh_token, $admin, $admin['expire'] * 2); + } + } if ($admin) { - Cache::set($token, $admin, $admin['expire']); - Cache::set($fresh_token, $admin, $admin['expire'] * 2); + return $admin['id']; } + return false; } - if ($admin) { - return $admin['id']; - } - return false; } /** @@ -130,19 +163,32 @@ class Token if (!$token) { return false; } - $admin = Cache::get($token); - if (!$admin) { - $fresh_token = self::$fresh_key . $token; - $admin = Cache::get($fresh_token); + if (self::$token_type == 1) { + try { + $token = self::verify($token, 1); + if (!$token) { + return false; + } + return $token['data'] ?? []; + } catch (Exception $e) { + Log::error("--token admin error--" . $e); + return false; + } + } else { + $admin = Cache::get($token); + if (!$admin) { + $fresh_token = self::$fresh_key . $token; + $admin = Cache::get($fresh_token); + if ($admin['expire'] ?? "") { + Cache::set($token, $admin, $admin['expire']); + Cache::set($fresh_token, $admin, $admin['expire'] * 2); + } + } if ($admin) { - Cache::set($token, $admin, $admin['expire']); - Cache::set($fresh_token, $admin, $admin['expire'] * 2); + return $admin; } + return false; } - if ($admin) { - return $admin; - } - return false; } /** @@ -160,7 +206,7 @@ class Token if (!$admin) { $fresh_token = self::$fresh_key . $token; $admin = Cache::get($fresh_token); - if ($admin) { + if ($admin['expire'] ?? "") { Cache::set($token, $admin, $admin['expire']); Cache::set($fresh_token, $admin, $admin['expire'] * 2); } @@ -183,58 +229,13 @@ class Token if (!$token) { return false; } - Cache::delete($token); - Cache::delete(self::$fresh_key . $token); - return true; + // 考虑维护一个redis哈希表-->保存所有token的唯一id的哈希表 + if (self::$token_type == 1) { + return true; + } else { + Cache::delete($token); + Cache::delete(self::$fresh_key . $token); + return true; + } } - - /** - * 验证 - */ - // public static function verify($token) - // { - // try { - // $key = config('auth.jwt.key'); - // JWT::decode($token, $key, array('HS256')); - // return true; - // } catch (\Exception $e) { - // return false; - // } - // } - //获取用户id - // public static function userId($token) - // { - // if (!$token) { - // return false; - // } - // try { - // $key = config('auth.jwt.key'); - // $decode = JWT::decode($token, $key, array('HS256')); - // $member_id = $decode->data->id; - // return $member_id; - // } catch (\Exception $e) { - // Log::error("--token userId error--" . $e); - // return false; - // } - - // } - // /** - // * 获取用户信息 - // */ - // public static function admin($token) - // { - // if (!$token) { - // return false; - // } - // try { - // $key = config('auth.jwt.key'); - // $decode = JWT::decode($token, $key, array('HS256')); - // return (array) $decode->data; - // } catch (\Exception $e) { - // Log::error("--token admin error--" . $e); - // return false; - // } - - // } - } diff --git a/extend/util/Upload.php b/extend/util/Upload.php index 98c59ac..51bbe4f 100644 --- a/extend/util/Upload.php +++ b/extend/util/Upload.php @@ -12,10 +12,38 @@ use think\facade\Filesystem; */ class Upload { + // 本地文件上传路径 + protected $_file_path = ''; + + // 是否压缩 0 不压缩, 1 压缩 + protected $_squeeze = 0; + + // 压缩图片大小 长/宽 + protected $_file_thumb = []; + + // 压缩图片前缀 + protected $_sq_prefix = ""; + public $config = []; - public function __construct() + + public function __construct($config = []) { $this->config = config('upload'); + + //文件上传路径 + $this->_file_path = config('logic.uploads') ?? 'uploads/'; + + //上传图片压缩的大小 长/宽 + $this->_file_thumb = config('logic.file_thumb') ?? ['300', '300']; + + //是否压缩 0 不压缩, 1 压缩 + $this->_squeeze = config('logic.squeeze') ?? 0; + if ($config['squeeze'] ?? "") { + $this->_squeeze = $config['squeeze']; + } + + //压缩图片前缀 + $this->_sq_prefix = config('logic.sq_prefix') ?? "it_"; } /** @@ -64,16 +92,30 @@ class Upload try { $savename = Filesystem::disk('public')->putFile($filename, $file); + // 生成缩略图 + $squee_path = $thumb_url = ""; + if ($this->_squeeze) { + $path = pathinfo($savename); + $squee_path = $this->_file_path . $path['dirname'] . '/' . $this->_sq_prefix . $path['basename']; + $Image = \think\Image::open($this->_file_path . $savename); + $Image->thumb($this->_file_thumb[0], $this->_file_thumb[1])->save($squee_path); + } + $upload_config = sysconfig('upload'); $domain = isset($upload_config['upload_url']) ? $upload_config['upload_url'] : ''; if (!$domain) { return ['msg' => '请到后台配置管理-系统管理-上传配置中配置:本地图片路径', 'url' => ""]; } - $savepath = "/storage/"; - $url = $domain . $savepath . str_replace(DIRECTORY_SEPARATOR, '/', $savename); - return ['msg' => '上传成功', 'url' => $url]; + $url = $domain . "/" . $this->_file_path . str_replace(DIRECTORY_SEPARATOR, '/', $savename); + if ($this->_squeeze) { + $thumb_url = $domain . "/" . $squee_path; + return ['msg' => '上传成功', 'url' => $url, 'thumb_url' => $thumb_url]; + } else { + return ['msg' => '上传成功', 'url' => $url]; + } } catch (\Exception $e) { return ['msg' => '上传失败', 'url' => ""]; } + } } -- Gitee