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 appasecontroller;

use thinkApp;
use thinkController;

use appasemodelLog;
use appasemodelUrole;
use appasemodelAdmin;
use appasemodelMember;
use appasemodelSystem;
use appasemodelBanner;
use appasemodelContent;
use appasemodelUaction;
use appasemodelClassify;

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:1322816443@qq.com
     * @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:1322816443@qq.com
     * @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:1322816443@qq.com
     * @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:1322816443@qq.com
     * @param string $code
     * @param string $msg
     * @return array
     */
    static public function showReturnCodeWithOutData($code = '', $msg = '')
    {
        return self::showReturnCode($code,[],$msg);
    }

    /**
     * 获取当前类名称
     * Power: ZYF
     * Email:1322816443@qq.com
     * @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:1322816443@qq.com
     * @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);
    }



}
原文地址:https://www.cnblogs.com/zyfeng/p/14086807.html