104 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			PHP
		
	
	
		
		
			
		
	
	
			104 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			PHP
		
	
	
|  | <?php | ||
|  | 
 | ||
|  | namespace app\controller\manager; | ||
|  | 
 | ||
|  | use app\controller\BaseController; | ||
|  | use app\service\File as FileTool; | ||
|  | use Exception; | ||
|  | use think\exception\ValidateException; | ||
|  | use think\response\Json; | ||
|  | use think\response\Redirect; | ||
|  | use think\response\View; | ||
|  | 
 | ||
|  | /** | ||
|  |  * 控制器基础类 | ||
|  |  */ | ||
|  | class Base extends BaseController | ||
|  | { | ||
|  |     protected $data = []; | ||
|  |     protected $auth = null; | ||
|  | 
 | ||
|  |     protected function initialize() | ||
|  |     { | ||
|  |         $this->middleware      = [ | ||
|  |             'auth' => ['except' => array_merge($this->noNeedLogin, $this->noNeedRight)], | ||
|  |             //            'jwt'  => ['except' => $this->noNeedRight],
 | ||
|  |         ]; | ||
|  |         $this->auth            = session('auth'); | ||
|  |         $this->data['member']  = $this->auth; | ||
|  |         $this->data['_token']  = $this->auth['token'] ?? ''; | ||
|  |         $this->data['groupId'] = $this->auth['groupId'] ?? 0; | ||
|  | 
 | ||
|  |         $this->fileDomain(); | ||
|  |     } | ||
|  | 
 | ||
|  |     //变量赋值到模板
 | ||
|  |     protected function view(string $template = '') | ||
|  |     { | ||
|  |         return view($template)->assign($this->data); | ||
|  |     } | ||
|  | 
 | ||
|  |     /** | ||
|  |      * @param  string  $msg | ||
|  |      * @param  string|null  $url | ||
|  |      * @param  string  $data | ||
|  |      * @param  int  $wait | ||
|  |      * @return Redirect | ||
|  |      */ | ||
|  |     protected function error($msg = '', string $url = null, $data = '', int $wait = 3): Redirect | ||
|  |     { | ||
|  |         if (is_null($url)) { | ||
|  |             $url = $this->request->isAjax() ? '' : 'javascript:history.back(-1);'; | ||
|  |         } elseif ($url) { | ||
|  |             $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : $this->app->route->buildUrl($url); | ||
|  |         } | ||
|  |         $result = [ | ||
|  |             'code' => 0, | ||
|  |             'msg'  => $msg, | ||
|  |             'data' => $data, | ||
|  |             'url'  => $url, | ||
|  |             'wait' => $wait, | ||
|  |         ]; | ||
|  | 
 | ||
|  |         return $this->redirect(url('/manager/error/jump', $result)); | ||
|  |     } | ||
|  | 
 | ||
|  |     public function __call($name, $args) | ||
|  |     { | ||
|  |         return $this->view('/manager/error/jump'); | ||
|  |     } | ||
|  | 
 | ||
|  |     /** | ||
|  |      * 验证器 | ||
|  |      * | ||
|  |      * @param  array  $data | ||
|  |      * @param $validate | ||
|  |      * @param  array  $message | ||
|  |      * @param  bool  $batch | ||
|  |      * @return Redirect | ||
|  |      * @throws Exception | ||
|  |      */ | ||
|  |     protected function validateError(array $data, $validate, array $message = [], bool $batch = false): Redirect | ||
|  |     { | ||
|  |         try { | ||
|  |             parent::validate($data, $validate, $message, $batch); | ||
|  |         } catch (ValidateException $e) { | ||
|  |             $msg = $e->getMessage(); | ||
|  |             if ($batch) { | ||
|  |                 $msg = implode(',', $e->getError()); | ||
|  |             } | ||
|  | 
 | ||
|  |             return $this->error($msg); | ||
|  |         } catch (Exception $e) { | ||
|  |             throw $e; | ||
|  |         } | ||
|  |     } | ||
|  | 
 | ||
|  |     /** | ||
|  |      * 文件域名前缀 | ||
|  |      */ | ||
|  |     public function fileDomain() | ||
|  |     { | ||
|  |         $this->data['fileDomain'] = FileTool::getFileDomain(); | ||
|  |     } | ||
|  | } |