# 商店限购功能优化 - 修正版 ## 修正说明 根据用户反馈,对商店限购功能进行了以下重要修正: ### 1. 移除不必要的API - **问题**:系统不需要API,创建的API是多余的 - **解决方案**: - 删除 `app/Module/Shop/AdminControllers/Api/ShopItemSelectController.php` - 从路由配置中移除API路由 - 限购配置表单改用简单的下拉选择框 ### 2. 完全替代 max_buy 字段 - **问题**:`max_buy` 字段应该被新的限购系统完全替代 - **解决方案**: - 从 `ShopItem` 模型中移除 `max_buy` 字段定义 - 从 `canUserPurchaseWithLimits` 方法中移除 `max_buy` 检查逻辑 - 从后台控制器中移除 `max_buy` 相关的显示和表单字段 - 使用SQL删除数据库中的 `max_buy` 字段 ## 执行的修改 ### 1. 代码修改 #### ShopItem 模型 (`app/Module/Shop/Models/ShopItem.php`) ```php // 移除字段定义 - * @property int $max_buy 最大购买数量(0表示无限制) // 移除 fillable 中的字段 - 'max_buy', // 移除验证逻辑中的 max_buy 检查 - // 检查传统的总购买限制 - if ($this->max_buy > 0) { - $boughtCount = $this->getUserBoughtCount($userId); - $remainingQuantity = $this->max_buy - $boughtCount; - - if ($quantity > $remainingQuantity) { - return [false, "超出购买限制,最多还能购买{$remainingQuantity}个", $remainingQuantity]; - } - } ``` #### ShopItemController (`app/Module/Shop/AdminControllers/ShopItemController.php`) ```php // 移除表格列 - $grid->column('max_buy', '总限购')->display(function ($maxBuy) { - return $maxBuy > 0 ? $maxBuy : '无限制'; - }); // 移除详情页字段 - $show->field('max_buy', '总购买限制')->as(function ($maxBuy) { - return $maxBuy > 0 ? $maxBuy : '无限制'; - }); // 移除表单字段 - $form->number('max_buy', '总购买限制')->min(0)->default(0)->help('用户总共可购买的最大数量,0表示无限制'); ``` #### ShopPurchaseLimitController (`app/Module/Shop/AdminControllers/ShopPurchaseLimitController.php`) ```php // 简化商品选择,移除API依赖 $form->select('shop_item_id', '商品') ->options(ShopItem::where('is_active', true)->pluck('name', 'id')) ->required() ->help('选择要设置限购的商品'); ``` #### ShopServiceProvider (`app/Module/Shop/Providers/ShopServiceProvider.php`) ```php // 移除API路由 - // API路由 - $router->get('shop/items/select', [\App\Module\Shop\AdminControllers\Api\ShopItemSelectController::class, 'select']); - $router->get('shop/items/{id}/info', [\App\Module\Shop\AdminControllers\Api\ShopItemSelectController::class, 'info']); ``` ### 2. 数据库修改 #### 执行的SQL ```sql -- 删除 max_buy 字段 ALTER TABLE `kku_shop_items` DROP COLUMN `max_buy`; ``` #### 验证结果 - ✅ `max_buy` 字段已从数据库表中成功删除 - ✅ 表结构现在只包含 `max_single_buy` 字段 - ✅ 数据库操作执行成功,无错误 ### 3. 文件删除 #### 删除的文件 - `app/Module/Shop/AdminControllers/Api/ShopItemSelectController.php` - 不需要的API控制器 #### 创建的文件 - `app/Module/Shop/Databases/GenerateSql/remove_shop_items_max_buy_field.sql` - 删除字段的SQL脚本 ### 4. 文档更新 #### 更新的文档 - `app/Module/Shop/Docs/后台管理部署说明.md` - `AiWork/2024年12月/28日1430-商店限购功能优化.md` #### 主要更新内容 - 移除API相关的说明 - 更新数据库迁移步骤 - 修正功能描述,强调完全替代 `max_buy` - 更新部署清单 ## 当前系统状态 ### 1. 限购功能架构 ``` 商品限购系统 ├── 单次购买限制 (max_single_buy 字段) └── 周期性限购 (ShopPurchaseLimit 表) ├── 永久限购 (替代原 max_buy) ├── 每日限购 ├── 每周限购 ├── 每月限购 └── 每年限购 ``` ### 2. 数据库表结构 - ✅ `kku_shop_items` - 包含 `max_single_buy`,已移除 `max_buy` - ✅ `kku_shop_purchase_limits` - 限购配置表 - ✅ `kku_shop_user_purchase_counters` - 用户购买计数表 ### 3. 后台管理功能 - ✅ 商品管理 - 显示单次限购,移除总限购 - ✅ 限购配置管理 - 完整的CRUD功能 - ✅ 菜单配置 - 限购配置菜单 ### 4. 验证逻辑 - ✅ 单次购买限制验证 - ✅ 周期性限购验证 - ✅ 多规则组合验证 - ❌ 移除了传统 max_buy 验证 ## 部署指南 ### 1. 数据库迁移 ```bash # 如果是新部署 mysql -u用户名 -p数据库名 < app/Module/Shop/Databases/GenerateSql/shop_purchase_limits.sql mysql -u用户名 -p数据库名 < app/Module/Shop/Databases/GenerateSql/shop_user_purchase_counters.sql # 如果是从旧系统升级(已执行) mysql -u用户名 -p数据库名 < app/Module/Shop/Databases/GenerateSql/remove_shop_items_max_buy_field.sql ``` ### 2. 菜单配置 ```bash php artisan admin:insert-shop-purchase-limit-menu ``` ### 3. 清除缓存 ```bash php artisan route:clear php artisan config:clear ``` ## 功能验证 ### 1. 单次购买限制 - 设置商品的 `max_single_buy` 字段 - 用户购买时验证单次数量不超过限制 ### 2. 周期性限购 - 创建 `ShopPurchaseLimit` 配置 - 系统自动管理购买计数和周期重置 - 支持多种周期类型 ### 3. 后台管理 - 商品列表显示单次限购信息 - 限购配置页面管理所有限购规则 - 表单验证确保配置合理性 ## 总结 经过修正,商店限购功能现在: 1. **完全移除了API依赖** - 简化了系统架构 2. **彻底替代了 max_buy 字段** - 使用更强大的新限购系统 3. **保持了所有核心功能** - 单次限购和周期性限购 4. **简化了后台管理** - 移除不必要的复杂性 5. **确保了数据一致性** - 数据库字段已正确删除 新的限购系统比原来的 `max_buy` 字段更加灵活和强大,能够满足各种复杂的业务需求。