CarController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace App\Http\Controllers\Api\V1;
  3. use App\Models\S1CartInfoModel;
  4. use App\Models\S1GoodsInfoModel;
  5. use App\Services\Base\ErrorCode;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Log;
  8. use Illuminate\Support\Facades\Validator;
  9. class CarController extends Controller
  10. {
  11. /**
  12. * @api {get} /api/car/index 购物车
  13. * @apiDescription 购物车
  14. * @apiGroup Car
  15. * @apiParam {string} appid appid
  16. * @apiPermission none
  17. * @apiVersion 0.1.0
  18. * @apiSuccessExample {json} Success-Response:
  19. * HTTP/1.1 200 OK
  20. * {
  21. "status": true,
  22. "status_code": 0,
  23. "message": "",
  24. "data": {
  25. "current_page": 1,
  26. "data": [
  27. {
  28. "id": 1,
  29. "goods_num": 2, 加入购物车商品的数量
  30. "created_at": null,
  31. "goods": {
  32. "name": "商品1",
  33. "pic": "/upload/s1/goods/face/20171013/effbecdc6d9de83d0128e3f08ec6d636.jpg",
  34. "status": 1,
  35. "price": 101100, 商品价格(分)
  36. "sale_number": 91, 已售数量
  37. "stock": 200, 总库存
  38. }
  39. },
  40. ],
  41. "first_page_url": "http://www.s1.com/api/car/index?page=1",
  42. "from": 1,
  43. "last_page": 1,
  44. "last_page_url": "http://www.s1.com/api/car/index?page=1",
  45. "next_page_url": null,
  46. "path": "http://www.s1.com/api/car/index",
  47. "per_page": 15,
  48. "prev_page_url": null,
  49. "to": 2,
  50. "total": 2
  51. }
  52. }
  53. * @apiErrorExample {json} Error-Response:
  54. * HTTP/1.1 400 Bad Request
  55. */
  56. public function index(Request $request)
  57. {
  58. $user = \Auth::guard('api')->user();
  59. if (!$user) return $this->error(ErrorCode::ERROR_POWER);
  60. $validator = Validator::make($request->all(),
  61. [
  62. 'appid' => 'required',
  63. ],
  64. [
  65. 'appid.required' => 'appid不存在',
  66. ]
  67. );
  68. if ($validator->fails()) {
  69. return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS);
  70. }
  71. $appid = $request->input('appid');
  72. $goods = S1CartInfoModel::where('appid',$appid)->where('wx_user_id',$user->id)->with('goods')->orderBy('created_at','desc')->paginate();
  73. return $this->api($goods);
  74. }
  75. /**
  76. * @api {post} /api/car/update 编辑
  77. * @apiDescription 编辑(修改购物车商品数量)
  78. * @apiGroup Car
  79. * @apiParam {int} id 商品id
  80. * @apiParam {int} number 购买数量
  81. * @apiPermission none
  82. * @apiVersion 0.1.0
  83. * @apiSuccessExample {json} Success-Response:
  84. * HTTP/1.1 200 OK
  85. * {
  86. "status": true,
  87. "status_code": 0,
  88. "message": "",
  89. "data": 2005
  90. }
  91. * @apiErrorExample {json} Error-Response:
  92. * HTTP/1.1 400 Bad Request
  93. */
  94. public function update(Request $request)
  95. {
  96. $user = \Auth::guard('api')->user();
  97. if (!$user) return $this->error(ErrorCode::ERROR_POWER);
  98. $validator = Validator::make($request->all(),
  99. [
  100. 'id' => 'required',
  101. 'number' => 'required',
  102. ],
  103. [
  104. 'id.required' => '商品不存在',
  105. 'number.required' => '购买数量不能为空',
  106. ]
  107. );
  108. if ($validator->fails()) {
  109. return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  110. }
  111. $id = $request->input('id');
  112. $number = $request->input('number');
  113. // 检查购买数量是否大于库存量剩余量
  114. $goods = S1GoodsInfoModel::find($id);
  115. if (empty($goods)) return $this->error(ErrorCode::GOODS_NOT_EXIST);
  116. $stock = $goods->stock - $goods->sale_number;
  117. if($number>$stock) return $this->error(ErrorCode::GOODS_MAX);
  118. if($number<1) return $this->error(ErrorCode::GOODS_MIN);
  119. $cat_goods = S1CartInfoModel::where('wx_user_id',$user->id)->where('s1_goods_id',$id)->first();
  120. if(empty($cat_goods)) return $this->error(ErrorCode::CAT_ERROR);
  121. $cat_goods->goods_num = $number;
  122. $ok = $cat_goods->save();
  123. if($ok) return $this->api(ErrorCode::OP_SUCCESS);
  124. return $this->error(ErrorCode::OP_ERROR);
  125. }
  126. /**
  127. * @api {get} /api/car/delete 删除
  128. * @apiDescription 删除
  129. * @apiGroup Car
  130. * @apiParam {int} ids 主键id 数组
  131. * @apiPermission none
  132. * @apiVersion 0.1.0
  133. * @apiSuccessExample {json} Success-Response:
  134. * HTTP/1.1 200 OK
  135. {
  136. "status": true,
  137. "status_code": 0,
  138. "message": "",
  139. "data": 2005
  140. }
  141. * @apiErrorExample {json} Error-Response:
  142. * HTTP/1.1 400 Bad Request
  143. */
  144. public function delete(Request $request)
  145. {
  146. $user = \Auth::guard('api')->user();
  147. if (!$user) return $this->error(ErrorCode::ERROR_POWER);
  148. $validator = Validator::make($request->all(),
  149. [
  150. 'ids' => 'required',
  151. ],
  152. [
  153. 'ids.required' => '主键id数组不存在',
  154. ]
  155. );
  156. if ($validator->fails()) {
  157. return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
  158. }
  159. $ids = $request->input('ids');
  160. $ids = json_decode($ids,true); //对JSON格式的字符串进行编码
  161. $ok = S1CartInfoModel::destroy($ids);
  162. if($ok) return $this->api('');
  163. return $this->error(ErrorCode::OP_ERROR);
  164. }
  165. }