|
@@ -8,13 +8,18 @@ use app\middleware\WxMiddleware;
|
|
|
use app\model\saas\SaasCart;
|
|
use app\model\saas\SaasCart;
|
|
|
use app\model\saas\SaasDiscount;
|
|
use app\model\saas\SaasDiscount;
|
|
|
use app\model\saas\SaasOrder;
|
|
use app\model\saas\SaasOrder;
|
|
|
|
|
+use app\model\saas\SaasOrderDetail;
|
|
|
use app\model\saas\SaasPrintClient;
|
|
use app\model\saas\SaasPrintClient;
|
|
|
|
|
+use app\model\saas\SaasShop;
|
|
|
use app\model\saas\SaasUser;
|
|
use app\model\saas\SaasUser;
|
|
|
|
|
+use app\model\saas\SaasUserBuy;
|
|
|
|
|
+use app\model\saas\SaasUserLog;
|
|
|
use LinFly\Annotation\Route\Controller;
|
|
use LinFly\Annotation\Route\Controller;
|
|
|
use LinFly\Annotation\Route\Middleware;
|
|
use LinFly\Annotation\Route\Middleware;
|
|
|
use LinFly\Annotation\Route\Route;
|
|
use LinFly\Annotation\Route\Route;
|
|
|
use support\Request;
|
|
use support\Request;
|
|
|
use support\Response;
|
|
use support\Response;
|
|
|
|
|
+use think\facade\Db;
|
|
|
|
|
|
|
|
|
|
|
|
|
#[Controller(prefix: "/wx_api/order"),Middleware(WxMiddleware::class)]
|
|
#[Controller(prefix: "/wx_api/order"),Middleware(WxMiddleware::class)]
|
|
@@ -29,6 +34,75 @@ class Order extends Base
|
|
|
'2_2_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
|
|
* @param Request $request
|
|
|
* @return Response
|
|
* @return Response
|
|
@@ -40,15 +114,15 @@ class Order extends Base
|
|
|
$param = $this->_valid([
|
|
$param = $this->_valid([
|
|
|
"shop.require" => trans("empty.require"),
|
|
"shop.require" => trans("empty.require"),
|
|
|
"print.require" => trans("empty.require"),
|
|
"print.require" => trans("empty.require"),
|
|
|
- "pay.require" => trans("empty.require"),
|
|
|
|
|
|
|
+ "pay.require" => trans("empty.require"), // 1微信支付 2会员卡支付 3会员卡充值并支付
|
|
|
"printName.default" => "",
|
|
"printName.default" => "",
|
|
|
"express.require" => trans("empty.require"),
|
|
"express.require" => trans("empty.require"),
|
|
|
- "card.default" => [],
|
|
|
|
|
|
|
+ "card.default" => "",
|
|
|
"gift.default" => 0
|
|
"gift.default" => 0
|
|
|
],$request->method());
|
|
],$request->method());
|
|
|
if (!is_array($param)) return error($param);
|
|
if (!is_array($param)) return error($param);
|
|
|
- $cart = (new SaasCart)->where("shop_id",$param['shop'])->order("create_at desc")->select();
|
|
|
|
|
- if ($cart->isEmpty()) return success('ok',['cart' => []]);
|
|
|
|
|
|
|
+ $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;
|
|
$totalAmount = $totalDiscount = 0;
|
|
|
foreach ($cart as $k=>$v){
|
|
foreach ($cart as $k=>$v){
|
|
|
$key = $v['color'] . '_' . $v['duplex'] . '_' . $v['source'];
|
|
$key = $v['color'] . '_' . $v['duplex'] . '_' . $v['source'];
|
|
@@ -73,48 +147,100 @@ class Order extends Base
|
|
|
$totalDiscount += $v['discount']; // 折扣后金额
|
|
$totalDiscount += $v['discount']; // 折扣后金额
|
|
|
}
|
|
}
|
|
|
$orderSn = CodeExtend::uniqidDate(16).date("is").rand(1,9);
|
|
$orderSn = CodeExtend::uniqidDate(16).date("is").rand(1,9);
|
|
|
- $totalDay = (new SaasOrder)->where("shop_id",$param['shop'])->whereDay("create_at")->count();
|
|
|
|
|
|
|
+ $totalDay = (new SaasOrder)->where("shop_id",$param['shop'])->where("openid",$request->user['openid'])->whereDay("create_at")->count();
|
|
|
$orderData = [
|
|
$orderData = [
|
|
|
"shop_id" => $param['shop'], // 所属店铺
|
|
"shop_id" => $param['shop'], // 所属店铺
|
|
|
"parent_id" => $param['shop'], // 消费店铺
|
|
"parent_id" => $param['shop'], // 消费店铺
|
|
|
|
|
+ "openid" => $request->user['openid'],
|
|
|
"order_sn" => $orderSn,
|
|
"order_sn" => $orderSn,
|
|
|
"money" => $totalAmount,
|
|
"money" => $totalAmount,
|
|
|
"discount" => $totalDiscount,
|
|
"discount" => $totalDiscount,
|
|
|
- "uuid" => $request->user['id'],
|
|
|
|
|
"print_name" => $param['printName'],
|
|
"print_name" => $param['printName'],
|
|
|
"package" => $param['express'],
|
|
"package" => $param['express'],
|
|
|
"package_sn" => date('md')."-".sprintf("%02d",($totalDay+1)),
|
|
"package_sn" => date('md')."-".sprintf("%02d",($totalDay+1)),
|
|
|
- "extra_money" => "",
|
|
|
|
|
|
|
+ "extra_money" => 0,
|
|
|
"remark" => $param['remark']??''
|
|
"remark" => $param['remark']??''
|
|
|
];
|
|
];
|
|
|
- if ($param['pay'] == 2) { // 会员卡支付或开通会员卡并充值
|
|
|
|
|
|
|
+ $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();
|
|
$card = (new SaasUser)->where("openid",$request->user['openid'])->where("shop_id",$param['shop'])->field("balance")->findOrEmpty();
|
|
|
- if ($card->isEmpty()) // 创建会员并发起支付
|
|
|
|
|
- {
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
- if ($totalDiscount > 0 && $totalDiscount > $card['balance']) {
|
|
|
|
|
|
|
+ $payMoney = $totalDiscount > 0 ? $totalDiscount : $totalAmount;
|
|
|
|
|
+ if ($payMoney > $card['balance']) {
|
|
|
return error("卡内余额不足~");
|
|
return error("卡内余额不足~");
|
|
|
}
|
|
}
|
|
|
- if ($totalDiscount == 0 && $totalAmount > $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' => []]);
|
|
|
}
|
|
}
|
|
|
- $orderDetail = [];
|
|
|
|
|
- foreach ($cart as $key=>$val)
|
|
|
|
|
- {
|
|
|
|
|
- unset($val['id']);
|
|
|
|
|
- $orderDetail[$key] = $val;
|
|
|
|
|
- $orderDetail[$key]['money'] = $val['money']*100;
|
|
|
|
|
- $orderDetail[$key]['order_sn'] = $orderSn;
|
|
|
|
|
- $orderDetail[$key]['print_name'] = $param['printName'];
|
|
|
|
|
|
|
+ $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";
|
|
|
}
|
|
}
|
|
|
- print_r($orderDetail);
|
|
|
|
|
-
|
|
|
|
|
|
|
+ (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) {
|
|
} catch (\Throwable $throwable) {
|
|
|
echo $throwable->getLine()."\n";
|
|
echo $throwable->getLine()."\n";
|
|
|
|
|
+ echo $throwable->getFile()."\n";
|
|
|
return error($throwable->getMessage());
|
|
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")
|
|
|
|
|
+ ];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
}
|
|
}
|