PHP语言对接中控考勤机实现打卡推送

更新时间:2023-12-09 21:24:06 阅读: 评论:0

2023年12月9日发(作者:教师节材料)

-

PHP语言对接中控考勤机实现打卡推送

PHP语言对接中控考勤机实现打卡推送

我选的框架是tp5.1

考勤机型号:中控M880。固件版本:ver 6.5.4(build162)

制造商:ZKTECO Inc

mcu ver 27

push 2.32

camera ver V2.0,c2

push文档获取:公众号搜索 霖深时见露 回复 push文档 获取

几个要点,做这个功能的肯定都有push文档。第一个路由对应到。 iclock/cdata。构造一个这样的路由。apache下,可以选择重写,也

可以选择route改变路由。nginx下重写一下路由。基本就可以了。下面贴代码:

<?php

namespace appiclockcontroller;

u thinkController;

u thinkfacadeRequest;

u appiclockmodelMechine;

u appiclockmodelChecklog;

u thinkfacadeLog;

u appiclockmodelCmds;

//u appmobilecontrollerPushcheck;

class Cdata extends Controller

{

public function login()

{

//exit;

$flag = json_encode(Request::param());

$sn = Request::param('SN');

file_put_contents('',$_EOL,FILE_APPEND);

//1.判断考勤机是否存在

$res = Mechine::where('sn',$sn)->find();

if($res)

{

//存在,先更新考勤机信息

Mechine::where('sn',$sn)->update(['update_time'=>time()]);

if(Request::param('options') == 'all')

{

//首次接收考勤机发来的消息

$return = "GET OPTION FROM : {$sn}";

$array = [

'Stamp' => time(),

'OpStamp' => time(),

'PhotoStamp' => time(),

'ErrorDelay' => 30,

'Delay' => 30,

'TransFlag' => '',

'Realtime' => 1,

'TransInterval'=>0,

'Encrypt' => 0,

'ServerVer' => '2.32 '.date('Y-m-d'),

'ATTLOGStamp' => time(),

'ATTPHOTOStamp'=>time(),

];

];

$unix_time = date_default_timezone_t('GMT');

header("HTTP/1.0 200 OK");

header("Content-Type: text/plain");

header("Date:".$unix_time);

$string = "";

foreach ($array as $key => $val)

$return .= $key .'='.$_EOL;

return $return;

}

el

{

if(Request::param('table') == 'ATTLOG')

{

//获取刷卡时间和卡号

$stamp = json_encode(Request::param('Stamp'));

// file_put_contents('',$_EOL,FILE_APPEND);

$text = json_encode(file_get_contents("php://input"));

file_put_contents('',$_EOL,FILE_APPEND);

return "ok";

}

elif(Request::param('table') == 'OPERLOG')

{

$stamp = json_encode(Request::param());

//file_put_contents('',$_EOL,FILE_APPEND);

$text = json_encode(file_get_contents("php://input"));

// file_put_contents('',$_EOL,FILE_APPEND);

return "ok";

}

elif(Request::param('table') == 'ATTPHOTO')

{

$text = file_get_contents("php://input");

file_put_contents('',$text,FILE_APPEND);

$str2 = substr($text,68);

$filepath = 'attendance/';

if(!is_dir($filepath))mkdir($filepath,0777,true);

$filename=$().'.jpg';

$tmp = explode('.jpg', $text);

$pin = explode('-', $tmp[0]);

$checktimetmp = $pin[0];

$checktime = explode('=', $checktimetmp);

$checktime = strtotime($checktime[1]);

//pin:用户工号

$pin = $pin[1];

if($pin){ //用户验证通过

//sn:设备号

$sntmp = explode('=', $text);

$sn = str_replace('size', '', $sntmp[2]);

$checklog = model('Checklog')->get(['pin'=>$pin,'checktime'=>$checktime,'sn'=>$sn]);

if($checklog){

return iconv("UTF-8", "GB2312//IGNORE", 'ok');

}el{

file_put_contents($filename,$str2);

file_put_contents('',$_EOL,FILE_APPEND);

$info = Cmds::get(function($query) u ($pin){

$query->where(['pin'=>$pin,'status'=>1])->order('update_time desc')->field('card,student_name');

});

$card = $info->card;

$student_name = $info->student_name;

$model = new Checklog();

$rs = $model->save(['pin'=>$pin,'checktime'=>$checktime,'pic'=>$filename,'sn'=>$sn,'card'=>$card,'student_name'=>$student_name]);

if(!$rs){

$rs = $model->save(['pin'=>$pin,'checktime'=>$checktime,'pic'=>$filename,'sn'=>$sn,'card'=>$card,'student_name'=>$student_name]);

}

//推送考勤信息

//推送考勤信息

// $this->ndTempletemsg($model->id);

$this->https_request(''.config('domain').'/mobile/pushcheck/ndTempletemsg?checkid='.$model->id);

}

return iconv("UTF-8", "GB2312//IGNORE", 'ok');

}

/*结束*/

}

}

}

el

{

//不存在,写入考勤机信息

$data = ['sn' => $sn,'options'=>$flag,'create_time'=>time()];

Mechine::inrt($data);

}

}

function get_contents() {

$filename=time().'.png';

if(file_put_contents($filename,$xmlstr)){

echo 'success';

}el{

echo 'failed';

}

}

//两个字符串之间的字符串

public function str_between(){

$str = 'DATA UPDATE USERINFO PIN=1tName=测试2tPasswd=123456tPri=14tsCard=[1985100]';

$str1 = 'Name=';

$str2 = 'tPasswd';

$start = mb_strpos($str,$str1)+mb_strlen($str1);

$end = mb_strpos($str,$str2);

$length = $end-$start;

dump($length);

$needle = mb_substr($str,$start,$length,'utf8');

dump($needle);

return $needle;

}

$xmlstr = file_get_contents('php://input')?file_get_contents('php://input') : gzuncompress($GLOBALS['HTTP_RAW_POST_DATA']);//得到post过来的二进制原

//发送考勤打卡推送模板消息

public function ndTempletemsg($checkid){

// $checkid = input('d'); //考勤打卡id

$checkinfo = Checklog::get(intval($checkid)); //考勤打卡信息

if(!$checkinfo)return;

$card_sn = $checkinfo->card; //卡号

$ur = model('ur')->get(array('card_sn'=>$card_sn)); //根据卡号查找学生信息

if(!$ur)return;

$ur_id = $ur->id;

$parentinfo = model('wxur')->all(array('ur_id'=>intval($ur_id))); //查询绑定了该学生的所有家长老师

$access_token = $this->getWXToken();

$url = '/cgi-bin/message/template/nd?access_token='.$access_token;

foreach ($parentinfo as $key=>$value){

$openid = $value->openid;

$data = array(

"tour"=>$openid,

"template_id"=>"Y44crAtSKT47r8Cd5mnCM844vn7Iw6uVvU2Dq7p2tng",

'url'=>''.config('domain').'/'.$checkinfo->pic,

'data'=>array(

'first'=>array(

'first'=>array(

'value'=>'您好,您有一条考勤消息提醒!',

'color'=>'#41495e'

),

'keyword1'=>array(

'value'=>$ur['real_name'], //学生姓名

),

'keyword2'=>array(

'value'=>date('Y-m-d H:i:s',$checkinfo->checktime), //打卡时间

),

'keyword3'=>array(

'value'=>$checkinfo->status, //考勤状态

),

'keyword4'=>array(

'value'=>$checkinfo->card, //接送卡号

),

'keyword5'=>array(

'value'=>$checkinfo->address, //地点

),

"remark"=>array(

'value'=>"点击查看考勤图片",

'color'=>'#41495e'

)

)

);

$result = $this->https_request($url,json_encode($data));

$result = json_decode($result,true);

if($result['errcode'] == 0){

//保存推送通知

$model = new Checklog();

$model->save(['pushstate'=>1],['id'=>$checkid]);

return true;

}el{

//推送失败 TODO

return fal;

}

}

}

//获取微信access_token

public function getWXToken(){

// error_reporting(E_ERROR | E_WARNING | E_PARSE);

// $wxconfig = config('wxconfig');

// $cache_file = dirname(__FILE__).'/../../../access_';

// if(file_exists($cache_file)) $info = json_decode(file_get_contents($cache_file),true);

// if($info['access_token'] && $info['expire_time']>time()){

// return $info['access_token'];

// }

// $url = '/cgi-bin/token?grant_type=client_credential&appid='.$wxconfig['appid'].'&cret='.$wxconfig['appcret'];

// $result = $this->https_request($url);

// $data = json_decode($result,true);

// if($data['access_token']){

// $arr = array();

// $arr['access_token'] = $data['access_token'];

// $arr['expire_time'] = time() + 7000;

// file_put_contents($cache_file,json_encode($arr));

// return $data['access_token'];

// }

// el return '';

}

//https请求(支持GET和POST)

public function https_request($url, $data = null){

$curl = curl_init();

curl_topt($curl, CURLOPT_URL, $url);

curl_topt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);

curl_topt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

if (!empty($data)){ if (!empty($data)){ curl_topt($curl, CURLOPT_POST, 1); curl_topt($curl, CURLOPT_POSTFIELDS, $data); } curl_topt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_clo($curl); return $output; }}<?phpnamespace appiclockcontroller;u thinkController;u thinkfacadeRequest;u appiclockmodelCmds;class Getrequest extends Controller{ public function login(){ $sn = Request::param('SN'); // file_put_contents('',$_EOL,FILE_APPEND);// $check = Cmds::where('id',11)->find(); $check = Cmds::where('c_name','like','%DATA UPDATE USERINFO%')->where('status',0)->order('id asc')->find(); if(!$check)return fal; //转换卡号,发送命令 $c_name = $this->changecard($check['c_name']); if($check['status'] == '0' && $check['sn'] == $sn) { $unix_time = date_default_timezone_t('GMT'); header("HTTP/1.0 200 OK"); header("Content-Type: text/plain"); header("Date:".$unix_time); $cmd = "C:{$check['c_id']}:{$c_name}".PHP_EOL; Cmds::where('id',$check['id'])->update(['update_time'=>time(),'status'=>1]); //把CMD的命令 $gbkcmd = iconv("UTF-8", "GB2312//IGNORE", $cmd); return $gbkcmd; } } //转换卡号为16进制后重新组合 public function changecard($c_name){ $card = explode('sCard=',$c_name); $tmpc_name = $card[0]; $card = str_replace('[', '', $card[1]); $card = dechex(str_replace(']', '', $card)); //转换成16进制 $str1 = substr($card, 0,2); $str2 = substr($card, 4,2); $str3 = substr($card, 2,2); $card = $str2.$str3.$str1; //重新排列后的卡号 $c_name = $tmpc_name.'sCard=['.$card.'0000]'; $c_name = str_replace('t',iconv("UTF-8", "GBK//IGNORE", "t") , $c_name); return $c_name; } public function hello($name = 'ThinkPHP5'){ return 'hello,' . $name; } public function test(){ $check = Cmds::where('c_name','like','%DATA UPDATE USERINFO%')->where('status',0)->order('id asc')->find(); dump($check); }} // "C:10:DATA UPDATE USERINFO cardno=tpin=32tpassword=123tgroup=0tstarttime=0tendtime=0tname=miketsuperauthorize=

<?php

namespace appiclockcontroller;

u thinkController;

u thinkfacadeRequest;

class Devicecmd extends Controller

{

public function index()

{

$text = file_get_contents("php://input");

if(stristr($text,'Return=0'))

{

$text = json_encode($text);

file_put_contents('', $text . PHP_EOL, FILE_APPEND);

$unix_time = date_default_timezone_t('GMT');

header("HTTP/1.1 200 OK");

header("Content-Type: text/plain");

header("Date:" . $unix_time);

// $cmd = "C:1:SHELL CMD_STRING".PHP_EOL;

// return $cmd;

return 'OK';

}

}

public function hello($name = 'ThinkPHP5')

{

return 'hello,' . $name;

}

}

之后的逻辑可以自己按需求来写

-

PHP语言对接中控考勤机实现打卡推送

本文发布于:2023-12-09 21:24:06,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/zhishi/a/1702128246241129.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:PHP语言对接中控考勤机实现打卡推送.doc

本文 PDF 下载地址:PHP语言对接中控考勤机实现打卡推送.pdf

标签:考勤机   推送   考勤   打卡
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 实用文体写作网旗下知识大全大全栏目是一个全百科类宝库! 优秀范文|法律文书|专利查询|