diff --git a/app/Http/Controllers/GoodController.php b/app/Http/Controllers/GoodController.php index b1cfc57..83f0c55 100644 --- a/app/Http/Controllers/GoodController.php +++ b/app/Http/Controllers/GoodController.php @@ -34,75 +34,95 @@ class GoodController extends Controller */ public function getList(Request $request) { + $paginate_limit = 16; $data = []; $input = $request->all(); - $query = ""; + $query = ""; + $cat_id = $start_price = $end_price = $start_count = $sort = ""; + if(isset($input['cat_id'])) + $cat_id = $input['cat_id']; + if(isset($input['start_price'])) + $start_price = $input['start_price']; + if(isset($input['end_price'])) + $end_price = $input['end_price']; + if(isset($input['start_count'])) + $start_count = $input['start_count']; + if(isset($input['sort'])) + $sort = $input['sort']; if(isset($input['query'])) $query = $input['query']; $data['cats'] = GoodCat::orderby('cat_index', 'asc')->get(); - $data['goods'] = GoodInfo::where('good_name', 'like', "%$query%"); - $data['cat_id'] = 0; - if(isset($input['cat_id'])){ + $data['goods'] = GoodInfo::where('good_name', 'like', "%$query%"); + //$data['cat_id'] = 0; + if(isset($input['cat_id']) && $input['cat_id'] != ""){ $data['goods'] = GoodInfo::where('cat_id', $input['cat_id']); $data['cat_id'] = $input['cat_id']; } //同时给定价格上下限 - if(isset($input['start_price']) && isset($input['end_price'])){ + if(isset($input['start_price']) && isset($input['end_price']) + && $input['start_price'] != "" && $input['end_price'] != ""){ //确保上限大于下限 if($input['start_price'] > $input['end_price']){ - $temp = $input['start_price']; - $input['start_price'] = $input['end_price']; - $input['end_price'] = $temp; + $temp = $input['start_price']; + $input['start_price'] = $input['end_price']; + $input['end_price'] = $temp; } $data['goods'] = $data['goods']->where([ - ['price', '>=', $input['start_price']], - ['price', '<=', $input['end_price']] - ]); + ['price', '>=', $input['start_price']], + ['price', '<=', $input['end_price']] + ]); } //只设上限或下限时 else{ - if(isset($input['start_price'])) + if(isset($input['start_price']) && $input['start_price'] != "") $data['goods'] = $data['goods']->where('price', '>=', $input['start_price']); - if(isset($input['end_price'])) + if(isset($input['end_price']) && $input['end_price'] != "") $data['goods'] = $data['goods']->where('price', '<=', $input['end_price']); - } + } //对数量筛选只设下限 - if(isset($input['start_count'])) + if(isset($input['start_count']) && $input['start_count'] != "") $data['goods'] = $data['goods']->where('count', '>=', $input['start_count']); - else + else $data['goods'] = $data['goods']->where('count', '>', 0); // 最后在排序筛选baned==0以及paginate分页 // p为价格,c为数量,d为倒序,值相同时按id倒序排列 - if(isset($input['sort'])){ + if(isset($input['sort']) && $input['sort'] != ""){ if ($input['sort'] == 'old'){ - $data['goods'] = $data['goods']->orderby('id', 'asc')->where('baned', 0)->paginate(16); + $data['goods'] = $data['goods']->orderby('id', 'asc')->where('baned', 0)->paginate($paginate_limit); } elseif ($input['sort'] == 'p'){ - $data['goods'] = $data['goods']->orderby('price', 'asc')->orderby('id', 'desc')->where('baned', 0)->paginate(16); + $data['goods'] = $data['goods']->orderby('price', 'asc')->orderby('id', 'desc')->where('baned', 0)->paginate($paginate_limit); } elseif ($input['sort'] == 'pd') { - $data['goods'] = $data['goods']->orderby('price', 'desc')->orderby('id', 'desc')->where('baned', 0)->paginate(16); + $data['goods'] = $data['goods']->orderby('price', 'desc')->orderby('id', 'desc')->where('baned', 0)->paginate($paginate_limit); } elseif ($input['sort'] == 'c') { - $data['goods'] = $data['goods']->orderby('count', 'asc')->orderby('id', 'desc')->where('baned', 0)->paginate(16); + $data['goods'] = $data['goods']->orderby('count', 'asc')->orderby('id', 'desc')->where('baned', 0)->paginate($paginate_limit); } elseif ($input['sort'] == 'cd') { - $data['goods'] = $data['goods']->orderby('count', 'desc')->orderby('id', 'desc')->where('baned', 0)->paginate(16); + $data['goods'] = $data['goods']->orderby('count', 'desc')->orderby('id', 'desc')->where('baned', 0)->paginate($paginate_limit); } } //未指定排序规则时按id倒序排序 else - $data['goods'] = $data['goods']->orderby('id', 'desc')->where('baned', 0)->paginate(16); - if($request->session()->has('user_id')) - $data['user_id'] = $request->session()->get('user_id'); - else - $data['user_id'] = NULL; - if($request->session()->has('is_admin')) - $data['is_admin'] = $request->session()->get('is_admin'); - else - $data['is_admin'] = NULL; + $data['goods'] = $data['goods']->orderby('id', 'desc')->where('baned', 0)->paginate($paginate_limit); + if($request->session()->has('user_id')) + $data['user_id'] = $request->session()->get('user_id'); + else + $data['user_id'] = NULL; + if($request->session()->has('is_admin')) + $data['is_admin'] = $request->session()->get('is_admin'); + else + $data['is_admin'] = NULL; $data['page'] = 1; - if(isset($input['page'])) $data['page'] = $input['page']; + if(isset($input['page']) && $input['page'] != "") $data['page'] = $input['page']; + $data['cat_id'] = $cat_id; + $data['query'] = $query; + $data['start_price'] = $start_price; + $data['end_price'] = $end_price; + $data['start_count'] = $start_count; + $data['sort'] = $sort; + //dd($data['cat_id'] == ""); return view::make('good.goodList')->with($data); } diff --git a/public/js/good/good_list.js b/public/js/good/good_list.js index 9dce720..6b2371a 100644 --- a/public/js/good/good_list.js +++ b/public/js/good/good_list.js @@ -59,5 +59,16 @@ function setc(ha){ if($("#searchq").val()!=""){ hr=hr+"&query="+$("#searchq").val(); } + if(getUrlParam('cat_id')) + hr = hr + '&cat_id=' + getUrlParam('cat_id'); location.href=hr; +} + +function getUrlParam(name) +{ + var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); + var r = window.location.search.substr(1).match(reg); + if (r != null) + return r[2]; + return null; } \ No newline at end of file diff --git a/resources/views/good/goodList.blade.php b/resources/views/good/goodList.blade.php index 76ce66d..bb09b0c 100644 --- a/resources/views/good/goodList.blade.php +++ b/resources/views/good/goodList.blade.php @@ -76,7 +76,15 @@ - {{ $goods->links() }} + {{ $goods->appends([ + 'start_price' => $start_price, + 'end_price' => $end_price, + 'start_count' => $start_count, + 'sort' => $sort, + 'query' => $query, + 'cat_id' => $cat_id + ]) + ->links() }}