docs: 新增注释

Change-Id: Ia27ac9b49a9b9374cc4ce0a5f41a6fc49927edf5
This commit is contained in:
ui_none 2024-08-14 17:33:38 +08:00
parent f611f52158
commit 305157392e
1 changed files with 318 additions and 283 deletions

View File

@ -11,7 +11,8 @@ $json = file_get_contents("php://input");
$post = json_decode($json, true, 512, JSON_BIGINT_AS_STRING);
$serialno = $post['serialno'];
if(!$serialno) exit;
if (!$serialno)
exit;
// 获取数据库表数据POS机设备信息
$deviceInfo = $db->get_one("select * from tb_pos_device where serialno = '" . addslashes($serialno) . "'");
@ -35,6 +36,7 @@ if(strlen($s1) > 2000) {
$db->query("insert into tb_pos_device_log set device_id = {$device_id}, serialno = '" . addslashes($serialno) . "', ip = '" . addslashes($ip) . "', path = '" . addslashes($qs) . "', request = '" . addslashes($s1) . "', response = '', addtime = now() ");
// 获取最近一次成功插入的记录的自增id
$log_id = $db->insert_id();
// 如果设备表中没有对应设备的信息,则更新设备记录表并直接返回
if (!$deviceInfo) {
$db->query("update tb_pos_device_log set response='no_device' where id=" . $log_id);
exit;
@ -54,7 +56,8 @@ if($sign2 !== $post['sign']) { //签名错误
// 将api和data数据处理编码后插入到tb_pos_device_log表并终止脚本运行
// 回应时填充参数、返回的API接口数据、错误提示等。
function response_pos($api, $data) {
function response_pos($api, $data)
{
global $deviceInfo, $post, $log_id, $db;
$noncestr = md5(microtime() . '_' . rand());
$time = time();
@ -72,12 +75,14 @@ function response_pos($api, $data) {
}
// 向本机地址发送请求拼接端口号、路径、post参数和header调用自定义gquery函数
function self_query($path, $post) {
function self_query($path, $post)
{
return gquery("http://127.0.0.1:" . $_SERVER['SERVER_PORT'] . $path, $post, array('Host: ' . $_SERVER['SERVER_NAME']));
}
// 从post参数中获取pay_mode等参数实时消费接口中使用包括infoquery takemeal takemealok posonline
function pos_check_user() {
function pos_check_user()
{
global $post, $db, $company_id;
$pay_mode = $post['params']['pay_mode'];
@ -87,16 +92,19 @@ function pos_check_user() {
// 返回code值和type
if ($pay_mode == '5') { //二维码
$qr_code = trim($post['params']['qr_code']);
if(!$qr_code) exit;
if (!$qr_code)
exit;
if(substr($qr_code, 0, 4) != '[st]' || substr($qr_code, -1) != ';') exit;
if (substr($qr_code, 0, 4) != '[st]' || substr($qr_code, -1) != ';')
exit;
// 截取qr_code截取第4位到倒数第2位字符
$s = substr($qr_code, 4, -1);
// 将字符串分割成数组
$a = explode(",", $s);
if(count($a) != 2) exit;
if (count($a) != 2)
exit;
$code = $a[0];
$hash = $a[1];
@ -106,7 +114,8 @@ function pos_check_user() {
}
// 搜索父字符串中子字符串的位置如果pay_code中不以ymzs_开头则退出
if(strpos($pay_code, 'yzms_') !== 0) exit;
if (strpos($pay_code, 'yzms_') !== 0)
exit;
$uid = intval(substr($pay_code, 5));
$uInfo = $db->get_one("select * from tb_user where id = '{$uid}' and enabled != '0' and company_id=" . $company_id);
@ -157,9 +166,10 @@ if($qs == 'heartbeat' || $qs == 'addperson' || $qs == 'delperson') { //心跳
// 更新posdevice表的同步时间
$db->query("update tb_pos_device set synctime=now() where id = " . $device_id);
// 从tb_user表中获取所有启用且属于公司公司的用户列表
// 从tb_user表中获取所有启用且属于公司公司的用户列表id username cellphonne deptname
$userList_s = $db->get_all("select id, username, cellphone, deptname from tb_user where enabled = '1' and company_id = '{$company_id}'");
// 从tb_pos_device_user表中获取对应用户设备信息
// 获取指定设备的同步状态
// 新版Android取餐机宇航用于下发人脸、用户信息到Android取餐机用的记录表方便知道每台机现在的同步情况
$userList_d = $db->get_all("select * from tb_pos_device_user where device_id = '{$device_id}'");
@ -170,6 +180,7 @@ if($qs == 'heartbeat' || $qs == 'addperson' || $qs == 'delperson') { //心跳
// 提取上一步得到的关联信息并添加到faceInfo数组中判断此文件是否存在于服务器后台目录存在则将item信息存进dict数据中key是user_idvalue是item
foreach ($data as $item) {
if (is_file("../backstage/" . $item['path'])) {
// 建立用户id到用户人脸信息的映射 id => faceInfo
$faceInfo[$item['user_id']] = $item;
}
}
@ -179,12 +190,16 @@ if($qs == 'heartbeat' || $qs == 'addperson' || $qs == 'delperson') { //心跳
$cardInfo = array();
// 提取上一步获取的信息并将数据循环加入到卡信息数组中类似List[Dict]key是user_idvalue是item
foreach ($data as $item) {
// 建立用户id到用户卡信息的映射
$cardInfo[$item['user_id']] = $item;
}
// 从获取的该公司user_list表信息中循环遍历获取对应用户id的facecode,path,cardno信息添加到userList_s数组中
// 最终facecode,path和cardno作为用户设备需要下发的信息添加到userList_s中
// updateList_s: [[id => 1, name => x, age => 2], ...]
foreach ($userList_s as $key => $item) {
// 更新userList_s给其加上facecode和facepath字段并且加上cardno字段
// 即给对应的用户表查询结果增加facecode facepath cardno
$uid = $item['id'];
$info1 = $faceInfo[$uid];
$userList_s[$key]['facecode'] = $info1['facecode'] . '';
@ -196,7 +211,8 @@ if($qs == 'heartbeat' || $qs == 'addperson' || $qs == 'delperson') { //心跳
$sUserInfo = array();
// 遍历更新完成的userList_s中数据存入sUserInfo数组中key是用户idvalue是对应的item信息
// ["id1" => {xx}, "id2" => {xx}, "id3" => {xx}]
// sUserInfo: ["id1" => [id => 1, name => x], "id2" => {xx}, "id3" => {xx}]
// {"id1": {"id": "1", "name": "2"}, ...}
foreach ($userList_s as $item) {
$sUserInfo[$item['id']] = $item;
}
@ -205,6 +221,7 @@ if($qs == 'heartbeat' || $qs == 'addperson' || $qs == 'delperson') { //心跳
// 遍历获取的tb_pos_device_user表即用户设备表将其数据通过循环变为uid => item数据的数组
// 最终得到uid => item信息的数组
foreach ($userList_d as $item) {
// dUserInfo: [id1 => [id => 1, name => 2, ...], ...]
$dUserInfo[$item['uid']] = $item;
}
@ -216,14 +233,24 @@ if($qs == 'heartbeat' || $qs == 'addperson' || $qs == 'delperson') { //心跳
$addList = array(); //增加列表
$maxcount = 100;
// 遍历用户表
// 遍历当前公司用户表
// 其逻辑是:获取当前公司的用户表,然后获取对应设备中的用户列表,对比两张表中获取的数据,如果不同,则将用户添加到添加列表中
// 如果已下发并且距离上次下发时间已超过5分钟也将用户添加到添加列表
// 其实这样做有bug因为这样做设备的用户信息被记录在服务器而不是通过设备自主上传因此服务器并不知道设备实时的人员名单对于初始设备来说没有问题但是如果设备重置过人员名单那么将无法从服务器获取人员名单
foreach ($userList_s as $item) {
// 增加名单有三种情况:
/*
1. 设备用户表中不存在用户表中对应租户部分用户的信息
2. 设备用户表中记录的用户人脸代码等信息和用户表中的人脸代码等信息不一致
3. 设备用户表中状态为已下发但是过了5分钟之后仍未将设备用户表中的信息更新为已确认即未收到已确认的信号
*/
$uid = $item['id'];
// 设备用户表不存在对应用户的信息
if (!$dUserInfo[$uid]) {
// 往数组末尾添加一个item
$addList[] = $item;
if(count($addList) >= $maxcount) break; //限制一次20个
if (count($addList) >= $maxcount)
break; //限制一次20个
continue;
}
// 设备表的用户信息
@ -238,7 +265,8 @@ if($qs == 'heartbeat' || $qs == 'addperson' || $qs == 'delperson') { //心跳
) {
// 则将item加到addList末尾
$addList[] = $item;
if(count($addList) >= $maxcount) break;
if (count($addList) >= $maxcount)
break;
continue;
}
@ -246,7 +274,8 @@ if($qs == 'heartbeat' || $qs == 'addperson' || $qs == 'delperson') { //心跳
if ($item2['status'] == 1 && time() - strtotime($item2['sendtime']) > 300) { //距上次下发5分钟后还没收到确认
// 增加item到addList
$addList[] = $item;
if(count($addList) >= $maxcount) break;
if (count($addList) >= $maxcount)
break;
continue;
}
}
@ -261,11 +290,11 @@ if($qs == 'heartbeat' || $qs == 'addperson' || $qs == 'delperson') { //心跳
if ($item['facepath']) {
$face_url = 'https://' . $host . '/backstage/' . $item['facepath'];
}
// 获取设备用户表中对应设备和对应用户的数据
$row1 = $db->get_one("select * from tb_pos_device_user where device_id = '" . addslashes($device_id) . "' and uid = '" . addslashes($uid) . "'");
// sql拓展字符串
$sqlext = " name = '" . addslashes($item['username']) . "', cellphone = '" . addslashes($item['cellphone']) . "', deptname = '" . addslashes($item['deptname']) . "', cardno = '" . addslashes($item['cardno']) . "', facecode = '" . addslashes($item['facecode']) . "', sendtime = now(), status = 1, rs = '' ";
// 如果用户设备表中没有对应设备id和用户id的数据
// 如果用户设备表中没有对应设备id和用户id的数据,则插入记录,如果有,则更新
if (!$row1) {
// 插入记录
$db->query("insert into tb_pos_device_user set device_id = '" . addslashes($device_id) . "', uid = '" . addslashes($uid) . "', {$sqlext}, addtime = now() ", 'SILENT');
@ -331,7 +360,8 @@ if($qs == 'heartbeat' || $qs == 'addperson' || $qs == 'delperson') { //心跳
$db->query("update tb_pos_device_user set sendtime = now(), status = 4, rs = '',err_num=err_num+1 where id=" . $item['id']);
// 追加删除列表
$delList[] = $item;
if(count($delList) >= $maxcount) break; //限制一次20个
if (count($delList) >= $maxcount)
break; //限制一次20个
} else {
// 错误次数大于等于3直接删除记录
$db->query("delete from tb_pos_device_user where id=" . $item['id']);
@ -343,7 +373,8 @@ if($qs == 'heartbeat' || $qs == 'addperson' || $qs == 'delperson') { //心跳
// 不在用户表中添加item到删除列表
if (!$sUserInfo[$uid]) {
$delList[] = $item;
if(count($delList) >= $maxcount) break; //限制一次20个
if (count($delList) >= $maxcount)
break; //限制一次20个
continue;
}
}
@ -434,14 +465,16 @@ else if($qs == 'real') {
if ($uInfo['type'] == 'qr') { //取餐码
$uInfo = $db->get_one("select * from tb_user where id = '" . $order['uid'] . "'");
if(!$uInfo) exit;
if (!$uInfo)
exit;
$uid = $uInfo['id'];
}
break;
}
}
if($order) break;
if ($order)
break;
}
if (!$order) {
@ -487,7 +520,8 @@ else if($qs == 'real') {
if ($rs['status'] != 1) {
$message = '取餐失败';
if($rs['message']) $message = $rs['message'];
if ($rs['message'])
$message = $rs['message'];
$arr = array(
'result_code' => '2',
@ -542,8 +576,7 @@ else if($qs == 'real') {
'timeout' => $timeout, //取餐超时时间,秒为单位,注:特殊修改机型才有此功能
);
response_pos($api, $arr);
}
else if($api == 'infoquery') { //消费前查询用户
} else if ($api == 'infoquery') { //消费前查询用户
$uInfo = pos_check_user();
$uid = $uInfo['id'];
@ -570,13 +603,13 @@ else if($qs == 'real') {
'detail' => array(),
);
response_pos('infoquery', $arr);
}
else if($api == 'posonline') { //消费
} else if ($api == 'posonline') { //消费
$uInfo = pos_check_user();
$uid = $uInfo['id'];
$amount = intval($post['params']['amount']);
if($amount <= 0) exit;
if ($amount <= 0)
exit;
$amount = $amount / 100;
@ -584,16 +617,19 @@ else if($qs == 'real') {
$post = array('ftid' => $dining_hall_id, 'channelid' => $channel_id, 'fee' => $amount);
if ($pay_mode == 0) { //0人脸
$row1 = $db->get_one("select * from tb_user_face where user_id = '{$uid}'");
if(!$row1) exit;
if (!$row1)
exit;
$post['type'] = 'face';
$post['code'] = $row1['card'];
} else if ($pay_mode == 2) { //2卡序列号
$row1 = $db->get_one("select * from tb_user_idcard where user_id = '{$uid}' and state = '1'");
if(!$row1) exit;
if (!$row1)
exit;
$post['type'] = 'ic';
$post['code'] = $row1['cardno'];
} else if ($pay_mode == 5) { //5付款码
if($uInfo['type'] != 'qr') exit;
if ($uInfo['type'] != 'qr')
exit;
$post['type'] = 'qr';
$post['code'] = $uInfo['code'];
@ -606,7 +642,8 @@ else if($qs == 'real') {
$rs = json_decode($s, true);
if ($rs['status'] != 1) {
$message = '付款失败';
if($rs['message']) $message = $rs['message'];
if ($rs['message'])
$message = $rs['message'];
$arr = array(
'result_code' => '2',
@ -644,9 +681,7 @@ else if($qs == 'real') {
);
response_pos('posonline', $arr);
}
}
else if($qs == 'takephoto') {
} else if ($qs == 'takephoto') {
$arr = array(
'result_code' => '0', //0成功
'result_msg' => '',