Favorite.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace app\api\controller\user;
  3. use app\api\controller\Controller;
  4. use app\api\model\user\Favorite as FavoriteModel;
  5. /**
  6. * 我的收藏商品和关注店铺
  7. */
  8. class Favorite extends Controller
  9. {
  10. protected $user;
  11. /**
  12. * 构造方法
  13. */
  14. public function initialize()
  15. {
  16. parent::initialize();
  17. $this->user = $this->getUser();
  18. }
  19. /*
  20. *收藏
  21. */
  22. public function add(){
  23. $data = $this->request->param();
  24. $model = FavoriteModel::detail($data['pid'], $data['type'], $this->user['user_id']);
  25. if($model && $model->cancelFav()){
  26. return $this->renderSuccess('操作成功');
  27. }else{
  28. $data['user_id'] = $this->user['user_id'];
  29. $model = new FavoriteModel();
  30. if($model->fav($data)){
  31. return $this->renderSuccess('操作成功');
  32. }
  33. }
  34. return $this->renderSuccess($model->getError()?:'操作失败');
  35. }
  36. //我的收藏/关注
  37. public function list(){
  38. $Favorite = new FavoriteModel;
  39. $where['user_id'] = $this->user['user_id'];
  40. $where['type'] = $this->request->param('type');
  41. $list = $Favorite->getList($where,$this->postData());
  42. return $this->renderSuccess('',compact('list'));
  43. }
  44. }