123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- <?php
- namespace App\Http\Controllers\Api\V1;
- use App\Models\S1CartInfoModel;
- use App\Models\S1GoodsInfoModel;
- use App\Services\Base\ErrorCode;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Validator;
- class CarController extends Controller
- {
- /**
- * @api {get} /api/car/index 购物车
- * @apiDescription 购物车
- * @apiGroup Car
- * @apiParam {string} appid appid
- * @apiPermission none
- * @apiVersion 0.1.0
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- * {
- "status": true,
- "status_code": 0,
- "message": "",
- "data": {
- "current_page": 1,
- "data": [
- {
- "id": 1,
- "goods_num": 2, 加入购物车商品的数量
- "created_at": null,
- "goods": {
- "name": "商品1",
- "pic": "/upload/s1/goods/face/20171013/effbecdc6d9de83d0128e3f08ec6d636.jpg",
- "status": 1,
- "price": 101100, 商品价格(分)
- "sale_number": 91, 已售数量
- "stock": 200, 总库存
- }
- },
- ],
- "first_page_url": "http://www.s1.com/api/car/index?page=1",
- "from": 1,
- "last_page": 1,
- "last_page_url": "http://www.s1.com/api/car/index?page=1",
- "next_page_url": null,
- "path": "http://www.s1.com/api/car/index",
- "per_page": 15,
- "prev_page_url": null,
- "to": 2,
- "total": 2
- }
- }
- * @apiErrorExample {json} Error-Response:
- * HTTP/1.1 400 Bad Request
- */
- public function index(Request $request)
- {
- $user = \Auth::guard('api')->user();
- if (!$user) return $this->error(ErrorCode::ERROR_POWER);
- $validator = Validator::make($request->all(),
- [
- 'appid' => 'required',
- ],
- [
- 'appid.required' => 'appid不存在',
- ]
- );
- if ($validator->fails()) {
- return $this->validatorError($validator->messages()->all(), ErrorCode::CLIENT_WRONG_PARAMS);
- }
- $appid = $request->input('appid');
- $goods = S1CartInfoModel::where('appid',$appid)->where('wx_user_id',$user->id)->with('goods')->orderBy('created_at','desc')->paginate();
- return $this->api($goods);
- }
- /**
- * @api {post} /api/car/update 编辑
- * @apiDescription 编辑(修改购物车商品数量)
- * @apiGroup Car
- * @apiParam {int} id 商品id
- * @apiParam {int} number 购买数量
- * @apiPermission none
- * @apiVersion 0.1.0
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- * {
- "status": true,
- "status_code": 0,
- "message": "",
- "data": 2005
- }
- * @apiErrorExample {json} Error-Response:
- * HTTP/1.1 400 Bad Request
- */
- public function update(Request $request)
- {
- $user = \Auth::guard('api')->user();
- if (!$user) return $this->error(ErrorCode::ERROR_POWER);
- $validator = Validator::make($request->all(),
- [
- 'id' => 'required',
- 'number' => 'required',
- ],
- [
- 'id.required' => '商品不存在',
- 'number.required' => '购买数量不能为空',
- ]
- );
- if ($validator->fails()) {
- return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
- }
- $id = $request->input('id');
- $number = $request->input('number');
- // 检查购买数量是否大于库存量剩余量
- $goods = S1GoodsInfoModel::find($id);
- if (empty($goods)) return $this->error(ErrorCode::GOODS_NOT_EXIST);
- $stock = $goods->stock - $goods->sale_number;
- if($number>$stock) return $this->error(ErrorCode::GOODS_MAX);
- if($number<1) return $this->error(ErrorCode::GOODS_MIN);
- $cat_goods = S1CartInfoModel::where('wx_user_id',$user->id)->where('s1_goods_id',$id)->first();
- if(empty($cat_goods)) return $this->error(ErrorCode::CAT_ERROR);
- $cat_goods->goods_num = $number;
- $ok = $cat_goods->save();
- if($ok) return $this->api(ErrorCode::OP_SUCCESS);
- return $this->error(ErrorCode::OP_ERROR);
- }
- /**
- * @api {get} /api/car/delete 删除
- * @apiDescription 删除
- * @apiGroup Car
- * @apiParam {int} ids 主键id 数组
- * @apiPermission none
- * @apiVersion 0.1.0
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- {
- "status": true,
- "status_code": 0,
- "message": "",
- "data": 2005
- }
- * @apiErrorExample {json} Error-Response:
- * HTTP/1.1 400 Bad Request
- */
- public function delete(Request $request)
- {
- $user = \Auth::guard('api')->user();
- if (!$user) return $this->error(ErrorCode::ERROR_POWER);
- $validator = Validator::make($request->all(),
- [
- 'ids' => 'required',
- ],
- [
- 'ids.required' => '主键id数组不存在',
- ]
- );
- if ($validator->fails()) {
- return $this->validatorError($validator->messages()->all(),ErrorCode::CLIENT_WRONG_PARAMS);
- }
- $ids = $request->input('ids');
- $ids = json_decode($ids,true); //对JSON格式的字符串进行编码
- $ok = S1CartInfoModel::destroy($ids);
- if($ok) return $this->api('');
- return $this->error(ErrorCode::OP_ERROR);
- }
- }
|