fastadmin默认的controller已实现的方法

fastadmin控制器

<?php

namespace app\admin\controller\peizi;

use app\common\controller\Backend;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Config extends Backend
{
    
    /**
     * Config模型对象
     * @var \app\admin\model\weixinitem\Config
     */
    //protected $model = null;

    public function _initialize()
    {
      //  parent::_initialize();
        //$this->model = new \app\admin\model\peizi\Config;
        //$this->view->assign("gztypeList", $this->model->getGztypeList());
    }
    
    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     *
     * 下面将这些方法属性放下面
     */
     


    //application/admin/library/traits/Backend.php的trait Backend类
    /**
     * 查看
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage,则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $list = collection($list)->toArray();
            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 回收站
     */
    public function recyclebin()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->model
                ->onlyTrashed()
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->onlyTrashed()
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }

    /**
     * 添加
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
                    $params[$this->dataLimitField] = $this->auth->id;
                }
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
                        $this->model->validate($validate);
                    }
                    $result = $this->model->allowField(true)->save($params);
                    if ($result !== false) {
                        $this->success();
                    } else {
                        $this->error($this->model->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                } catch (\think\Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        return $this->view->fetch();
    }

    /**
     * 编辑
     */
    public function edit($ids = NULL)
    {
        $row = $this->model->get($ids);
        if (!$row)
            $this->error(__('No Results were found'));
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            if (!in_array($row[$this->dataLimitField], $adminIds)) {
                $this->error(__('You have no permission'));
            }
        }
        if ($this->request->isPost()) {
            $params = $this->request->post("row/a");
            if ($params) {
                try {
                    //是否采用模型验证
                    if ($this->modelValidate) {
                        $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                        $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                        $row->validate($validate);
                    }
                    $result = $row->allowField(true)->save($params);
                    if ($result !== false) {
                        $this->success();
                    } else {
                        $this->error($row->getError());
                    }
                } catch (\think\exception\PDOException $e) {
                    $this->error($e->getMessage());
                } catch (\think\Exception $e) {
                    $this->error($e->getMessage());
                }
            }
            $this->error(__('Parameter %s can not be empty', ''));
        }
        $this->view->assign("row", $row);
        return $this->view->fetch();
    }

    /**
     * 删除
     */
    public function del($ids = "")
    {
        if ($ids) {
            $pk = $this->model->getPk();
            $adminIds = $this->getDataLimitAdminIds();
            if (is_array($adminIds)) {
                $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
            }
            $list = $this->model->where($pk, 'in', $ids)->select();
            $count = 0;
            foreach ($list as $k => $v) {
                $count += $v->delete();
            }
            if ($count) {
                $this->success();
            } else {
                $this->error(__('No rows were deleted'));
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }

    /**
     * 真实删除
     */
    public function destroy($ids = "")
    {
        $pk = $this->model->getPk();
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
        }
        if ($ids) {
            $this->model->where($pk, 'in', $ids);
        }
        $count = 0;
        $list = $this->model->onlyTrashed()->select();
        foreach ($list as $k => $v) {
            $count += $v->delete(true);
        }
        if ($count) {
            $this->success();
        } else {
            $this->error(__('No rows were deleted'));
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }

    /**
     * 还原
     */
    public function restore($ids = "")
    {
        $pk = $this->model->getPk();
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds)) {
            $this->model->where($this->dataLimitField, 'in', $adminIds);
        }
        if ($ids) {
            $this->model->where($pk, 'in', $ids);
        }
        $count = 0;
        $list = $this->model->onlyTrashed()->select();
        foreach ($list as $index => $item) {
            $count += $item->restore();
        }
        if ($count) {
            $this->success();
        }
        $this->error(__('No rows were updated'));
    }

    /**
     * 批量更新
     */
    public function multi($ids = "")
    {
        $ids = $ids ? $ids : $this->request->param("ids");
        if ($ids) {
            if ($this->request->has('params')) {
                parse_str($this->request->post("params"), $values);
                if (!$this->auth->isSuperAdmin()) {
                    $values = array_intersect_key($values, array_flip(is_array($this->multiFields) ? $this->multiFields : explode(',', $this->multiFields)));
                }
                if ($values) {
                    $adminIds = $this->getDataLimitAdminIds();
                    if (is_array($adminIds)) {
                        $this->model->where($this->dataLimitField, 'in', $adminIds);
                    }
                    $count = 0;
                    $list = $this->model->where($this->model->getPk(), 'in', $ids)->select();
                    foreach ($list as $index => $item) {
                        $count += $item->allowField(true)->isUpdate(true)->save($values);
                    }
                    if ($count) {
                        $this->success();
                    } else {
                        $this->error(__('No rows were updated'));
                    }
                } else {
                    $this->error(__('You have no permission'));
                }
            }
        }
        $this->error(__('Parameter %s can not be empty', 'ids'));
    }

    /**
     * 导入
     */
    protected function import()
    {
        $file = $this->request->request('file');
        if (!$file) {
            $this->error(__('Parameter %s can not be empty', 'file'));
        }
        $filePath = ROOT_PATH . DS . 'public' . DS . $file;
        if (!is_file($filePath)) {
            $this->error(__('No results were found'));
        }
        $PHPReader = new \PHPExcel_Reader_Excel2007();
        if (!$PHPReader->canRead($filePath)) {
            $PHPReader = new \PHPExcel_Reader_Excel5();
            if (!$PHPReader->canRead($filePath)) {
                $PHPReader = new \PHPExcel_Reader_CSV();
                if (!$PHPReader->canRead($filePath)) {
                    $this->error(__('Unknown data format'));
                }
            }
        }

        //导入文件首行类型,默认是注释,如果需要使用字段名称请使用name
        $importHeadType = isset($this->importHeadType) ? $this->importHeadType : 'comment';

        $table = $this->model->getQuery()->getTable();
        $database = \think\Config::get('database.database');
        $fieldArr = [];
        $list = db()->query("SELECT COLUMN_NAME,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?", [$table, $database]);
        foreach ($list as $k => $v) {
            if ($importHeadType == 'comment') {
                $fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME'];
            } else {
                $fieldArr[$v['COLUMN_NAME']] = $v['COLUMN_NAME'];
            }
        }

        $PHPExcel = $PHPReader->load($filePath); //加载文件
        $currentSheet = $PHPExcel->getSheet(0);  //读取文件中的第一个工作表
        $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
        $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
        $maxColumnNumber = \PHPExcel_Cell::columnIndexFromString($allColumn);
        for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
            for ($currentColumn = 0; $currentColumn < $maxColumnNumber; $currentColumn++) {
                $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
                $fields[] = $val;
            }
        }
        $insert = [];
        for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
            $values = [];
            for ($currentColumn = 0; $currentColumn < $maxColumnNumber; $currentColumn++) {
                $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
                $values[] = is_null($val) ? '' : $val;
            }
            $row = [];
            $temp = array_combine($fields, $values);
            foreach ($temp as $k => $v) {
                if (isset($fieldArr[$k]) && $k !== '') {
                    $row[$fieldArr[$k]] = $v;
                }
            }
            if ($row) {
                $insert[] = $row;
            }
        }
        if (!$insert) {
            $this->error(__('No rows were updated'));
        }
        try {
            $this->model->saveAll($insert);
        } catch (\think\exception\PDOException $exception) {
            $this->error($exception->getMessage());
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }

        $this->success();
    }




    

    //app\common\controller\Backend类属性
    /**
     * 无需登录的方法,同时也就不需要鉴权了
     * @var array
     */
    protected $noNeedLogin = [];

    /**
     * 无需鉴权的方法,但需要登录
     * @var array
     */
    protected $noNeedRight = [];

    /**
     * 布局模板
     * @var string
     */
    protected $layout = 'default';

    /**
     * 权限控制类
     * @var Auth
     */
    protected $auth = null;

    /**
     * 快速搜索时执行查找的字段
     */
    protected $searchFields = 'id';

    /**
     * 是否是关联查询
     */
    protected $relationSearch = false;

    /**
     * 是否开启数据限制
     * 支持auth/personal
     * 表示按权限判断/仅限个人 
     * 默认为禁用,若启用请务必保证表中存在admin_id字段
     */
    protected $dataLimit = false;

    /**
     * 数据限制字段
     */
    protected $dataLimitField = 'admin_id';

    /**
     * 是否开启Validate验证
     */
    protected $modelValidate = false;

    /**
     * 是否开启模型场景验证
     */
    protected $modelSceneValidate = false;

    /**
     * Multi方法可批量修改的字段
     */
    protected $multiFields = 'status';





   //app\common\controller\Backend类方法
    /**
     * 加载语言文件
     * @param string $name
     */
    protected function loadlang($name)
    {
    }

    /**
     * 渲染配置信息
     * @param mixed $name 键名或数组
     * @param mixed $value 值 
     */
    protected function assignconfig($name, $value = '')
    {    
    }

    /**
     * 生成查询所需要的条件,排序方式
     * @param mixed $searchfields 快速查询的字段
     * @param boolean $relationSearch 是否关联查询
     * @return array
     */
    protected function buildparams($searchfields = null, $relationSearch = null)
    {    
    }

    /**
     * 获取数据限制的管理员ID
     * 禁用数据限制时返回的是null
     * @return mixed
     */
    protected function getDataLimitAdminIds()
    {    
    }

    /**
     * Selectpage的实现方法
     * 
     * 当前方法只是一个比较通用的搜索匹配,请按需重载此方法来编写自己的搜索逻辑,$where按自己的需求写即可
     * 这里示例了所有的参数,所以比较复杂,实现上自己实现只需简单的几行即可
     * 
     */
    protected function selectpage()
    {
    }    
}

 

原文地址:https://www.cnblogs.com/lichihua/p/10468218.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


是“帐”还是“账”---由FastAdmin用户中心引出的讨论有小伙伴对FastAdmin用户中心的“账号”提出异议,应该为“帐号”。查了资料,其实都没有问题不大。最初只有一个“帐”,后来为了区分造出了“账”。“帐”通“账,但不可以反过来,就是说使用“账”的地方可以使用“帐”,但“
好久不见了各位朋友,昨天在kali下倒腾东西的时候发现,我下了第三方框架fastadmin框架并安装时遇到的一些问题,然后整理下过程。下面我们开始今天的正题。首先先看下我们的环境,如下图所示:  然后我们看着官方文档一步步来执行。这里是官方文档链接https://doc.fastadmin.net
手把手教你安装FastAdmin到虚拟主机(phpStudy)原文: https://forum.fastadmin.nethread/2524下载FastAdmin下载FastAdmin1并解压到C:/www/FastAdmin目录目录列表如下安装phpStudy2018安装phpStudy20182并修改phpStudy为php-7.1.13-nts+Apache配置虚拟主机点
fastadmin控制器<?phpnamespaceapp\admin\controller\peizi;useapp\common\controller\Backend;/*****@iconfafa-circle-o*/classConfigextendsBackend{/***Config模型对象*@var\app\admin\model\weixinitem\Config*///p
创建用户CREATEUSER'video'@'%'IDENTIFIEDBY'video2019';.限制用户权限GRANTSELECT,INSERT,UPDATE,REFERENCES,DELETE,CREATE,DROP,ALTER,INDEX,CREATEVIEW,SHOWVIEWON`video_fastadmin`.*TO'video'@'%';
FastAdminCMS插件标签使用说明在CMS插件中的前端视图模板中有大量使用了自定义标签,我们在修改或制作模板的时候可以方便快捷的使用自定义标签来调用我们相关的数据。标签库位于/addons/cmsaglib/Cms.php文件,我们可以看到标签库有定义可调用的标签和属性,如下protected$tags
在项目所对应的js文件中的table.bootstrapTable({url:$.fn.bootstrapTable.defaults.extend.index_url,pk:'id',sortName:'id',columns:[{field:'operate',title:__('Operate'),t
为什么后台给管理员所在组分配了权限,管理员仍然提示没有权限访问如果是你自己添加的控制器,可以使用一键生成菜单命令(phpthinkmenu-c控制器名)来生成菜单,如果你是手动添加的规则菜单,权限规则必须细化到控制器的方法才可以。
我们都知道后台/admin是我们最常用的登录入口,方便的同时也留下了隐患,如果你刚好使用了admin/123456这种账号密码的方式,会导致我们的后台完全暴露在外。因此我们建议修改后台的登录入口,达到隐藏后台登录入口的效果。操作步骤1、首页修改application/config.php中deny_module_list
这是在edit方法里重写 staff是表名,username是字段名,$ids是当前数据的idtry{//是否采用模型验证if($this->modelValidate){$name=str_replace("\\model\\","\\validate\\",get_cla
FastAdmin自学笔记-目录FastAdmin开发第一天:了解FastAdmin框架(2019-09-09)FastAdmin开发第二天:安装环境FastAdmin开发第三天:FastAdmin开发第三天:安装FastAdmin
第一个图片在id前面加了模型名是因为第一个控制器//当前是否为关联查询$this->relationSearch=true; 
问题通过fieldlist动态添加的组件,是没法自动渲染的。关于内置组件的渲染fa官网也只对10个内置的组件,提供了重新渲染的方法。以下是:十个组件,两种方法。方法1:全部重新渲染Form.api.bindevent("form[role=form]")方法2:精确渲染并提供了10个组件精确渲染的方法,如下图如何
FastAdmin安装后点登录没有反应怎么办?很多小伙伴安装后点“登录”没有反应。这个URL是对的,但是页面就是不改变。如果这时候点击URL变了,那没有到登陆界面,一般是URL重写问题。一般如果是Apache服务器,FastAdmin是有默认的重写规则。一般这个问题出现在NGINX的服务
页面  视图内<divid="toolbar"class="toolbar"><ahref="javascript:;"class="btnbtn-primarybtn-refresh"title="{:__('Refresh')}"><i
server{listen80;server_namefastadmin.test*.fastadmin.test;root"C:/laragon/www/fastadmin/public/";indexindex.htmlindex.htmindex.php;location/{try_files$uri$uri//index.php$is_args$args;
在CMS插件中的前端视图模板中有大量使用了自定义标签,我们在修改或制作模板的时候可以方便快捷的使用自定义标签来调用我们相关的数据。标签库位于/addons/cmsaglib/Cms.php文件,我们可以看到标签库有定义可调用的标签和属性,如下protected$tags=[//标签定义:attr属性列
1、先把composer调成国内镜像:composerconfig-grepo.packagistcomposerhttps://packagist.phpcomposer.com不更换也行,但是很慢,建议更换 2、进项目根目录(不是public)composerrequireworkerman/gateway-worker 
学习thinkphp5的方法1.看文档2.看视频3.实际操作4.安装使用fastadmin研究代码今天第一步下载安装1.安装tp5,网上的composer下载安装,还需要切换国内镜像,一切从简。直接从官网,下载相关的包就行了。2.安装fastadmin,直接下载,不需要composer3.tp5文档地址:https://www.kancloud
使用下面,在检测当前的权限用户$adminIds=$this->getDataLimitAdminIds();halt($adminIds);使用下面,来检测信息的权限(获取查询的sql语句,会发现,admin_idinXXX)halt($this->model->getLastSql());一个配置方法,可能需要