diff --git a/app/Constants/v3/ErrorCode.php b/app/Constants/v3/ErrorCode.php index d6bbf3f..1798098 100644 --- a/app/Constants/v3/ErrorCode.php +++ b/app/Constants/v3/ErrorCode.php @@ -347,5 +347,11 @@ class ErrorCode extends AbstractConstants * 超出配送范围 * @Message("您的地址超出配送范围,请重新选择地址") */ - const LOCATION_LONG_DISTANCE= 1302; + const LOCATION_LONG_DISTANCE = 1302; + + /** + * 超出配送范围 + * @Message("获取收货地址失败") + */ + const LOCATION_USER_ADDRESS = 1303; } \ No newline at end of file diff --git a/app/Controller/v3/OrderOfflineController.php b/app/Controller/v3/OrderOfflineController.php index e929c3b..52d1003 100644 --- a/app/Controller/v3/OrderOfflineController.php +++ b/app/Controller/v3/OrderOfflineController.php @@ -47,7 +47,7 @@ class OrderOfflineController extends BaseController $params = $validator->validated(); $store = $this->storeService->detail($params['store_id']); - return $this->success(['store' => $store, 'digit_length' => 8]); + return $this->success(['store' => $store, 'digit_length' => 7]); } /** diff --git a/app/Controller/v3/OrderOnlineController.php b/app/Controller/v3/OrderOnlineController.php index 8c717c1..9c74126 100644 --- a/app/Controller/v3/OrderOnlineController.php +++ b/app/Controller/v3/OrderOnlineController.php @@ -107,7 +107,7 @@ class OrderOnlineController extends BaseController $distance = $this->locationService->getDistanceByTencent($market->lng,$market->lat,$address->lng,$address->lat); } - if(isset($distance) && $distance > 8000){ + if(isset($distance) && $distance < 8000){ $distributionPrice = $this->distributionPriceService->do($distance); $res['location'] = [ 'address' => $address, diff --git a/app/Controller/v3/StoreController.php b/app/Controller/v3/StoreController.php index d81e327..2afc46e 100644 --- a/app/Controller/v3/StoreController.php +++ b/app/Controller/v3/StoreController.php @@ -54,6 +54,13 @@ class StoreController extends BaseController $data = $this->storeService->detail($params['store_id']); $data['is_collected'] = (bool)$this->collectService->check($params['user_id'],$params['store_id']); $data['goods_types'] = $this->categoryService->allForStore($params['store_id']); + $data['in_business'] = $this->storeService->check($params['store_id']); + if($data['in_business'] === true) + { + $data['in_business_text'] = '营业中'; + }else{ + $data['in_business_text'] = '休息中'; + } return $this->success(['store' => $data]); } diff --git a/app/Model/v3/Coupon.php b/app/Model/v3/Coupon.php index 35607c5..f3df3bf 100644 --- a/app/Model/v3/Coupon.php +++ b/app/Model/v3/Coupon.php @@ -17,7 +17,8 @@ class Coupon extends Model ]; protected $casts = [ - 'activity_available' => 'array' + 'activity_available' => 'array', + 'tags' => 'array' ]; public function getFullAmountTextAttribute() diff --git a/app/Service/v3/Implementations/GoodsActivityService.php b/app/Service/v3/Implementations/GoodsActivityService.php index 1bbd217..da5516a 100644 --- a/app/Service/v3/Implementations/GoodsActivityService.php +++ b/app/Service/v3/Implementations/GoodsActivityService.php @@ -12,6 +12,7 @@ use App\Constants\v3\Store; use App\Constants\v3\Goods; use App\Model\v3\GoodsActivity; use App\Model\v3\GoodsActivityBanner; +use Hyperf\Redis\Redis; use Hyperf\Utils\ApplicationContext; class GoodsActivityService implements GoodsActivityServiceInterface @@ -24,6 +25,9 @@ class GoodsActivityService implements GoodsActivityServiceInterface public function check(GoodsActivity $goods, $num, $userId) { + $redis = ApplicationContext::getContainer()->get(Redis::class); + $inventoryKey = 'goods_inventory_sold_2_'.$goods->id; // 拼接activity_type和goods_id + if (empty($goods)) { return ErrorCode::GOODS_ACTIVITY_NOT_EXISTS; } @@ -44,10 +48,20 @@ class GoodsActivityService implements GoodsActivityServiceInterface } // 商品库存不足 - if($goods->is_infinite != 1 && $goods->inventory < $num){ + // 获取冻结的库存 + $inventoryFrozen = (int)$redis->get($inventoryKey); + if($goods->is_infinite != 1 && $goods->inventory < ($num+$inventoryFrozen)){ return ErrorCode::GOODS_ACTIVITY_INVENTORY_ERROR; } + // 压redis库存 + // redis记录当前商品的购买数量,压库存,下单失败、下单成功扣库存成功、订单取消的时候释放 + if (!$redis->exists($inventoryKey)) { + $redis->set($inventoryKey, $num); + } else { + $redis->incrBy($inventoryKey, $num); + } + // 是否超过限购数量 if ($goods->restrict_num != 0 && $goods->restrict_num < $num) { return ErrorCode::GOODS_ACTIVITY_RESTRICT_LIMIT; diff --git a/app/Service/v3/Implementations/GoodsService.php b/app/Service/v3/Implementations/GoodsService.php index bf240f3..1198ce8 100644 --- a/app/Service/v3/Implementations/GoodsService.php +++ b/app/Service/v3/Implementations/GoodsService.php @@ -10,6 +10,9 @@ use App\Model\v3\Goods; use App\Model\v3\GoodsBanner; use App\Constants\v3\Store; use App\Constants\v3\goods as goodsConstants; +use Hyperf\Redis\Redis; +use Hyperf\Utils\ApplicationContext; + class GoodsService implements GoodsServiceInterface { public function do($goodsId) @@ -17,10 +20,12 @@ class GoodsService implements GoodsServiceInterface } - public function check(Goods $goods,$num = 1) { + $redis = ApplicationContext::getContainer()->get(Redis::class); + $inventoryKey = 'goods_inventory_sold_1_'.$goods->id; // 拼接activity_type和goods_id + if (empty($goods)) { return ErrorCode::GOODS_NOT_EXISTS; } @@ -34,9 +39,18 @@ class GoodsService implements GoodsServiceInterface return ErrorCode::GOODS_ON_SALE_NO; } // 商品库存不足 - if($goods->is_infinite != 1 && $goods->inventory < $num){ + // 获取冻结的库存 + $inventoryFrozen = (int)$redis->get($inventoryKey); + if($goods->is_infinite != 1 && $goods->inventory < ($num+$inventoryFrozen)){ return ErrorCode::GOODS_INVENTORY_ERROR; } + // 压redis库存 + // redis记录当前商品的购买数量,压库存,下单失败、下单成功扣库存成功、订单取消的时候释放 + if (!$redis->exists($inventoryKey)) { + $redis->set($inventoryKey, $num); + } else { + $redis->incrBy($inventoryKey, $num); + } // 是否超过限购数量 if ($goods->restrict_num != 0 && $goods->restrict_num < $num) { diff --git a/app/Service/v3/Implementations/OrderOnlineService.php b/app/Service/v3/Implementations/OrderOnlineService.php index 50fd29c..a2824dd 100644 --- a/app/Service/v3/Implementations/OrderOnlineService.php +++ b/app/Service/v3/Implementations/OrderOnlineService.php @@ -34,6 +34,7 @@ use Hyperf\Database\Model\Model; use Hyperf\DbConnection\Db; use Hyperf\Di\Annotation\Inject; use App\Service\v3\Interfaces\OrderOnlineServiceInterface; +use Hyperf\Redis\Redis; use Hyperf\Snowflake\IdGeneratorInterface; use Hyperf\Utils\ApplicationContext; @@ -115,6 +116,8 @@ class OrderOnlineService implements OrderOnlineServiceInterface */ public function do($marketId, $userId, $userAddrId, $storeList, $totalMoney, $deliveryTimeNote='尽快送达', $serviceMoney=0, $receiveCouponIds=null, $plat=''){ + $redis = ApplicationContext::getContainer()->get(Redis::class); + $carts = []; Db::beginTransaction(); try { @@ -345,6 +348,17 @@ class OrderOnlineService implements OrderOnlineServiceInterface return $this->paymentService->do($globalOrderId, $totalAmount, $userId, config('wechat.notify_url.online')); } catch (Exception $e) { Db::rollBack(); + // 释redis库存 + foreach ($carts as $k => &$cart) { + // 拼接activity_type和goods_id + $inventoryKey = 'goods_inventory_sold_'.$cart->activity_type.'_'.$cart->goods_id; + // redis记录当前商品的购买数量,压库存,下单失败、下单成功扣库存成功、订单取消的时候释放 + if (!$redis->exists($inventoryKey)) { + $redis->set($inventoryKey, 0); + } else { + $redis->decrBy($inventoryKey, $cart->num); + } + } $this->log->event(LogLabel::ORDER_ONLINE_LOG, ['msg' => $e->getMessage()]); throw new ErrorCodeException(ErrorCode::ORDER_ONLINE_FAIL); } @@ -388,15 +402,31 @@ class OrderOnlineService implements OrderOnlineServiceInterface // 撤销活动商品购买记录 $orders = Order::query()->where(['order_main_id' => $globalOrderId])->get()->toArray(); $orderGoods = OrderGoods::query() - ->where('activity_type', 2) + // ->where('activity_type', 2) ->whereIn('order_id', array_values(array_column($orders, 'id'))) - ->get(); + ->get() + ->toArray(); foreach ($orderGoods as $key => &$goods) { - $this->goodsActivityService->clearCacheRecord($goods->goods_id, $goods->number, $orderMain->user_id); + if ($goods['activity_type'] == 2) { + $this->goodsActivityService->clearCacheRecord($goods['goods_id'], $goods['number'], $orderMain['user_id']); + } } Db::commit(); + // 释redis库存 + $redis = ApplicationContext::getContainer()->get(Redis::class); + foreach ($orderGoods as $k => &$goodsItem) { + // 拼接activity_type和goods_id + $inventoryKey = 'goods_inventory_sold_'.$goodsItem['activity_type'].'_'.$goodsItem['goods_id']; + // redis记录当前商品的购买数量,压库存,下单失败、下单成功扣库存成功、订单取消的时候释放 + if (!$redis->exists($inventoryKey)) { + $redis->set($inventoryKey, 0); + } else { + $redis->decrBy($inventoryKey, $goodsItem['number']); + } + } + // 记录badge $orderChildIds = array_values(array_column($orders, 'store_id')); $this->badgeService->doByOrder($orderMain->user_id, $orderChildIds, $orderMain->global_order_id, OrderState::CANCELED); @@ -479,6 +509,19 @@ class OrderOnlineService implements OrderOnlineServiceInterface Db::commit(); + // 释redis库存 + $redis = ApplicationContext::getContainer()->get(Redis::class); + foreach ($orderGoods as $k => &$goodsItem) { + // 拼接activity_type和goods_id + $inventoryKey = 'goods_inventory_sold_'.$goodsItem['activity_type'].'_'.$goodsItem['goods_id']; + // redis记录当前商品的购买数量,压库存,下单失败、下单成功扣库存成功、订单取消的时候释放 + if (!$redis->exists($inventoryKey)) { + $redis->set($inventoryKey, 0); + } else { + $redis->decrBy($inventoryKey, $goodsItem['number']); + } + } + // 月销流水 $statistics = []; foreach ($orders as $key => &$order) { diff --git a/app/Service/v3/Implementations/UserAddressService.php b/app/Service/v3/Implementations/UserAddressService.php index ed5cadc..0851fe2 100644 --- a/app/Service/v3/Implementations/UserAddressService.php +++ b/app/Service/v3/Implementations/UserAddressService.php @@ -85,6 +85,10 @@ class UserAddressService implements UserAddressServiceInterface { $address = $this->get($userAddressId); $market = Market::query()->select('lng','lat')->find($marketId); + if(empty($address['address']->lng) || empty($address['address']->lat) || empty($market->lng) || empty($market->lat)){ + throw new ErrorCodeException(ErrorCode::LOCATION_USER_ADDRESS); + } + var_dump($address->lng,$market); $distance = $this->locationService->getDistanceByTencent($address['address']->lng,$address['address']->lat,$market->lng,$market->lat); $distributionPrice = $this->distributionPriceService->do($distance); $res['address'] = $address; diff --git a/app/Service/v3/Implementations/UserCenterBlockService.php b/app/Service/v3/Implementations/UserCenterBlockService.php index 55b8386..2572c42 100644 --- a/app/Service/v3/Implementations/UserCenterBlockService.php +++ b/app/Service/v3/Implementations/UserCenterBlockService.php @@ -34,11 +34,11 @@ class UserCenterBlockService implements UserCenterBlockServiceInterface 'type' => 'user', 'title' => '常用功能', 'items' => [ - ['name' => '收货地址', 'icon' => $img_host . 'user_icons/user_addr.png', 'type' => 'page', 'path' => '/pages/address/userAddress/userAddress','command'=>'user_addr'], - ['name' => '当面付订单查询', 'icon' => $img_host . 'user_icons/offline_orders.png', 'type' => 'page', 'path' => '/pages/facepay/facepay','command'=>'offline_orders'], - ['name' => '在线客服', 'icon' => $img_host . 'user_icons/online_kf.png', 'type' => 'page', 'path' => '','command'=>'online_kf'], - ['name' => '联系服务站', 'icon' => $img_host . 'user_icons/contact_fwz.png', 'type' => 'page', 'path' => '/pages/contactMarket/contactMarket','command'=>'contact_fwz'], - ['name' => '关于懒族', 'icon' => $img_host . 'user_icons/about_lz.png', 'type' => 'page', 'path' => '/pages/aboutUs/aboutUs','command'=>'about'], + ['name' => '收货地址', 'icon' => $img_host . 'user_icons/user_addr2.png', 'type' => 'page', 'path' => '/pages/address/userAddress/userAddress','command'=>'user_addr'], + ['name' => '当面付订单查询', 'icon' => $img_host . 'user_icons/offline_orders2.png', 'type' => 'page', 'path' => '/pages/facepay/facepay','command'=>'offline_orders'], + ['name' => '在线客服', 'icon' => $img_host . 'user_icons/online_kf2.png', 'type' => 'page', 'path' => '','command'=>'online_kf'], + ['name' => '联系服务站', 'icon' => $img_host . 'user_icons/contact_fwz2.png', 'type' => 'page', 'path' => '/pages/contactMarket/contactMarket','command'=>'contact_fwz'], + ['name' => '关于懒族', 'icon' => $img_host . 'user_icons/about_lz2.png', 'type' => 'page', 'path' => '/pages/aboutUs/aboutUs','command'=>'about'], ] ] ]; @@ -47,7 +47,7 @@ class UserCenterBlockService implements UserCenterBlockServiceInterface 'type' => 'store_user', 'title' => '商家相关', 'items' => [ - ['name' => '商家入口', 'icon' => $img_host . 'user_icons/shop_enter.png', 'type' => 'page', 'path' => '/pages/shopLogin/shopLogin','command'=>'store_login'] + ['name' => '商家入口', 'icon' => $img_host . 'user_icons/shop_enter2.png', 'type' => 'page', 'path' => '/pages/shopLogin/shopLogin','command'=>'store_login'] ] ]; @@ -69,7 +69,7 @@ class UserCenterBlockService implements UserCenterBlockServiceInterface 'type' => 'sp_user', 'title' => '服务专员', 'items' => [ - ['name' => '评价', 'icon' => $img_host . 'user_icons/shop_enter.png', 'type' => 'page', 'path' => '/zh_cjdianc/pages/appraise/index?service_personnel_id='.$item['data']['id'],'command'=>'sp_login'] + ['name' => '评价', 'icon' => $img_host . 'user_icons/service2.png', 'type' => 'page', 'path' => '/zh_cjdianc/pages/appraise/index?service_personnel_id='.$item['data']['id'],'command'=>'sp_login'] ] ]; }