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

230 lines
8.3 KiB
PHP

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
use App\Services\LogService;
use Illuminate\Support\Facades\DB;
class Tools
{
//查询某天是星期几
public static function GetWeekName($date)
{
$dayOfWeek = date('N', strtotime($date)); // 获取星期几1代表星期一7代表星期日
$weekname='';
switch ($dayOfWeek) {
case 1:
$weekname= "星期一";
break;
case 2:
$weekname= "星期二";
break;
case 3:
$weekname="星期三";
break;
case 4:
$weekname= "星期四";
break;
case 5:
$weekname= "星期五";
break;
case 6:
$weekname= "星期六";
break;
case 7:
$weekname= "星期日";
break;
default:
$weekname= "无效日期";
}
return $weekname;
}
//计算年龄,返回年龄 如 31
public static function calculateAge($birthdate) {
$birthdate = new DateTime($birthdate);
$currentDate = new DateTime();
$age = $currentDate->diff($birthdate)->y;
return $age;
}
//计算年龄,返回年龄 如 31岁3月3天
public static function calculateAgeText($birthdate) {
$birthdate = new DateTime($birthdate);
$currentDate = new DateTime();
$ageDiff = $currentDate->diff($birthdate);
$years = $ageDiff->y;
$months = $ageDiff->m;
$days = $ageDiff->d;
$ageString = "";
if ($years > 0) {
$ageString .= $years . "";
}
if ($months > 0) {
$ageString .= $months . "";
}
if ($days > 0) {
$ageString .= $days . "";
}
return $ageString;
}
//根据生日返回年龄
public static function getAgeFromBirthday($birthday, $format = 'Y-m-d')
{
// 尝试按照指定格式解析日期
$date = DateTime::createFromFormat($format, $birthday);
// 检查日期是否有效以及是否完全匹配格式
if (!$date || $date->format($format) !== $birthday) {
return ''; // 无效的日期格式,返回空字符串
}
$today = new DateTime();
$interval = $today->diff($date);
return $interval->y;
}
public static function Post($url, $data_string,$mark='')
{
$data=[
'request_url'=>$url,
'post_data'=>$data_string,
];
$log_id=self::RequestLog($data,0,$mark);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($data_string)
]);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string);
$r = curl_exec($curl);
curl_close($curl);
// $r='{"resultMsg":"登记成功1111","resultCode":"0","resultDatas":[{"queueNo":"16","checkNo":"2108180032","videoRoom":"超声影像室","isStudyGroupRegistration":"N","imageNo":"p2108180029","currentNum":1},{"queueNo":"17","checkNo":"2108180033","videoRoom":"超声影像室","isStudyGroupRegistration":"N","imageNo":"p2108180029","currentNum":2},{"queueNo":"18","checkNo":"2108180034","videoRoom":"超声影像室","isStudyGroupRegistration":"N","imageNo":"p2108180029","currentNum":3}],"isSuccess":true}';
$data=[
'response_data'=>$r
];
self::RequestLog($data,$log_id);
return $r;
}
public static function PostSoap($url, $data_string,$mark='')
{
$data=[
'request_url'=>$url,
'post_data'=>$data_string,
];
$log_id=self::RequestLog($data,0,$mark);
$xml_data = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:good="http://goodwillcis.com" xmlns:bjg="http://bjgoodwillcis.com">
<soapenv:Header>
<good:JiaheSecurity>
<!--Optional:-->
<good:UserName>?</good:UserName>
<!--Optional:-->
<good:Token>?</good:Token>
<!--Optional:-->
<good:Sign>?</good:Sign>
<!--Optional:-->
<good:TimeStamp>?</good:TimeStamp>
</good:JiaheSecurity>
</soapenv:Header>
<soapenv:Body>
<bjg:Send>
<!--Optional:-->
<bjg:pInput><![CDATA['.$data_string.']]></bjg:pInput>
</bjg:Send>
</soapenv:Body>
</soapenv:Envelope> ';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/soap+xml;charset=utf-8',
'Content-Length: ' . strlen($xml_data)
]);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
$r = curl_exec($ch);
\Illuminate\Support\Facades\Log::info($r);
try {
$xml = simplexml_load_string($r, 'SimpleXMLElement', LIBXML_NOCDATA);
$namespaces = $xml->getNamespaces(true);
$xml->registerXPathNamespace('soapenv', $namespaces['SOAP-ENV']);
$xml->registerXPathNamespace('ns', 'http://bjgoodwillcis.com');
// 提取 CDATA 内容
$sendResult = $xml->xpath('//ns:SendResult')[0];
$jsonData = (string)$sendResult;
// $res = explode('<SOAP-ENV:Body>', $r)[1];
// $res = explode('</SOAP-ENV:Body>', $res)[0];
// $xmlData = simplexml_load_string($res);
// $jsonData = json_encode($xmlData, JSON_UNESCAPED_UNICODE);
}catch (Exception $e) {
$jsonData = $e->getMessage();
}
// dd($res);
// $result = json_decode($jsonData, true);
curl_close($ch);
$data=[
'response_data'=>$r
];
self::RequestLog($data,$log_id);
return $jsonData;
}
public static function RequestLog($arr,$id,$mark='')
{ //记录请求日志
date_default_timezone_set('PRC');
LogService::CheckTableName();
$table_name = 'zz_request_log_' . date('ym');
$response_data = isset($arr['response_data']) ? self::JsonEncode($arr['response_data']) : '';
$header_data = isset($arr['request_header']) ?self::JsonEncode($arr['request_header']):'';
$post_data = isset($arr['post_data'])?self::JsonEncode($arr['post_data']):'';
$get_data = isset($arr['get_data'])?self::JsonEncode($arr['get_data'], JSON_UNESCAPED_UNICODE):'';
$milliseconds = round(microtime(true) * 1000);
$date = date("Y-m-d H:i:s", $milliseconds / 1000);
$formatted_date = sprintf("%s.%03d", $date, $milliseconds % 1000);
// $i=DB::insert("insert into ".$table_name." (request_ip, response_data,header_data,post_data,get_data,request_url,create_time,update_time)
// values (?,?,?,?,?,?,?,?)",[$arr['ip'],$response_data,$header_data,$post_data,$get_data,$arr['request_url'],$formatted_date,$formatted_date]);
// var_dump($i);
if ($id > 0) {
return DB::table($table_name)->where('id', $id)->update([
'response_data' => $response_data,
]);
} else {
return DB::table($table_name)->insertGetId([
'mark'=>$mark,
'request_ip' => '127.0.0.1',
'response_data' => $response_data,
'header_data' => $header_data,
'post_data' => $post_data,
'get_data' => $get_data,
'request_url' => $arr['request_url'],
'create_time' => $formatted_date,
'update_time' => $formatted_date
]);
}
}
public static function JsonEncode($data){ //格式化数据转json
$post_data =$data;
$post_data = json_encode($post_data, JSON_UNESCAPED_UNICODE);
$str_len = mb_strlen($post_data);
$str_size = $str_len / 1024;
if ($str_size > 40) $post_data = '{"data":"Row size too large"}';
return $post_data;
}
}