1. 程式人生 > 實用技巧 >TP5.1 控制器(基類)

TP5.1 控制器(基類)

直接上程式碼

<?php
/**
 * Created by PhpStorm.
 * User: Zhangyongfeng
 * Date: 2020/12/1
 * Time: 11:31
 *
 * ━━━━━━━━━神獸出沒━━━━━━━━━
 *
 *        ┏┓   ┏┓+ +
 *       ┏┛┻━━━┛┻┓ + +
 *       ┃       ┃  
 *       ┃   ━   ┃ ++ + + +
 *       ████━████ ┃+
 *       ┃       ┃ +
 *       ┃   ┻   ┃
 *       ┃       ┃ + +
 *       ┗━┓   ┏━┛
 *         ┃   ┃           
 *         ┃   ┃ + + + +
 *         ┃   ┃    Code is far away from bug with the animal protecting       
 *         ┃   ┃ +     神獸保佑,程式碼無bug  
 *         ┃   ┃
 *         ┃   ┃  +         
 *         ┃    ┗━━━┓ + +
 *         ┃        ┣┓
 *         ┃        ┏┛
 *         ┗┓┓┏━┳┓┏┛ + + + +
 *          ┃┫┫ ┃┫┫
 *          ┗┻┛ ┗┻┛+ + + +
 *
 * ━━━━━━━━━感覺萌萌噠━━━━━━━━━
 
*/ namespace app\base\controller; use think\App; use think\Controller; use app\base\model\Log; use app\base\model\Urole; use app\base\model\Admin; use app\base\model\Member; use app\base\model\System; use app\base\model\Banner; use app\base\model\Content; use app\base\model\Uaction; use app\base\model\Classify;
abstract class Base extends Controller { protected $file = []; // 操作檔案 protected $param = []; // 所有引數 protected $config = []; // 系統配置 protected $u_admin = []; // 後臺使用者 protected $u_index = []; // 前臺使用者 protected $diyConfig = []; //
使用者配置 // 模型 protected $log; protected $urole; protected $admin; protected $member; protected $system; protected $banner; protected $content; protected $uaction; protected $classify; public function __construct(App $app = null) { parent::__construct($app); $this->initialize(); } protected function initialize() { parent::initialize(); // TODO: Change the autogenerated stub $this->log = new Log(); $this->urole = new Urole(); $this->admin = new Admin(); $this->member = new Member(); $this->system = new System(); $this->banner = new Banner(); $this->content = new Content(); $this->uaction = new Uaction(); $this->classify = new Classify(); $this->file = new File(); $this->param = request()->param(); $this->config = $this->system->getAllList(); $this->diyConfig = config('diy.diy'); $this->u_admin = session('u_admin', '', 'admin'); $this->u_index = session('u_index', '', 'index'); $this->assign([ 'config' => $this->config, 'diyConfig' => $this->diyConfig, ]); } /** * 返回請求碼 * Power: ZYF * Email:[email protected] * @param string $code * @param array $data * @param string $msg * @return array */ static public function showReturnCode($code = '', $data = [], $msg = '') { $return_data = [ 'code' => '500', 'msg' => '未定義訊息', 'data' => $code == 1001 ? $data : [], ]; if (empty($code)) return $return_data; $return_data['code'] = $code; if(!empty($msg)){ $return_data['msg'] = $msg; }else if (isset(ReturnCode::$return_code[$code]) ) { $return_data['msg'] = ReturnCode::$return_code[$code]; } return $return_data; } /** * 獲取返回碼數組別名函式 以json格式返回 * Power: ZYF * Email:[email protected] * @param string $code * @param string $msg * @return array */ static public function showJsonReturnCode($code = '', $data = [], $msg = ''){ config('default_return_type', 'json'); return self::showReturnCode($code, $data, $msg); } /** * 獲取返回碼數組別名函式 以json格式返回 無返回值 * Power: ZYF * Email:[email protected] * @param string $code * @param string $msg * @return array */ static public function showJsonReturnCodeWithOutData($code = '', $msg = ''){ config('default_return_type', 'json'); return self::showReturnCode($code, [], $msg); } /** * 獲取返回碼數組別名函式 無返回值 * Power: ZYF * Email:[email protected] * @param string $code * @param string $msg * @return array */ static public function showReturnCodeWithOutData($code = '', $msg = '') { return self::showReturnCode($code,[],$msg); } /** * 獲取當前類名稱 * Power: ZYF * Email:[email protected] * @param bool|false $all * @return string */ public function getClassName($all = false){ return $all ? get_called_class() : basename(str_replace('\\', '/', get_called_class()),'.php'); } /** * 資料庫欄位 網頁欄位轉換 * Power: ZYF * Email:[email protected] * @param array $array 標識為資料庫欄位 值為瀏覽器提交欄位 * @param bool|false $uuid 是否追加UUID資訊 * @return array */ protected function buildParam($array, $uuid = false) { $data = []; foreach( $array as $item => $value ){ $data[$item] = $this->request->param($value); } if ($uuid && isset($this->uuid)){ $data['uuid'] = $this->uuid; } return $data; } /** * 獲取上傳檔案的 檔名 和 儲存路徑 * @param object file 檔案 * @return array|object */ public function upfile(){ $path = []; // 獲取表單上傳檔案 例如上傳了001.jpg $file = request()->file('file'); // 移動到框架應用根目錄/uploads/ 目錄下 $names = explode('.',$_FILES['file']['name']); $name = $names[0].'.'.$names[count($names)-1]; $info = $file->move( './upload'); if($info){ $getSaveName = str_replace("\\","/",$info->getSaveName()); $path[] = [ 'name' => $name, 'path' => '/upload/'.$getSaveName, ]; } return json($path); } }