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.

181 lines
5.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
namespace App\Lib;
use DateTime;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
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;
}
public static function SendMsg($r_yyid,$tel,$name,$time){
if($r_yyid==1){
$yyid=6;
}
if($r_yyid==4){
$yyid=2;
}
$content="时间:".$time.";科室:健康管理中心1区。温馨提醒:您的预约已成功,请在预约时间前 30 分钟达到科室凭身份证原件开单。建议您体检前3天清淡饮食、禁烟酒";
$url="http://220.174.210.111:82/tuisong.aspx?yyid=".$yyid."&type=8&mobile=".$tel."&msg1=".urlencode($name)."&msg2=".urlencode($content);
$response = Http::get($url);
if ($response->successful()) {
}else{
Log::info("短信发送失败");
}
}
//根据生日 获取年龄
public static function GetAge($birthday) {
$dob = new DateTime($birthday);
$now = new DateTime();
// 计算两个日期之间的差值
$interval = $now->diff($dob);
// 返回年龄
return $interval->y;
}
//生成随机字符串
public static function RandomString($length = 6) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
// 使用random_int确保随机性
$randomString .= $characters[random_int(0, $charactersLength - 1)];
}
return $randomString;
}
//请求三方接口日志记录
public static $request;
public static function Post($url,$data,$mark,$big_title)
{
self::RequestLog($url, $data, $mark, $big_title);
$response = Http::post($url,$data);
if ($response->successful()) {
$res = $response->json();
$res_string=json_encode($res, JSON_UNESCAPED_UNICODE);
$str_len = mb_strlen($res_string, 'utf-8');
$str_size = $str_len / 1024;
$save_res = $res_string;
if ($str_size > 10) $save_res = '{"data":"Row size too large"}';
self::$request->response_data = $save_res;
self::$request->save();
return $res;
} else {
$status = $response->status();
// 获取响应体作为字符串
$body = $response->body();
self::$request->response_data = $body;
self::$request->save();
throw new HttpResponseException(\Yz::echoError1("调用".$mark."接口失败:" . $status . "body:" . $body));
}
}
public static function RequestLog($url, $post_data, $mark, $code = 0)
{
self::CheckTableName();
foreach ($post_data as $key => $post_datum) {
$str_len = mb_strlen(json_encode($post_datum, JSON_UNESCAPED_UNICODE), 'utf-8');
$str_size = $str_len / 1024;
if ($str_size > 10) {
$post_data["$key"] = 'Row size too large';
}
}
$post_data = json_encode($post_data, JSON_UNESCAPED_UNICODE);
self::$request->code = $code;
self::$request->mark = $mark;
self::$request->post_data = $post_data == '[]' ? '{}' : $post_data;
self::$request->request_url = $url;
self::$request->save();
}
public static function CheckTableName()
{
$table_name = 'zz_peis_log_' . date('ym');
$table_count = DB::select('select count(1) as c from information_schema.TABLES where table_schema = ? and table_name = ?', [env('DB_DATABASE'), $table_name])[0];
if ($table_count->c === 0) {
Schema::create($table_name, function (Blueprint $table) {
$table->id();
$table->string('code', 50)->index();
$table->string('mark', 50)->index();
$table->text('post_data');
$table->text('response_data')->nullable();
$table->string('request_url', 2000);
$table->timestamps();
});
}
self::$request = new \App\Models\PEISLog();
self::$request->setTable($table_name);
}
public static function AESEncrypt($data, $key,$iv){
// 使用openssl_encrypt进行加密
$encryptedData = openssl_encrypt(
$data,
'AES-256-CBC',
$key,
OPENSSL_RAW_DATA,
$iv
);
// 返回包含IV的加密数据通常会将IV放在加密数据之前
return base64_encode( $encryptedData);
}
//AES解密
public static function AESDecrypt($data, $key,$iv){
// 解码base64编码的数据
$data = base64_decode($data);
$encryptedData = $data;
// 使用openssl_decrypt进行解密
return openssl_decrypt(
$encryptedData,
'AES-256-CBC',
$key,
OPENSSL_RAW_DATA,
$iv
);
}
}