Recommend.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace app\controller\admin\warehouse;
  3. use app\validate\admin\RecommendOperation;
  4. use crmeb\basic\BaseController;
  5. use think\exception\ValidateException;
  6. use think\facade\Db;
  7. class Recommend extends BaseController
  8. {
  9. public function lst(){
  10. [$page, $limit] = $this->getPage();
  11. $keyword=$this->request->params(['keyword']);
  12. $where = [];
  13. $query = Db::name('Recommend')
  14. -> alias('r')
  15. -> join('store_product sp','sp.product_id = r.product_id')
  16. -> when(isset($keyword['keyword']) && !empty($keyword['keyword']) ,function($query) use ($keyword){
  17. $query->where('name', 'like', "%" . $keyword['keyword'] . "%");
  18. $query->whereOr('sp.store_name', 'like', "%" . $keyword['keyword'] . "%");
  19. })
  20. -> where($where)
  21. -> field('r.id,sp.store_name,r.name,r.show_time,r.status');
  22. $count = $query ->count();
  23. $list = $query -> order('id desc') -> page($page,$limit) -> select();
  24. return app('json')->success(compact('count','list'));
  25. }
  26. public function operation(){
  27. $data = $this->request->param();
  28. try {
  29. validate(RecommendOperation::class)->check($data);
  30. }catch (ValidateException $exception){
  31. return app('json') -> fail($exception->getError());
  32. }
  33. $datas = $this -> request -> param(['id'=>0]);
  34. $id = $datas['id'];
  35. unset($datas['id']);
  36. if (count($data['images']) > 10) return app('json')->fail('最多10张图片');
  37. $data['images'] = json_encode($data['images'],JSON_UNESCAPED_UNICODE);
  38. if($id == 0){
  39. //添加
  40. $res = Db::name('Recommend') -> insert($data);
  41. if($res )return app('json')->success('添加成功');
  42. }else{
  43. //编辑
  44. $res = Db::name('Recommend') -> where('id',$id) -> update($data);
  45. if($res )return app('json')->success('修改成功');
  46. }
  47. return app('json')->fail('出错');
  48. }
  49. public function detail(){
  50. $data = $this -> request -> params(['id']);
  51. if(empty($data['id'])){
  52. return app('json')->fail('参数错误');
  53. }
  54. $detail = Db::name('Recommend')
  55. -> where('id',$data['id'])
  56. -> field('name,product_id,show_time,recommend_image,details,images,status')
  57. -> find();
  58. $detail['images'] = json_decode($detail['images'],true);
  59. return app('json')->success($detail);
  60. }
  61. }