Browse Source

Merge remote-tracking branch 'origin/master'

zhangdehua 1 year ago
parent
commit
134e9a1196

+ 1 - 1
app/index/controller/Checkout.php

@@ -150,7 +150,7 @@ class Checkout extends Controller
         // 购物车ID集
         //$cartIds = $this->getCartIds();//不需要接口传过来
         $CartModel = new CartService;
-        $cartIds = $this->getCartIds();
+        $cartIds = $CartModel->getCartIds();
         // 商品结算信息
         // 购物车商品列表
         $goodsList = $CartModel->getOrderGoodsList($cartIds);

+ 265 - 0
app/index/model/Goods.php

@@ -0,0 +1,265 @@
+<?php
+// +----------------------------------------------------------------------
+// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
+// +----------------------------------------------------------------------
+// | Author: 萤火科技 <admin@yiovo.com>
+// +----------------------------------------------------------------------
+declare (strict_types=1);
+
+namespace app\index\model;
+
+use app\api\service\Goods as GoodsService;
+use app\api\service\user\Grade as UserGradeService;
+use app\api\model\GoodsSku as GoodsSkuModel;
+use app\api\model\GoodsSpecRel as GoodsSpecRelModel;
+use app\common\model\Goods as GoodsModel;
+use app\common\enum\goods\Status as GoodsStatusEnum;
+use cores\exception\BaseException;
+
+/**
+ * 商品模型
+ * Class Goods
+ * @package app\api\model
+ */
+class Goods extends GoodsModel
+{
+    /**
+     * 隐藏字段
+     * @var array
+     */
+    public $hidden = [
+        'images',
+        'delivery',
+        'deduct_stock_type',
+        'sales_initial',
+        'sales_actual',
+        'sort',
+        'is_delete',
+        'store_id',
+        'create_time',
+        'update_time'
+    ];
+
+    // 是否设置会员折扣价
+    private $isGoodsGradeMoney = true;
+
+    /**
+     * 商品详情:HTML实体转换回普通字符
+     * @param $value
+     * @return string
+     */
+    public function getContentAttr($value): string
+    {
+        return htmlspecialchars_decode((string)$value);
+    }
+
+    /**
+     * 是否设置会员折扣价
+     * @param bool $value
+     * @return $this
+     */
+    public function isGoodsGradeMoney(bool $value): Goods
+    {
+        $this->isGoodsGradeMoney = $value;
+        return $this;
+    }
+
+    /**
+     * 获取商品列表
+     * @param array $param 查询条件
+     * @param int $listRows 分页数量
+     * @return mixed|\think\model\Collection|\think\Paginator
+     * @throws \think\db\exception\DbException
+     */
+    public function getList(array $param = [], int $listRows = 15)
+    {
+        // 整理查询参数
+        $params = array_merge($param, ['status' => GoodsStatusEnum::ON_SALE]);
+        // 获取商品列表
+        $list = parent::getList($params, $listRows);
+        if ($list->isEmpty()) {
+            return $list;
+        }
+        // 隐藏冗余的字段
+        $list->hidden(array_merge($this->hidden, ['content', 'goods_images', 'images']));
+        // 整理列表数据并返回
+        return $this->setGoodsListDataFromApi($list);
+    }
+
+    /**
+     * 获取商品详情 (详细数据用于页面展示)
+     * @param int $goodsId 商品ID
+     * @param bool $verifyStatus 是否验证商品状态(上架)
+     * @return mixed
+     * @throws BaseException
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function getDetails(int $goodsId, bool $verifyStatus = true)
+    {
+        // 关联查询(商品图片、sku列表)
+        $with = ['images.file', 'skuList.image', 'video', 'videoCover'];
+        // 获取商品记录
+        $goodsInfo = $this->getGoodsMain($goodsId, $with, $verifyStatus);
+        // 商品规格列表
+        $goodsInfo['specList'] = GoodsSpecRelModel::getSpecList($goodsInfo['goods_id']);
+        return $goodsInfo->hidden(array_merge($this->hidden, ['images']));
+    }
+
+    /**
+     * 获取商品详情 (仅包含主商品信息和商品图片)
+     * @param int $goodsId 商品ID
+     * @param bool $verifyStatus 是否验证商品状态(上架)
+     * @return mixed
+     * @throws BaseException
+     */
+    public function getBasic(int $goodsId, bool $verifyStatus = true)
+    {
+        // 关联查询(商品图片)
+        $with = ['images.file'];
+        // 获取商品记录
+        return $this->getGoodsMain($goodsId, $with, $verifyStatus);
+    }
+
+    /**
+     * 获取商品规格数据
+     * @param int $goodsId
+     * @return array
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public function getSpecData(int $goodsId): array
+    {
+        $data = [];
+        // 商品SKU列表
+        $data['skuList'] = GoodsSkuModel::getSkuList($goodsId);
+        // 商品规格列表
+        $data['specList'] = GoodsSpecRelModel::getSpecList($goodsId);
+        return $data;
+    }
+
+    /**
+     * 获取商品主体信息
+     * @param int $goodsId 商品ID
+     * @param array $with 关联查询
+     * @param bool $verifyStatus 是否验证商品状态(上架)
+     * @return mixed
+     * @throws BaseException
+     */
+    private function getGoodsMain(int $goodsId, array $with = [], bool $verifyStatus = true)
+    {
+        // 获取商品记录
+        $goodsInfo = static::detail($goodsId, $with);
+        // 判断商品是否存在
+        if (empty($goodsInfo) || $goodsInfo['is_delete']) {
+            throwError('很抱歉,商品信息不存在');
+        }
+        // 判断商品状态(上架)
+        if ($verifyStatus && $goodsInfo['status'] == GoodsStatusEnum::OFF_SALE) {
+            throwError('很抱歉,当前商品已下架');
+        }
+        // 整理商品数据并返回
+        return $this->setGoodsDataFromApi($goodsInfo);
+    }
+
+    /**
+     * 根据商品id集获取商品列表
+     * @param array $goodsIds
+     * @return mixed
+     */
+    public function getListByIdsFromApi(array $goodsIds)
+    {
+        // 获取商品列表
+        $data = $this->getListByIds($goodsIds, GoodsStatusEnum::ON_SALE);
+        // 整理列表数据并返回
+        return $this->setGoodsListDataFromApi($data);
+    }
+
+    /**
+     * 获取商品指定的sku信息并且设置商品的会员价
+     * @param mixed $goodsInfo 商品信息
+     * @param string $goodsSkuId 商品SKUID
+     * @param bool $isGoodsGradeMoney 是否设置会员折扣价
+     * @return \app\common\model\GoodsSku|array|null
+     * @throws BaseException
+     */
+    public static function getSkuInfo($goodsInfo, string $goodsSkuId, bool $isGoodsGradeMoney = true)
+    {
+        $goodsInfo['skuInfo'] = GoodsService::getSkuInfo($goodsInfo['goods_id'], $goodsSkuId);
+        $isGoodsGradeMoney && (new static)->setGoodsGradeMoney($goodsInfo);
+        return $goodsInfo['skuInfo'];
+    }
+
+    /**
+     * 设置商品展示的数据 api模块
+     * @param $data
+     * @return mixed
+     */
+    private function setGoodsListDataFromApi($data)
+    {
+        return $this->setGoodsListData($data, function ($goods) {
+            // 整理商品数据 api模块
+            $this->setGoodsDataFromApi($goods);
+        });
+    }
+
+    /**
+     * 整理商品数据 api模块
+     * @param $goodsInfo
+     * @return mixed
+     */
+    private function setGoodsDataFromApi($goodsInfo)
+    {
+        return $this->setGoodsData($goodsInfo, function ($goods) {
+            // 计算并设置商品会员价
+            $this->isGoodsGradeMoney && $this->setGoodsGradeMoney($goods);
+        });
+    }
+
+    /**
+     * 设置商品的会员价
+     * @param Goods $goods
+     * @throws BaseException
+     */
+    private function setGoodsGradeMoney(self $goods)
+    {
+        // 设置当前商品是否使用会员等级折扣价
+        $goods['is_user_grade'] = false;
+        // 获取当前登录用户的会员等级信息
+        $gradeInfo = UserGradeService::getCurrentGradeInfo();
+        // 判断商品是否参与会员折扣
+        if (empty($gradeInfo) || !$goods['is_enable_grade']) {
+            return;
+        }
+        // 默认的折扣比例
+        $discountRatio = $gradeInfo['equity']['discount'];
+        // 商品单独设置了会员折扣
+        if ($goods['is_alone_grade'] && isset($goods['alone_grade_equity'][$gradeInfo['grade_id']])) {
+            $discountRatio = $goods['alone_grade_equity'][$gradeInfo['grade_id']];
+        }
+        if (empty($discountRatio)) {
+            return;
+        }
+        // 标记参与会员折扣
+        $goods['is_user_grade'] = true;
+        // 会员折扣价: 商品基础价格
+        $goods['goods_price_min'] = UserGradeService::getDiscountPrice($goods['goods_price_min'], $discountRatio);
+        $goods['goods_price_max'] = UserGradeService::getDiscountPrice($goods['goods_price_max'], $discountRatio);
+        // 会员折扣价: 商品sku列表
+        if ($goods->getRelation('skuList')) {
+            foreach ($goods['skuList'] as &$skuItem) {
+                $skuItem['goods_price'] = UserGradeService::getDiscountPrice($skuItem['goods_price'], $discountRatio);
+            }
+        }
+        // 会员折扣价: 已选择的商品sku(用于购物车)
+        if ($goods->getAttr('skuInfo')) {
+            $goods['skuInfo']['goods_price'] = UserGradeService::getDiscountPrice($goods['skuInfo']['goods_price'], $discountRatio);
+        }
+    }
+}

+ 33 - 0
app/index/model/OrderAddress.php

@@ -0,0 +1,33 @@
+<?php
+// +----------------------------------------------------------------------
+// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
+// +----------------------------------------------------------------------
+// | Author: 萤火科技 <admin@yiovo.com>
+// +----------------------------------------------------------------------
+declare (strict_types = 1);
+
+namespace app\index\model;
+
+use app\common\model\OrderAddress as OrderAddressModel;
+
+/**
+ * 订单收货地址模型
+ * Class OrderAddress
+ * @package app\api\model
+ */
+class OrderAddress extends OrderAddressModel
+{
+    /**
+     * 隐藏字段
+     * @var array
+     */
+    protected $hidden = [
+        'store_id',
+        'create_time',
+    ];
+
+}

+ 50 - 0
app/index/model/OrderGoods.php

@@ -0,0 +1,50 @@
+<?php
+// +----------------------------------------------------------------------
+// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
+// +----------------------------------------------------------------------
+// | Author: 萤火科技 <admin@yiovo.com>
+// +----------------------------------------------------------------------
+declare (strict_types = 1);
+
+namespace app\index\model;
+
+use app\common\model\OrderGoods as OrderGoodsModel;
+
+/**
+ * 订单商品模型
+ * Class OrderGoods
+ * @package app\api\model
+ */
+class OrderGoods extends OrderGoodsModel
+{
+    /**
+     * 隐藏字段
+     * @var array
+     */
+    protected $hidden = [
+        'content',
+        'store_id',
+        'create_time',
+    ];
+
+    /**
+     * 获取未评价的商品
+     * @param int $orderId 订单ID
+     * @return \think\Collection
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public static function getNotCommentGoodsList(int $orderId)
+    {
+        return (new static)->with(['image'])
+            ->where('order_id', '=', $orderId)
+            ->where('is_comment', '=', 0)
+            ->select();
+    }
+
+}

+ 33 - 0
app/index/model/Setting.php

@@ -0,0 +1,33 @@
+<?php
+// +----------------------------------------------------------------------
+// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
+// +----------------------------------------------------------------------
+// | Author: 萤火科技 <admin@yiovo.com>
+// +----------------------------------------------------------------------
+declare (strict_types = 1);
+
+namespace app\index\model;
+
+use app\common\model\store\Setting as SettingModel;
+
+/**
+ * 系统设置模型
+ * Class Setting
+ * @package app\api\model
+ */
+class Setting extends SettingModel
+{
+    /**
+     * 获取积分名称
+     * @return string
+     */
+    public static function getPointsName()
+    {
+        return static::getItem('points')['points_name'];
+    }
+
+}

+ 182 - 0
app/index/model/UserAddress.php

@@ -0,0 +1,182 @@
+<?php
+// +----------------------------------------------------------------------
+// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
+// +----------------------------------------------------------------------
+// | Author: 萤火科技 <admin@yiovo.com>
+// +----------------------------------------------------------------------
+declare (strict_types = 1);
+
+namespace app\index\model;
+
+use app\api\model\User as UserModel;
+use app\api\service\User as UserService;
+use app\common\model\UserAddress as UserAddressModel;
+use app\common\exception\BaseException;
+
+/**
+ * 用户收货地址模型
+ * Class UserAddress
+ * @package app\common\model
+ */
+class UserAddress extends UserAddressModel
+{
+    /**
+     * 隐藏字段
+     * @var array
+     */
+    protected $hidden = [
+        'is_delete',
+        'store_id',
+        'create_time',
+        'update_time'
+    ];
+
+//    /**
+//     * 地区名称
+//     * @param $value
+//     * @param $data
+//     * @return array
+//     */
+//    public function getRegionAttr($value, $data)
+//    {
+//        return array_values(parent::getRegionAttr($value, $data));
+//    }
+
+    /**
+     * 获取收货地址列表
+     * @return \think\Collection
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @throws BaseException
+     */
+    public function getList()
+    {
+        $userId = UserService::getCurrentLoginUserId();
+        return $this->where('user_id', '=', $userId)
+            ->where('is_delete', '=', 0)
+            ->select();
+    }
+
+    /**
+     * 新增收货地址
+     * @param array $data
+     * @return mixed
+     * @throws BaseException
+     */
+    public function add(array $data)
+    {
+        // 当前用户信息
+        $user = UserService::getCurrentLoginUser(true);
+        // 省市区ID
+        list($data['province_id'], $data['city_id'], $data['region_id']) = $this->getRegionId($data);
+        // 添加收货地址
+        return $this->transaction(function () use ($user, $data) {
+            $this->save([
+                'name' => $data['name'],
+                'phone' => $data['phone'],
+                'province_id' => $data['province_id'],
+                'city_id' => $data['city_id'],
+                'region_id' => $data['region_id'],
+                'detail' => $data['detail'],
+                'user_id' => $user['user_id'],
+                'store_id' => self::$storeId
+            ]);
+            // 设为默认收货地址
+            !$user['address_id'] && $this->setDefault((int)$this['address_id']);
+            return true;
+        });
+    }
+
+    /**
+     * 格式化用户上传的省市区数据
+     * @param array $data
+     * @return array
+     * @throws BaseException
+     */
+    private function getRegionId(array $data)
+    {
+        if (!isset($data['region'])) {
+            throwError('省市区不能为空');
+        }
+        if (count($data['region']) != 3) {
+            throwError('省市区数据不合法');
+        }
+        return array_map(function ($item) {
+            return $item['value'];
+        }, $data['region']);
+    }
+
+    /**
+     * 编辑收货地址
+     * @param array $data
+     * @return bool
+     * @throws BaseException
+     */
+    public function edit(array $data)
+    {
+        // 省市区ID
+        list($data['province_id'], $data['city_id'], $data['region_id']) = $this->getRegionId($data);
+        // 更新收货地址
+        return $this->save([
+                'name' => $data['name'],
+                'phone' => $data['phone'],
+                'province_id' => $data['province_id'],
+                'city_id' => $data['city_id'],
+                'region_id' => $data['region_id'],
+                'detail' => $data['detail']
+            ]) !== false;
+    }
+
+    /**
+     * 设为默认收货地址
+     * @param int $addressIid
+     * @return bool
+     * @throws BaseException
+     */
+    public function setDefault(int $addressIid)
+    {
+        // 设为默认地址
+        $userId = UserService::getCurrentLoginUserId();
+        return UserModel::updateBase(['address_id' => $addressIid], ['user_id' => $userId]);
+    }
+
+    /**
+     * 删除收货地址
+     * @return bool
+     * @throws BaseException
+     */
+    public function remove()
+    {
+        // 查询当前是否为默认地址
+        $user = UserService::getCurrentLoginUser(true);
+        // 清空默认地址
+        if ($user['address_id'] == $this['address_id']) {
+            UserModel::updateBase(['address_id' => 0], ['user_id' => $this['user_id']]);
+        }
+        // 标记为已删除
+        return $this->save(['is_delete' => 1]);
+    }
+
+    /**
+     * 收货地址详情
+     * @param int $addressId
+     * @return UserAddress|array|null
+     * @throws BaseException
+     */
+    public static function detail(int $addressId)
+    {
+        $userId = UserService::getCurrentLoginUserId();
+        $detail = self::get(['user_id' => $userId, 'address_id' => $addressId]);
+        if (empty($detail)) {
+            throwError('未找到该收货地址');
+            return false;
+        }
+        return $detail;
+    }
+
+}

+ 206 - 0
app/index/model/UserCoupon.php

@@ -0,0 +1,206 @@
+<?php
+// +----------------------------------------------------------------------
+// | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
+// +----------------------------------------------------------------------
+// | Author: 萤火科技 <admin@yiovo.com>
+// +----------------------------------------------------------------------
+declare (strict_types=1);
+
+namespace app\index\model;
+
+use app\api\service\User as UserService;
+use app\api\model\Coupon as CouponModel;
+use app\common\model\UserCoupon as UserCouponModel;
+use app\common\enum\coupon\CouponType as CouponTypeEnum;
+use app\common\enum\coupon\ApplyRange as ApplyRangeEnum;
+use app\common\library\helper;
+use cores\exception\BaseException;
+
+/**
+ * 用户优惠券模型
+ * Class UserCoupon
+ * @package app\api\model
+ */
+class UserCoupon extends UserCouponModel
+{
+    /**
+     * 获取用户优惠券列表
+     * @param int $userId
+     * @param array $param
+     * @return \think\Paginator
+     * @throws \think\db\exception\DbException
+     */
+    public function getList(int $userId, array $param): \think\Paginator
+    {
+        $filter = $this->getFilter($param);
+        return $this->where($filter)
+            ->where('user_id', '=', $userId)
+            ->paginate();
+    }
+
+    /**
+     * 检索查询条件
+     * @param array $param
+     * @return array
+     */
+    private function getFilter(array $param = []): array
+    {
+        // 设置默认查询参数
+        $params = $this->setQueryDefaultValue($param, [
+            'dataType' => 'all',     // all:全部 isUsable:可用的 isExpire:已过期 isUse:已使用
+            'amount' => null,        // 订单消费金额
+        ]);
+        // 检索列表类型
+        $filter = [];
+        // 可用的优惠券
+        if ($params['dataType'] === 'isUsable') {
+            $filter[] = ['is_use', '=', 0];
+            $filter[] = ['is_expire', '=', 0];
+            $filter[] = ['start_time', '<=', time()];
+            $filter[] = ['end_time', '>', time()];
+        }
+        // 未使用的优惠券
+        if ($params['dataType'] === 'isUnused') {
+            $filter[] = ['is_use', '=', 0];
+            $filter[] = ['is_expire', '=', 0];
+            $filter[] = ['end_time', '>', time()];
+        }
+        // 已过期的优惠券
+        if ($params['dataType'] === 'isExpire') {
+            $filter[] = ['is_expire', '=', 1];
+        }
+        // 已使用的优惠券
+        if ($params['dataType'] === 'isUse') {
+            $filter[] = ['is_use', '=', 1];
+        }
+        // 订单消费金额
+        $params['amount'] > 0 && $filter[] = ['min_price', '<=', $params['amount']];
+        return $filter;
+    }
+
+    /**
+     * 获取用户优惠券总数量(可用)
+     * @param int $userId
+     * @return int
+     */
+    public function getCount(int $userId): int
+    {
+        return $this->where('user_id', '=', $userId)
+            ->where('is_use', '=', 0)
+            ->where('is_expire', '=', 0)
+            ->where('end_time', '>', time())
+            ->count();
+    }
+
+    /**
+     * 获取用户优惠券ID集
+     * @param int $userId
+     * @return array
+     */
+    public function getUserCouponIds(int $userId): array
+    {
+        return $this->where('user_id', '=', $userId)->column('coupon_id');
+    }
+
+    /**
+     * 领取优惠券
+     * @param int $couponId 优惠券ID
+     * @return bool
+     * @throws BaseException
+     */
+    public function receive(int $couponId): bool
+    {
+        // 当前用户ID
+        $userId = UserService::getCurrentLoginUserId(true);
+        // 获取优惠券信息
+        $couponInfo = Coupon::detail($couponId);
+        // 验证优惠券是否可领取
+        if (!$this->checkReceive($userId, $couponInfo)) {
+            return false;
+        }
+        // 添加领取记录
+        return $this->add($userId, $couponInfo);
+    }
+
+    /**
+     * 验证优惠券是否可领取
+     * @param int $userId 当前用户ID
+     * @param CouponModel $couponInfo 优惠券详情
+     * @return bool
+     */
+    private function checkReceive(int $userId, CouponModel $couponInfo): bool
+    {
+        if (empty($couponInfo)) {
+            $this->error = '当前优惠券不存在';
+            return false;
+        }
+        // 验证优惠券状态是否可领取
+        $model = new CouponModel;
+        if (!$model->checkReceive($couponInfo)) {
+            $this->error = $model->getError() ?: '优惠券状态不可领取';
+            return false;
+        }
+        // 验证当前用户是否已领取
+        if (static::checktUserCoupon($couponInfo['coupon_id'], $userId)) {
+            $this->error = '当前用户已领取该优惠券';
+            return false;
+        }
+        return true;
+    }
+
+    /**
+     * 订单结算优惠券列表
+     * @param int $userId 用户id
+     * @param float $orderPayPrice 订单商品总金额
+     * @return array
+     * @throws \think\db\exception\DbException
+     */
+    public static function getUserCouponList(int $userId, float $orderPayPrice): array
+    {
+        // 获取用户可用的优惠券列表
+        $list = (new static)->getList($userId, ['dataType' => 'isUsable', 'amount' => $orderPayPrice]);
+        $data = $list->isEmpty() ? [] : $list->toArray()['data'];
+        foreach ($data as &$item) {
+            // 计算最大能折扣的金额
+            if ($item['coupon_type'] == CouponTypeEnum::DISCOUNT) {
+                $reducePrice = helper::bcmul($orderPayPrice, $item['discount'] / 10);
+                $item['reduced_price'] = helper::bcsub($orderPayPrice, $reducePrice);
+            } else {
+                $item['reduced_price'] = $item['reduce_price'];
+            }
+        }
+        // 根据折扣金额排序并返回
+        return !empty($data) ? array_sort($data, 'reduced_price', true) : [];
+    }
+
+    /**
+     * 判断当前优惠券是否满足订单使用条件
+     * @param array $couponList
+     * @param array $orderGoodsIds 订单商品ID集
+     * @return array
+     */
+    public static function couponListApplyRange(array $couponList, array $orderGoodsIds): array
+    {
+        // 名词解释(is_apply):允许用于抵扣当前订单
+        foreach ($couponList as &$item) {
+            if ($item['apply_range'] == ApplyRangeEnum::ALL) {
+                // 1. 全部商品
+                $item['is_apply'] = true;
+            } elseif ($item['apply_range'] == ApplyRangeEnum::SOME) {
+                // 2. 指定商品, 判断订单商品是否存在可用
+                $applyGoodsIds = array_intersect($item['apply_range_config']['applyGoodsIds'], $orderGoodsIds);
+                $item['is_apply'] = !empty($applyGoodsIds);
+            } elseif ($item['apply_range'] == ApplyRangeEnum::EXCLUDE) {
+                // 2. 排除商品, 判断订单商品是否全部都在排除行列
+                $excludedGoodsIds = array_intersect($item['apply_range_config']['excludedGoodsIds'], $orderGoodsIds);
+                $item['is_apply'] = count($excludedGoodsIds) != count($orderGoodsIds);
+            }
+            !$item['is_apply'] && $item['not_apply_info'] = '该优惠券不支持当前商品';
+        }
+        return $couponList;
+    }
+}

+ 3 - 2
app/index/service/Cart.php

@@ -68,9 +68,10 @@ class Cart extends BaseService
         if (empty($userId))return [];
 
         $model = new CartModel;
-        $model->where('user_id', '=', $userId)
+        $carts = $model->where('user_id', '=', $userId)
             ->where('is_delete', '=', 0)
-            ->select();
+            ->select()->toArray();
+        return array_column($carts,'id');
     }
 
     /**

+ 4 - 4
app/index/service/order/Checkout.php

@@ -12,12 +12,12 @@ declare (strict_types=1);
 
 namespace app\index\service\order;
 
-use app\api\model\Order as OrderModel;
+use app\index\model\Order as OrderModel;
 
 use app\index\model\User as UserModel;
-use app\api\model\Goods as GoodsModel;
-use app\api\model\Setting as SettingModel;
-use app\api\model\UserCoupon as UserCouponModel;
+use app\index\model\Goods as GoodsModel;
+use app\index\model\Setting as SettingModel;
+use app\index\model\UserCoupon as UserCouponModel;
 
 use app\index\service\User as UserService;
 use app\api\service\Payment as PaymentService;