UserFavorite.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace app\common\model;
  3. /**
  4. * 用户收藏模型
  5. * Class UserFavorite
  6. * @package app\common\model
  7. */
  8. class UserFavorite extends BaseModel
  9. {
  10. protected $name = 'user_favorites';
  11. /**
  12. * 获取收藏对应的多态模型。
  13. */
  14. public function target()
  15. {
  16. return $this->morphTo('target', [
  17. 1 => 'app\common\model\Goods', //商品
  18. ]);
  19. }
  20. //获取列表
  21. public function getList($userId, $target_type = 1, $exclude_target_id = [])
  22. {
  23. if (!empty($exclude_target_id)) {
  24. $this->where('target_id', 'not in', $exclude_target_id);
  25. }
  26. $list = $this->where('user_id', '=', $userId)
  27. ->where('target_type', '=', $target_type)->order(['id' => 'desc'])
  28. ->paginate();
  29. return $list;
  30. }
  31. public function getOne($user_id, $target_type, $target_id)
  32. {
  33. return $this->where('user_id', '=', $user_id)
  34. ->where('target_type', $target_type)
  35. ->where('target_id', $target_id)
  36. ->find();
  37. }
  38. public function addOne($user_id, $target_type, $target_id)
  39. {
  40. $data['user_id'] = $user_id;
  41. $data['target_type'] = $target_type;
  42. $data['target_id'] = $target_id;
  43. $this->save($data);
  44. }
  45. public function deleteOne($user_id, $target_type, $target_id)
  46. {
  47. $data['user_id'] = $user_id;
  48. $data['target_type'] = $target_type;
  49. $data['target_id'] = $target_id;
  50. $this->where($data)->delete();
  51. }
  52. public function getGoodCollect($user_id, $target_id, $target_type = 1)
  53. {
  54. if (empty($user_id)) {
  55. return false;
  56. }
  57. $one = $this->getOne($user_id, $target_type, $target_id);
  58. if (empty($one)) {
  59. return false;
  60. }
  61. return true;
  62. }
  63. }