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.

172 lines
5.2 KiB

8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 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
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use Log;
  4. use 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 CommonController
  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('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. //关键词搜索
  58. if($request->input('keyword', null) != null)
  59. {
  60. //添加搜索关键词
  61. $goodssearchword = new GoodsSearchwordLogic();
  62. $goodssearchword->add(array('name'=>$keyword));
  63. }
  64. //排序
  65. $orderby = '';
  66. if($request->input('orderby', null) != null)
  67. {
  68. switch ($request->input('orderby'))
  69. {
  70. case 1:
  71. $orderby = ['sale','desc']; //销量从高到低
  72. break;
  73. case 2:
  74. $orderby = ['comments','desc']; //评论从高到低
  75. break;
  76. case 3:
  77. $orderby = ['price','desc']; //价格从高到低
  78. break;
  79. case 4:
  80. $orderby = ['price','asc']; //价格从低到高
  81. break;
  82. default:
  83. $orderby = ['pubdate','desc']; //最新
  84. }
  85. }
  86. $res = $this->getLogic()->getList($where, $orderby, model('Goods')->common_field, $offset, $limit);
  87. if($res['count']>0)
  88. {
  89. foreach($res['list'] as $k=>$v)
  90. {
  91. if(!empty($res['list'][$k]->litpic)){$res['list'][$k]->litpic = http_host().$res['list'][$k]->litpic;}
  92. $res['list'][$k]->goods_detail_url = route('weixin_goods_detail',array('id'=>$v->id));
  93. }
  94. }
  95. return ReturnData::create(ReturnData::SUCCESS, $res);
  96. }
  97. public function goodsDetail(Request $request)
  98. {
  99. //参数
  100. if(!checkIsNumber($request->input('id',null))){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  101. $id = $request->input('id');
  102. $where['id'] = $id;
  103. $res = $this->getLogic()->getOne($where);
  104. if(!$res)
  105. {
  106. return ReturnData::create(ReturnData::RECORD_NOT_EXIST);
  107. }
  108. $res->goods_detail_url = route('weixin_goods_detail',array('id'=>$res->id));
  109. return ReturnData::create(ReturnData::SUCCESS, $res);
  110. }
  111. //添加
  112. public function goodsAdd(Request $request)
  113. {
  114. if(Helper::isPostRequest())
  115. {
  116. $_POST['user_id'] = Token::$uid;
  117. return $this->getLogic()->add($_POST);
  118. }
  119. }
  120. //修改
  121. public function goodsUpdate(Request $request)
  122. {
  123. if(!checkIsNumber(request('id',null))){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  124. $id = request('id');
  125. if(Helper::isPostRequest())
  126. {
  127. unset($_POST['id']);
  128. $where['id'] = $id;
  129. //$where['user_id'] = Token::$uid;
  130. return $this->getLogic()->edit($_POST,$where);
  131. }
  132. }
  133. //删除
  134. public function goodsDelete(Request $request)
  135. {
  136. if(!checkIsNumber(request('id',null))){return ReturnData::create(ReturnData::PARAMS_ERROR);}
  137. $id = request('id');
  138. if(Helper::isPostRequest())
  139. {
  140. $where['id'] = $id;
  141. //$where['user_id'] = Token::$uid;
  142. return $this->getLogic()->del($where);
  143. }
  144. }
  145. }