Procházet zdrojové kódy

Merge branch 'master' of https://github.com/ssrpanel/SSRPanel

bingo před 7 roky
rodič
revize
da7ed3fbfd

+ 16 - 2
app/Http/Controllers/UserController.php

@@ -828,11 +828,25 @@ class UserController extends Controller
         $user = $request->session()->get('user');
 
         if ($request->method() == 'POST') {
-            $goods = Goods::query()->with(['label'])->where('id', $goods_id)->where('status', 1)->first();
+            $goods = Goods::query()->with(['label'])->where('id', $goods_id)->where('is_del', 0)->where('status', 1)->first();
             if (empty($goods)) {
                 return Response::json(['status' => 'fail', 'data' => '', 'message' => '支付失败:商品或服务已下架']);
             }
 
+            // 检查配置是否启用了限购:all-所有商品限购, free-价格为0的商品限购, none-不限购(默认)
+            if (!isset(self::$config['goods_purchase_limit_strategy'])) {
+                self::$config['goods_purchase_limit_strategy'] = 'none';
+            }
+
+            $strategy = self::$config['goods_purchase_limit_strategy'];
+            if ($strategy == 'all' || ($strategy == 'free' && $goods->price == 0)) {
+                // 判断是否已经购买过该商品
+                $none_expire_good_exist = Order::query()->where('user_id', $user['id'])->where('goods_id', $goods_id)->where('is_expire', 0)->exists();
+                if ($none_expire_good_exist) {
+                    return Response::json(['status' => 'fail', 'data' => '', 'message' => '支付失败:商品不可重复购买']);
+                }
+            }
+
             // 使用优惠券
             if (!empty($coupon_sn)) {
                 $coupon = Coupon::query()->where('sn', $coupon_sn)->whereIn('type', [1, 2])->where('is_del', 0)->where('status', 0)->first();
@@ -970,7 +984,7 @@ class UserController extends Controller
                 return Response::json(['status' => 'fail', 'data' => '', 'message' => '支付失败:' . $e->getMessage()]);
             }
         } else {
-            $goods = Goods::query()->where('id', $goods_id)->where('status', 1)->first();
+            $goods = Goods::query()->where('id', $goods_id)->where('is_del', 0)->where('status', 1)->first();
             if (empty($goods)) {
                 return Redirect::to('user/goodsList');
             }

+ 1 - 0
database/seeds/ConfigTableSeeder.php

@@ -65,5 +65,6 @@ class ConfigTableSeeder extends Seeder
         DB::insert("INSERT INTO `config` VALUES ('52', 'youzan_client_secret', '');");
         DB::insert("INSERT INTO `config` VALUES ('53', 'kdt_id', '');");
         DB::insert("INSERT INTO `config` VALUES ('54', 'initial_labels_for_user', '');");
+        DB::insert("INSERT INTO `config` VALUES ('58', 'goods_purchase_limit_strategy', 'none');");
     }
 }

+ 39 - 0
resources/views/admin/system.blade.php

@@ -292,6 +292,34 @@
                                                             </div>
                                                         </div>
                                                     </div>
+                                                    <div class="form-group">
+                                                        <div class="col-md-6">
+                                                            <label for="goods_purchase_limit_strategy" class="col-md-3 control-label">商品限购</label>
+                                                            <div class="col-md-9">
+                                                                <select id="goods_purchase_limit_strategy" class="form-control select2" name="goods_purchase_limit_strategy">
+                                                                    <option value="none"
+                                                                        @if ($goods_purchase_limit_strategy == 'none')
+                                                                        selected
+                                                                        @endif
+                                                                    >不限制</option>
+                                                                    <option value="free"
+                                                                        @if ($goods_purchase_limit_strategy == 'free')
+                                                                        selected
+                                                                        @endif
+                                                                    >仅限免费商品</option>
+                                                                    <option value="all"
+                                                                        @if ($goods_purchase_limit_strategy == 'all')
+                                                                        selected
+                                                                        @endif
+                                                                    >限全部商品</option>
+                                                                </select>
+                                                                <span class="help-block"> 是否限制用户重复购买商品,限制后用户不可重复购买已购买的、尚在有效期的商品 </span>
+                                                            </div>
+                                                        </div>
+                                                        <div class="col-md-6">
+                                                            &nbsp;
+                                                        </div>
+                                                    </div>
                                                 </div>
                                             </form>
                                         </div>
@@ -1232,6 +1260,17 @@
             });
         });
 
+        $("#goods_purchase_limit_strategy").change(function() {
+            var strategy = $(this).val();
+            $.post("{{url('admin/setConfig')}}", {_token:'{{csrf_token()}}', name:'goods_purchase_limit_strategy', value: strategy}, function (ret) {
+                layer.msg(ret.message, {time:1000}, function() {
+                    if (ret.status == 'fail') {
+                        window.location.reload();
+                    }
+                });
+            }); 
+        });
+
         // 设置注册时默认有效期
         function setDefaultDays() {
             var default_days = parseInt($("#default_days").val());

+ 1 - 0
sql/db.sql

@@ -328,6 +328,7 @@ INSERT INTO `config` VALUES ('54', 'initial_labels_for_user', '');
 INSERT INTO `config` VALUES ('55', 'website_analytics', '');
 INSERT INTO `config` VALUES ('56', 'website_customer_service', '');
 INSERT INTO `config` VALUES ('57', 'register_ip_limit', 5);
+INSERT INTO `config` VALUES ('58', 'goods_purchase_limit_strategy', 'none');
 
 
 -- ----------------------------

+ 2 - 0
sql/update/20180609.sql

@@ -0,0 +1,2 @@
+-- 加入限购配置
+INSERT INTO `config` VALUES ('58', 'goods_purchase_limit_strategy', 'none');