From 99b4420459110931d145436e9c9e0f264d0f0b3a Mon Sep 17 00:00:00 2001 From: ztt <835303992@qq.com> Date: Mon, 26 Feb 2024 11:04:56 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=AE=A2=E5=8D=95?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E7=8A=B6=E6=80=81=E5=92=8C=E5=BA=97=E9=95=BF?= =?UTF-8?q?=E5=8F=91=E8=B4=A7=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/Goods.php | 7 +++++++ app/api/model/Cart.php | 8 ++++++++ app/api/model/Order.php | 5 ++++- app/store/model/Order.php | 20 +++++++++++++------- app/store/service/order/Delivery.php | 9 +++++++++ 5 files changed, 41 insertions(+), 8 deletions(-) diff --git a/app/api/controller/Goods.php b/app/api/controller/Goods.php index a10108d6..a0165ccc 100644 --- a/app/api/controller/Goods.php +++ b/app/api/controller/Goods.php @@ -16,6 +16,7 @@ use app\api\model\{Goods as GoodsModel}; use app\api\service\{Goods as GoodsService, User as UserService}; use app\common\service\qrcode\Goods as GoodsPoster; use cores\exception\BaseException; +use think\facade\Cache; use think\response\Json; /** @@ -187,7 +188,13 @@ class Goods extends Controller public function recommended(): Json { $service = new GoodsService; + $cache_key = "goods_recommended".$this->storeId; + if(Cache::has($cache_key)) { + $goodsList = Cache::get($cache_key); + return $this->renderSuccess(compact('goodsList')); + } $goodsList = $service->recommended(); + Cache::set($cache_key, $goodsList, 60*60); return $this->renderSuccess(compact('goodsList')); } diff --git a/app/api/model/Cart.php b/app/api/model/Cart.php index 88ac8e2d..a9a95e98 100644 --- a/app/api/model/Cart.php +++ b/app/api/model/Cart.php @@ -178,4 +178,12 @@ class Cart extends CartModel ->where('is_delete', '=', 0) ->sum('goods_num'); } + + public static function getCount() { + if (!UserService::isLogin()) return 0; + $userId = UserService::getCurrentLoginUserId(); + return self::where('user_id', '=', $userId) + ->where('is_delete', '=', 0) + ->sum('goods_num'); + } } diff --git a/app/api/model/Order.php b/app/api/model/Order.php index fb491480..c50d8081 100644 --- a/app/api/model/Order.php +++ b/app/api/model/Order.php @@ -497,6 +497,7 @@ class Order extends OrderModel */ public function getTodoCounts($user_type): array { + if ($user_type == OrderType::ORDER) {//商城 return [ 'payment_number' => $this->getCount('payment'), // 待付款的订单 @@ -580,11 +581,13 @@ class Order extends OrderModel */ public function getActionCounts(): array { + $userId = UserService::getCurrentLoginUserId(); + return [ 'reservation_number' => RecoveryOrder::getCount(), // 上门预约待服务订单总数 'view_number' => GoodsBrowseLog::getCount(), // 浏览记录 'send_number' => TransferRecord::getCount(), // 发货数量 - 'integral_number' => User::get(UserService::getCurrentLoginUserId())->points, // 积分数量 + 'integral_number' => User::get($userId)->points, // 积分数量 'coupon_number' => UserCoupon::getCount(), // 优惠券 'goods_collect_number' => 0, // 商品采集数量 'cart_number' => Cart::getCount(), // 购物车数量 diff --git a/app/store/model/Order.php b/app/store/model/Order.php index e15a481d..284583b5 100644 --- a/app/store/model/Order.php +++ b/app/store/model/Order.php @@ -68,11 +68,14 @@ class Order extends OrderModel } $filterOr = []; if (!empty($param['searchValue']) && $param['searchType'] == 'all') { +// $filterOr = [ +// ['order.order_no', 'like', "%{$param['searchValue']}%"], +// ['user.nick_name', 'like', "%{$param['searchValue']}%"], +// ['address.name', 'like', "%{$param['searchValue']}%"], +// ['address.phone', 'like', "%{$param['searchValue']}%"], +// ]; $filterOr = [ - ['order.order_no', 'like', "%{$param['searchValue']}%"], - ['user.nick_name', 'like', "%{$param['searchValue']}%"], - ['address.name', 'like', "%{$param['searchValue']}%"], - ['address.phone', 'like', "%{$param['searchValue']}%"], + ['order.order_no|user.nick_name|address.name|address.phone', 'like', "%{$param['searchValue']}%"] ]; } @@ -88,7 +91,8 @@ class Order extends OrderModel ->where($filter) ->where('order.is_delete', '=', 0); if (!empty($filterOr)) { - $query = $query->whereOr($filterOr); +// $query = $query->whereOr($filterOr); + $query = $query->where($filterOr); } $list = $query->order(['order.create_time' => 'desc']) ->paginate(10); @@ -191,7 +195,8 @@ class Order extends OrderModel $filter = [ ['pay_status', '=', PayStatusEnum::SUCCESS], ['delivery_status', '<>', DeliveryStatusEnum::DELIVERED], - ['order_status', 'in', [OrderStatusEnum::NORMAL, OrderStatusEnum::APPLY_CANCEL]] +// ['order_status', 'in', [OrderStatusEnum::NORMAL, OrderStatusEnum::APPLY_CANCEL]] + ['order_status', 'in', [OrderStatusEnum::NORMAL]] ]; break; case DataTypeEnum::RECEIPT: @@ -222,7 +227,8 @@ class Order extends OrderModel public function updatePrice(array $data): bool { if ($this['pay_status'] != PayStatusEnum::PENDING) { - $this->error = '该订单不合法'; +// $this->error = '该订单不合法'; + $this->error = '该订单已支付,无法修改价格'; return false; } // 实际付款金额 diff --git a/app/store/service/order/Delivery.php b/app/store/service/order/Delivery.php index 0fca089c..66f2a93e 100644 --- a/app/store/service/order/Delivery.php +++ b/app/store/service/order/Delivery.php @@ -12,6 +12,7 @@ declare (strict_types=1); namespace app\store\service\order; +use app\common\enum\dealer\withdraw\PayType; use think\facade\Db; use app\store\model\Order as OrderModel; use app\store\model\Express as ExpressModel; @@ -49,6 +50,14 @@ class Delivery extends BaseService */ public function delivery(int $orderId, array $param): bool { + if(empty($param['expressId'])) { + $this->error = '物流公司不能为空'; + return false; + } + if (empty($param['expressNo'])) { + $this->error = '物流单号不能为空'; + return false; + } // 设置默认的参数 $param = $this->buildParam($param); // 获取订单详情 From 266f300a6d4558f59921cbfb1520d7825158ad08 Mon Sep 17 00:00:00 2001 From: ztt <835303992@qq.com> Date: Mon, 26 Feb 2024 11:13:49 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E5=BA=97=E9=95=BF=E9=80=80=E6=AC=BE?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/store/model/OrderRefund.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/app/store/model/OrderRefund.php b/app/store/model/OrderRefund.php index 4b00c60d..7070d284 100644 --- a/app/store/model/OrderRefund.php +++ b/app/store/model/OrderRefund.php @@ -43,10 +43,14 @@ class OrderRefund extends OrderRefundModel } $filterOr = []; if (!empty($param['searchValue']) && $param['searchType'] == 'all') { +// $filterOr = [ +// ['order.order_no', 'like', "%{$param['searchValue']}%"], +// ['user.nick_name', 'like', "%{$param['searchValue']}%"], +// ['order.user_id', '=', (int)$param['searchValue']] +// ]; + $filterOr = [ - ['order.order_no', 'like', "%{$param['searchValue']}%"], - ['user.nick_name', 'like', "%{$param['searchValue']}%"], - ['order.user_id', '=', (int)$param['searchValue']] + ['order.order_no|user.nick_name|order.user_id', 'like', "%{$param['searchValue']}%"] ]; } @@ -57,7 +61,8 @@ class OrderRefund extends OrderRefundModel ->join('user', 'user.user_id = order.user_id') ->where($filter); if(!empty($filterOr)){ - $query = $query->whereOr($filterOr); +// $query = $query->whereOr($filterOr); + $query = $query->where($filterOr); } $list = $query->order(['refund.create_time' => 'desc', 'refund.' . $this->getPk()]) ->paginate(10); From 8d2048fe2a20e0f38e401a207ccb86d17f31bec4 Mon Sep 17 00:00:00 2001 From: ztt <835303992@qq.com> Date: Mon, 26 Feb 2024 11:19:23 +0800 Subject: [PATCH 3/4] del cart --- app/api/model/Cart.php | 189 ----------------------------------------- 1 file changed, 189 deletions(-) delete mode 100644 app/api/model/Cart.php diff --git a/app/api/model/Cart.php b/app/api/model/Cart.php deleted file mode 100644 index a9a95e98..00000000 --- a/app/api/model/Cart.php +++ /dev/null @@ -1,189 +0,0 @@ - -// +---------------------------------------------------------------------- -declare (strict_types=1); - -namespace app\api\model; - -use app\api\model\Goods as GoodsModel; -use app\api\model\GoodsSku as GoodsSkuModel; -use app\api\service\User as UserService; -use app\common\model\Cart as CartModel; -use app\common\enum\goods\Status as GoodsStatusEnum; -use app\common\enum\goods\GoodsType as GoodsTypeEnum; -use cores\exception\BaseException; - -/** - * 购物车管理 - * Class Cart - * @package app\api\model - */ -class Cart extends CartModel -{ - /** - * 隐藏字段 - * @var array - */ - protected $hidden = [ - 'is_delete', - 'store_id', - 'create_time', - 'update_time' - ]; - - /** - * 加入购物车 - * @param int $goodsId 商品ID - * @param string $goodsSkuId 商品sku唯一标识 - * @param int $goodsNum 商品数量 - * @return bool - * @throws \cores\exception\BaseException - * @throws \cores\exception\BaseException - * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException - */ - public function add(int $goodsId, string $goodsSkuId, int $goodsNum): bool - { - // 判断是否已存在购物车记录 - $detail = $this->getInfo($goodsId, $goodsSkuId, false); - // 如果已存在购物车记录, 则累计商品数量 - !empty($detail) && $goodsNum += $detail['goods_num']; - // 验证商品的状态 - $this->checkGoodsStatus($goodsId, $goodsSkuId, $goodsNum); - // 获取当前用户ID - $userId = UserService::getCurrentLoginUserId(); - // 实例化模型 - $model = $detail ?: (new static); - return $model->save([ - 'goods_id' => $goodsId, - 'goods_sku_id' => $goodsSkuId, - 'goods_num' => $goodsNum, - 'user_id' => $userId, - 'store_id' => self::$storeId, - ]); - } - - /** - * 更新购物车记录 - * @param int $goodsId 商品ID - * @param string $goodsSkuId 商品sku唯一标识 - * @param int $goodsNum 商品数量 - * @return bool - * @throws BaseException - * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException - */ - public function sUpdate(int $goodsId, string $goodsSkuId, int $goodsNum): bool - { - // 验证商品的状态 - $this->checkGoodsStatus($goodsId, $goodsSkuId, $goodsNum); - // 获取购物车记录 - $model = $this->getInfo($goodsId, $goodsSkuId, true); - // 更新记录 - return $model->save(['goods_num' => $goodsNum]); - } - - /** - * 验证商品的状态 - * @param int $goodsId 商品ID - * @param string $goodsSkuId 商品sku唯一标识 - * @param int $goodsNum 商品数量 - * @return void - * @throws BaseException - */ - private function checkGoodsStatus(int $goodsId, string $goodsSkuId, int $goodsNum): void - { - // 获取商品详情 - $goods = GoodsModel::detail($goodsId); - // 商品不存在 - if (empty($goods) || $goods['is_delete']) { - throwError('很抱歉, 商品信息不存在'); - } - // 判断商品类型 - if ($goods['goods_type'] == GoodsTypeEnum::VIRTUAL) { - throwError('很抱歉, 虚拟商品不支持加入购物车'); - } - // 商品已下架 - if ($goods['status'] == GoodsStatusEnum::OFF_SALE) { - throwError('很抱歉, 该商品已经下架'); - } - // 获取SKU信息 - $skuInfo = GoodsSkuModel::detail($goodsId, $goodsSkuId); - if ($skuInfo['stock_num'] < $goodsNum) { - throwError('很抱歉, 该商品库存数量不足'); - } - } - - /** - * 获取购物车记录 - * @param int $goodsId 商品ID - * @param string $goodsSkuId 商品sku唯一标识 - * @param bool $isForce - * @return static|bool - * @throws BaseException - * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException - * @throws \think\db\exception\ModelNotFoundException - */ - private function getInfo(int $goodsId, string $goodsSkuId, bool $isForce = true) - { - // 获取当前用户ID - $userId = UserService::getCurrentLoginUserId(); - // 获取购物车记录 - $model = static::detail($userId, $goodsId, $goodsSkuId); - if (empty($model)) { - $isForce && throwError('购物车中没有该记录'); - return false; - } - return $model; - } - - /** - * 删除购物车中指定记录 - * @param array $cartIds 购物车ID集, 如果为空删除所有 - * @return bool - * @throws BaseException - */ - public function clear(array $cartIds = []): bool - { - // 获取当前用户ID - $userId = UserService::getCurrentLoginUserId(); - // 设置更新条件 - $where = [['user_id', '=', $userId]]; - // 购物车ID集 - !empty($cartIds) && $where[] = ['id', 'in', $cartIds]; - // 更新记录 - return $this->updateBase(['is_delete' => 1], $where); - } - - /** - * 获取当前用户购物车商品总数量 - * @return float - * @throws BaseException - */ - public function getCartTotal() - { - if (!UserService::isLogin()) return 0; - $userId = UserService::getCurrentLoginUserId(); - return $this->where('user_id', '=', $userId) - ->where('is_delete', '=', 0) - ->sum('goods_num'); - } - - public static function getCount() { - if (!UserService::isLogin()) return 0; - $userId = UserService::getCurrentLoginUserId(); - return self::where('user_id', '=', $userId) - ->where('is_delete', '=', 0) - ->sum('goods_num'); - } -} From 47365d35a6da51e5fe675f18c29c92e752113866 Mon Sep 17 00:00:00 2001 From: ztt <835303992@qq.com> Date: Mon, 26 Feb 2024 11:21:33 +0800 Subject: [PATCH 4/4] 1 --- app/api/model/Cart.php | 189 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 app/api/model/Cart.php diff --git a/app/api/model/Cart.php b/app/api/model/Cart.php new file mode 100644 index 00000000..a9a95e98 --- /dev/null +++ b/app/api/model/Cart.php @@ -0,0 +1,189 @@ + +// +---------------------------------------------------------------------- +declare (strict_types=1); + +namespace app\api\model; + +use app\api\model\Goods as GoodsModel; +use app\api\model\GoodsSku as GoodsSkuModel; +use app\api\service\User as UserService; +use app\common\model\Cart as CartModel; +use app\common\enum\goods\Status as GoodsStatusEnum; +use app\common\enum\goods\GoodsType as GoodsTypeEnum; +use cores\exception\BaseException; + +/** + * 购物车管理 + * Class Cart + * @package app\api\model + */ +class Cart extends CartModel +{ + /** + * 隐藏字段 + * @var array + */ + protected $hidden = [ + 'is_delete', + 'store_id', + 'create_time', + 'update_time' + ]; + + /** + * 加入购物车 + * @param int $goodsId 商品ID + * @param string $goodsSkuId 商品sku唯一标识 + * @param int $goodsNum 商品数量 + * @return bool + * @throws \cores\exception\BaseException + * @throws \cores\exception\BaseException + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function add(int $goodsId, string $goodsSkuId, int $goodsNum): bool + { + // 判断是否已存在购物车记录 + $detail = $this->getInfo($goodsId, $goodsSkuId, false); + // 如果已存在购物车记录, 则累计商品数量 + !empty($detail) && $goodsNum += $detail['goods_num']; + // 验证商品的状态 + $this->checkGoodsStatus($goodsId, $goodsSkuId, $goodsNum); + // 获取当前用户ID + $userId = UserService::getCurrentLoginUserId(); + // 实例化模型 + $model = $detail ?: (new static); + return $model->save([ + 'goods_id' => $goodsId, + 'goods_sku_id' => $goodsSkuId, + 'goods_num' => $goodsNum, + 'user_id' => $userId, + 'store_id' => self::$storeId, + ]); + } + + /** + * 更新购物车记录 + * @param int $goodsId 商品ID + * @param string $goodsSkuId 商品sku唯一标识 + * @param int $goodsNum 商品数量 + * @return bool + * @throws BaseException + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function sUpdate(int $goodsId, string $goodsSkuId, int $goodsNum): bool + { + // 验证商品的状态 + $this->checkGoodsStatus($goodsId, $goodsSkuId, $goodsNum); + // 获取购物车记录 + $model = $this->getInfo($goodsId, $goodsSkuId, true); + // 更新记录 + return $model->save(['goods_num' => $goodsNum]); + } + + /** + * 验证商品的状态 + * @param int $goodsId 商品ID + * @param string $goodsSkuId 商品sku唯一标识 + * @param int $goodsNum 商品数量 + * @return void + * @throws BaseException + */ + private function checkGoodsStatus(int $goodsId, string $goodsSkuId, int $goodsNum): void + { + // 获取商品详情 + $goods = GoodsModel::detail($goodsId); + // 商品不存在 + if (empty($goods) || $goods['is_delete']) { + throwError('很抱歉, 商品信息不存在'); + } + // 判断商品类型 + if ($goods['goods_type'] == GoodsTypeEnum::VIRTUAL) { + throwError('很抱歉, 虚拟商品不支持加入购物车'); + } + // 商品已下架 + if ($goods['status'] == GoodsStatusEnum::OFF_SALE) { + throwError('很抱歉, 该商品已经下架'); + } + // 获取SKU信息 + $skuInfo = GoodsSkuModel::detail($goodsId, $goodsSkuId); + if ($skuInfo['stock_num'] < $goodsNum) { + throwError('很抱歉, 该商品库存数量不足'); + } + } + + /** + * 获取购物车记录 + * @param int $goodsId 商品ID + * @param string $goodsSkuId 商品sku唯一标识 + * @param bool $isForce + * @return static|bool + * @throws BaseException + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + private function getInfo(int $goodsId, string $goodsSkuId, bool $isForce = true) + { + // 获取当前用户ID + $userId = UserService::getCurrentLoginUserId(); + // 获取购物车记录 + $model = static::detail($userId, $goodsId, $goodsSkuId); + if (empty($model)) { + $isForce && throwError('购物车中没有该记录'); + return false; + } + return $model; + } + + /** + * 删除购物车中指定记录 + * @param array $cartIds 购物车ID集, 如果为空删除所有 + * @return bool + * @throws BaseException + */ + public function clear(array $cartIds = []): bool + { + // 获取当前用户ID + $userId = UserService::getCurrentLoginUserId(); + // 设置更新条件 + $where = [['user_id', '=', $userId]]; + // 购物车ID集 + !empty($cartIds) && $where[] = ['id', 'in', $cartIds]; + // 更新记录 + return $this->updateBase(['is_delete' => 1], $where); + } + + /** + * 获取当前用户购物车商品总数量 + * @return float + * @throws BaseException + */ + public function getCartTotal() + { + if (!UserService::isLogin()) return 0; + $userId = UserService::getCurrentLoginUserId(); + return $this->where('user_id', '=', $userId) + ->where('is_delete', '=', 0) + ->sum('goods_num'); + } + + public static function getCount() { + if (!UserService::isLogin()) return 0; + $userId = UserService::getCurrentLoginUserId(); + return self::where('user_id', '=', $userId) + ->where('is_delete', '=', 0) + ->sum('goods_num'); + } +}