UserShare.php 1.5 KB

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