From 4593540bf72244bfdf61e670fe07273b2d3359cf Mon Sep 17 00:00:00 2001 From: wangxiaowei Date: Sat, 14 Dec 2024 16:47:15 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9localstore=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/basics/LocalStoreBase.php | 307 ++++++++++++++++++ .../model/localstore/LocalStoreAdmin.php | 13 + app/localstore/controller/Index.php | 4 +- app/localstore/logic/LoginLogic.php | 9 +- app/localstore/validate/LoginValidate.php | 11 +- 5 files changed, 330 insertions(+), 14 deletions(-) create mode 100755 app/common/basics/LocalStoreBase.php create mode 100644 app/common/model/localstore/LocalStoreAdmin.php diff --git a/app/common/basics/LocalStoreBase.php b/app/common/basics/LocalStoreBase.php new file mode 100755 index 00000000..5e8b7510 --- /dev/null +++ b/app/common/basics/LocalStoreBase.php @@ -0,0 +1,307 @@ +app = $app; + $this->request = $this->app->request; + + // 控制器初始化 + $this->initialize(); + } + + /** + * 初始化 + */ + protected function initialize() + { + //默认设置参数 + $this->initConfig(); + + //验证登录 + $this->checkLogin(); + + //验证权限 + $this->checkAuth(); + + //默认页面参数 + $this->setViewValue(); + return true; + } + + + /** + * Notes: 基础配置参数 + * @author 段誉(2021/4/9 14:18) + */ + protected function initConfig() + { + $this->local_store = session('shop_info'); + $this->lstore_id = session('shop_info.lstore_id'); + $this->local_store_name = session('shop_info.shop_name'); + $this->local_store_admin_id = session('shop_info.id'); + $this->local_store_admin_name = session('shop_info.name'); + //分页参数 + $page_no = (int)$this->request->get('page_no'); + $this->page_no = $page_no && is_numeric($page_no) ? $page_no : $this->page_no; + $page_size = (int)$this->request->get('page_size'); + $this->page_size = $page_size && is_numeric($page_size) ? $page_size : $this->page_size; + $this->page_size = min($this->page_size, 100); + } + + + /** + * 设置视图全局变量 + */ + private function setViewValue() + { + $app = Config::get('project'); + View::assign([ + 'view_env_name' => $app['env_name'], + 'view_admin_name' => $app['admin_name'], + 'view_theme_color' => $app['theme_color'], + 'view_theme_button' => $app['theme_button'], + 'front_version' => $app['front_version'], + 'version' => $app['version'], + 'dateTime' => Time::getTime(), + 'storageUrl' => UrlServer::getFileUrl('/'), + 'company_name' => ConfigServer::get('copyright', 'company_name') + ]); + $this->assignJs('image_upload_url', ''); + } + + + /** + * Notes: 检查登录 + * @author 段誉(2021/4/9 14:05) + * @return bool + */ + protected function checkLogin() + { + //已登录的访问登录页 + if ($this->local_store && !$this->isNotNeedLogin()) { + return true; + } + + //已登录的访问非登录页 + if ($this->local_store && $this->isNotNeedLogin()) { + $this->redirect(url('index/index')); + } + + //未登录的访问非登录页 + if (!$this->local_store && $this->isNotNeedLogin()) { + return true; + } + + //未登录访问登录页 + $this->redirect(url('login/login')); + } + + + /** + * Notes: 验证登录角色权限 + * @author 段誉(2021/4/13 11:34) + * @return bool + */ + protected function checkAuth() + { + //未登录的无需权限控制 + if (empty(session('shop_info'))) { + return true; + } + + //如果id为1,视为系统超级管理,无需权限控制 + if (session('shop_info.id') == 1) { + return true; + } + + //权限控制判断 + $controller_action = request()->controller() . '/' . request()->action();// 当前访问 + $controller_action = strtolower($controller_action); + + //没有的权限 + $none_auth = AuthServer::getRoleNoneAuthUris(session('shop_info.role_id')); + if (empty($none_auth) || !in_array($controller_action, $none_auth)) { + //通过权限控制 + return true; + } + + $this->redirect(url('dispatch/dispatch_error',['msg' => '权限不足,无法访问'])); + return false; + } + + + /** + * Notes: js + * @param $name + * @param $value + * @author 段誉(2021/4/9 14:23) + */ + protected function assignJs($name, $value) + { + $this->js_data[$name] = $value; + $js_code = ""; + View::assign('js_code', $js_code); + } + + + /** + * Notes: 是否无需登录 + * @author 段誉(2021/4/9 14:03) + * @return bool + */ + private function isNotNeedLogin() + { + if (empty($this->like_not_need_login)) { + return false; + } + $action = strtolower(request()->action()); + $data = array_map('strtolower', $this->like_not_need_login); + if (!in_array($action, $data)) { + return false; + } + return true; + } + + + /** + * Notes: 自定义重定向 + * @param mixed ...$args + * @author 段誉(2021/4/9 14:04) + */ + public function redirect(...$args) + { + throw new HttpResponseException(redirect(...$args)); + } + + +} \ No newline at end of file diff --git a/app/common/model/localstore/LocalStoreAdmin.php b/app/common/model/localstore/LocalStoreAdmin.php new file mode 100644 index 00000000..2c6d4ce7 --- /dev/null +++ b/app/common/model/localstore/LocalStoreAdmin.php @@ -0,0 +1,13 @@ +alias('a') ->join('shop s', 's.id = a.shop_id') ->field(['a.id', 'a.account', 'a.name', 'role_id', 'shop_id', 's.name' => 'shop_name', 's.type']) @@ -74,11 +75,9 @@ class LoginLogic extends Logic */ public static function logout() { - session('shop_info', null); + session('local_store_info', null); } - - public static function config() { $config = [ @@ -95,6 +94,4 @@ class LoginLogic extends Logic ]; return $config; } - - } \ No newline at end of file diff --git a/app/localstore/validate/LoginValidate.php b/app/localstore/validate/LoginValidate.php index 014a91f6..9298027b 100755 --- a/app/localstore/validate/LoginValidate.php +++ b/app/localstore/validate/LoginValidate.php @@ -22,7 +22,7 @@ namespace app\localstore\validate; use app\common\basics\Validate; -use app\common\model\shop\ShopAdmin; +use app\common\model\localstore\LocalStoreAdmin; use think\facade\Cache; /** @@ -61,13 +61,12 @@ class LoginValidate extends Validate return false; } - $adminModel = new ShopAdmin(); + $adminModel = new LocalStoreAdmin(); $admin_info = $adminModel->alias('a') ->field('a.*') - ->join('shop s', 's.id = a.shop_id') + ->join('local_store ls', 'ls.lstore_id = a.lstore_id') ->where(['a.account' => $data['account'], 'a.del' => 0]) ->find(); - if (empty($admin_info)) { $this->safe(true); return '账号不存在'; @@ -93,11 +92,11 @@ class LoginValidate extends Validate */ protected function safe($add = false) { - $cache_name = 'shop_admin_login_error_count' . request()->ip(); + $cache_name = 'local_store_admin_login_error_count' . request()->ip(); if ($add) { $admin_login_error_count = Cache::get($cache_name); $admin_login_error_count++; - Cache::tag('shop_admin_login_error_count')->set($cache_name, $admin_login_error_count, 1800); + Cache::tag('local_store_admin_login_error_count')->set($cache_name, $admin_login_error_count, 1800); } $count = Cache::get($cache_name);