优化收银台API逻辑,后台新增监听模式功能

This commit is contained in:
技术老胡 2024-08-31 15:38:40 +08:00
parent 5115b932fc
commit 0f3fad95c8
8 changed files with 53 additions and 54 deletions

View File

@ -109,9 +109,11 @@ class PayController
if ($order_id) { if ($order_id) {
$act_order = Order::where('order_id', $order_id)->find(); $act_order = Order::where('order_id', $order_id)->find();
if ($act_order) { if ($act_order) {
$passtime = strtotime($act_order->close_time) - time();
$data = []; $data = [];
if ($act_order->state === 0) { if ($act_order->state === 0) {
$data['order_id'] = $act_order->order_id; $data['order_id'] = $act_order->order_id;
$data['passtime'] = $passtime > 0 ? $passtime : 0;
$data['state'] = $act_order->state; $data['state'] = $act_order->state;
return json($data); return json($data);
} elseif ($act_order->state === 1) { } elseif ($act_order->state === 1) {
@ -125,6 +127,7 @@ class PayController
$res_return_url = $act_order->return_url . '?' . http_build_query($notify); $res_return_url = $act_order->return_url . '?' . http_build_query($notify);
// 响应消息 // 响应消息
$data['order_id'] = $act_order->order_id; $data['order_id'] = $act_order->order_id;
$data['passtime'] = $passtime > 0 ? $passtime : 0;
$data['state'] = $act_order->state; $data['state'] = $act_order->state;
$data['return_url'] = $res_return_url; $data['return_url'] = $res_return_url;
return json($data); return json($data);
@ -245,21 +248,19 @@ class PayController
// 检测本账号订单 // 检测本账号订单
$orders = []; $orders = [];
foreach ($order_list as $key => $val) { foreach ($order_list as $key => $val) {
if ($pid == $val['pid'] && $aid == $val['aid']) { if ($pid == $val['pid'] && $aid == $val['aid'] && $val['patt'] == 1) {
$orders[] = $order_list[$key]; $orders[] = $order_list[$key];
} }
} }
if (!$orders) { if (!$orders) {
return \json(['code' => 0, 'msg' => '非本账号订单']); return \json(['code' => 0, 'msg' => '非本账号订单']);
} }
// 收款平台
$platform = ['sqbpay' => 'ShouQianBa', 'storepay' => 'ZhiHuiJingYing', 'mqpay' => 'MaQian', 'ysepay' => 'Ysepay'];
// 登陆账号 // 登陆账号
$config = ['username' => $pay_config['account'], 'password' => $pay_config['password']]; $config = ['username' => $pay_config['account'], 'password' => $pay_config['password']];
// 收款查询 // 收款查询
$query = $pay_config['query']; $query = $pay_config['query'];
// 实例监听客户端 // 实例监听客户端
$payclient_name = $platform[$pay_config['platform']]; $payclient_name = $pay_config['payclass'];
$payclient_path = "\\payclient\\{$payclient_name}"; $payclient_path = "\\payclient\\{$payclient_name}";
$Payclient = new $payclient_path($config); $Payclient = new $payclient_path($config);
// 获取支付明细 // 获取支付明细
@ -281,7 +282,7 @@ class PayController
$is_user = User::checkUser($pid, $sign); $is_user = User::checkUser($pid, $sign);
$path = '../runtime/order.json'; $path = '../runtime/order.json';
if ($is_user) { if ($is_user) {
$orders = Order::scope('activeOrder')->field('id,pid,aid,cid')->select(); $orders = Order::scope('activeOrder')->field('id,pid,aid,cid,patt')->select();
if (!file_exists($path)) { if (!file_exists($path)) {
file_put_contents($path, '[]'); file_put_contents($path, '[]');
} }

View File

@ -119,6 +119,7 @@ class PayManageController extends BaseController
'platform' => $acc->getData('platform'), 'platform' => $acc->getData('platform'),
'account' => $acc->account, 'account' => $acc->account,
'password' => $acc->password, 'password' => $acc->password,
'payclass' => $platform->class_name,
'query' => \var_export($query, \true) 'query' => \var_export($query, \true)
]; ];
$config = View::fetch('tpl/account_config', $data); $config = View::fetch('tpl/account_config', $data);

View File

@ -44,6 +44,8 @@ class Order extends BaseModel
'param' => serialize(self::getParams($data)), 'param' => serialize(self::getParams($data)),
// 等待/过期0, 支付成功1 // 等待/过期0, 支付成功1
'state' => 0, 'state' => 0,
// 开启监听1, 关闭监听0
'patt' => $channel['patt'],
// 订单创建时间 // 订单创建时间
'create_time' => self::getFormatTime($my_time), 'create_time' => self::getFormatTime($my_time),
// 订单关闭时间 // 订单关闭时间
@ -109,9 +111,6 @@ class Order extends BaseModel
$order->qrcode = $a_list->payChannel[0]->qrcode ?? '···'; $order->qrcode = $a_list->payChannel[0]->qrcode ?? '···';
return $order->toArray(); return $order->toArray();
} }
// 选择收款通道 // 选择收款通道
private static function setChannel($pid): array private static function setChannel($pid): array
{ {
@ -120,7 +119,8 @@ class Order extends BaseModel
if (!$channel_info || !$aids) { if (!$channel_info || !$aids) {
return []; return [];
} }
$channel = ['aid' => $channel_info->account_id, 'cid' => $channel_info->id]; $patt = PayAccount::find($channel_info->account_id);
$channel = ['aid' => $channel_info->account_id, 'cid' => $channel_info->id, 'patt' => $patt->pattern];
PayChannel::update(['last_time' => self::getFormatTime(), 'id' => $channel['cid']]); PayChannel::update(['last_time' => self::getFormatTime(), 'id' => $channel['cid']]);
return $channel; return $channel;
} }

View File

@ -6,7 +6,7 @@ return [
// 用户账号配置 // 用户账号配置
'user' => [ 'user' => [
'pid' => 1001, 'pid' => 1001,
'key' => '7ImzF6Rf8OciQcmRJv8oTNBwIp6uqF0p' 'key' => '953c4d682d9ab148277b76a06e215ce7'
], ],
// 收款平台账号配置 // 收款平台账号配置
'pay' => [ 'pay' => [
@ -14,10 +14,12 @@ return [
'aid' => 40, 'aid' => 40,
// 收款平台 // 收款平台
'platform' => 'mqpay', 'platform' => 'mqpay',
// 收款平台
'payclass' => 'MaQian',
// 账号 // 账号
'account' => '258000000', 'account' => '18657945333',
// 密码 // 密码
'password' => '123456', 'password' => 'Aa12345678',
// 订单查询参数配置 // 订单查询参数配置
'query' => array ( 'query' => array (
'terminalType' => '', 'terminalType' => '',

View File

@ -1 +1 @@
{"code":0,"msg":"没有新订单"} {"code":1,"msg":"\u67091\u4e2a\u65b0\u8ba2\u5355","orders":[{"id":27409,"pid":1001,"aid":30,"cid":31,"patt":0}]}

View File

@ -76,7 +76,7 @@
.msg>p { .msg>p {
color: red; color: red;
font-weight: 700; font-weight: 700;
line-height: 1.5em; line-height: 2em;
} }
.msg>p:nth-child(1) { .msg>p:nth-child(1) {
@ -98,7 +98,7 @@
.note { .note {
color: red; color: red;
font-size: 24px; font-size: 22px;
} }
</style> </style>
</head> </head>
@ -135,10 +135,10 @@
<div class="qrcode"><img id="qrcode" src="/static/img/loading.gif"> <div class="qrcode"><img id="qrcode" src="/static/img/loading.gif">
</div> </div>
<div class="msg"> <div class="msg">
<p>请付款 <span id="s_money" class="note"> <p>请付款 <span class="note">
<?php echo htmlentities($really_price); ?> <?php echo htmlentities($really_price); ?>
</span>,注意不能多付或少付</p> </span>注意不能多付或少付</p>
<p>付款后请等待5秒查看</p> <p>付款后,请等待 5 秒查看</p>
<p id="divTime"></p> <p id="divTime"></p>
</div> </div>
<div class="shanxinzha"> <div class="shanxinzha">
@ -147,13 +147,11 @@
</div> </div>
</div> </div>
</div> </div>
<script src="/static/js/layui.min.js"></script> <script src="/static/js/layui.min.js"></script>
<script src="/static/js/awesome-qr.min.js"></script> <script src="/static/js/awesome-qr.min.js"></script>
<script> <script>
const payCode = '<?php echo $payUrl; ?>'; const payCode = '<?php echo htmlentities($payUrl); ?>';
const payType = '<?php echo htmlentities($type); ?>'; const payType = '<?php echo htmlentities($type); ?>';
const order = '<?php echo htmlentities($order_id); ?>'; const order = '<?php echo htmlentities($order_id); ?>';
const QR = AwesomeQR.AwesomeQR; const QR = AwesomeQR.AwesomeQR;
@ -170,18 +168,16 @@
} }
// 生成二维码 // 生成二维码
document.getElementById('qrcode').src = await getQrcode(payCode, QR); document.getElementById('qrcode').src = await getQrcode(payCode, QR);
// 订单倒计时 // 订单过期时间
const intDiff = <?php echo strtotime($close_time) - time() > 0 ? strtotime($close_time) - time() : 0; ?>; let passtime = 0;
const timerId = timer(intDiff);
// 订单状态查询 // 订单状态查询
let orderTimer = null; let orderTimer = 0;
const queryOrder = async (url) => { const queryOrder = async (url) => {
if (orderTimer) { clearTimeout(orderTimer); } if (orderTimer) { clearTimeout(orderTimer); }
const res = await fetch(url); const res = await fetch(url);
const jsonData = await res.json(); const jsonData = await res.json();
orderTimer = setTimeout(() => { queryOrder(url) }, 1000); orderTimer = setTimeout(() => { queryOrder(url) }, 1000);
if (jsonData.state === 1) { if (jsonData.state === 1) {
clearInterval(timerId);
clearTimeout(orderTimer); clearTimeout(orderTimer);
document.getElementById('divTime').innerHTML = '<small class="note">订单支付成功</small>'; document.getElementById('divTime').innerHTML = '<small class="note">订单支付成功</small>';
document.getElementById('qrcode').src = '/static/img/pay_ok.png';// 输出支付成功提示图片 document.getElementById('qrcode').src = '/static/img/pay_ok.png';// 输出支付成功提示图片
@ -189,13 +185,13 @@
location.href = jsonData.return_url; location.href = jsonData.return_url;
}, 1500); }, 1500);
} }
if (jsonData.state === 2) { passtime = jsonData.passtime;
if (passtime <= 0) {
clearTimeout(orderTimer); clearTimeout(orderTimer);
} }
timer(passtime);
} }
if (intDiff > 0) { queryOrder(`/getOrderState/${order}`);
queryOrder(`/getOrderState/${order}`);
}
})(); })();
// 生成二维码 // 生成二维码
@ -208,30 +204,19 @@
}) })
return qrcodeUrl; return qrcodeUrl;
} }
// 计时 // 倒计时
function timer(intDiff) { function timer(passtime) {
const timerId = setInterval(() => { let minute = 0, second = 0; // 时间默认值
let day = 0, hour = 0, minute = 0, second = 0;//时间默认值 if (passtime > 0) {
if (intDiff > 0) { minute = Math.floor(passtime / 60);
day = Math.floor(intDiff / (60 * 60 * 24)); second = Math.floor(passtime) - (minute * 60);
hour = Math.floor(intDiff / (60 * 60)) - (day * 24); }
minute = Math.floor(intDiff / 60) - (day * 24 * 60) - (hour * 60); if (passtime <= 0) {
second = Math.floor(intDiff) - (day * 24 * 60 * 60) - (hour * 60 * 60) - (minute * 60); document.getElementById('divTime').innerHTML = '<small class="note">订单二维码已过期</small>';
} document.getElementById('qrcode').src = '/static/img/qrcode_timeout.png';// 输出过期二维码提示图片
if (minute <= 9) minute = '0' + minute; } else {
if (second <= 9) second = '0' + second; document.getElementById('divTime').innerHTML = `二维码有效时间:<small class="note">${minute}</small><small class="note">${second}</small> 秒,失效勿付`;
if (hour <= 0 && minute <= 0 && second <= 0) { }
document.getElementById('divTime').innerHTML = '<small class="note">订单二维码已过期</small>';
document.getElementById('qrcode').src = '/static/img/qrcode_timeout.png';// 输出过期二维码提示图片
} else {
document.getElementById('divTime').innerHTML = `二维码有效时间: <small class="note">${minute}</small><small style="color:red; font-size:24px">${second}</small> 秒,失效勿付`;
}
if (intDiff < 0) {
clearInterval(timerId);
}
intDiff--;
}, 1000);
return timerId;
} }
</script> </script>
</body> </body>

View File

@ -11,6 +11,14 @@
bottom: 8px; bottom: 8px;
right: 8px; right: 8px;
} }
.layui-card {
border-radius: 10px;
}
#mainbox {
padding: 16px;
}
</style> </style>
</head> </head>

View File

@ -14,6 +14,8 @@ return [
'aid' => <?php echo $aid; ?>, 'aid' => <?php echo $aid; ?>,
// 收款平台 // 收款平台
'platform' => '<?php echo $platform; ?>', 'platform' => '<?php echo $platform; ?>',
// 收款平台
'payclass' => '<?php echo $payclass; ?>',
// 账号 // 账号
'account' => '<?php echo $account; ?>', 'account' => '<?php echo $account; ?>',
// 密码 // 密码