| 
						
						
							
								
							
						
						
					 | 
				
				 | 
				
					@ -4,6 +4,7 @@ namespace App\Http\Controllers\Api; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					use App\Http\Controllers\Controller; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					use App\Models\AgentProduct; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					use App\Models\Coupon; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					use App\Models\UserMoneyLog; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					use App\Models\Order; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					use Illuminate\Http\Request; | 
				
			
			
		
	
	
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
				
				 | 
				
					@ -22,25 +23,48 @@ class OrderController extends Controller | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$formData = $request->only(['page', 'status']); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$request->validate([ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								'page' => 'regex:/^\d+$/', | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								'status' => 'integer' | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								'status' => 'nullable|integer' | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							], [ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								'page.regex' => '页码错误', | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								'status.integer' => '未指定订单状态' | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								'status.integer' => '订单状态错误' | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							]); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							if (array_key_exists('status', $formData) && $formData['status'] !== null) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							if (isset($formData['status'])) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								$where['status'] = $formData['status']; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$where['user_id'] = $this->user_id; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$list = Order::where($where) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								->with(['product' => fn($query) => $query->select('id', 'title', 'pictures')]) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								->select('id', 'product_id', 'price', 'num', 'created_at') | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$order_list = Order::where($where) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								->with('product:id,title,pictures') | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								->select('id', 'product_id', 'price', 'num', 'status', 'created_at') | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								->orderBy('id', 'DESC') | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								->simplePaginate(15); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							return $this->success($list); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								->simplePaginate(15) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								->toArray(); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$time = time(); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$timeout_ids = []; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							//10分钟内未付款订单提示付款
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							foreach ($order_list['data'] as &$v) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								if ($v['status'] == 0) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									$minute = $time - $v['created_at']; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									//订单创建后10分钟内未付款则提示,否则取消订单
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									if ($minute < 600) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
										$v['status_text'] = '请在' . ceil($minute / 60) . '分钟内付款'; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									} else { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
										$timeout_ids[] = $v['id']; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
										$v['status'] = -1; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
										$v['status_text'] = '已取消'; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							//超时订单设置为已取消
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							Order::query()->whereIn('id', $timeout_ids)->update(['status' => -1]); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							return $this->success($order_list); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						//提交订单
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						public function create(Request $request) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						{ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$formData = $request->only(['id', 'name', 'mobile', 'pay_type', 'num']); | 
				
			
			
		
	
	
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
				
				 | 
				
					@ -66,22 +90,36 @@ class OrderController extends Controller | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								'num.min' => '购买数量输入错误', | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							]); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$agent_product = AgentProduct::query() | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$ap = AgentProduct::query() | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								->where('id', $formData['id']) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								->with('coupon') | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								->with('product') | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								->first(); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							if (!$agent_product || !$agent_product->product) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							if (!$ap || !$ap->product) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								$this->error('产品不存在'); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							if ($ap->product->stock < $formData['num']) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								$this->error('对不起,库存不足'); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$coupon_ids = []; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							if ($ap->coupon) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								foreach ($ap->coupon as $v) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									$coupon_ids[] = $v['id']; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							DB::beginTransaction(); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							try { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								$price = $this->calc($agent_product, $formData['num']); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								$title = $agent_product->product->title; //产品标题
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								//增加销量
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								$agent_product->increment('sale', $formData['num']); //代理商产品表销量
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								$agent_product->product->increment('sale', $formData['num']); //供应商产品表
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								$price = $this->calc($ap, $formData['num']); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								$title = $ap->product->title; //产品标题
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								//代理商产品表增加销量
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								$ap->increment('sale', $formData['num']); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								//供应商产品表加销量、减库存
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								$ap->product->sale += $formData['num']; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								$ap->product->stock -= $formData['num']; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								$ap->product->save(); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								// 存入订单表
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								$order = Order::query()->create([ | 
				
			
			
		
	
	
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
				
				 | 
				
					@ -92,10 +130,12 @@ class OrderController extends Controller | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									'name' => $formData['name'], | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									'mobile' => $formData['mobile'], | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									'title' => $title, | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									'agent_product_id' => $agent_product->id, | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									'product_id' => $agent_product->product_id, | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									'picture' => $ap->product->picture, | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									'agent_product_id' => $ap->id, | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									'product_id' => $ap->product_id, | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									'status' => 0, | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									'pay_type' => $formData['pay_type'], | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									'coupon_id' => join(',', $coupon_ids), | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								]); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								//资金流水
 | 
				
			
			
		
	
	
		
			
				
					| 
						
							
								
							
						
						
							
								
							
						
						
					 | 
				
				 | 
				
					@ -128,12 +168,35 @@ class OrderController extends Controller | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$agent_product = AgentProduct::query() | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								->with(['coupon' => fn($query) => $query->select('*')]) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								->with('coupon:agent_product_id,type,detail,agent_id,start_at,end_at') | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								->find($id); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$final_price = $this->calc($agent_product, $num); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							return $this->success(['price' => $final_price]); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						//订单详情
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						public function show() | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						{ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$id = (int)request()->input('id'); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$fields = ['id', 'order_no', 'agent_product_id', 'num', 'price', 'title', | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								'picture', 'status', 'pay_type', 'coupon_id', 'paid_at', 'created_at']; | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$order = Order::query() | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								->where('user_id', $this->user_id) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								->find($id, $fields); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							if (!$order) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								return $this->error('订单不存在'); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$order->coupon = Coupon::query() | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								->whereIn('id', $order->coupon_id) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								->where(['agent_id' => $this->agent_id, 'agent_product_id' => $order->agent_product_id,]) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								->get(['tag']); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							return $this->success($order); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						/** | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						 * 计算最终价格(扣除优惠券之后的价格) | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						 * @param $agent_product | 
				
			
			
		
	
	
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
				
				 | 
				
					@ -150,6 +213,7 @@ class OrderController extends Controller | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$coupon = $agent_product->coupon->toArray(); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							foreach ($coupon as $v) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								// TODO 未判断优惠券有效期
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
								if ($v['type'] == 1 && !empty($v['detail']['full']) && !empty($v['detail']['reduction'])) { //满减
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
									if ($total_price >= $v['detail']['full']) { | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
										$total_price -= $v['detail']['reduction']; | 
				
			
			
		
	
	
		
			
				
					| 
						
						
						
							
								
							
						
					 | 
				
				 | 
				
					@ -162,10 +226,10 @@ class OrderController extends Controller | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						// 生成订单号
 | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						private function getOrderNo() | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						private function getOrderNo(): string | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						{ | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							list($micro, $sec) = explode(' ', microtime()); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							$micro = str_pad(floor($micro * 1000000), 6, 0, STR_PAD_LEFT); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							return date('YmdHis', $sec) . $micro . mt_rand(100, 999); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
							return date('YmdHis', $sec) . $micro . mt_rand(1000, 9999); | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
						} | 
				
			
			
		
	
		
			
				
					 | 
					 | 
				
				 | 
				
					} |