only(['page', 'status']); $request->validate([ 'page' => 'regex:/^\d+$/', 'status' => 'integer' ], [ 'page.regex' => '页码错误', 'status.integer' => '未指定订单状态' ]); if (array_key_exists('status', $formData) && $formData['status'] !== null) { $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') ->orderBy('id', 'DESC') ->simplePaginate(15); return $this->success($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' => '购买数量输入错误', ]); $agent_product = AgentProduct::query() ->where('id', $formData['id']) ->with('coupon') ->with('product') ->first(); if (!$agent_product || !$agent_product->product) { $this->error('产品不存在'); } 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']); //供应商产品表 // 存入订单表 $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, 'agent_product_id' => $agent_product->id, 'product_id' => $agent_product->product_id, 'status' => 0, 'pay_type' => $formData['pay_type'], ]); //资金流水 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' => fn($query) => $query->select('*')]) ->find($id); $final_price = $this->calc($agent_product, $num); return $this->success(['price' => $final_price]); } /** * 计算最终价格(扣除优惠券之后的价格) * @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) { 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() { 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); } }