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.

159 lines
5.6 KiB

7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
6 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
6 years ago
6 years ago
7 years ago
6 years ago
7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
7 years ago
6 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Logic;
  3. use App\Common\ReturnData;
  4. use App\Http\Model\Goods;
  5. use App\Http\Requests\GoodsRequest;
  6. use Validator;
  7. use App\Http\Model\Comment;
  8. class GoodsLogic extends BaseLogic
  9. {
  10. public function __construct()
  11. {
  12. parent::__construct();
  13. }
  14. public function getModel()
  15. {
  16. return model('Goods');
  17. }
  18. public function getValidate($data, $scene_name)
  19. {
  20. //数据验证
  21. $validate = new GoodsRequest();
  22. return Validator::make($data, $validate->getSceneRules($scene_name), $validate->getSceneRulesMessages());
  23. }
  24. //列表
  25. public function getList($where = array(), $order = '', $field = '*', $offset = '', $limit = '')
  26. {
  27. $res = $this->getModel()->getList($where, $order, $field, $offset, $limit);
  28. if($res['count'] > 0)
  29. {
  30. foreach($res['list'] as $k=>$v)
  31. {
  32. $res['list'][$k] = $this->getDataView($v);
  33. $res['list'][$k]->price = $this->getModel()->get_goods_final_price($v);
  34. $res['list'][$k]->is_promote_goods = $this->getModel()->bargain_price($v->promote_price,$v->promote_start_date,$v->promote_end_date); //is_promote_goods等于0,说明不是促销商品
  35. $res['list'][$k]->goods_detail_url = route('weixin_goods_detail',array('id'=>$v->id));
  36. }
  37. }
  38. return $res;
  39. }
  40. //分页html
  41. public function getPaginate($where = array(), $order = '', $field = '*', $limit = '')
  42. {
  43. $res = $this->getModel()->getPaginate($where, $order, $field, $limit);
  44. if($res->count() > 0)
  45. {
  46. foreach($res as $k=>$v)
  47. {
  48. $res[$k] = $this->getDataView($v);
  49. }
  50. }
  51. return $res;
  52. }
  53. //全部列表
  54. public function getAll($where = array(), $order = '', $field = '*', $limit = '')
  55. {
  56. $res = $this->getModel()->getAll($where, $order, $field, $limit);
  57. if($res)
  58. {
  59. foreach($res as $k=>$v)
  60. {
  61. $res[$k] = $this->getDataView($v);
  62. $res[$k]->price = $this->getModel()->get_goods_final_price($v); //商品最终价格
  63. $res[$k]->is_promote_goods = $this->getModel()->bargain_price($v->promote_price,$v->promote_start_date,$v->promote_end_date); //is_promote_goods等于0,说明不是促销商品
  64. $res[$k]->goods_detail_url = route('weixin_goods_detail',array('id'=>$v->id));
  65. }
  66. }
  67. return $res;
  68. }
  69. //详情
  70. public function getOne($where = array(), $field = '*')
  71. {
  72. $res = $this->getModel()->getOne($where, $field);
  73. if(!$res){return false;}
  74. $res = $this->getDataView($res);
  75. $res->price = $this->getModel()->get_goods_final_price($res); //商品最终价格
  76. $res->is_promote_goods = $this->getModel()->bargain_price($res->promote_price,$res->promote_start_date,$res->promote_end_date); //is_promote_goods等于0,说明不是促销商品
  77. $res->goods_detail_url = route('weixin_goods_detail',array('id'=>$res->id));
  78. $res->goods_img_list = model('GoodsImg')->getDb()->where(['goods_id'=>$res->id])->get();
  79. $res->type_name = model('GoodsType')->getDb()->where(['id'=>$res->typeid])->value('name');
  80. //商品评论数
  81. $where2['comment_type'] = Comment::GOODS_COMMENT_TYPE;
  82. $where2['status'] = Comment::SHOW_COMMENT;
  83. $where2['id_value'] = $res->id;
  84. $res->goods_comments_num = model('Comment')->getCount($where2);
  85. $this->getModel()->getDb()->where($where)->increment('click', 1); //点击量+1
  86. return $res;
  87. }
  88. //添加
  89. public function add($data = array(), $type=0)
  90. {
  91. if(empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  92. $validator = $this->getValidate($data, 'add');
  93. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  94. $data['add_time'] = $data['pubdate'] = time();//添加、更新时间
  95. $res = $this->getModel()->add($data,$type);
  96. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  97. return ReturnData::create(ReturnData::FAIL);
  98. }
  99. //修改
  100. public function edit($data, $where = array())
  101. {
  102. if(empty($data)){return ReturnData::create(ReturnData::SUCCESS);}
  103. $validator = $this->getValidate($data, 'edit');
  104. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  105. $res = $this->getModel()->edit($data,$where);
  106. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  107. return ReturnData::create(ReturnData::FAIL);
  108. }
  109. //删除
  110. public function del($where)
  111. {
  112. if(empty($where)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  113. $validator = $this->getValidate($where,'del');
  114. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  115. $res = $this->getModel()->del($where);
  116. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  117. return ReturnData::create(ReturnData::FAIL);
  118. }
  119. /**
  120. * 数据获取器
  121. * @param array $data 要转化的数据
  122. * @return array
  123. */
  124. private function getDataView($data = array())
  125. {
  126. return getDataAttr($this->getModel(),$data);
  127. }
  128. }