[ 'op' => '操作', 'id' => '订单ID', 'product_id' => '商品ID', 'product_type' => '商品类型', 'product_name' => '商品名称', 'user_coupon' => '优惠码', 'price' => '金额', 'status' => '状态', 'create_time' => '创建时间', 'update_time' => '更新时间', ], ]; /** * @throws Exception */ public function index(ServerRequest $request, Response $response, array $args): ResponseInterface { return $response->write( $this->view() ->assign('details', self::$details) ->fetch('user/order/index.tpl') ); } /** * @throws Exception */ public function create(ServerRequest $request, Response $response, array $args): ResponseInterface { echo 123;exit; $product_id = $this->antiXss->xss_clean($request->getQueryParams()['product_id']) ?? null; $redir = Cookie::get('redir'); if ($redir !== null) { Cookie::set(['redir' => ''], time() - 1); } if ($product_id === null || $product_id === '') { return $response->withRedirect('/user/product'); } $product = (new Product())->where('id', $product_id)->first(); $product->type_text = $product->type(); $product->content = json_decode($product->content); $oHuodong = Huodong::dateHit(); // $oHuodong = Huodong::dateHit("2025-01-01"); $product->huodong = $oHuodong; return $response->write( $this->view() ->assign('product', $product) ->fetch('user/order/create.tpl') ); } /** * @throws Exception */ public function detail(ServerRequest $request, Response $response, array $args): ResponseInterface { $id = $this->antiXss->xss_clean($args['id']); $order = (new Order())->where('user_id', $this->user->id)->where('id', $id)->first(); if ($order === null) { return $response->withRedirect('/user/order'); } $order->product_type_text = $order->productType(); $order->status = $order->status(); $order->create_time = Tools::toDateTime($order->create_time); $order->update_time = Tools::toDateTime($order->update_time); $order->content = json_decode($order->product_content); $invoice = (new Invoice())->where('order_id', $id)->first(); $invoice->status = $invoice->status(); $invoice->create_time = Tools::toDateTime($invoice->create_time); $invoice->update_time = Tools::toDateTime($invoice->update_time); $invoice->pay_time = Tools::toDateTime($invoice->pay_time); $invoice->content = json_decode($invoice->content); return $response->write( $this->view() ->assign('order', $order) ->assign('invoice', $invoice) ->fetch('user/order/view.tpl') ); } public function process(ServerRequest $request, Response $response, array $args): ResponseInterface { echo 123;exit; $coupon_raw = $this->antiXss->xss_clean($request->getParam('coupon')); $product_id = $this->antiXss->xss_clean($request->getParam('product_id')); $product = (new Product())->find($product_id); if ($product === null || $product->stock === 0) { return $response->withJson([ 'ret' => 0, 'msg' => '商品不存在或库存不足', ]); } $buy_price = $product->price; $user = $this->user; if ($user->is_shadow_banned) { return $response->withJson([ 'ret' => 0, 'msg' => '商品不存在或库存不足', ]); } if ($coupon_raw !== '') { $coupon = (new UserCoupon())->where('code', $coupon_raw)->first(); //// start 特别活动禁用优惠码 || 不禁用了,变为调整 // if ($oHuodong) { // if ($oHuodong->class == "A") { // // 月套餐以上禁用优惠码 // if ($product->class_time > 32) { // return $response->withJson([ // 'ret' => 0, // 'msg' => '该商品在【'.$oHuodong->title.'】期间不能使用优惠码', // ]); // } // } // } //// end if ($coupon === null || ($coupon->expire_time !== 0 && $coupon->expire_time < time())) { return $response->withJson([ 'ret' => 0, 'msg' => '优惠码不存在或已过期', ]); } $coupon_limit = json_decode($coupon->limit); if ($coupon_limit->disabled) { return $response->withJson([ 'ret' => 0, 'msg' => '优惠码已被禁用', ]); } if ($coupon_limit->product_id !== '' && ! in_array($product_id, explode(',', $coupon_limit->product_id))) { return $response->withJson([ 'ret' => 0, 'msg' => '优惠码不适用于此商品', ]); } $coupon_use_limit = $coupon_limit->use_time; if ($coupon_use_limit > 0) { $user_use_count = (new Order())->where('user_id', $user->id)->where('coupon', $coupon->code)->count(); if ($user_use_count >= $coupon_use_limit) { return $response->withJson([ 'ret' => 0, 'msg' => '优惠码使用次数已达上限', ]); } } if (property_exists($coupon_limit, 'total_use_time')) { $coupon_total_use_limit = $coupon_limit->total_use_time; } else { $coupon_total_use_limit = -1; } if ($coupon_total_use_limit > 0 && $coupon->use_count >= $coupon_total_use_limit) { return $response->withJson([ 'ret' => 0, 'msg' => '优惠码使用次数已达上限', ]); } $content = json_decode($coupon->content); //// start 特别活动期间限制优惠码额度 $sDateNow = date("Y-m-d"); // $sDateNow = "2025-05-02"; $oHuodong = Huodong::where("start_date", "<=", $sDateNow)->where("end_date", ">=", $sDateNow)->first(); if ($oHuodong !== null) { if ($oHuodong->class == "A") { if ($content->type === 'percentage') { $content->value = $oHuodong->coupon_global; } } } //// end if ($content->type === 'percentage') { $discount = $product->price * $content->value / 100; } else { $discount = $content->value; } $buy_price = $product->price - $discount; } $product_limit = json_decode($product->limit); if ($product_limit->class_required !== '' && $user->class < (int) $product_limit->class_required) { return $response->withJson([ 'ret' => 0, 'msg' => '您的账户等级不足,无法购买此商品', ]); } if ($product_limit->node_group_required !== '' && $user->node_group !== (int) $product_limit->node_group_required) { return $response->withJson([ 'ret' => 0, 'msg' => '您所在的用户组无法购买此商品', ]); } if ($product_limit->new_user_required !== 0) { $order_count = (new Order())->where('user_id', $user->id)->count(); if ($order_count > 0) { return $response->withJson([ 'ret' => 0, 'msg' => '此商品仅限新用户购买', ]); } } $order = new Order(); $order->user_id = $user->id; $order->product_id = $product->id; $order->product_type = $product->type; $order->product_name = $product->name; $order->product_content = $product->content; $order->coupon = $coupon_raw; $order->price = $buy_price; $order->status = 'pending_payment'; $order->create_time = time(); $order->update_time = time(); if (isset($oHuodong) && $oHuodong !== null && $oHuodong->class) { $order->huodong_class = $oHuodong->class; } $order->save(); $invoice_content = []; $invoice_content[] = [ 'content_id' => 0, 'name' => $product->name, 'price' => $product->price, ]; if ($coupon_raw !== '') { $invoice_content[] = [ 'content_id' => 1, 'name' => '优惠码 ' . $coupon_raw, 'price' => '-' . $discount, ]; } $invoice = new Invoice(); $invoice->user_id = $user->id; $invoice->order_id = $order->id; $invoice->content = json_encode($invoice_content); $invoice->price = $buy_price; $invoice->status = 'unpaid'; $invoice->create_time = time(); $invoice->update_time = time(); $invoice->pay_time = 0; $invoice->save(); if ($product->stock > 0) { $product->stock -= 1; } $product->sale_count += 1; $product->save(); if ($coupon_raw !== '') { $coupon->use_count += 1; $coupon->save(); } return $response->withJson([ 'ret' => 1, 'msg' => '成功创建订单,正在跳转账单页面', 'invoice_id' => $invoice->id, ]); } public function ajax(ServerRequest $request, Response $response, array $args): ResponseInterface { $orders = (new Order())->orderBy('id', 'desc')->where('user_id', $this->user->id)->get(); foreach ($orders as $order) { $order->op = '查看'; if ($order->status === 'pending_payment') { $invoice_id = (new Invoice())->where('order_id', $order->id)->first()->id; $order->op .= ' 支付'; } $order->product_type = $order->productType(); $order->status = $order->status(); $order->create_time = Tools::toDateTime($order->create_time); $order->update_time = Tools::toDateTime($order->update_time); } return $response->withJson([ 'orders' => $orders, ]); } }