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.

145 lines
4.3 KiB

7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Logic;
  3. use App\Common\ReturnData;
  4. use App\Http\Model\CollectGoods;
  5. use App\Http\Requests\CollectGoodsRequest;
  6. use Validator;
  7. class CollectGoodsLogic extends BaseLogic
  8. {
  9. public function __construct()
  10. {
  11. parent::__construct();
  12. }
  13. public function getModel()
  14. {
  15. return new CollectGoods();
  16. }
  17. public function getValidate($data, $scene_name)
  18. {
  19. //数据验证
  20. $validate = new CollectGoodsRequest();
  21. return Validator::make($data, $validate->getSceneRules($scene_name), $validate->getSceneRulesMessages());
  22. }
  23. //列表
  24. public function getList($where = array(), $order = '', $field = '*', $offset = '', $limit = '')
  25. {
  26. $res = $this->getModel()->getList($where, $order, $field, $offset, $limit);
  27. if($res['count'] > 0)
  28. {
  29. foreach($res['list'] as $k=>$v)
  30. {
  31. $res['list'][$k] = $this->getDataView($v);
  32. $goods = logic('Goods')->getOne(array('id'=>$v->goods_id));
  33. $res['list'][$k]->goods = $goods;
  34. }
  35. }
  36. return $res;
  37. }
  38. //分页html
  39. public function getPaginate($where = array(), $order = '', $field = '*', $limit = '')
  40. {
  41. $res = $this->getModel()->getPaginate($where, $order, $field, $limit);
  42. if($res->count() > 0)
  43. {
  44. foreach($res as $k=>$v)
  45. {
  46. $res[$k] = $this->getDataView($v);
  47. }
  48. }
  49. return $res;
  50. }
  51. //全部列表
  52. public function getAll($where = array(), $order = '', $field = '*', $limit = '')
  53. {
  54. $res = $this->getModel()->getAll($where, $order, $field, $limit);
  55. if($res)
  56. {
  57. foreach($res as $k=>$v)
  58. {
  59. $res[$k] = $this->getDataView($v);
  60. }
  61. }
  62. return $res;
  63. }
  64. //详情
  65. public function getOne($where = array(), $field = '*')
  66. {
  67. $res = $this->getModel()->getOne($where, $field);
  68. if(!$res){return false;}
  69. $res = $this->getDataView($res);
  70. return $res;
  71. }
  72. //添加
  73. public function add($data = array(), $type=0)
  74. {
  75. if(empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  76. $data['add_time'] = time();
  77. $validator = $this->getValidate($data, 'add');
  78. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  79. if($this->getModel()->getOne(array('user_id'=>$data['user_id'],'goods_id'=>$data['goods_id']))){return ReturnData::create(ReturnData::FAIL,null,'亲,您已经收藏啦!');}
  80. $res = $this->getModel()->add($data,$type);
  81. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  82. return ReturnData::create(ReturnData::FAIL);
  83. }
  84. //修改
  85. public function edit($data, $where = array())
  86. {
  87. if(empty($data)){return ReturnData::create(ReturnData::SUCCESS);}
  88. $validator = $this->getValidate($data, 'edit');
  89. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  90. $res = $this->getModel()->edit($data,$where);
  91. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  92. return ReturnData::create(ReturnData::FAIL);
  93. }
  94. //删除
  95. public function del($where)
  96. {
  97. if(empty($where)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  98. $validator = $this->getValidate($where,'del');
  99. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  100. if(!$this->getModel()->getOne(array('user_id'=>$where['user_id'],'goods_id'=>$where['goods_id']))){return '商品未收藏';}
  101. $res = $this->getModel()->del($where);
  102. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  103. return ReturnData::create(ReturnData::FAIL);
  104. }
  105. /**
  106. * 数据获取器
  107. * @param array $data 要转化的数据
  108. * @return array
  109. */
  110. private function getDataView($data = array())
  111. {
  112. return getDataAttr($this->getModel(),$data);
  113. }
  114. }