You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.6 KiB

7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Model;
  3. use App\Common\Token;
  4. class CollectGoods extends BaseModel
  5. {
  6. //商品收藏
  7. protected $table = 'collect_goods';
  8. public $timestamps = false;
  9. /**
  10. * 不能被批量赋值的属性
  11. *
  12. * @var array
  13. */
  14. protected $guarded = [];
  15. //获取列表
  16. public static function getList(array $param)
  17. {
  18. extract($param); //参数:limit,offset
  19. $where['user_id'] = Token::$uid;
  20. $limit = isset($limit) ? $limit : 10;
  21. $offset = isset($offset) ? $offset : 0;
  22. $model = new CollectGoods;
  23. if(isset($type)){$where['type'] = $type;}
  24. $model = $model->where($where);
  25. $res['count'] = $model->count();
  26. $res['list'] = array();
  27. if($res['count']>0)
  28. {
  29. $res['list'] = $model->skip($offset)->take($limit)->orderBy('id','desc')->get();
  30. if($res['list'])
  31. {
  32. foreach($res['list'] as $k=>$v)
  33. {
  34. $goods = Goods::getOne(array('id'=>$v['goods_id'],'field'=>array('id', 'typeid', 'tuijian', 'click', 'title', 'sn', 'price','litpic', 'pubdate', 'add_time', 'market_price', 'goods_number', 'sale', 'comments','promote_start_date','promote_price','promote_end_date','goods_img','spec','point')));
  35. $res['list'][$k]['goods'] = $goods;
  36. }
  37. }
  38. }
  39. else
  40. {
  41. return false;
  42. }
  43. return $res;
  44. }
  45. public static function getOne(array $param)
  46. {
  47. extract($param); //参数
  48. $where['id'] = $id;
  49. return self::where($where)->first();
  50. }
  51. public static function add(array $data)
  52. {
  53. if(self::where(array('user_id'=>$data['user_id'],'goods_id'=>$data['goods_id']))->first()){return '亲,您已经收藏啦!';}
  54. if ($id = self::insertGetId($data))
  55. {
  56. return true;
  57. }
  58. return false;
  59. }
  60. public static function modify($where, array $data)
  61. {
  62. if (self::where($where)->update($data))
  63. {
  64. return true;
  65. }
  66. return false;
  67. }
  68. //删除一条记录
  69. public static function remove(array $data)
  70. {
  71. if(!self::where(array('user_id'=>$data['user_id'],'goods_id'=>$data['goods_id']))->first()){return '商品未收藏';}
  72. if (!self::where(array('user_id'=>$data['user_id'],'goods_id'=>$data['goods_id']))->delete())
  73. {
  74. return false;
  75. }
  76. return true;
  77. }
  78. }