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.
		
		
		
		
		
			
		
			
				
					
					
						
							464 lines
						
					
					
						
							15 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							464 lines
						
					
					
						
							15 KiB
						
					
					
				
								<?php
							 | 
						|
								
							 | 
						|
								namespace App\Http\Controllers\Api;
							 | 
						|
								
							 | 
						|
								use App\Common\PayType;
							 | 
						|
								use App\Http\Controllers\Controller;
							 | 
						|
								use App\Models\Agent;
							 | 
						|
								use App\Models\AgentProduct;
							 | 
						|
								use App\Models\AgentSetting;
							 | 
						|
								use App\Models\Coupon;
							 | 
						|
								use App\Models\OrderProductItem;
							 | 
						|
								use App\Models\Product;
							 | 
						|
								use App\Models\User;
							 | 
						|
								use App\Models\Order;
							 | 
						|
								use EasyWeChat\Factory;
							 | 
						|
								use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
							 | 
						|
								use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
							 | 
						|
								use GuzzleHttp\Exception\GuzzleException;
							 | 
						|
								use Illuminate\Database\Eloquent\Model;
							 | 
						|
								use Illuminate\Http\Request;
							 | 
						|
								use Illuminate\Support\Facades\DB;
							 | 
						|
								use App\Common\OrderStatus as Status;
							 | 
						|
								use Illuminate\Support\Facades\Storage;
							 | 
						|
								
							 | 
						|
								/**
							 | 
						|
								 * 订单
							 | 
						|
								 * Class OrderController
							 | 
						|
								 * @package App\Http\Controllers\Api
							 | 
						|
								 */
							 | 
						|
								class OrderController extends Controller
							 | 
						|
								{
							 | 
						|
									private $earnest = 200; //定金和首款默认金额 TODO 定金支付和首款支付暂定为200
							 | 
						|
								
							 | 
						|
									//订单列表
							 | 
						|
								    public function index(Request $request)
							 | 
						|
									{
							 | 
						|
										$formData = $request->only(['page', 'status']);
							 | 
						|
										$request->validate([
							 | 
						|
											'page' => 'regex:/^\d+$/',
							 | 
						|
											'status' => 'nullable|regex:/^\d+(,\d+)*$/'
							 | 
						|
										], [
							 | 
						|
											'page.regex' => '页码错误',
							 | 
						|
											'status.regex' => '订单状态错误'
							 | 
						|
										]);
							 | 
						|
								
							 | 
						|
										$order_list = Order::where('user_id', $this->user_id);
							 | 
						|
										if (isset($formData['status'])) {
							 | 
						|
											if (preg_match('/^\d+$/', $formData['status'])) {
							 | 
						|
												$order_list = $order_list->where('status', $formData['status']);
							 | 
						|
											} else {
							 | 
						|
												$order_list = $order_list->whereIn('status', explode(',', $formData['status']));
							 | 
						|
											}
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$order_list = $order_list->select('id', 'agent_product_id', 'product_id', 'title', 'picture', 'price', 'num', 'status', 'timeout', 'created_at')
							 | 
						|
											->orderBy('id', 'DESC')
							 | 
						|
											->simplePaginate(15)
							 | 
						|
											->toArray();
							 | 
						|
								
							 | 
						|
										$time = time();
							 | 
						|
										$timeout_ids = [];
							 | 
						|
										$prefix = Storage::disk('public')->url('');
							 | 
						|
										foreach ($order_list['data'] as &$v) {
							 | 
						|
											//图片加上域名
							 | 
						|
											if (strpos($v['picture'], $prefix) === false) {
							 | 
						|
												$v['picture'] = $prefix . $v['picture'];
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											if (strpos($v['picture'], $prefix) === false) {
							 | 
						|
												$v['picture'] = $prefix . $v['picture'];
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											if (!empty($v['pictures']) && is_array($v['pictures'])) {
							 | 
						|
												$v['pictures'] = array_map(function($item) use ($prefix) {
							 | 
						|
													return strpos($item, $prefix) === false ? $prefix . $item : $item;
							 | 
						|
												}, $v['pictures']);
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											//未付款订单提示剩余付款时间
							 | 
						|
											if ($v['status'] == Status::UNPAID) {
							 | 
						|
												$second = strtotime($v['timeout']) - $time;
							 | 
						|
								
							 | 
						|
												if ($second > 0) {
							 | 
						|
													$v['status_text'] = '请在' . ceil($second / 60) . '分钟内付款';
							 | 
						|
												} /*else { //TODO 此部分由定时
							 | 
						|
													$timeout_ids[] = $v['id'];
							 | 
						|
													$v['status'] = Status::CANCEL;
							 | 
						|
													$v['status_text'] = '已取消';
							 | 
						|
													//TODO 加回库存,未考虑到几天/几个月后再打开订单列表页的情况,需要定时任务处理
							 | 
						|
													Product::query()->find($v['product_id'])->increment('stock', $v['num']);
							 | 
						|
												}*/
							 | 
						|
											}
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										//超时订单设置为已取消 TODO 测试阶段暂时注释
							 | 
						|
										if ($timeout_ids) {
							 | 
						|
											Order::query()->whereIn('id', $timeout_ids)->update(['status' => Status::CANCEL]);
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										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); //过滤,删除首尾空
							 | 
						|
								
							 | 
						|
										//表单验证
							 | 
						|
										$pay_type_values = join(',', array_keys(PayType::array()));
							 | 
						|
										$request->validate([
							 | 
						|
											'id' => ['required', 'regex:/^\d+$/'],
							 | 
						|
											'name' => ['required', 'between:2,20'],
							 | 
						|
											'mobile' => ['required', 'regex:/^1[3-9]\d{9}$/'],
							 | 
						|
											'pay_type' => ['required', 'in:' . $pay_type_values],
							 | 
						|
											'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')
							 | 
						|
											->has('product')
							 | 
						|
											->first();
							 | 
						|
										if (!$ap || !$ap->product) {
							 | 
						|
											$this->error('产品不存在或已下架');
							 | 
						|
										}
							 | 
						|
										//TODO 供应商产品需要判断组合产品库存
							 | 
						|
										if ($ap->stock < $formData['num'] || $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->price, $formData['num'], $formData['pay_type'], $ap);
							 | 
						|
											$title = $ap->title; //产品标题
							 | 
						|
								
							 | 
						|
											//供应商产品表减库存
							 | 
						|
											$product_ids = explode(',', $ap->product_ids);
							 | 
						|
											$affect_row = Product::query()
							 | 
						|
												->where('stock', '>=', $formData['num']) //乐观锁
							 | 
						|
												->whereIn('id', $product_ids)
							 | 
						|
												->decrement('stock', $formData['num']);
							 | 
						|
											if ($affect_row != count($product_ids)) {
							 | 
						|
												throw new \Exception('供应产品库存不足');
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											//代理商产品表减库存
							 | 
						|
											$affect_row = AgentProduct::query()
							 | 
						|
												->where('stock', '>=', $formData['num']) //乐观锁
							 | 
						|
												->where('id', $ap->id)
							 | 
						|
												->decrement('stock', $formData['num']);
							 | 
						|
											if (!$affect_row) {
							 | 
						|
												throw new \Exception('商户产品库存不足');
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											if ($formData['pay_type'] == PayType::DEPOSIT_PAY && $ap->deposit_timeout > 0) {
							 | 
						|
												$timeout = date('Y-m-d H:i:s', time() + $ap->deposit_timeout * 60);
							 | 
						|
											} else if ($formData['pay_type'] == PayType::EARNEST_PAY && $ap->earnest_timeout > 0) {
							 | 
						|
												$timeout = date('Y-m-d H:i:s', time() + $ap->earnest_timeout * 60);
							 | 
						|
											} else {
							 | 
						|
												$order_timeout = AgentSetting::val($this->agent_id, 'order_timeout') ?? 60;
							 | 
						|
												$timeout = date('Y-m-d H:i:s', time() + $ap->earnest_timeout * $order_timeout);
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											// 存入订单表
							 | 
						|
											$order = Order::query()->create([
							 | 
						|
												'user_id' => $this->user_id,
							 | 
						|
												'agent_id' => $this->agent_id,
							 | 
						|
												'order_no' => $this->getOrderNo(),
							 | 
						|
												'num' => $formData['num'],
							 | 
						|
												'price' => $price,
							 | 
						|
												'name' => $formData['name'],
							 | 
						|
												'mobile' => $formData['mobile'],
							 | 
						|
												'title' => $title,
							 | 
						|
												'picture' => $ap->picture,
							 | 
						|
												'agent_product_id' => $ap->id,
							 | 
						|
												'product_id' => $ap->product_id,
							 | 
						|
												'product_ids' => $ap->product->product_ids ?? $ap->product_id,
							 | 
						|
												'status' => $formData['pay_type'] == PayType::OFFLINE ? Status::OFFLINE_UNPAID : Status::UNPAID,
							 | 
						|
												'pay_type' => $formData['pay_type'],
							 | 
						|
												'coupon_id' => join(',', $coupon_ids),
							 | 
						|
												'guide_id' => $ap->guide_id,
							 | 
						|
												'timeout' => $timeout,
							 | 
						|
											]);
							 | 
						|
								
							 | 
						|
											//存入订单产品表
							 | 
						|
											$supplier_product_info = Product::whereIn('id', $product_ids)
							 | 
						|
												->orderBy('id')->get(['id AS product_id', 'supplier_id'])->toArray();
							 | 
						|
								
							 | 
						|
											$order_id = $order->id;
							 | 
						|
											$agent_id = $this->agent_id;
							 | 
						|
											$agent_product_id = $ap->id;
							 | 
						|
								
							 | 
						|
											$supplier_product_info = array_map(function ($v) use($order_id, $agent_id, $agent_product_id) {
							 | 
						|
												$v['order_id'] = $order_id;
							 | 
						|
												$v['agent_id'] = $agent_id;
							 | 
						|
												$v['agent_product_id'] = $agent_product_id;
							 | 
						|
												return $v;
							 | 
						|
											}, $supplier_product_info);
							 | 
						|
											OrderProductItem::insert($supplier_product_info);
							 | 
						|
								
							 | 
						|
											DB::commit();
							 | 
						|
										} catch (\Exception $e) {
							 | 
						|
											DB::rollBack();
							 | 
						|
											return $this->error($e->getMessage());
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										if ($formData['pay_type'] == PayType::OFFLINE) { //线下支付
							 | 
						|
											return $this->success('操作成功,请及时联系客服付款');
							 | 
						|
										} else { //在线支付或定金支付
							 | 
						|
											$config = $this->payConfig($order, $price);
							 | 
						|
											if (!empty($config['paySign'])) {
							 | 
						|
												return $this->success($config);
							 | 
						|
											} else {
							 | 
						|
												return $this->error($config['err_code_des'] ?? join(',', $config));
							 | 
						|
											}
							 | 
						|
										}
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									//申请退款
							 | 
						|
									public function refund(Request $request)
							 | 
						|
									{
							 | 
						|
										$formData = $request->only(['id', 'desc', 'pictures']);
							 | 
						|
								
							 | 
						|
										$request->validate([
							 | 
						|
											'id' => 'required|integer',
							 | 
						|
											'desc' => 'required|string',
							 | 
						|
											'pictures' => 'nullable|array',
							 | 
						|
										], [
							 | 
						|
											'*.required' => '内容输入不完整',
							 | 
						|
											'pictures.array' => '图片必须是数组',
							 | 
						|
										]);
							 | 
						|
								
							 | 
						|
										//去掉图片地址前的域名
							 | 
						|
										$prefix = Storage::disk('public')->url('');
							 | 
						|
										foreach ($formData['pictures'] as &$v) {
							 | 
						|
											$v = str_replace($prefix, '', $v);
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										//TODO 需要后台处理,然后向微信发起退款申请
							 | 
						|
										$order = Order::firstWhere(['id' => $formData['id'], 'user_id' => $this->user_id]);
							 | 
						|
										if (!$order) {
							 | 
						|
											return $this->error('订单不存在');
							 | 
						|
										}
							 | 
						|
										//订金/定金/首付款不允许退款,只有付全款才能申请退款
							 | 
						|
										if (!in_array($order->status, [Status::PAID, Status::PAID_RETAINAGE])) {
							 | 
						|
											return $this->error('当前订单状态不允许退款');
							 | 
						|
										}
							 | 
						|
										$order->refund_info = [
							 | 
						|
											'desc' => strip_tags($formData['desc']),
							 | 
						|
											'refund_no' => $this->getOrderNo(), //退款单号
							 | 
						|
											'pictures' => $formData['pictures'] ?? [],
							 | 
						|
											'old_status' => $order->status,
							 | 
						|
										];
							 | 
						|
										$order->status = Status::REFUNDING;
							 | 
						|
										$order->save();
							 | 
						|
								
							 | 
						|
										return $this->success();
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									//获取应付金额及相关产品信息
							 | 
						|
									public function getPrice(Request $request)
							 | 
						|
									{
							 | 
						|
										$formData = $request->only(['id', 'num', 'pay_type']);
							 | 
						|
										$request->validate([
							 | 
						|
											'id' => 'required|integer',
							 | 
						|
											'num' => 'required|integer',
							 | 
						|
											'pay_type' => 'required|integer',
							 | 
						|
										], [
							 | 
						|
											'*.required' => '参数缺失',
							 | 
						|
											'*.integer' => '参数类型错误',
							 | 
						|
										]);
							 | 
						|
								
							 | 
						|
										if (!$formData['num'] || $formData['num'] < 1) {
							 | 
						|
											return $this->error('未指定产品数量');
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$ap = AgentProduct::query()
							 | 
						|
											->has('product')
							 | 
						|
											->with('coupon:agent_product_id,type,detail,agent_id,tag,start_at,end_at')
							 | 
						|
											->find($formData['id'], ['id', 'price', 'original_price', 'product_id', 'title', 'pictures', 'earnest', 'earnest_timeout', 'deposit', 'deposit_timeout']);
							 | 
						|
								
							 | 
						|
										if (!$ap) {
							 | 
						|
											return $this->error('产品信息不存在');
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$prefix = Storage::disk('public')->url('');
							 | 
						|
										$ap->pictures = array_map(fn($v) => $prefix . $v, $ap->pictures);
							 | 
						|
								
							 | 
						|
										//如果是线下支付,显示的价格跟在线全款支付价格一样
							 | 
						|
										if ($formData['pay_type'] == PayType::OFFLINE) {
							 | 
						|
											$formData['pay_type'] = PayType::ONLINE;
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$ap->final_price = $this->calc($ap->price, $formData['num'], $formData['pay_type'], $ap);
							 | 
						|
										$ap->num = $formData['num'];
							 | 
						|
										return $this->success($ap);
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									//订单支付(在订单列表发起)
							 | 
						|
									public function pay(Request $request)
							 | 
						|
									{
							 | 
						|
										$id = (int)request()->input('id');
							 | 
						|
								
							 | 
						|
										//订单信息
							 | 
						|
										$order = Order::query()
							 | 
						|
											->with('agentProduct')
							 | 
						|
											->where(['user_id' => $this->user_id, 'agent_id' => $this->agent_id])
							 | 
						|
											->whereIn('status', [Status::UNPAID, Status::PAY_EARNEST])
							 | 
						|
											->find($id);
							 | 
						|
										if (!$order) {
							 | 
						|
											return $this->error('订单不存在或已支付');
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$ap = AgentProduct::with('coupon')->find($order->agent_product_id);
							 | 
						|
								
							 | 
						|
										//如果已经付定金或首付款,则仅支付尾款
							 | 
						|
										if ($order->status == Status::PAY_EARNEST) {
							 | 
						|
											$price = $order->price - $order->paid_money;
							 | 
						|
										} else {
							 | 
						|
											$price = $this->calc($order->price, $order->num, $order->pay_type, $ap);
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$config = $this->payConfig($order, $price);
							 | 
						|
										if (!empty($config['paySign'])) {
							 | 
						|
											return $this->success($config);
							 | 
						|
										} else {
							 | 
						|
											return $this->error($config['err_code_des'] ?? join(',', $config));
							 | 
						|
										}
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									//获取支付配置信息
							 | 
						|
									private function payConfig($order, $price)
							 | 
						|
									{
							 | 
						|
										//用户openid
							 | 
						|
										$openid = User::query()->where('id', $this->user_id)->value('openid'); //此处要用where,value()用find有BUG
							 | 
						|
								
							 | 
						|
										//代理商信息
							 | 
						|
										$agent = Agent::query()->find($this->agent_id);
							 | 
						|
								
							 | 
						|
										$config = config('wechat.payment.default');
							 | 
						|
										$config = array_merge($config, [
							 | 
						|
											'app_id' => $agent->appid,
							 | 
						|
											'mch_id' => $agent->mchid,
							 | 
						|
											'key' => $agent->mchkey,
							 | 
						|
										]);
							 | 
						|
								
							 | 
						|
										$app = Factory::payment($config);
							 | 
						|
										try {
							 | 
						|
											$result = $app->order->unify([
							 | 
						|
												'body' => $order->title,
							 | 
						|
												'out_trade_no' => $order->order_no . '-' . $order->status, //后面加status,主要是为了方便微信支付回调时区分定金(首付款)和尾款支付
							 | 
						|
												'total_fee' => 1, //TODO 测试暂时注释 round($price * 100), //支付金额单位为分
							 | 
						|
												'notify_url' => route('wxpay_notify', ['agent_id' => $this->agent_id]), // 支付结果通知网址,如果不设置则会使用配置里的默认地址
							 | 
						|
												'trade_type' => 'JSAPI',
							 | 
						|
												'openid' => $openid,
							 | 
						|
												'profit_sharing' => 'Y', //Y分账,N不分账,默认不分账,Y大写
							 | 
						|
											]);
							 | 
						|
										} catch (InvalidArgumentException | InvalidConfigException | GuzzleException $e) {
							 | 
						|
											return ['error' => $e->getMessage(), 'line' => $e->getLine()];
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										if (empty($result['prepay_id'])) {
							 | 
						|
											return $result;
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$jssdk = $app->jssdk;
							 | 
						|
										return $jssdk->bridgeConfig($result['prepay_id'], false) + ['id' => $order->id, 'order_no' => $order->order_no]; // 返回数组
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									//订单详情
							 | 
						|
									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_money', 'paid_at', 'refund_info', 'verify_code', 'created_at'];
							 | 
						|
										$order = Order::query()
							 | 
						|
											->where('user_id', $this->user_id)
							 | 
						|
											->find($id, $fields);
							 | 
						|
								
							 | 
						|
										if (!$order) {
							 | 
						|
											return $this->error('订单不存在');
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										//订单ID和核销码拼接,查询时通过订单ID和核销码来查询,这样核销码不用建索引
							 | 
						|
										$order->verify_code = $order->verify_code ? $order->id . '-' . $order->verify_code : '';
							 | 
						|
								
							 | 
						|
										$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);
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									/**
							 | 
						|
									 * 计算最终价格(扣除优惠券之后的价格)
							 | 
						|
									 * $price:原价;$coupon:优惠券;$num:产品数量;$pay_type:支付方式
							 | 
						|
									 * @param float $price
							 | 
						|
									 * @param int $num
							 | 
						|
									 * @param int $pay_type
							 | 
						|
									 * @param Model $agent_product
							 | 
						|
									 * @return float
							 | 
						|
									 */
							 | 
						|
									private function calc($price, $num, $pay_type, $agent_product)
							 | 
						|
									{
							 | 
						|
										//根据支付方式计算价格
							 | 
						|
										if (in_array($pay_type, [PayType::DEPOSIT_PAY, PayType::EARNEST_PAY, PayType::DOWN_PAYMENT])) {
							 | 
						|
											if ($pay_type == PayType::DEPOSIT_PAY && $agent_product->deposit && $agent_product->deposit_timeout) {
							 | 
						|
												return $agent_product->deposit;
							 | 
						|
											}
							 | 
						|
											if ($pay_type == PayType::EARNEST_PAY && $agent_product->earnest && $agent_product->earnest_timeout) {
							 | 
						|
												return $agent_product->earnest;
							 | 
						|
											}
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$total_price = $price * $num;
							 | 
						|
										/*//没有任何优惠券时直接返回最终价
							 | 
						|
										if ($coupon && $coupon->isEmpty()) {
							 | 
						|
											return $total_price;
							 | 
						|
										}
							 | 
						|
								
							 | 
						|
										$coupon = $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);
							 | 
						|
									}
							 | 
						|
								}
							 |