Collect.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\model\Collect as CollectModel;
  4. use app\common\model\Goods;
  5. /**
  6. * 收藏
  7. */
  8. class Collect extends Base
  9. {
  10. protected $noNeedLogin = [];
  11. /**
  12. * 添加/取消收藏
  13. */
  14. public function optionCollect()
  15. {
  16. $goods_id = $this->request->post('goods_id');
  17. if (!$this->request->isPost()) {
  18. $this->error('请求错误');
  19. }
  20. if (!$goods_id) {
  21. $this->error('缺少参数!');
  22. }
  23. $data = (new Goods())->get($goods_id);
  24. if (!$data || $data['status'] != 'normal') {
  25. $this->error('收藏的商品已失效!');
  26. }
  27. $status = CollectModel::addOrCancel($this->auth->id, $goods_id);
  28. if ($status) {
  29. $this->success('操作成功');
  30. }
  31. $this->error('操作失败');
  32. }
  33. /**
  34. * 我的收藏
  35. */
  36. public function collectList()
  37. {
  38. $param = $this->request->param();
  39. $param['user_id'] = $this->auth->id;
  40. $list = CollectModel::tableList($param);
  41. $this->success('', $list);
  42. }
  43. }