diff --git a/app/Http/Controllers/Api/AgentProductController.php b/app/Http/Controllers/Api/AgentProductController.php index 4390758..1af06c3 100644 --- a/app/Http/Controllers/Api/AgentProductController.php +++ b/app/Http/Controllers/Api/AgentProductController.php @@ -135,14 +135,20 @@ class AgentProductController extends Controller //分页列表产品图片加域名 private function paginatePicAddHost($list) { - if (!$list->isEmpty()) { - $prefix = Storage::disk('public')->url(''); - foreach ($list->items() as $k=>&$v) { - $v->pictures = array_map(function($item) use ($prefix) { - return strpos($item, $prefix) === false ? $prefix . $item : $item; - }, $v->pictures); + //如果是新入驻代理商没有数据,则显示最早的几条 + if ($list->isEmpty()) { + if (AgentProduct::list($this->agent_id)->count() === 0) { + $list = AgentProduct::list($this->agent_id)->orWhereRaw(1)->orderBy('id')->simplePaginate(); } } + + $prefix = Storage::disk('public')->url(''); + foreach ($list->items() as $k=>&$v) { + $v->pictures = array_map(function($item) use ($prefix) { + return strpos($item, $prefix) === false ? $prefix . $item : $item; + }, $v->pictures); + } + return $list; } diff --git a/app/Http/Controllers/Api/OrderController.php b/app/Http/Controllers/Api/OrderController.php index 5a65231..fce639f 100644 --- a/app/Http/Controllers/Api/OrderController.php +++ b/app/Http/Controllers/Api/OrderController.php @@ -132,10 +132,14 @@ class OrderController extends Controller ]); $ap = AgentProduct::query() - ->where(['id' => $formData['id'], 'status' => ProductStatus::ON_SALE]) - ->where('stock', '>=', $formData['num']) ->with(['coupon', 'product', 'agentCloudProduct:id,price']) - ->has('product') + ->where('stock', '>=', $formData['num']) + ->where(['id' => $formData['id'], 'status' => ProductStatus::ON_SALE, 'agent_id' => $this->agent_id]) //判断agent_id,防止新入驻小程序的演示产品被下单 + ->whereDoesntHave('agentProductItem', function ($query) { + return $query->whereHas('product', function ($query) { + return $query->where('stock', '<=', 0)->orWhere('status', '<>', ProductStatus::ON_SALE); + }); + }) ->first(); if (!$ap || !$ap->product) { return $this->error('产品已下架或库存不足'); diff --git a/app/Models/AgentProduct.php b/app/Models/AgentProduct.php index b35ab09..04c9464 100644 --- a/app/Models/AgentProduct.php +++ b/app/Models/AgentProduct.php @@ -97,7 +97,7 @@ class AgentProduct extends BaseModel return $query->where('stock', '<=', 0)->orWhere('status', '<>', ProductStatus::ON_SALE); }); }) - ->where(['agent_id' => $agent_id, 'status' => ProductStatus::ON_SALE])->where('stock', '>', 0) + ->where('stock', '>', 0)->where(['agent_id' => $agent_id, 'status' => ProductStatus::ON_SALE]) ->select('id', 'sale', 'product_id', 'price', 'original_price', 'title', 'pictures'); }