多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
### 一、列表答疑 **(1)为什么控制器继承了应用基础控制器后我们可以直接使用$this->dataList() ?** 因为应用基础控制器继承了BaseLikeShopController,而BaseLikeShopController中存在dataLists方法,所以只要我们自己的控制器继承了应用基础控制器就可以直接调用该方法。 **(2)调用了$this->dataList()方法就能马上得到数据吗?还要做什么?** 不会马上得到数据,还要创建相应的列表类。原因可从dataLists()这个方法的实现中可以看出: 调用方法不传参数,需创建以当前控制器名+Lists的列表类 调用方法传了指定列表类实例,实现指定列表类即可 列表类通常存放于当前应用的 lists目录中 **(3)列表请求方式必须是GET吗**?是的! (4) 特别注意: 分页改为用limit()方法,不要再使用page()** 原因:底层导出功能设计需要limit()支持,而page()无法满足需求 前端传递分页参数不变,仍然是 page_no/page_size, 底层会自动获取前端分页参数并计算出$this->limitOffset, $this->limitLength的值 ``` limit($this->limitOffset, $this->limitLength) ``` **(5)参考例子: 文章列表类** ``` <?php namespace app\adminapi\lists; use app\common\lists\ListsSearchInterface; use app\common\model\Article; class ArticleLists extends BaseAdminDataLists implements ListsSearchInterface { /** * @notes 设置搜索 * @return \string[][] * @author Tab * @date 2021/7/14 9:48 */ public function setSearch() : array { return [ '=' => ['type', 'cid', 'is_notice'], '%like%' => ['title'] ]; } /** * @notes 文章/帮助列表 * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * @author Tab * @date 2021/7/14 9:48 */ public function lists() : array { $lists = Article::field('id,title,image,cid,is_notice,is_show,visit,likes,sort,create_time') ->where($this->searchWhere) ->limit($this->limitOffset, $this->limitLength) ->select() ->toArray(); return $lists; } /** * @notes 文章/帮助总记录数 * @return int * @author Tab * @date 2021/7/14 9:48 */ public function count() : int { return Article::where($this->searchWhere)->count(); } } ``` ### 二、搜索接口使用 **(1) 须实现`ListsSearchInterface`接口,该接口包含一个需要实现的`setSearch()`**,该方法用于设置搜索条件 ``` public function setSearch() : array { return [ '=' => ['type', 'cid'], '%like%' => ['title'] ]; } ``` **(2) 组装后的搜索条件通过!!#ff0000 $this->searchWhere!!属性接收** 具体组装逻辑可参考:app/common/lists/ListsSearchTrait.php 多个条件关系是: and关系 例 : 前端传过来的值 type = 1, cid = 10 后端:['=' =>[ 'type', 'cid']] 组装后生成 $this->searchWhere = [ ['type', '=', '1'], ['cid', '=', '10'] ] 后端:['<>' => [ 'type', 'cid']] 组装后生成 $this->searchWhere = [ ['type', '<>', '1'], ['cid', '<>', '10'] ] 后端:['>' => [ 'type', 'cid']] 组装后生成 $this->searchWhere = [ ['type', '>', '1'], ['cid', '>', '10'] ] 后端:['>=' => [ 'type', 'cid']] 组装后生成 $this->searchWhere = [ ['type', '>=', '1'], ['cid', '>=', '10'] ] 后端:['<' => [ 'type', 'cid']] 组装后生成 $this->searchWhere = [ ['type', '<', '1'], ['cid', '<', '10'] ] 后端:['<=' => [ 'type', 'cid']] 组装后生成 $this->searchWhere = [ ['type', '<=', '1'], ['cid', '<=', '10'] ] 后端:['in' => [ 'type', 'cid']] 组装后生成 $this->searchWhere = [ ['type', 'in', '1'], ['cid', 'in', '10'] ] 例 : 前端传过来的值 name = 好象 后端:['%like%' => ['name']] 组装后生成 $this->searchWhere = [ 'name', 'like', '%好象%' ] 后端:['%like' => ['name']] 组装后生成 $this->searchWhere = [ 'name', 'like', '%好象' ] 后端:['like%' => ['name']] 组装后生成 $this->searchWhere = [ 'name', 'like', '好象%' ] 例 : 前端传过来的值 start_time = 2021-07-01 end_time = 2021-07-31 后端:['between_time' => 'create_time'] 组装后生成 $this->searchWhere = [ 'create_time', 'between', [66666666,88888888]] 注:66666666对应的是2021-07-01的时间戳,88888888对应的是2021-07-01的时间戳 例 : 前端传过来的值 start = 10 end = 100 后端:['between' => 'sort'] 组装后生成 $this->searchWhere = [ 'sort', 'between', [10,100]] **例:支持别名** 后端:['=' => [ 'a.type', 'a.cid']] 组装后生成 $this->searchWhere = [ ['a.type', '=', '1'], ['a.cid', '=', '10'] ] **(3) 使用参考 ** Admin::where(!!#ff0000 $this->searchWhere!!)->select(); **(4)、更复杂的条件** 若setSearch()方法中无法实现的搜索条件,可不实现搜索接口,在lists()及count()方法中自行定义自己需要的搜索条件即可 ``` public function lists() : array { // $where[] = [xxx,xxx,xx]; 这里叠加自已搜索条件 $lists = Footprint::where($where)->select()->toArray(); return $lists; } public function count() : int { // $where[] = [xxx,xxx,xx]; 这里叠加自已搜索条件 return Footprint::where( $where)->count(); } ``` ### 三、排序接口使用 **(1) 列表须实现`ListsSortInterface`接口,该接口中包含两个需要实现的方法`setSortFields()`、`setDefaultOrder()`** `setSortFields()` 设置允许的排序字段,例: ``` public function setSortFields(): array { // 格式: ['前端传过来的字段名' => '数据库中的字段名']; // 前端传过来create_time,后端会根据create_time排序 // 前端传过来id,但后端会根据user_id排序 return ['create_time' => 'create_time','id'=>'user_id']; } ``` `setDefaultOrder()` 设置默认排序规则 什么情况下会使用默认排序: 1.1、前端未传排序字段 field时 1.2、前端未传排序规则 order_by时 1.3、后端setSortFileds()方法返回 空数组时 1.4、前端传过来的排序字段不在允许的排序字段中时,例:前端传过来排序字段money, 但允许的排序字段数组 ['create_time' => 'create_time'] 中并没有money这个字段 1.5、允许排序字段格式设置不正确时,例: ['create_time' => 'create_time'] 设置成了 ['create_time'] ``` public function setDefaultOrder(): array { return ['id' => 'desc']; } ``` **(2) 前端必须传的两个参数** order_by排序规则(desc-倒序 asc-升序) ,field排序字段 **(3) 后端组装后的排序规则通过 $this->sortOrder属性接收** 具本的排序组装逻辑请参考: app/common/lists/ListsSortTrait.php 例:前端传 { order_by: desc, field: create_time } 后端:$this->sortOrder = [‘create_time’ => ‘desc’]; 前端传 { order_by: desc, field: id } 后端:$this->sortOrder = [‘user_id’ => ‘desc’]; 前端不传排序相关参数 或 符合设置默认排序中说明的情况时 后端:$this->sortOrder = [‘id’ => ‘desc’]; **(4) 使用参考 ** Admin::order(!!#ff0000 $this->sortOrder!!)->select();