['name' => '彩色-单面', 'amount' => 0, 'quantity' => 0,'discount' => 0], '1_2_1' => ['name' => '彩色-双面', 'amount' => 0, 'quantity' => 0,'discount' => 0], '2_1_1' => ['name' => '黑白-单面', 'amount' => 0, 'quantity' => 0,'discount' => 0], '2_2_1' => ['name' => '黑白-双面', 'amount' => 0, 'quantity' => 0,'discount' => 0], ]; protected array $color = ["1" => "彩色", "2" => "黑白"]; protected array $duplex = ["1" => "单面", "2" => "双面"]; protected array $package = ["1" => "店内打印", "2" => '远程自取' , "3" => "商家配送"]; #[Route(path: "list",methods: "get")] public function getOrderList(Request $request): Response { try { $param = $this->_valid([ "page.require" => "参数错误", "size.require" => "参数错误", "type.require" => "参数错误", "shop.require" => "请选择店铺" ]); if (!is_array($param)) return error($param); $map = ["openid" => $request->user['openid'],"shop_id" => $param['shop']]; $model = (new SaasOrder); if ($param['type'] > 0) { $map['status'] = $param['type'] - 1; } else { $model = $model->where("status",">",0); } $resp = $model->where($map)->append(["total"])->with(['shop' => function($query){ $query->field('shop_id,shop_name'); }])->withAttr(["total" => function($val,$resp){ return (new SaasOrderDetail)->where("order_sn",$resp['order_sn'])->sum("number"); }])->order("create_at desc")->paginate([ "list_rows" => $param['size'], "page" => $param['page'] ]); return success("ok",$resp->toArray()); } catch (\Throwable $throwable) { return error($throwable->getMessage()); } } /** * 订单详情 * @param Request $request * @return Response */ #[Route(path: "detail",methods: "get")] public function getOrderDetail(Request $request): Response { try { $param = $this->_valid([ "order.require" => "参数错误" ]); if (!is_array($param)) return error($param); $resp = (new SaasOrder)->where("openid",$request->user['openid'])->where("order_sn",$param['order'])->append(["total","subscribe"])->with(['shop' => function($query){ $query->field('shop_id,shop_name,shop_address'); },"detail" => function($query){ $query->field('order_sn,name,color,paper_size,duplex,number,page,extension,path,icon'); }])->withAttr(["total" => function($val,$resp){ return (new SaasOrderDetail)->where("order_sn",$resp['order_sn'])->sum("number"); },"subscribe" => function(){ return ["495E40hqOKoz5j_mWcf-UcmF6wkj_yIwCrTXicicH5w","UXJjDQ7NGstSwOxrKf_laGDmpID8Mm5MpXwFAd45d8U"]; }])->findOrEmpty(); if ($resp->isEmpty()) return error("订单数据错误"); $resp['package_name'] = $this->package[$resp['package']]; $resp['money'] = format_money($resp['money'] / 100); return success("ok",$resp->toArray()); } catch (\Throwable $throwable) { return error($throwable->getMessage()); } } /** * @param Request $request * @return Response */ #[Route(path: "create",methods: "post")] public function createOrder(Request $request): Response { try { $param = $this->_valid([ "shop.require" => trans("empty.require"), "print.require" => trans("empty.require"), "pay.require" => trans("empty.require"), // 1微信支付 2会员卡支付 3会员卡充值并支付 "printName.default" => "", "express.require" => trans("empty.require"), "card.default" => "", "gift.default" => 0 ],$request->method()); if (!is_array($param)) return error($param); $cart = (new SaasCart)->where("shop_id",$param['shop'])->where("openid",$request->user['openid'])->order("create_at desc")->select(); if ($cart->isEmpty()) return error('请重新下单进行支付'); $totalAmount = $totalDiscount = 0; foreach ($cart as $k=>$v){ $key = $v['color'] . '_' . $v['duplex'] . '_' . $v['source']; if (isset($this->types[$key])) { $this->types[$key]['quantity'] += $v['page']; $this->types[$key]['amount'] += $v['money']; } $cart[$k] = $v; $cart[$k]['money'] = format_money($v['money'] / 100,2); $cart[$k]['name'] = msubstr($v['name'],0,12); } $printData = (new SaasPrintClient)->where("shop_id",$param['shop'])->where("code",$param['print'])->select(); if ($printData->isEmpty()) return error('无可用打印机'); // 计算折扣 foreach ($this->types as $k=>$v) { $discount = (new SaasDiscount)->where("shop_id",$param['shop'])->where("keys",$k)->where("number",'<',$v['quantity'])->findOrEmpty(); if (!$discount->isEmpty()) { $v['discount'] = round($v['amount'] * $discount['rate']); $this->types[$k]['discount'] = $v['discount']; } $totalAmount += $v['amount']; // 实际金额 $totalDiscount += $v['discount']; // 折扣后金额 } $orderSn = CodeExtend::uniqidDate(16).date("is").rand(1,9); $totalDay = (new SaasOrder)->where("shop_id",$param['shop'])->where("openid",$request->user['openid'])->whereDay("create_at")->count(); $orderData = [ "shop_id" => $param['shop'], // 所属店铺 "parent_id" => $param['shop'], // 消费店铺 "openid" => $request->user['openid'], "order_sn" => $orderSn, "money" => $totalAmount, "discount" => $totalDiscount, "print_name" => $param['printName'], "package" => $param['express'], "package_sn" => date('md')."-".sprintf("%02d",($totalDay+1)), "extra_money" => 0, "remark" => $param['remark']??'' ]; $shop = (new SaasShop)->where("shop_id",$param['shop'])->findOrEmpty(); if ($param['pay'] == 2) { // 会员卡支付 $card = (new SaasUser)->where("openid",$request->user['openid'])->where("shop_id",$param['shop'])->field("balance")->findOrEmpty(); $payMoney = $totalDiscount > 0 ? $totalDiscount : $totalAmount; if ($payMoney > $card['balance']) { return error("卡内余额不足~"); } (new SaasUserLog)->insertGetId([ "openid" => $request->user['openid'], "shop_id" => $param['shop'], "money" => $payMoney, "type" => 1 ]); // 直接支付 $card->balance = Db::raw("balance - {$payMoney}"); $card->total_consume = Db::raw("total_consume + {$payMoney}"); $card->save(); $orderData['pay_type'] = 2; $orderData['status'] = 1; $orderData['pay_at'] = getDateFull(); (new SaasOrder)->insertGetId($orderData); events("create-order",['shop' => $param['shop'],'openid' => $request->user['openid'],"order" => $orderSn]); return success("支付成功",['type' => 2,'data' => []]); } $options = [ 'body' => $shop['shop_name']."-打印", 'out_trade_no' => $orderSn."-".$orderData['package_sn'], "attach" => $orderSn, 'total_fee' => $orderData['money'] * 100, 'openid' => $request->user['openid'], 'trade_type' => 'JSAPI', 'spbill_create_ip' => $request->getRealIp(), "notify_url" => "https://apiv.ujia5.com/notify/wx" ]; if ($param['pay'] == 3 && !empty($param['card'])) { // 开通会员卡并充值 $buyCard = json_decode($param['card'],true); (new SaasUserBuy)->insertGetId([ "shop_id" => $param['shop'], "money" => $buyCard['money'] * 100, "total_money" => ($buyCard['money'] * 100) + ($buyCard['old_money'] * 100), "order_sn" => $orderSn, "openid" => $request->user['openid'] ]); $options['body'] = $shop['shop_name']."-充值支付"; $options['total_fee'] = $buyCard['money'] * 100; $options['notify_url'] = "https://apiv.ujia5.com/notify/payrecharge"; } (new SaasOrder)->insertGetId($orderData); $wechat = new \WeChat\Pay($this->getWxConfig()); // 生成预支付码 $result = $wechat->createOrder($options); if (!isset($result['return_code']) || $result['return_code'] !== "SUCCESS") { return error("发起支付失败"); } // 创建JSAPI参数签名 $resp = $wechat->createParamsForJsApi($result['prepay_id']); return success("ok",['type' => 1,"data" => $resp]); } catch (\Throwable $throwable) { echo $throwable->getLine()."\n"; echo $throwable->getFile()."\n"; return error($throwable->getMessage()); } } /** * 小程序配置 * @return array */ protected function getWxConfig(): array { return [ 'token' => 'test', 'appid' => sConf("wechat.mini_appid"), 'appsecret' => sConf("wechat.mini_secret"), 'encodingaeskey' => 'BJIUzE0gqlWy0GxfPp4J1oPTBmOrNDIGPNav1YFH5Z5', // 配置商户支付参数(可选,在使用支付功能时需要) 'mch_id' => sConf("wechat.mch_id"), 'mch_key' => sConf("wechat.mch_key") ]; } }