ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
<!-- 浏览方式:看云粘贴进去 --> [文档浏览链接](https://www.kancloud.cn/loqhcn/mulo_code/1352556) [TOC] # 概述 主要实现通过几 # 文档规则 @/ = 对应控制器/模块/ # 通用增删改查说明 ## 使用: 1,继承 \app\common\BaseCURD 2,在初始化前置操作中 ```php use app\web\model\NewsModel; public function initialize() { $this->model = new NewsModel(); parent::initialize(); } ``` ## 继承属性 ```php //当前操作模型 由子类实例化 protected $model = null; //页面大小 protected $psize = 10; //搜索列 protected $searchRow = "title"; //排序字段 protected $sortRow = "id"; //排序方式 protected $sortType = 'desc'; //操作后跳转链接 protected $addGoUrl = 'index'; //保存数据后跳转到方法 protected $updateGoUrl = 'index'; ``` ## 继承方法 # 列表页 url: /controller/module/index veiw: @/index.html ## 创建一个列表页 > 创建对应控制器的index.html模板文件 , 这是一个 ```php <extend name="default@index_base" /> <block name="rowtitle"> <th>ID</th> <th>名称</th> <th>创建时间</th> <th>状态</th> <th width="180">{:lang('ACTIONS')}</th> </block> <block name="row"> <tr id='' style=''> <td>{$vo.id}</td> <td>{$vo.name}</td> <td>{:date('Y-m-d H:i:s',$vo['create_time'])}</td> <td> <switch name="$vo['state']"> <case value="1">正常</case> <case value="2">失效</case> </switch> </td> <td> <a class="btn btn-xs btn-primary" href="{:url('edit',array('id'=>$vo['id']))}">{:lang('EDIT')}</a> <a href="{:url('del',array('id'=>$vo['id']))}" class="btn btn-xs btn-danger js-ajax-delete">{:lang('DELETE')}</a> </td> </tr> </block> ``` ## 搜索和筛选 - 通过配置实现列表页搜索筛选功能 ```php //搜索和筛选配置 protected $indexToolConfig = [ //false:关闭, String:搜索框文字 'search' => true, // 时间筛选,时间筛选字段 'time'=>true, 'time_field'=>'get_time', //状态筛选配置 'selects' => [ ['type' => 'select', 'name' => 'state', 'title' => '广告状态', 'values' => ['-1' => '不限', '1' => '正常', '2' => '禁用']], ], ]; //搜索的列 逗号分隔 protected $searchRow = "title"; ``` ## 钩子函数 - 控制器内继承对应的函数实现那些功能 > index页面渲染前 用于数据处理 ```php protected function beforeIndexRender(&$list){ } ``` > 自定义列表数据 ```php /** 自定义列表数据查询 * @param [type] $wheres * @return Model::paginate() */ function indexListData($wheres) { //关联用户查询示例 * 用了别名后,搜索和筛选需要单独处理 return $this->model->alias('o')->join(['zoo_member' => 'm']) ->field('o.*,m.mobile') ->order("o.order_id {$this->sortType}") ->paginate($this->psize); } ``` ## 导出excel ```php protected function export($data) { foreach ($data as $key => &$li) { $li['get_time'] = date('Y-m-d H:i:s',$li['get_time']); $li['use_time'] = $li['use_time']?date('Y-m-d H:i:s',$li['use_time']) : '-'; } unset($li); $exportTime = date('Y年m月d日 H:i:s'); return $this->exportExcel_1("{$exportTime}",[ 'name'=>'名称', 'get_time'=>'获得时间', 'use_time'=>'使用时间', 'mobile'=>'使用者手机号' ],$data); } ``` # 编辑 or 添加 添加 /controller/module/post 修改 /controller/module/post?id=x 对应模块目录/post.html *编辑和修改 ##模板页面 ## 钩子函数 - 继承下列函数实现对数据操作 ```php ``` ## 添加和编辑的模板分离 ## 图片上传 ```html <tr> <th>展示图片:</th> <td class="col-sm-8"> <php> $include_uploadimg_param = [ 'name' => 'ad_pic', 'img_name' => isset($row['ad_pic']) && $row['ad_pic']? $row['ad_pic']:'', 'img' => isset($row['ad_pic']) && $row['ad_pic']? cmf_get_image_preview_url( $row['ad_pic'] ):'' ]; </php> <include file="web@uploadimg" /> <span id="err_attr_name" style="color:#F00; ">图片不能超过1M</span> </td> </tr> ``` ## radios ```html <tr> <th>状态:</th> <td> <php> $include_radios_param = [ 'list'=>[ ['name'=>'正常','val'=>'1'], ['name'=>'隐藏','val'=>'2'] ], 'name'=>'state', 'checked'=>$id?@$row['state']:2, ]; </php> <include file='web@public/radios' /> </td> </tr> ``` # 删除 url: @/del 或者 @/delete ## 自定义删除 ```php //继承删除操作函数 protected function onDelete($id) { return $this->model->where('表主键', $id)->update([ 'isdel' => 1 ]); } ``` # 常用复制 ## 页面控制器 ```php <?php namespace app\web\controller; use cmf\controller\AdminBaseController; use app\web\WebBaseCURD; use app\web\model\StoreStyleModel; class StoreStyleController extends WebBaseCURD { //搜索列 protected $searchRow = "name"; //排序字段 protected $sortRow = "sorting"; //模块名称 protected $moduleName = '门店风格'; public function initialize() { $this->model = new StoreStyleModel(); parent::initialize(); } /** * 添加或者更新的时候 输入数据 * */ function validateRule() { $rule = [ 'name|风格名称' => 'require|max:60', 'pic|图片' => 'require|max:255', 'store_style|门店风格' => 'require', 'state|显示状态' => 'require', ]; return $rule; } } ```