首頁
產品
文檔
正版
授權查詢
3.5折優惠
渠道合作
更多
關于我們
提交工單
聯系我們
AI編程
likeadmin
AI數字人
碼多多AI
演示中心
源碼下載
登錄/注冊
likeshop開發文檔
開發文檔
展開
? 必看說明
說明
? 部署上線
服務器域名準備工作
阿里云怎么安裝寶塔面板、域名解析、開放端口等①
騰訊云怎么安裝寶塔面板、域名解析、開放端口等②
華為云怎么安裝寶塔面板、域名解析、開放端口③
寶塔部署
服務端寶塔面板部署①
phpStudy部署安裝②
發布上線
準備工作
微信小程序如何發布上線①
微信公眾號商城發布上線②
安卓蘋果APP③
PC端SEO模式④
定時任務
寶塔定時任務配置①
linux定時任務配置②
在線客服
環境配置
系統設置
短信配置
騰訊短信配置
??? 數據接口
必看
接口文檔
?? 數據庫字典
數據庫字典
??? 二次開發
功能
后臺DIY組件二次開發
后臺菜單
目錄結構
likeshop單商戶高級版目錄結構
前端
管理后臺二開編譯上線
nuxt.js PC端如何并發布
uniapp H5(手機網頁)編譯后如何發布
服務端
接口說明
列表類使用
導出功能
問題合集
支付寶支付后,后臺訂單顯示未支付
顯示當前無法更新版本
進行授權后無法訪問
前端使用nvm切換node版本問題
列表類使用
### 一、列表答疑 **(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)參考例子: 文章列表類** ``` ['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();