You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							235 lines
						
					
					
						
							6.3 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							235 lines
						
					
					
						
							6.3 KiB
						
					
					
				
								<?php
							 | 
						|
								
							 | 
						|
								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;
							 | 
						|
								use Illuminate\Support\Facades\DB;
							 | 
						|
								
							 | 
						|
								/**
							 | 
						|
								 * 订单
							 | 
						|
								 * Class OrderController
							 | 
						|
								 * @package App\Http\Controllers\Api
							 | 
						|
								 */
							 | 
						|
								class OrderController extends Controller
							 | 
						|
								{
							 | 
						|
									//订单列表
							 | 
						|
								    public function index(Request $request)
							 | 
						|
									{
							 | 
						|
										$formData = $request->only(['page', 'status']);
							 | 
						|
										$request->validate([
							 | 
						|
											'page' => 'regex:/^\d+$/',
							 | 
						|
											'status' => 'nullable|integer'
							 | 
						|
										], [
							 | 
						|
											'page.regex' => '页码错误',
							 | 
						|
											'status.integer' => '订单状态错误'
							 | 
						|
										]);
							 | 
						|
								
							 | 
						|
										if (isset($formData['status'])) {
							 | 
						|
											$where['status'] = $formData['status'];
							 | 
						|
										}
							 | 
						|
										$where['user_id'] = $this->user_id;
							 | 
						|
								
							 | 
						|
										$order_list = Order::where($where)
							 | 
						|
											->with('product:id,title,pictures')
							 | 
						|
											->select('id', 'product_id', 'price', 'num', 'status', 'created_at')
							 | 
						|
											->orderBy('id', 'DESC')
							 | 
						|
											->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']);
							 | 
						|
										$formData = array_map(fn($v) => trim($v), $formData); //过滤,删除首尾空
							 | 
						|
								
							 | 
						|
										//表单验证
							 | 
						|
										$request->validate([
							 | 
						|
											'id' => ['required', 'regex:/^\d+$/'],
							 | 
						|
											'name' => ['required', 'between:2,20'],
							 | 
						|
											'mobile' => ['required', 'regex:/^1[3-9]\d{9}$/'],
							 | 
						|
											'pay_type' => ['required', 'in:0,1,2'],
							 | 
						|
											'num' => ['required', 'min:1'],
							 | 
						|
										], [
							 | 
						|
											'id.required' => '未指定产品ID',
							 | 
						|
											'name.required' => '请输入联系人姓名',
							 | 
						|
											'mobile.required' => '请输入联系手机号',
							 | 
						|
											'id.regex' => '产品ID错误',
							 | 
						|
											'name.between' => '联系人姓名在2~20字符之间',
							 | 
						|
											'mobile.regex' => '请输入11位手机号',
							 | 
						|
											'pay_type.required' => '请选择支付方式',
							 | 
						|
											'pay_type.in' => '不存在此支付方式',
							 | 
						|
											'num.required' => '请输入购买数量',
							 | 
						|
											'num.min' => '购买数量输入错误',
							 | 
						|
										]);
							 | 
						|
								
							 | 
						|
										$ap = AgentProduct::query()
							 | 
						|
											->where('id', $formData['id'])
							 | 
						|
											->with('coupon')
							 | 
						|
											->with('product')
							 | 
						|
											->first();
							 | 
						|
										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($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([
							 | 
						|
												'user_id' => $this->user_id,
							 | 
						|
												'order_no' => $this->getOrderNo(),
							 | 
						|
												'num' => $formData['num'],
							 | 
						|
												'price' => $price,
							 | 
						|
												'name' => $formData['name'],
							 | 
						|
												'mobile' => $formData['mobile'],
							 | 
						|
												'title' => $title,
							 | 
						|
												'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),
							 | 
						|
											]);
							 | 
						|
								
							 | 
						|
											//资金流水
							 | 
						|
											UserMoneyLog::query()->create([
							 | 
						|
												'user_id' => $this->user_id,
							 | 
						|
												'agent_id' => $this->agent_id,
							 | 
						|
												'money' => -$price,
							 | 
						|
												'order_id' => $order->id,
							 | 
						|
												'type' => 1,
							 | 
						|
												'desc' => '购买产品:' . $title,
							 | 
						|
												'created_at' => time(), //已关闭模型自动写入时间
							 | 
						|
											]);
							 | 
						|
								
							 | 
						|
											DB::commit();
							 | 
						|
										} catch (\Exception $e) {
							 | 
						|
											DB::rollBack();
							 | 
						|
											return $e->getMessage();
							 | 
						|
										}
							 | 
						|
										return $this->success();
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									//获取应付金额
							 | 
						|
									public function getPrice(Request $request)
							 | 
						|
									{
							 | 
						|
										$id = (int)$request->input('id');
							 | 
						|
								
							 | 
						|
										$num = (float)$request->input('num');
							 | 
						|
										if (!$num || $num < 1) {
							 | 
						|
											return $this->error('未指定产品数量');
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$agent_product = AgentProduct::query()
							 | 
						|
											->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
							 | 
						|
									 * @param $num
							 | 
						|
									 * @return float
							 | 
						|
									 */
							 | 
						|
									private function calc($agent_product, $num)
							 | 
						|
									{
							 | 
						|
										$total_price = $agent_product->price * $num;
							 | 
						|
										//没有任何优惠券时直接返回最终价
							 | 
						|
										if ($agent_product->coupon->isEmpty()) {
							 | 
						|
											return $total_price;
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$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'];
							 | 
						|
												}
							 | 
						|
											} else if ($v['type'] == 2 && !empty($v['detail']['discount'])) { //打折
							 | 
						|
												$total_price *= $v['detail']['discount'];
							 | 
						|
											}
							 | 
						|
										}
							 | 
						|
										return round($total_price, 2);
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									// 生成订单号
							 | 
						|
									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(1000, 9999);
							 | 
						|
									}
							 | 
						|
								}
							 |