直接上代码,大家可以参考下
public function collectWorkList() { $user_id = input('param.user_id'); //当前页码,默认为第一页 $pageNum = empty(input('param.pageNum')) ? 1 : input('param.pageNum'); //每页数量 $numPerPage = 8; //测试数据 // $user_id = 1; // $pageNum = 1; // $numPerPage = 1; if(empty($user_id)){ $result['code'] = 2; $result['msg'] = '暂无用户信息!'; return json($result); } //起始数据 $index = ($pageNum-1)*$numPerPage; $where['a.user_id'] = $user_id; $where['a.is_cancel'] = 0; $now = Db::table('user_collect a') ->join('work b','a.work_id=b.id','LEFT') ->where($where) ->order(['b.is_urgent'=>'desc','b.addtime'=>'desc']) ->field('a.id,a.work_id,a.addtime,b.title,b.detailaddress,b.work_time,b.is_urgent,b.cost_type,b.cost,b.addtime baddtime') ->limit($index,$numPerPage) ->select(); $total = Db::table('user_collect a') ->join('work b','a.work_id=b.id','LEFT') ->where($where) ->order(['b.is_urgent'=>'desc','b.addtime'=>'desc']) ->count('a.id'); $nowTotal = count($now); if($nowTotal>0){ foreach ($now as $k=>$v){ $danwei = $v['cost_type'] == 0 ? '小时' : ($v['cost_type'] == 1 ? '其他' : '月'); $now[$k]['newPrice'] = "{$v['cost']}元/{$danwei}"; } } $result['total'] = $total; $alreadyLoad = ($pageNum-1)*$numPerPage + $nowTotal; if($nowTotal>0){ if($total-$alreadyLoad<=0){ $result['havenums'] = 0; }else{ $result['havenums'] = $total-$alreadyLoad; } $result['collectList'] = $now; $result['code'] = 1; }else{ $result['collectList'] = []; $result['code'] = 2; } return json($result); }