getPage(); $keyword=$this->request->params(['keyword']); $where = []; $query = Db::name('Recommend') -> alias('r') -> join('store_product sp','sp.product_id = r.product_id') -> when(isset($keyword['keyword']) && !empty($keyword['keyword']) ,function($query) use ($keyword){ $query->where('name', 'like', "%" . $keyword['keyword'] . "%"); $query->whereOr('sp.store_name', 'like', "%" . $keyword['keyword'] . "%"); }) -> where($where) -> field('r.id,sp.store_name,r.name,r.show_time,r.status'); $count = $query ->count(); $list = $query -> order('id desc') -> page($page,$limit) -> select(); return app('json')->success(compact('count','list')); } public function operation(){ $data = $this->request->param(); try { validate(RecommendOperation::class)->check($data); }catch (ValidateException $exception){ return app('json') -> fail($exception->getError()); } $datas = $this -> request -> param(['id'=>0]); $id = $datas['id']; unset($datas['id']); if (count($data['images']) > 10) return app('json')->fail('最多10张图片'); $data['images'] = json_encode($data['images'],JSON_UNESCAPED_UNICODE); if($id == 0){ //添加 $res = Db::name('Recommend') -> insert($data); if($res )return app('json')->success('添加成功'); }else{ //编辑 $res = Db::name('Recommend') -> where('id',$id) -> update($data); if($res )return app('json')->success('修改成功'); } return app('json')->fail('出错'); } public function detail(){ $data = $this -> request -> params(['id']); if(empty($data['id'])){ return app('json')->fail('参数错误'); } $detail = Db::name('Recommend') -> where('id',$data['id']) -> field('name,product_id,show_time,recommend_image,details,images,status') -> find(); $detail['images'] = json_decode($detail['images'],true); return app('json')->success($detail); } }