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.

174 lines
5.4 KiB

7 years ago
4 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 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
6 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
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
6 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use Illuminate\Support\Facades\Log;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Http\Request;
  6. use App\Common\ReturnData;
  7. use App\Common\Helper;
  8. use App\Common\Token;
  9. use App\Http\Model\Goods;
  10. use App\Http\Logic\GoodsLogic;
  11. use App\Http\Logic\GoodsSearchwordLogic;
  12. class GoodsController extends BaseController
  13. {
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. }
  18. public function getLogic()
  19. {
  20. return logic('Goods');
  21. }
  22. public function goodsList(Request $request)
  23. {
  24. //参数
  25. $limit = $request->input('limit', 10);
  26. $offset = $request->input('offset', 0);
  27. $where = function ($query) use ($request) {
  28. $query->where('status', Goods::GOODS_NORMAL_STATUS);
  29. if($request->input('typeid', null) != null && $request->input('typeid', '') != 0)
  30. {
  31. $query->where('typeid', $request->input('typeid'));
  32. }
  33. if($request->input('tuijian', null) != null)
  34. {
  35. $query->where('tuijian', $request->input('tuijian'));
  36. }
  37. if($request->input('keyword', null) != null)
  38. {
  39. $query->where(function ($query2) use ($request) {$query2->where('title', 'like', '%'.$request->input('keyword').'%')->orWhere('sn', 'like', '%'.$request->input('keyword').'%');});
  40. }
  41. //价格区间搜索
  42. if($request->input('min_price', null) != null && $request->input('max_price', null) != null)
  43. {
  44. $query->where('price', '>=', $request->input('min_price'))->where("price", "<=", $request->input('max_price'));
  45. }
  46. if($request->input('brand_id', null) != null)
  47. {
  48. $query->where('brand_id', $request->input('brand_id'));
  49. }
  50. //促销商品
  51. if($request->input('is_promote', 0) == 1)
  52. {
  53. $timestamp = time();
  54. $query->where("promote_start_date", "<=", $timestamp)->where('promote_end_date', '>=', $timestamp);
  55. }
  56. };
  57. //var_dump(model('Goods')->where($where)->toSql());exit;
  58. //关键词搜索
  59. if($request->input('keyword', null) != null)
  60. {
  61. //添加搜索关键词
  62. $goodssearchword = new GoodsSearchwordLogic();
  63. $goodssearchword->add(array('name'=>$request->input('keyword')));
  64. }
  65. //排序
  66. $orderby = ['id','desc'];
  67. if($request->input('orderby', null) != null)
  68. {
  69. switch ($request->input('orderby'))
  70. {
  71. case 1:
  72. $orderby = ['sale','desc']; //销量从高到低
  73. break;
  74. case 2:
  75. $orderby = ['comments','desc']; //评论从高到低
  76. break;
  77. case 3:
  78. $orderby = ['price','desc']; //价格从高到低
  79. break;
  80. case 4:
  81. $orderby = ['price','asc']; //价格从低到高
  82. break;
  83. default:
  84. $orderby = ['pubdate','desc']; //最新
  85. }
  86. }
  87. $res = $this->getLogic()->getList($where, $orderby, model('Goods')->common_field, $offset, $limit);
  88. if($res['count']>0)
  89. {
  90. foreach($res['list'] as $k=>$v)
  91. {
  92. if(!empty($res['list'][$k]->litpic)){$res['list'][$k]->litpic = http_host().$res['list'][$k]->litpic;}
  93. $res['list'][$k]->goods_detail_url = route('weixin_goods_detail',array('id'=>$v->id));
  94. }
  95. }
  96. return ReturnData::create(ReturnData::SUCCESS, $res);
  97. }
  98. public function goodsDetail(Request $request)
  99. {
  100. //参数
  101. if(!checkIsNumber($request->input('id',null))){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  102. $id = $request->input('id');
  103. $where['id'] = $id;
  104. $res = $this->getLogic()->getOne($where);
  105. if(!$res)
  106. {
  107. return ReturnData::create(ReturnData::RECORD_NOT_EXIST);
  108. }
  109. $res->goods_detail_url = route('weixin_goods_detail',array('id'=>$res->id));
  110. return ReturnData::create(ReturnData::SUCCESS, $res);
  111. }
  112. //添加
  113. public function goodsAdd(Request $request)
  114. {
  115. if(Helper::isPostRequest())
  116. {
  117. $_POST['user_id'] = Token::$uid;
  118. return $this->getLogic()->add($_POST);
  119. }
  120. }
  121. //修改
  122. public function goodsUpdate(Request $request)
  123. {
  124. if(!checkIsNumber(request('id',null))){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  125. $id = request('id');
  126. if(Helper::isPostRequest())
  127. {
  128. unset($_POST['id']);
  129. $where['id'] = $id;
  130. //$where['user_id'] = Token::$uid;
  131. return $this->getLogic()->edit($_POST,$where);
  132. }
  133. }
  134. //删除
  135. public function goodsDelete(Request $request)
  136. {
  137. if(!checkIsNumber(request('id',null))){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  138. $id = request('id');
  139. if(Helper::isPostRequest())
  140. {
  141. $where['id'] = $id;
  142. //$where['user_id'] = Token::$uid;
  143. return $this->getLogic()->del($where);
  144. }
  145. }
  146. }