137 行
4.6 KiB
PHP
137 行
4.6 KiB
PHP
<?php
|
|
|
|
namespace addons\shopro\listener\order;
|
|
|
|
use addons\shopro\exception\Exception;
|
|
use addons\shopro\model\Cart;
|
|
use addons\shopro\model\Config;
|
|
use addons\shopro\model\Order;
|
|
use addons\shopro\model\Store;
|
|
use addons\shopro\model\User;
|
|
use app\common\library\PrinterService;
|
|
|
|
/**
|
|
* 支付成功事件监听
|
|
*/
|
|
class Payed
|
|
{
|
|
|
|
// 订单支付成功事件
|
|
public function orderPayedAfter(&$params) {
|
|
// 订单对象
|
|
$order = $params["order"];
|
|
|
|
// 重新查询带明细项的完整订单
|
|
$order = Order::with("item")->where("id", $order["id"])->find();
|
|
$items = $order ? $order["item"] : [];
|
|
|
|
// 1. 自动触发芯烨云小票打印
|
|
$this->autoPrintOrder($order, $items);
|
|
|
|
// 2. 门店管理员通知
|
|
$storeIds = [];
|
|
foreach ($items as $item) {
|
|
if (in_array($item["dispatch_type"], ["store", "selfetch"]) && $item["store_id"]) {
|
|
$storeIds[] = $item["store_id"];
|
|
}
|
|
}
|
|
|
|
$data = [];
|
|
if ($storeIds) {
|
|
$data = [];
|
|
// 存在门店,查询门店管理员
|
|
$stores = Store::with(["userStore.user"])->where("id", "in", $storeIds)->select();
|
|
foreach ($stores as $key => $store) {
|
|
$userStoreList = $store["user_store"] ? : [];
|
|
unset($store["user_store"]);
|
|
|
|
// 当前门店所有用户管理员
|
|
$userList = [];
|
|
foreach ($userStoreList as $user) {
|
|
if ($user["user"]) {
|
|
$userList[] = $user["user"];
|
|
}
|
|
}
|
|
|
|
// 有用户才能发送消息
|
|
if ($userList) {
|
|
$data[] = [
|
|
"store" => $store,
|
|
"userList" => $userList
|
|
];
|
|
}
|
|
}
|
|
}
|
|
|
|
// 存在要通知的门店管理员
|
|
if ($data) {
|
|
// 按门店为单位发送通知
|
|
foreach ($data as $key => $sendData) {
|
|
\addons\shopro\library\notify\Notify::send(
|
|
$sendData["userList"],
|
|
new \addons\shopro\notifications\store\Order([
|
|
"store" => $sendData["store"],
|
|
"order" => $order,
|
|
"event" => "store_order_new"
|
|
])
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 自动小票打印逻辑
|
|
*/
|
|
protected function autoPrintOrder($order, $items)
|
|
{
|
|
if (!$order) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$printer = new PrinterService();
|
|
|
|
$orderItems = [];
|
|
if (!empty($items)) {
|
|
foreach ($items as $it) {
|
|
$title = isset($it["goods_title"]) ? $it["goods_title"] : (isset($it["title"]) ? $it["title"] : "商品");
|
|
$sku = !empty($it["goods_sku_text"]) ? " (" . $it["goods_sku_text"] . ")" : "";
|
|
$num = isset($it["goods_num"]) ? $it["goods_num"] : 1;
|
|
$price = isset($it["goods_price"]) ? $it["goods_price"] : "0.00";
|
|
|
|
$orderItems[] = [
|
|
"name" => $title . $sku,
|
|
"num" => $num,
|
|
"price" => $price
|
|
];
|
|
}
|
|
}
|
|
|
|
$addressStr = "";
|
|
if (isset($order["province_name"])) {
|
|
$addressStr .= $order["province_name"] . $order["city_name"] . $order["area_name"];
|
|
}
|
|
if (isset($order["address"])) {
|
|
$addressStr .= $order["address"];
|
|
}
|
|
|
|
$orderData = [
|
|
"shop_name" => "小众厨房设备商城",
|
|
"order_sn" => isset($order["order_sn"]) ? $order["order_sn"] : "",
|
|
"createtime" => isset($order["createtime"]) ? $order["createtime"] : time(),
|
|
"consignee" => isset($order["consignee"]) ? $order["consignee"] : "客户",
|
|
"phone" => isset($order["phone"]) ? $order["phone"] : "",
|
|
"address" => $addressStr ? $addressStr : "到店自提/商家配送",
|
|
"total_fee" => isset($order["pay_fee"]) ? sprintf("%.2f", $order["pay_fee"]) : (isset($order["total_fee"]) ? $order["total_fee"] : "0.00"),
|
|
"items" => $orderItems
|
|
];
|
|
|
|
// 调用芯烨云打单
|
|
$printer->printOrder($orderData, 1);
|
|
} catch (\Exception $e) {
|
|
\think\Log::record("自动打印小票失败: " . $e->getMessage(), "error");
|
|
}
|
|
}
|
|
|
|
}
|