12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace app\company\controller\api;
- use app\company\service\DataService;
- use library\Controller;
- use think\Db;
- class Push extends Controller
- {
-
- public function index()
- {
-
- $content = file_get_contents('php://input');
- if (empty($content)) $this->error('没有接收到数据');
-
- $where = ['is_deleted' => '0', 'status' => '1'];
- $users = Db::name('CompanyUser')->cache(10)->where($where)->column('id uid,nickname name,mobile_macs mac');
- if (empty($users)) $this->error('没有需要打卡的用户');
-
- $macs = [];
- foreach ($users as $user) foreach (explode("\n", preg_replace('/\s+/', "\n", $user['mac'])) as $mac) {
- if (DataService::applyMacValue($mac)) $macs[$mac] = ['uid' => $user['uid'], 'name' => $user['name']];
- }
-
- list($s, $e) = [0, 0];
- foreach (explode("\n", $content) as $line) {
- list($ip, $mac, $dsc) = explode(' ', preg_replace('/\s+/', ' ', trim($line)) . ' ');
- if (preg_match('/^(\d+\.?){4}$/', $ip) && DataService::applyMacValue($mac)) {
- if (isset($macs[$mac])) {
- $s++;
- $this->writeUser($ip, $mac, strtoupper($dsc), $macs);
- } else {
- $e++;
- $this->writeNone($ip, $mac, strtoupper($dsc));
- }
- }
- }
- return "接收到{$s}个已知设备推送,{$e}个未知设备推送。" . PHP_EOL . PHP_EOL;
- }
-
- private function writeUser($ip, $mac, $desc, $macs)
- {
- if (isset($macs[$mac])) {
- $data = $macs[$mac];
- $data['ip'] = $ip;
- $data['mac'] = $mac;
- $data['desc'] = $desc;
- $data['date'] = date('Y-m-d');
- $data['end_at'] = date('Y-m-d H:i:s');
- data_save('CompanyUserClock', $data, 'uid', ['date' => $data['date']]);
- }
- }
-
- private function writeNone($ip, $mac, $desc)
- {
-
- }
- }
|