setWhere(); $lists = User::field('id,avatar,nickname,mobile, create_time, id as fans, id as order_amount,id as order_num') ->where($where) ->order('id', 'desc') ->select() ->toArray(); // 自定义排序 $lists = $this->customizeOrder($lists); // 取指定页数据 return array_slice($lists, $this->limitOffset, $this->limitLength); } /** * @notes 粉丝数量 * @return int * @author Tab * @date 2021/8/5 18:45 */ public function count(): int { // 设置搜索 $where = $this->setWhere(); $count = User::field('avatar,nickname,mobile, create_time, id as fans, id as order_amount,id as order_num') ->where($where) ->count(); return $count; } /** * @notes 设置搜索 * @return array * @author Tab * @date 2021/8/5 18:29 */ public function setWhere() { // 粉丝类型 $type = $this->params['type'] ?? 'all'; switch ($type) { // 一级 case 'first': $where[] = ['first_leader', '=', $this->userId]; break; // 二级 case 'second': $where[] = ['second_leader', '=', $this->userId]; break; // 默认 default: $where[] = ['first_leader|second_leader', '=', $this->userId]; } // 关键字搜索 if (isset($this->params['keyword']) && !empty($this->params['keyword'])) { $where[] = ['nickname|mobile', 'like', '%' . $this->params['keyword'] .'%']; } return $where; } /** * @notes 自定义排序 * @return array * @author Tab * @date 2021/8/5 18:36 */ public function customizeOrder($lists) { foreach($lists as $key => $row) { $fans[$key] = $row['fans']; $orderAmount[$key] = $row['order_amount']; $orderNum[$key] = $row['order_num']; } // 排序 SORT_DESC = 3 倒序 , SORT_ASC = 4 升序 // 粉丝数 if(isset($this->params['fans']) && isset($fans)) { array_multisort($fans, strtolower($this->params['fans']) == 'asc' ? SORT_ASC : SORT_DESC, $lists); } // 已支付订单总金额 if(isset($this->params['order_amount']) && isset($fans)) { array_multisort($orderAmount, strtolower($this->params['order_amount']) == 'asc' ? SORT_ASC : SORT_DESC, $lists); } // 已支付订单数量 if(isset($this->params['order_num']) && isset($fans)) { array_multisort($orderNum, strtolower($this->params['order_num']) == 'asc' ? SORT_ASC : SORT_DESC, $lists); } return $lists; } }