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.

149 lines
5.3 KiB

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
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
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\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. return $res;
  45. }
  46. //全部列表
  47. public function getAll($where = array(), $order = '', $field = '*', $limit = '')
  48. {
  49. $res = $this->getModel()->getAll($where, $order, $field, $limit);
  50. if($res['count'] > 0)
  51. {
  52. foreach($res['list'] as $k=>$v)
  53. {
  54. $res['list'][$k] = $this->getDataView($v);
  55. $res['list'][$k]->price = $this->getModel()->get_goods_final_price($v); //商品最终价格
  56. $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,说明不是促销商品
  57. $res['list'][$k]->goods_detail_url = route('weixin_goods_detail',array('id'=>$v->id));
  58. }
  59. }
  60. return $res;
  61. }
  62. //详情
  63. public function getOne($where = array(), $field = '*')
  64. {
  65. $res = $this->getModel()->getOne($where, $field);
  66. if(!$res){return false;}
  67. $res = $this->getDataView($res);
  68. $res->price = $this->getModel()->get_goods_final_price($res); //商品最终价格
  69. $res->is_promote_goods = $this->getModel()->bargain_price($res->promote_price,$res->promote_start_date,$res->promote_end_date); //is_promote_goods等于0,说明不是促销商品
  70. $res->goods_detail_url = route('weixin_goods_detail',array('id'=>$res->id));
  71. $res->goods_img_list = model('GoodsImg')->getDb()->where(['goods_id'=>$res->id])->get();
  72. //商品评论数
  73. $where2['comment_type'] = Comment::GOODS_COMMENT_TYPE;
  74. $where2['status'] = Comment::SHOW_COMMENT;
  75. $where2['id_value'] = $res->id;
  76. $res->goods_comments_num = model('Comment')->getCount($where2);
  77. $this->getModel()->getDb()->where($where)->increment('click', 1); //点击量+1
  78. return $res;
  79. }
  80. //添加
  81. public function add($data = array(), $type=0)
  82. {
  83. if(empty($data)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  84. $validator = $this->getValidate($data, 'add');
  85. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  86. $res = $this->getModel()->add($data,$type);
  87. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  88. return ReturnData::create(ReturnData::FAIL);
  89. }
  90. //修改
  91. public function edit($data, $where = array())
  92. {
  93. if(empty($data)){return ReturnData::create(ReturnData::SUCCESS);}
  94. $validator = $this->getValidate($data, 'edit');
  95. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  96. $res = $this->getModel()->edit($data,$where);
  97. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  98. return ReturnData::create(ReturnData::FAIL);
  99. }
  100. //删除
  101. public function del($where)
  102. {
  103. if(empty($where)){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  104. $validator = $this->getValidate($where,'del');
  105. if ($validator->fails()){return ReturnData::create(ReturnData::PARAMS_ERROR, null, $validator->errors()->first());}
  106. $res = $this->getModel()->del($where);
  107. if($res){return ReturnData::create(ReturnData::SUCCESS,$res);}
  108. return ReturnData::create(ReturnData::FAIL);
  109. }
  110. /**
  111. * 数据获取器
  112. * @param array $data 要转化的数据
  113. * @return array
  114. */
  115. private function getDataView($data = array())
  116. {
  117. return getDataAttr($this->getModel(),$data);
  118. }
  119. }