Browse Source

购物车

master
Lemon 5 years ago
parent
commit
bf4ff8b1fa
  1. 6
      app/Constants/v3/ErrorCode.php
  2. 2
      app/Controller/v3/HomeController.php
  3. 10
      app/Controller/v3/ShopCartController.php
  4. 7
      app/Controller/v3/ShopCartUpdateController.php
  5. 44
      app/Request/v3/ShopCartUpdateRequest.php
  6. 5
      app/Service/v3/Implementations/GoodsService.php
  7. 75
      app/Service/v3/Implementations/ShopCartService.php
  8. 9
      app/Service/v3/Implementations/ShopCartUpdateService.php
  9. 24
      app/Service/v3/Implementations/StoreService.php
  10. 4
      app/Service/v3/Interfaces/ShopCartServiceInterface.php
  11. 2
      app/Service/v3/Interfaces/ShopCartUpdateServiceInterface.php
  12. 2
      app/Service/v3/Interfaces/StoreServiceInterface.php

6
app/Constants/v3/ErrorCode.php

@ -153,6 +153,12 @@ class ErrorCode extends AbstractConstants
*/
const GOODS_ON_SALE_NO = 1103;
/**
* 已下架
* @Message("商品不存在")
*/
const GOODS_NOT_EXISTS = 1104;
/************************************/
/* 活动商品相关 1151-1200 */
/************************************/

2
app/Controller/v3/HomeController.php

@ -149,7 +149,7 @@ class HomeController extends BaseController
public function storeIndex()
{
$data['detail'] = $this->storeInfoService->detail(1);
$data['detail'] = $this->storeService->detail(1);
$data['order_online'] = [
'count' => 6549,
'total' => 12654.12

10
app/Controller/v3/ShopCartController.php

@ -15,19 +15,23 @@ class ShopCartController extends BaseController
protected $shopCartService;
public function detail()
{
$userId = $this->request->input('user_id');
$marketId = $this->request->input('market_id');
//获取购物车商品信息
$res['store_lists'] = $this->shopCartService->do();
$res['store_lists'] = $this->shopCartService->do($userId,$marketId);
//获取购物车失效商品信息
$res['store_lists_invalid'] = $this->shopCartService->undo();
//计算购物车价格
$res['total'] = $this->shopCartService->getTotal();
$res['total'] = $this->shopCartService->getTotal($userId,$marketId);
return $this->success($res);
}
public function info()
{
$userId = $this->request->input('user_id');
$marketId = $this->request->input('market_id');
$res['shopcart']['count'] = $this->shopCartService->countGoods();
$res['shopcart']['total'] = $this->shopCartService->getTotal();
$res['shopcart']['total'] = $this->shopCartService->getTotal($userId,$marketId);
return $this->success($res);
}
}

7
app/Controller/v3/ShopCartUpdateController.php

@ -5,7 +5,7 @@ namespace App\Controller\v3;
use App\Controller\BaseController;
use Hyperf\Di\Annotation\Inject;
use App\Service\v3\Interfaces\ShopCartUpdateServiceInterface;
use App\Request\v3\ShopCartUpdateRequest;
class ShopCartUpdateController extends BaseController
{
/**
@ -13,14 +13,13 @@ class ShopCartUpdateController extends BaseController
* @var ShopCartUpdateServiceInterface
*/
protected $shopCarServiceUpdate;
public function update()
public function update(ShopCartUpdateRequest $request)
{
$user_id = $this->request->input('user_id',0);
$market_id = $this->request->input('market_id','');
$goods_id = $this->request->input('goods_id',0);
$num = $this->request->input('num','');
$activity_type = $this->request->input('activity_type',1);
$res = $this->shopCarServiceUpdate->do($user_id,$goods_id,$market_id,$num,$activity_type);
$res = $this->shopCarServiceUpdate->do($user_id,$goods_id,$num,$activity_type);
return $this->success($res);
}

44
app/Request/v3/ShopCartUpdateRequest.php

@ -0,0 +1,44 @@
<?php
declare(strict_types=1);
namespace App\Request\v3;
use App\Request\BaseFormRequest;
class ShopCartUpdateRequest extends BaseFormRequest
{
/**
* Get the validation rules that apply to the request.
*/
public function rules(): array
{
return [
'goods_id' => 'required|nonempty|integer',
'num' => 'required|nonempty|integer',
'user_id' => 'required|nonempty|integer|exists:lanzu_user,id',
];
}
/**
* @return array
*/
public function messages(): array
{
return [
'user_id.*' => ':attribute无效',
'goods_id.*' => ':attribute无效',
'num.*' => ':attribute无效',
];
}
public function attributes(): array
{
return [
'user_id' => '用户id',
'goods_id' => '商品ID',
'num' => '数量',
];
}
}

5
app/Service/v3/Implementations/GoodsService.php

@ -20,14 +20,15 @@ class GoodsService implements GoodsServiceInterface
public function check($goodsId,$num = 1)
{
$data = Goods::query()->with('store')->where('id', $goodsId)->first();
// 商户歇业
if($data->store->is_rest == 1){
return ErrorCode::GOODS_ON_SALE_NO;
}
// 商品下架或已删除
if($data->on_sale == 0 || !is_null($data->deleted_at)){
return ErrorCode::GOODS_ON_SALE_NO;
}
// 商品库存不足
if($data->is_infinite != 1 && $data->inventory < $num){
return ErrorCode::GOODS_INVENTORY_ERROR;
}

75
app/Service/v3/Implementations/ShopCartService.php

@ -2,24 +2,81 @@
namespace App\Service\v3\Implementations;
use App\Model\v3\Goods;
use App\Model\v3\GoodsActivity;
use App\Model\v3\ShoppingCart;
use App\Model\v3\Store;
use App\Service\v3\Interfaces\GoodsActivityServiceInterface;
use App\Service\v3\Interfaces\GoodsServiceInterface;
use App\Service\v3\Interfaces\ShopCartServiceInterface;
use App\Service\v3\Interfaces\StoreServiceInterface;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
class ShopCartService implements ShopCartServiceInterface
{
public function do()
/**
* @Inject
* @var StoreServiceInterface
*/
protected $storeService;
/**
* @Inject
* @var GoodsServiceInterface
*/
protected $goodsService;
/**
* @Inject
* @var GoodsActivityServiceInterface
*/
protected $goodsActivityService;
public function do($userId,$marketId)
{
$storeIds = Db::table('lanzu_shopping_cart')->where('user_id',15)->pluck('store_id')->toArray();
$res = Store::query()->with(['ShoppingCart' => function($query) {
$query->with('goods');
$storeIds = Db::table('lanzu_shopping_cart')->where([
['user_id','=',$userId],
['market_id','=',$marketId],
])->pluck('store_id')->toArray();
$stores = Store::query()->with(['ShoppingCart' => function($query) use ($userId,$marketId){
$query->where([
['user_id','=',$userId],
['market_id','=',$marketId],
]);
}])->whereIn('id',$storeIds)
->get();
foreach ($res as &$k){
$k->subtotal = '99.90';
->get()
->toArray();
$storeArr = [];
foreach ($stores as $key => &$store){
$sotreType = $this->storeService->check($store['id']);
if(!$sotreType){
unset($stores[$key]);
continue;
}
$subtotal = 0;
foreach ($store['shopping_cart'] as &$shopcart){
if($shopcart['activity_type'] == 1){
$builder = Goods::query();
}else{
$builder = GoodsActivity::query();
}
$shopcart['goods'] = $builder->where('id',$shopcart['goods_id'])->first()->toArray();
foreach ($shopcart['goods'] as $goods){
if($shopcart['activity_type'] == 1){
$goodsType = $this->goodsService->check($goods['id']);
}else{
$goodsType = $this->goodsActivityService->check($goods['id'],$shopcart['num'],$shopcart['user_id']);
}
if($goodsType !== true){
unset($shopcart['goods'][$key]);
continue;
}
$subtotal+= $shopcart['num'] * $shopcart['goods']['price'];
}
}
$store['subtotal'] = $subtotal;
$storeArr[] = $store;
}
return $res;
return $storeArr;
}
public function check($goodsId)
@ -60,7 +117,7 @@ class ShopCartService implements ShopCartServiceInterface
return mt_rand(1,100);
}
public function getTotal()
public function getTotal($userId,$marketId)
{
$randomFloat = rand(100,999)/100;
return $randomFloat;

9
app/Service/v3/Implementations/ShopCartUpdateService.php

@ -11,7 +11,7 @@ use App\Model\v3\GoodsActivity;
use App\Constants\v3\Goods as GoodsConstants;
class ShopCartUpdateService implements ShopCartUpdateServiceInterface
{
public function do($user_id,$goods_id,$market_id,$num,$activity_type)
public function do($user_id,$goods_id,$num,$activity_type)
{
//判断是普通商品还是特价商品
if(isset($params['activity']) && $params['activity'] == GoodsConstants::IS_ACTIVITY){
@ -19,7 +19,10 @@ class ShopCartUpdateService implements ShopCartUpdateServiceInterface
}else{
$builder = Goods::query();
}
$goods = $builder->select('store_id')->find($goods_id);
$goods = $builder->select('market_id','store_id')->find($goods_id);
if(empty($goods)){
throw new ErrorCodeException(ErrorCode::GOODS_NOT_EXISTS);
}
return ShoppingCart::query()->updateOrCreate(
[
'user_id' => $user_id,
@ -27,7 +30,7 @@ class ShopCartUpdateService implements ShopCartUpdateServiceInterface
'activity_type' => $activity_type
],
[
'market_id' => $market_id,
'market_id' => $goods->market_id,
'store_id' => $goods->store_id,
'num' => $num
]

24
app/Service/v3/Implementations/StoreService.php

@ -14,9 +14,29 @@ class StoreService implements StoreServiceInterface
// TODO: Implement do() method.
}
public function check()
public function check($storeId)
{
// TODO: Implement check() method.
$store = Store::query()->where([
['id','=',$storeId],
['is_rest','=',0],
['status','=',2]
])
->select('time1','time2','time3','time4')
->first();
if(!empty($store)){
$nowTime = time();
$time1 = strtotime(($store->time1));
$time2 = strtotime(($store->time2));
if($time1 < $nowTime && $nowTime < $time2){
return true;
}
$time3 = strtotime(($store->time3));
$time4 = strtotime(($store->time4));
if($time3 < $nowTime && $nowTime < $time4){
return true;
}
}
return false;
}
public function undo()

4
app/Service/v3/Interfaces/ShopCartServiceInterface.php

@ -4,7 +4,7 @@ namespace App\Service\v3\Interfaces;
interface ShopCartServiceInterface
{
public function do();
public function do($userId,$marketId);
public function check($goodsId);
@ -12,5 +12,5 @@ interface ShopCartServiceInterface
public function countGoods();
public function getTotal();
public function getTotal($userId,$marketId);
}

2
app/Service/v3/Interfaces/ShopCartUpdateServiceInterface.php

@ -4,7 +4,7 @@ namespace App\Service\v3\Interfaces;
interface ShopCartUpdateServiceInterface
{
public function do($user_id,$goods_id,$market_id,$num,$activity_type);
public function do($user_id,$goods_id,$num,$activity_type);
public function check();

2
app/Service/v3/Interfaces/StoreServiceInterface.php

@ -5,7 +5,7 @@ namespace App\Service\v3\Interfaces;
interface StoreServiceInterface
{
public function do();
public function check();
public function check($storeId);
public function undo();
public function detail($storeId);
}
Loading…
Cancel
Save