| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
- namespace App\Http\Controllers\Api\v1;
- use App\Http\Controllers\Api\webApp;
- use App\Services\Api\CartService;
- use App\Services\Api\GoodsService;
- use App\Services\Api\MemberService;
- /**
- * 购物车管理
- * @package App\Http\Controllers\Api
- */
- class CartController extends webApp
- {
- /**
- * 列表
- * @return array
- */
- public function index()
- {
- $params = request()->post();
- $pageSize = request()->post('pageSize', 12);
- $params['user_id'] = $this->userId;
- $datas = CartService::make()->getDataList($params, $pageSize);
- return message(1010, true, $datas);
- }
- /**
- * 添加购物车
- */
- public function add()
- {
- $params = request()->all();
- $id = isset($params['id']) ? intval($params['id']) : 0;
- $skuId = isset($params['sku_id']) ? intval($params['sku_id']) : 0;
- $num = isset($params['num']) && $params['num']? intval($params['num']) : 1;
- if (empty($id)) {
- return message(1036, false);
- }
- try {
- if ($result = CartService::make()->add($this->userId, $id, $skuId, $num)) {
- return message(CartService::make()->getError(), true, $result);
- } else {
- return message(CartService::make()->getError(), false);
- }
- } catch (\Exception $exception) {
- $error = ['data' => $exception->getTrace(), 'err' => $exception->getMessage()];
- return showJson(1046, false, $error);
- }
- }
- /**
- * 更新购物车
- */
- public function update()
- {
- $params = request()->all();
- $id = isset($params['id']) ? intval($params['id']) : 0;
- if (empty($id)) {
- return message(1036, false);
- }
- try {
- if ($result = CartService::make()->update($this->userId, $id)) {
- return message(CartService::make()->getError(), true, $result);
- } else {
- return message(CartService::make()->getError(), false);
- }
- } catch (\Exception $exception) {
- $error = ['data' => $exception->getTrace(), 'err' => $exception->getMessage()];
- return showJson(1046, false, $error);
- }
- }
- /**
- * 删除购物车
- */
- public function delete()
- {
- $params = request()->all();
- $ids = isset($params['ids']) && $params['ids']? $params['ids'] : [];
- try {
- if ($result = CartService::make()->deleteCart($this->userId, $ids)) {
- return message(CartService::make()->getError(), true, $result);
- } else {
- return message(CartService::make()->getError(), false);
- }
- } catch (\Exception $exception) {
- $error = ['data' => $exception->getTrace(), 'err' => $exception->getMessage()];
- return showJson(1046, false, $error);
- }
- }
- /**
- * 购物车数量
- */
- public function count()
- {
- try {
- if ($result = CartService::make()->getCount($this->userId)) {
- return message(1010, true, $result);
- } else {
- return message(1009, false);
- }
- } catch (\Exception $exception) {
- $error = ['data' => $exception->getTrace(), 'err' => $exception->getMessage()];
- return showJson(1046, false, $error);
- }
- }
- }
|