You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

434 lines
15 KiB

6 months ago
<?php
namespace App\Repositories;
use App\Models\Course;
use App\Models\ThirdAppointmentLog;
use App\Models\User;
use Illuminate\Support\Facades\Log;
/**
* 达实门禁
*/
class DoorRepository
{
public $baseUrl;
public $key;
public $dptNo;
public $profix;
public function __construct()
{
//测试
// $this->baseUrl = 'https://api.v8.c3soft.cn';
// $this->key = '80A31FBF735813072E6C47AD3DE71FCC';
// $this->dptNo = '111111';
$this->baseUrl = 'http://221.224.221.130:8015';
$this->key = '750F712953A723381F5366D3FC957597';
$this->dptNo = '06';
$this->profix = 'ap_';
}
/**
* 卡片信息
* @return false|mixed
*/
public function getCardInfo()
{
$url = $this->baseUrl . '/api/AccessInterface/GetCardInfo';
$header[] = 'Content-Type: application/json;charset=UTF-8';
$params = [
'pageIndex' => 1,
'pageSize' => 10,
'timestamp' => time()
];
$params['sign'] = $this->sign($params);
try {
$params = json_encode($params);
$result = httpCurl($url, 'POST', $params, $header);
$result = json_decode($result, true);
if (empty($result['code'])) {
return $result['data'];
} else {
return false;
}
} catch (\Exception $e) {
return false;
}
}
/**
* 获取部门列表
*/
public function getDptInfo()
{
$url = $this->baseUrl . '/api/AccessInterface/GetDptInfo';
$header[] = 'Content-Type: application/json;charset=UTF-8';
$params = [
'pageIndex' => 1,
'pageSize' => 50,
'timestamp' => time()
];
$params['sign'] = $this->sign($params);
try {
$params = json_encode($params);
$result = httpCurl($url, 'POST', $params, $header);
$result = json_decode($result, true);
if (empty($result['code'])) {
return $result['data'];
} else {
return false;
}
} catch (\Exception $e) {
return false;
}
}
/**
* 同步人员信息
*/
public function putEmpInfo($model, &$out)
{
$url = $this->baseUrl . '/api/AccessInterface/PutEmpInfo';
$header[] = 'Content-Type: application/json;charset=UTF-8';
$params = [
'empNo' => $this->profix . $model->mobile,
'empName' => $model->name,
'empSex' => idCardToSex($model->idcard),
'dptNo' => $this->dptNo,
'empStatusId' => 1,
'etGrpDay' => date('Y-m-d H:i:s'),
'timestamp' => time()
];
$params['sign'] = $this->sign($params);
$result = [];
$finally = 0;
try {
$params = json_encode($params);
$result = httpCurl($url, 'POST', $params, $header);
$result = json_decode($result, true);
if (empty($result['code'])) {
$finally = 1;
return true;
} else {
$out = $result['msg'];
return false;
}
} catch (\Exception $e) {
$out = $e->getMessage();
return false;
} finally {
// 写日志
ThirdAppointmentLog::add($model->id, 0, $model->user_id, $url, $params, $result, $finally, '门禁预约-同步人员信息');
}
}
/**
* 门禁权限预约
*/
public function generateEmpAuthorSet1($model, $door, &$out)
{
if (!isset($door['doorName'])) {
$out = '门禁信息不存在';
return false;
}
// 同步人员信息
$putEmpInfo = $this->putEmpInfo($model, $outPutEmpInfo);
if (!$putEmpInfo) {
$out = $outPutEmpInfo;
return false;
}
$url = $this->baseUrl . '/api/AccessInterface/GenerateEmpAuthorSet1';
$header[] = 'Content-Type: application/json;charset=UTF-8';
$endTime = date('Y-m-d 23:59:59', strtotime($model->end_time));
$params = [
'empNo' => $this->profix . $model->mobile,
'doorName' => $door['doorName'],
'startTime' => date('Y-m-d 00:00:01', strtotime($model->start_time)),
'endTime' => $endTime,
'timestamp' => time()
];
// 预约开始时间需大于当前时间两分钟
if (date('Y-m-d', strtotime($model->start_time)) == date('Y-m-d')) {
$params['startTime'] = date('Y-m-d H:i:s', time() + 200);
}
$params['sign'] = $this->sign($params);
$result = [];
$finally = 0;
try {
$params = json_encode($params);
$resultJson = httpCurl($url, 'POST', $params, $header);
$result = json_decode($resultJson, true);
if (empty($result['code']) && ($result['msg'] == '执行成功' || str_contains($result['msg'], '已有权限'))) {
$finally = 1;
// 写日志
ThirdAppointmentLog::add($model->id, 0, $model->user_id, $url, $params, $result, $finally, '门禁预约', $door['doorName'], $endTime);
return true;
} else {
$out = $result['msg'];
// 写日志
ThirdAppointmentLog::add($model->id, 0, $model->user_id, $url, $params, $result, $finally, '门禁预约', $door['doorName'], $endTime);
return false;
}
} catch (\Exception $e) {
$out = $e->getMessage();
// 写日志
ThirdAppointmentLog::add($model->id, 0, $model->user_id, $url, $params, $result, $finally, '门禁预约', $door['doorName'], $endTime);
return false;
}
}
/**
* 门禁列表。更新配置room字段
*/
public function getAllDoorInfo()
{
$url = $this->baseUrl . '/api/AccessInterface/GetAllDoorInfo';
$header[] = 'Content-Type: application/json;charset=UTF-8';
$params = [
'pageIndex' => 1,
'pageSize' => 50,
'timestamp' => time()
];
$params['sign'] = $this->sign($params);
try {
$params = json_encode($params);
$result = httpCurl($url, 'POST', $params, $header);
$result = json_decode($result, true);
if (empty($result['code'])) {
return $result['data'];
} else {
return false;
}
} catch (\Exception $e) {
return false;
}
}
/**
* 获取员工开门二维码
*/
public function getEmpQrCode($model, &$out)
{
$url = $this->baseUrl . '/api/AccessInterface/GetEmpQrCode';
$header[] = 'Content-Type: application/json;charset=UTF-8';
$params = [
'empNo' => $this->profix . $model->mobile,
'timestamp' => time()
];
$params['sign'] = $this->sign($params);
$result = [];
$finally = 0;
try {
$params = json_encode($params);
$result = httpCurl($url, 'POST', $params, $header);
$result = json_decode($result, true);
if (empty($result['code'])) {
$model->qrcode = $result['data'];
$model->save();
$finally = 1;
return $result['data'];
} else {
$out = $result['msg'];
return false;
}
} catch (\Exception $e) {
$out = $e->getMessage();
return false;
} finally {
// 写日志
ThirdAppointmentLog::add($model->id, 0, $model->user_id, $url, $params, $result, $finally, '门禁预约-获取二维码信息');
}
}
/**
* 同步人员信息(报名渠道)
*/
public function putEmpInfoByCourse($model, $user, &$out)
{
$url = $this->baseUrl . '/api/AccessInterface/PutEmpInfo';
$header[] = 'Content-Type: application/json;charset=UTF-8';
$params = [
'empNo' => $this->profix . $user->mobile,
'empName' => $user->name,
'empSex' => idCardToSex($user->idcard),
'dptNo' => $this->dptNo,
'empStatusId' => 1,
'etGrpDay' => date('Y-m-d H:i:s'),
'timestamp' => time()
];
$params['sign'] = $this->sign($params);
$result = [];
$finally = 0;
try {
$params = json_encode($params);
$result = httpCurl($url, 'POST', $params, $header);
$result = json_decode($result, true);
if (empty($result['code'])) {
$finally = 1;
return true;
} else {
$out = $result['msg'];
return false;
}
} catch (\Exception $e) {
$out = $e->getMessage();
return false;
} finally {
// 写日志
ThirdAppointmentLog::add(0, $model->id, $model->user_id, $url, $params, $result, $finally, '门禁预约-同步人员信息');
}
}
/**
* 门禁权限预约(报名渠道)
*/
public function generateEmpAuthorSet1ByCourse($model, $door, &$out)
{
2 months ago
if (!is_array($door)) {
$door = json_decode($door, true);
}
6 months ago
if (!isset($door['doorName'])) {
$out = '门禁信息不存在';
return false;
}
$user = User::find($model->user_id);
$courseModel = Course::find($model->course_id);
// 同步人员信息
$putEmpInfo = $this->putEmpInfoByCourse($model, $user, $outPutEmpInfo);
if (!$putEmpInfo) {
$out = $outPutEmpInfo;
return false;
}
$url = $this->baseUrl . '/api/AccessInterface/GenerateEmpAuthorSet1';
$header[] = 'Content-Type: application/json;charset=UTF-8';
$endTime = date('Y-m-d 23:59:59', strtotime($courseModel->end_date));
$params = [
'empNo' => $this->profix . $user->mobile,
'doorName' => $door['doorName'],
'startTime' => date('Y-m-d 00:00:01', strtotime($courseModel->start_date)),
'endTime' => $endTime,
'timestamp' => time()
];
// 预约开始时间需大于当前时间两分钟
2 months ago
if (date('Y-m-d', strtotime($courseModel->start_date)) <= date('Y-m-d')) {
6 months ago
$params['startTime'] = date('Y-m-d H:i:s', time() + 200);
}
$params['sign'] = $this->sign($params);
$result = [];
$finally = 0;
try {
$params = json_encode($params, JSON_UNESCAPED_UNICODE);
$resultJson = httpCurl($url, 'POST', $params, $header);
$result = json_decode($resultJson, true);
if (empty($result['code'])) {
$finally = 1;
return true;
} else {
$out = $result['msg'];
return false;
}
} catch (\Exception $e) {
$out = $e->getMessage();
return false;
} finally {
// 写日志
ThirdAppointmentLog::add(0, $model->id, $model->user_id, $url, $params, $result, $finally, '门禁预约', $door['doorName'], $endTime);
}
}
/**
* 获取员工开门二维码(报名渠道)
*/
public function getEmpQrCodeByCourse($model, &$out)
{
$user = User::find($model->user_id);
$url = $this->baseUrl . '/api/AccessInterface/GetEmpQrCode';
$header[] = 'Content-Type: application/json;charset=UTF-8';
$params = [
'empNo' => $this->profix . $user->mobile,
'timestamp' => time()
];
$params['sign'] = $this->sign($params);
$result = [];
$finally = 0;
try {
$params = json_encode($params);
$result = httpCurl($url, 'POST', $params, $header);
$result = json_decode($result, true);
if (empty($result['code'])) {
$model->qrcode = $result['data'];
$model->save();
$finally = 1;
return $result['data'];
} else {
$out = $result['msg'];
return false;
}
} catch (\Exception $e) {
$out = $e->getMessage();
return false;
} finally {
// 写日志
ThirdAppointmentLog::add(0, $model->id, $model->user_id, $url, $params, $result, $finally, '门禁预约-获取二维码信息');
}
}
/**
* 签名
*/
public function sign($data)
{
// 确保时间戳参数存在且符合规范,这里仅作示意,实际应从$data中提取或生成
if (!isset($data['timestamp'])) {
return false;
}
// 过滤空值参数并按要求处理参数名
$filteredData = [];
foreach ($data as $key => $value) {
// 排除sign参数和空值参数同时将参数名首字母转为小写
if ($key !== 'sign' && $value !== '') {
$formattedKey = lcfirst($key); // 小驼峰命名法处理
$filteredData[$formattedKey] = $value;
}
}
// 按ASCII码字典序排序
ksort($filteredData, SORT_STRING | SORT_FLAG_CASE);
// 拼接URL键值对格式的字符串同时确保值被URL编码
$stringA = '';
foreach ($filteredData as $key => $value) {
$stringA .= $key . '=' . $value . '&';
}
$stringA = rtrim($stringA, '&'); // 去除尾部的&
// 拼接密钥并进行MD5运算
$stringSignTemp = $stringA . '&key=' . $this->key;
$signString = strtoupper(md5($stringSignTemp));
return $signString;
}
/**
* 判断是否合法预约门禁
*/
public function checkRepeatDoor($mobile, $appointmentConfig, $end_time, &$out)
{
foreach ($appointmentConfig as $value) {
$door = is_array($value->door) ? $value->door : json_decode($value->door, true);
$lastThirdAppointmentLogs = ThirdAppointmentLog::where('url', 'like', '%GenerateEmpAuthorSet1%')
->whereHas('appointment', function ($query) {
$query->where('status', 1);
})->where('finally', 1)
->where('send_data', 'like', '%' . $this->profix . $mobile . '%')
->where('door_name', $door['doorName'])
->orderBy('end_time', 'desc')
->first();
if ($lastThirdAppointmentLogs && strtotime($lastThirdAppointmentLogs->end_time) > strtotime($end_time)) {
$out = '请勿多次预约门禁';
return false;
}
}
return true;
}
}