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.

137 lines
4.1 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 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. return $res;
  43. }
  44. //全部列表
  45. public function getAll($where = array(), $order = '', $field = '*', $limit = '')
  46. {
  47. $res = $this->getModel()->getAll($where, $order, $field, $limit);
  48. /* if($res)
  49. {
  50. foreach($res as $k=>$v)
  51. {
  52. $res[$k] = $this->getDataView($v);
  53. }
  54. } */
  55. return $res;
  56. }
  57. //详情
  58. public function getOne($where = array(), $field = '*')
  59. {
  60. $res = $this->getModel()->getOne($where, $field);
  61. if(!$res){return false;}
  62. $res = $this->getDataView($res);
  63. return $res;
  64. }
  65. //添加
  66. public function add($data = array(), $type=0)
  67. {
  68. if(empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  69. $data['add_time'] = time();
  70. $validator = $this->getValidate($data, 'add');
  71. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  72. if($this->getModel()->getOne(array('user_id'=>$data['user_id'],'goods_id'=>$data['goods_id']))){return '亲,您已经收藏啦!';}
  73. $res = $this->getModel()->add($data,$type);
  74. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  75. return ReturnData::create(ReturnData::FAIL);
  76. }
  77. //修改
  78. public function edit($data, $where = array())
  79. {
  80. if(empty($data)){return ReturnData::create(ReturnData::SUCCESS);}
  81. $validator = $this->getValidate($data, 'edit');
  82. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  83. $res = $this->getModel()->edit($data,$where);
  84. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  85. return ReturnData::create(ReturnData::FAIL);
  86. }
  87. //删除
  88. public function del($where)
  89. {
  90. if(empty($where)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  91. $validator = $this->getValidate($where,'del');
  92. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  93. if(!$this->getModel()->getOne(array('user_id'=>$where['user_id'],'goods_id'=>$where['goods_id']))){return '商品未收藏';}
  94. $res = $this->getModel()->del($where);
  95. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  96. return ReturnData::create(ReturnData::FAIL);
  97. }
  98. /**
  99. * 数据获取器
  100. * @param array $data 要转化的数据
  101. * @return array
  102. */
  103. private function getDataView($data = array())
  104. {
  105. return getDataAttr($this->getModel(),$data);
  106. }
  107. }