123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- namespace app\common\basics;
-
-
- use app\admin\server\AuthServer;
- use app\common\server\ConfigServer;
- use app\common\server\UrlServer;
- use app\common\utils\Time;
- use think\App;
- use think\Controller;
- use think\exception\HttpResponseException;
- use think\facade\Config;
- use think\facade\Debug;
- use think\facade\View;
- use think\Response;
- use app\common\model\system\SystemLog;
-
-
- abstract class AdminBase
- {
-
-
- protected $request;
-
-
-
- protected $app;
-
-
-
- protected $adminId = null;
-
-
-
- protected $adminUser = null;
-
-
-
- protected $logic;
-
-
-
- protected $validate;
-
-
-
- public $like_not_need_login = [];
-
-
-
- protected $js_data = [];
-
-
-
- public $page_no = 1;
- public $page_size = 15;
-
-
-
- public $view_theme_color = '';
-
-
-
-
-
- public function __construct(App $app)
- {
- $this->app = $app;
- $this->request = $this->app->request;
-
-
- $this->initialize();
- }
-
-
-
- protected function initialize()
- {
-
- $this->initConfig();
-
-
- $this->checkLogin();
-
-
- $this->checkAuth();
-
-
- $this->setViewValue();
-
-
- $this->log();
-
- return true;
- }
-
-
-
- protected function log()
- {
- if(request()->action() != 'login') {
- $data = [
- 'admin_id' => $this->adminId,
- 'name' => $this->adminUser['name'],
- 'account' => $this->adminUser['account'],
- 'create_time' => time(),
- 'uri' => request()->baseUrl(),
- 'type' => request()->method(),
- 'param' => json_encode(request()->param(),JSON_UNESCAPED_UNICODE),
- 'ip' => request()->ip()
- ];
- SystemLog::create($data);
- }
- }
-
-
-
-
- protected function initConfig()
- {
- $this->adminUser = session('admin_info');
- $this->adminId = session('admin_info.id');
-
- $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', '');
- }
-
-
-
-
- protected function checkLogin()
- {
-
- if ($this->adminUser && !$this->isNotNeedLogin()) {
- return true;
- }
-
-
- if ($this->adminUser && $this->isNotNeedLogin()) {
- $this->redirect(url('index/index'));
- }
-
-
- if (!$this->adminUser && $this->isNotNeedLogin()) {
- return true;
- }
-
-
- $this->redirect(url('login/login'));
- }
-
-
-
-
- protected function checkAuth()
- {
-
- if (empty(session('admin_info'))) {
- return true;
- }
-
-
- if (session('admin_info.id') == 1) {
- return true;
- }
-
-
- $controller_action = request()->controller() . '/' . request()->action();
- $controller_action = strtolower($controller_action);
-
-
- $none_auth = AuthServer::getRoleNoneAuthUris(session('admin_info.role_id'));
- if (empty($none_auth) || !in_array($controller_action, $none_auth)) {
-
- return true;
- }
-
- $this->redirect(url('dispatch/dispatch_error',['msg' => '权限不足,无法访问']));
- return false;
- }
-
-
-
-
- protected function assignJs($name, $value)
- {
- $this->js_data[$name] = $value;
- $js_code = "<script>";
- foreach ($this->js_data as $name => $value) {
- if (is_array($value)) {
- $value = json_encode($value);
- } elseif (!is_integer($value)) {
- $value = '"' . $value . '"';
- }
- $js_code .= $name . '=' . $value . ';';
- }
- $js_code .= "</script>";
- View::assign('js_code', $js_code);
- }
-
-
-
-
- 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;
- }
-
-
-
-
- public function redirect(...$args)
- {
- throw new HttpResponseException(redirect(...$args));
- }
-
-
-
- }
|