docs: 增加文档

Change-Id: Ic52b02e89c54f15a9f57e52356f55bce732a7723
This commit is contained in:
ui_none 2024-08-12 18:00:43 +08:00
parent 08211299d8
commit 9cd4020dda
1 changed files with 61 additions and 6 deletions

View File

@ -25,6 +25,11 @@ if(strlen($s1) > 2000) {
$s1 = substr($s1, 0, 2000).'..'; $s1 = substr($s1, 0, 2000).'..';
} }
// 于请求中获取了serialno、remote_addr信息
// 通过serialno从数据库中获取了设备信息在顶层中获取了记录id、company_id、dining_hall_id和channel_id信息
// 并于请求的POST参数中获取了sign和其他详细信息
// 同时将记录插入到日志中
// 插入POS机日志记录 // 插入POS机日志记录
$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() "); $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() ");
$log_id = $db->insert_id(); $log_id = $db->insert_id();
@ -46,6 +51,7 @@ if($sign2 !== $post['sign']) { //签名错误
} }
// 将api和data数据处理编码后插入到tb_pos_device_log表并终止脚本运行 // 将api和data数据处理编码后插入到tb_pos_device_log表并终止脚本运行
// 回应时填充参数、返回的API接口数据、错误提示等。
function response_pos($api, $data) { function response_pos($api, $data) {
global $deviceInfo, $post, $log_id, $db; global $deviceInfo, $post, $log_id, $db;
$noncestr = md5(microtime().'_'.rand()); $noncestr = md5(microtime().'_'.rand());
@ -117,47 +123,62 @@ function pos_check_user() {
// 顶层代码判断qs中参数 // 顶层代码判断qs中参数
if($qs == 'heartbeat' || $qs == 'addperson' || $qs == 'delperson') { //心跳 if($qs == 'heartbeat' || $qs == 'addperson' || $qs == 'delperson') { //心跳
// 新增人员 // 新增人员
// 设备收到服务器心跳包响应需要执行人员增加之后,向服务器发送设备增加名单完成应答接口
if($qs == 'addperson' && $post['whitelist']) { if($qs == 'addperson' && $post['whitelist']) {
// 循环遍历人员名单
foreach($post['whitelist'] as $item) { foreach($post['whitelist'] as $item) {
// 将account_id中的yzms_前缀去掉
$uid = intval(str_replace('yzms_', '', $item['account_id'])); $uid = intval(str_replace('yzms_', '', $item['account_id']));
// 获取记录
$rec_id = intval($item['rec_id']); $rec_id = intval($item['rec_id']);
// 更新用户设备表中对应记录rec_id和对应用户uid的状态status=2表示确认增加成功
$db->query("update tb_pos_device_user set status=2,rs='".$item['result_code']."' where id = '{$rec_id}' and uid = '{$uid}'"); $db->query("update tb_pos_device_user set status=2,rs='".$item['result_code']."' where id = '{$rec_id}' and uid = '{$uid}'");
} }
} }
// 删除人员 // 删除人员
if($qs == 'delperson' && $post['whitelist']) { if($qs == 'delperson' && $post['whitelist']) {
// 循环遍历人员名单
foreach($post['whitelist'] as $item) { foreach($post['whitelist'] as $item) {
// 从account_id中去掉yzms_前缀
$uid = intval(str_replace('yzms_', '', $item['account_id'])); $uid = intval(str_replace('yzms_', '', $item['account_id']));
// 获取记录id
$rec_id = intval($item['rec_id']); $rec_id = intval($item['rec_id']);
$db->query("delete from tb_pos_device_user where id = '{$rec_id}' and uid = '{$uid}'"); $db->query("delete from tb_pos_device_user where id = '{$rec_id}' and uid = '{$uid}'");
} }
} }
// 心跳 // 如果当前查询并非心跳或者当前时间和设备的synctime差超过1分钟则执行
if($qs != 'heartbeat' || (time() - strtotime($deviceInfo['synctime']) > 60)) { //1分钟检测一次用户同步 if($qs != 'heartbeat' || (time() - strtotime($deviceInfo['synctime']) > 60)) { //1分钟检测一次用户同步
// 更新posdevice表的同步时间
$db->query("update tb_pos_device set synctime=now() where id = ".$device_id); $db->query("update tb_pos_device set synctime=now() where id = ".$device_id);
// 从tb_user表中获取所有启用且属于公司公司的用户列表
$userList_s = $db->get_all("select id, username, cellphone, deptname from tb_user where enabled = '1' and company_id = '{$company_id}'"); $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}'"); $userList_d = $db->get_all("select * from tb_pos_device_user where device_id = '{$device_id}'");
// tb_user_face和tb_user表关联获取对应信息
$data = $db->get_all("select a.* from tb_user_face a, tb_user b where a.user_id=b.id and b.company_id = '{$company_id}'"); $data = $db->get_all("select a.* from tb_user_face a, tb_user b where a.user_id=b.id and b.company_id = '{$company_id}'");
$faceInfo = array(); $faceInfo = array();
// 提取上一步得到的关联信息并添加到faceInfo数组中判断此文件是否存在于服务器后台目录存在则将item信息存进dict数据中key是user_idvalue是item
foreach($data as $item) { foreach($data as $item) {
if(is_file("../backstage/".$item['path'])) { if(is_file("../backstage/".$item['path'])) {
$faceInfo[$item['user_id']] = $item; $faceInfo[$item['user_id']] = $item;
} }
} }
// 关联用户表和用户id卡表获取信息
$data = $db->get_all("select a.* from tb_user_idcard a, tb_user b where a.user_id=b.id and b.company_id = '{$company_id}' and a.state=1 order by a.id"); $data = $db->get_all("select a.* from tb_user_idcard a, tb_user b where a.user_id=b.id and b.company_id = '{$company_id}' and a.state=1 order by a.id");
$cardInfo = array(); $cardInfo = array();
// 提取上一步获取的信息并将数据循环加入到卡信息数组中类似List[Dict]key是user_idvalue是item
foreach($data as $item) { foreach($data as $item) {
$cardInfo[$item['user_id']] = $item; $cardInfo[$item['user_id']] = $item;
} }
// 从获取的该公司user_list表信息中循环遍历获取对应用户id的facecode,path,cardno信息添加到userList_s数组中
// 最终facecode,path和cardno作为用户设备需要下发的信息添加到userList_s中
foreach($userList_s as $key => $item) { foreach($userList_s as $key => $item) {
$uid = $item['id']; $uid = $item['id'];
$info1 = $faceInfo[$uid]; $info1 = $faceInfo[$uid];
@ -169,28 +190,40 @@ if($qs == 'heartbeat' || $qs == 'addperson' || $qs == 'delperson') { //心跳
} }
$sUserInfo = array(); $sUserInfo = array();
// 遍历更新完成的userList_s中数据存入sUserInfo数组中key是用户idvalue是对应的item信息
// ["id1" => {xx}, "id2" => {xx}, "id3" => {xx}]
foreach($userList_s as $item) { foreach($userList_s as $item) {
$sUserInfo[$item['id']] = $item; $sUserInfo[$item['id']] = $item;
} }
$dUserInfo = array(); $dUserInfo = array();
// 遍历获取的tb_pos_device_user表即用户设备表将其数据通过循环变为uid => item数据的数组
// 最终得到uid => item信息的数组
foreach($userList_d as $item) { foreach($userList_d as $item) {
$dUserInfo[$item['uid']] = $item; $dUserInfo[$item['uid']] = $item;
} }
// 从全局配置表中获取域名需要注意新版表中将class字段更改成了name字段
$row = $db->get_one("select * from tb_config where class = 'HOST'"); $row = $db->get_one("select * from tb_config where class = 'HOST'");
// 设置域名
$host = $row['value']; $host = $row['value'];
$addList = array(); //增加列表 $addList = array(); //增加列表
$maxcount = 100; $maxcount = 100;
// 遍历用户表
foreach($userList_s as $item) { foreach($userList_s as $item) {
$uid = $item['id']; $uid = $item['id'];
// 设备用户表不存在对应用户的信息
if(!$dUserInfo[$uid]) { if(!$dUserInfo[$uid]) {
// 往数组末尾添加一个item
$addList[] = $item; $addList[] = $item;
if(count($addList) >= $maxcount) break; //限制一次20个 if(count($addList) >= $maxcount) break; //限制一次20个
continue; continue;
} }
// 设备表的用户信息
$item2 = $dUserInfo[$uid]; $item2 = $dUserInfo[$uid];
// 如果设备用户表的记录和用户表的记录不一致
if( if(
($item2['facecode'].'') !== ($item['facecode'].'') ($item2['facecode'].'') !== ($item['facecode'].'')
|| ($item2['cardno'].'') !== ($item['cardno'].'') || ($item2['cardno'].'') !== ($item['cardno'].'')
@ -198,12 +231,15 @@ if($qs == 'heartbeat' || $qs == 'addperson' || $qs == 'delperson') { //心跳
|| ($item2['cellphone'].'') !== ($item['cellphone'].'') || ($item2['cellphone'].'') !== ($item['cellphone'].'')
|| ($item2['deptname'].'') !== ($item['deptname'].'') || ($item2['deptname'].'') !== ($item['deptname'].'')
) { ) {
// 则将item加到addList末尾
$addList[] = $item; $addList[] = $item;
if(count($addList) >= $maxcount) break; if(count($addList) >= $maxcount) break;
continue; continue;
} }
// 设备用户表的status为1表示已下发增加但还未收到确认并且距上次下发5分钟后还没收到确认
if($item2['status'] == 1 && time()-strtotime($item2['sendtime']) > 300) { //距上次下发5分钟后还没收到确认 if($item2['status'] == 1 && time()-strtotime($item2['sendtime']) > 300) { //距上次下发5分钟后还没收到确认
// 增加item到addList
$addList[] = $item; $addList[] = $item;
if(count($addList) >= $maxcount) break; if(count($addList) >= $maxcount) break;
continue; continue;
@ -216,21 +252,26 @@ if($qs == 'heartbeat' || $qs == 'addperson' || $qs == 'delperson') { //心跳
foreach($addList as $item) { foreach($addList as $item) {
$uid = $item['id']; $uid = $item['id'];
$face_url = ''; $face_url = '';
// 拼接人脸路径
if($item['facepath']) { if($item['facepath']) {
$face_url = 'https://'.$host.'/backstage/'.$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)."'"); $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 = '' "; $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的数据
if(!$row1) { if(!$row1) {
// 插入记录
$db->query("insert into tb_pos_device_user set device_id = '".addslashes($device_id)."', uid = '".addslashes($uid)."', {$sqlext}, addtime = now() ", 'SILENT'); $db->query("insert into tb_pos_device_user set device_id = '".addslashes($device_id)."', uid = '".addslashes($uid)."', {$sqlext}, addtime = now() ", 'SILENT');
$log_id1 = $db->insert_id(); $log_id1 = $db->insert_id();
} else { } else {
// 有则更新
$db->query("update tb_pos_device_user set {$sqlext} where id=".$row1['id']); $db->query("update tb_pos_device_user set {$sqlext} where id=".$row1['id']);
$log_id1 = $row1['id']; $log_id1 = $row1['id'];
} }
// 人员名单
$whitelist[] = array ( $whitelist[] = array (
'rec_id' => $log_id1.'', //可为数据库中的唯一id可以是字符串可以是整数可为空字符 'rec_id' => $log_id1.'', //可为数据库中的唯一id可以是字符串可以是整数可为空字符
'account_id' => 'yzms_'.$uid, //帐号唯一50个字符必填 'account_id' => 'yzms_'.$uid, //帐号唯一50个字符必填
@ -262,6 +303,7 @@ if($qs == 'heartbeat' || $qs == 'addperson' || $qs == 'delperson') { //心跳
'whitelist' => $whitelist, 'whitelist' => $whitelist,
); );
// 响应增加人员的api向设备回送数据往设备增加人员信息
response_pos('addperson', $arr); response_pos('addperson', $arr);
exit; exit;
} }
@ -275,19 +317,25 @@ if($qs == 'heartbeat' || $qs == 'addperson' || $qs == 'delperson') { //心跳
foreach($userList_d as $item) { foreach($userList_d as $item) {
$uid = $item['uid']; $uid = $item['uid'];
// status为4表示已下发删除
if($item['status'] == 4) { if($item['status'] == 4) {
if(time()-strtotime($item['sendtime']) > 300) { //距上次下发删除5分钟后还没收到确认 if(time()-strtotime($item['sendtime']) > 300) { //距上次下发删除5分钟后还没收到确认
// 错误次数小于3
if($item['err_num'] < 3) { if($item['err_num'] < 3) {
// 更新用户设备表错误次数
$db->query("update tb_pos_device_user set sendtime = now(), status = 4, rs = '',err_num=err_num+1 where id=".$item['id']); $db->query("update tb_pos_device_user set sendtime = now(), status = 4, rs = '',err_num=err_num+1 where id=".$item['id']);
// 追加删除列表
$delList[] = $item; $delList[] = $item;
if(count($delList) >= $maxcount) break; //限制一次20个 if(count($delList) >= $maxcount) break; //限制一次20个
} else { } else {
// 错误次数大于等于3直接删除记录
$db->query("delete from tb_pos_device_user where id=".$item['id']); $db->query("delete from tb_pos_device_user where id=".$item['id']);
} }
} }
continue; continue;
} }
// 不在用户表中添加item到删除列表
if(!$sUserInfo[$uid]) { if(!$sUserInfo[$uid]) {
$delList[] = $item; $delList[] = $item;
if(count($delList) >= $maxcount) break; //限制一次20个 if(count($delList) >= $maxcount) break; //限制一次20个
@ -295,6 +343,7 @@ if($qs == 'heartbeat' || $qs == 'addperson' || $qs == 'delperson') { //心跳
} }
} }
// 数据更新完成,开始删除列表
if($delList) { if($delList) {
$whitelist = array(); $whitelist = array();
foreach($delList as $item) { foreach($delList as $item) {
@ -314,23 +363,29 @@ if($qs == 'heartbeat' || $qs == 'addperson' || $qs == 'delperson') { //心跳
'whitelist' => $whitelist, 'whitelist' => $whitelist,
); );
// 删除人员
response_pos('delperson', $arr); response_pos('delperson', $arr);
exit; exit;
} }
} }
// 记录
$records = $post['records']; $records = $post['records'];
$arr = array ( $arr = array (
'setmname' => '', 'setmname' => '',
); );
// 如果有记录
if($records) { if($records) {
$records2 = array(); $records2 = array();
// 遍历记录并将record的记录变成"id" => realid加入到record2中
// like: [[id => 1], [id => 2], [id => 3]...]
foreach($records as $item) { foreach($records as $item) {
$records2[] = array('id' => $item['id']); $records2[] = array('id' => $item['id']);
} }
$arr['records'] = $records2; $arr['records'] = $records2;
} }
// 服务器向设备回送心跳包,包括记录
response_pos('heartbeat', $arr); response_pos('heartbeat', $arr);
} }
@ -503,7 +558,6 @@ else if($qs == 'real') {
'sign' => '', 'sign' => '',
'Corrections' => '', 'Corrections' => '',
) )
), ),
'title' => '云中美食', 'title' => '云中美食',
'emp_fname' => $uInfo['username'], 'emp_fname' => $uInfo['username'],
@ -542,6 +596,7 @@ else if($qs == 'real') {
exit; exit;
} }
// 服务器向本机/api/usr/qrpay接口发送数据
$s = self_query("/api/user/qrpay", $post); $s = self_query("/api/user/qrpay", $post);
$rs = json_decode($s, true); $rs = json_decode($s, true);
if($rs['status'] != 1) { if($rs['status'] != 1) {