460 lines
17 KiB
PHP
460 lines
17 KiB
PHP
<?php
|
||
|
||
namespace app\app\controller;
|
||
|
||
use think\Controller;
|
||
use think\Db;
|
||
use think\Cache;
|
||
use think\Log;
|
||
|
||
class Base extends Controller{
|
||
|
||
protected $ceshiyong_token = ['caadd1be045a65f30b92aa805f1de54a','e0966788d02cc93290d9d674921d9715'];
|
||
protected $base_call_method = ['内部'];
|
||
protected $token_time = 2592000;//30天的秒数
|
||
protected $return_data_all = [
|
||
// '0' => ['success',[]],
|
||
'10001'=>'关键参数缺失',
|
||
'10002'=>'操作失败',
|
||
'10003'=>'信息核实错误',
|
||
'10004'=>'未找到有效数据',
|
||
'10005'=>'参数格式错误',
|
||
'10006'=>'参数不能为空',
|
||
'10007'=>'',
|
||
'10008'=>'',
|
||
'10009'=>'',
|
||
'10010'=>'自定义信息',
|
||
'20001'=>'登录失效',
|
||
];
|
||
// 加 bcadd(,,20)
|
||
// 减 bcsub(,,20)
|
||
// 乘 bcmul(,,20)
|
||
// 除 bcdiv(,,20)
|
||
################################################################接口################################################################
|
||
################################################################接口################################################################
|
||
################################################################接口################################################################
|
||
|
||
// 操作记录留存
|
||
// $data = ['aud_id'=>'xxxxxxxxxxxxxxx','order_list'=>[1,2,3,4,5]]
|
||
public function abnormal_data_log_action($dacall_methoda = 0,$content='未记录的内容',$use_database_name='未记录的数据库名'){
|
||
$result = Db::table('app_data_log')->insert([
|
||
'create_time'=>date('Y-m-d H:i:s'),
|
||
'call_method'=>$this->base_call_method[$dacall_methoda],
|
||
'content'=>$content,
|
||
'use_database_name'=>$use_database_name,
|
||
]);
|
||
}
|
||
|
||
// 检查变量是否是一个只有数字的一维数组
|
||
public function is_num_array($array = [1,2,3],$type=1) {
|
||
if (!is_array($array)) {
|
||
return false; // 变量不是数组
|
||
}
|
||
foreach ($array as $value) {
|
||
if (!is_numeric($value)) {
|
||
return false; // 数组中包含非数字元素
|
||
}
|
||
}
|
||
if($type!=1){
|
||
return true;
|
||
}
|
||
$result = Db::table('app_card_data')->where(['is_del'=>0])->cache(true,3600)->select();//查询结果缓存3600秒
|
||
if(empty(array_diff($array, array_column($result, 'id')))){
|
||
return true;// 数组是一维的且只包含数字,且已经跟数据库比对过,每个数值都是有效
|
||
}else{
|
||
return false;//跟数据库比对过,存在无效数值
|
||
}
|
||
}
|
||
public function validate_user_identity($data) {
|
||
$validate_user = Db::table('app_user_data')->where(['id'=>$data])->count();
|
||
if($validate_user<=0){
|
||
return false;
|
||
}else{
|
||
return true;
|
||
}
|
||
}
|
||
// 判断字符串是手机还是邮箱
|
||
public function is_tel_email($str) {
|
||
// 手机号码的正则表达式(中国大陆格式)(下面正则实际判断的是是否为11位数字)
|
||
$mobilePattern = '/^\d{11}$/';
|
||
// 电子邮件地址的正则表达式
|
||
$emailPattern = '/^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/';
|
||
// 判断是否为手机号码
|
||
if (preg_match($mobilePattern, $str)) {
|
||
return 'tel';
|
||
}
|
||
// 判断是否为电子邮件地址
|
||
if (preg_match($emailPattern, $str)) {
|
||
return 'email';
|
||
}
|
||
// 如果都不是,返回其他
|
||
return false;
|
||
}
|
||
|
||
// 计算年龄
|
||
public function calculate_age($data = '1991-04-20'){
|
||
$today = time(); // 获取当前时间的 Unix 时间戳
|
||
$birthDate = strtotime($data); // 将出生日期字符串转换为 Unix 时间戳
|
||
if ($birthDate !== false) {
|
||
$age = date('Y', $today) - date('Y', $birthDate);
|
||
// 如果当前年份的月份和日期小于出生年份的月份和日期,那么年龄减一
|
||
if (date('m-d', $today) < date('m-d', $birthDate)) {
|
||
$age--;
|
||
}
|
||
return $age;
|
||
} else {
|
||
return false;
|
||
}
|
||
}
|
||
// 秒转化格式,00:00:00
|
||
public function handle_hour_branch_second($data = '2000'){
|
||
$data = abs($data);
|
||
$hours = intval($data / 3600);
|
||
$minutes = intval(($data % 3600) / 60);
|
||
$remainingSeconds = $data % 60;
|
||
return [
|
||
'h' => str_pad($hours, 2, '0', STR_PAD_LEFT),
|
||
'm' => str_pad($minutes, 2, '0', STR_PAD_LEFT),
|
||
's' => str_pad($remainingSeconds, 2, '0', STR_PAD_LEFT)
|
||
];
|
||
}
|
||
|
||
// 判断token是否过期
|
||
public function token_time_validate($token){
|
||
// if(in_array($token,$this->ceshiyong_token)){
|
||
// return true;
|
||
// }
|
||
// $time = cache($token);
|
||
// if($time === false){
|
||
// Log::record('用户尝试更新token时间,token:' . $token.',但是更新token失败,原因没有找到该token', 'log');
|
||
// return false;
|
||
// }
|
||
// $diff_time = bcsub(time(),$time,0);
|
||
// if($diff_time > $this->token_time){
|
||
// Log::record('用户尝试更新token时间,token:' . $token.',但是更新token失败,原因当前时间戳减去记录时间戳后为'.$diff_time.'秒,大于规定秒数'.$this->token_time, 'log');
|
||
// return false;
|
||
// }
|
||
// cache($token, time());
|
||
// Log::record('用户尝试更新token时间,token:' . $token.',记录成功,最新的秒数为'.time(), 'log');
|
||
// return true;
|
||
|
||
|
||
if(cache($token) === false){
|
||
Log::record('用户尝试更新token时间,token:' . $token.',但是更新token失败,原因没有找到该token,或该token已经超过30天', 'log');
|
||
return false;
|
||
}
|
||
$time_now = date('Y-m-d H:i:s');
|
||
cache($token,$time_now,$this->token_time);
|
||
Log::record('用户尝试更新token时间,token:' . $token.',记录成功,最新的时间为'.$time_now, 'log');
|
||
return true;
|
||
|
||
}
|
||
|
||
// 计算天数
|
||
public function daysSince($pastDate,$now = false)
|
||
{
|
||
// 创建一个表示过去日期的 DateTime 对象
|
||
$past = new \DateTime($pastDate);
|
||
if($now === false){
|
||
// 创建一个表示当前日期的 DateTime 对象
|
||
$now = new \DateTime();
|
||
}else{
|
||
$now = new \DateTime($now);
|
||
}
|
||
// 使用 DateTime::diff() 方法计算两个日期之间的差值
|
||
$interval = $past->diff($now);
|
||
// 返回相差的天数
|
||
return $interval->format('%a');
|
||
}
|
||
|
||
// 计算月龄
|
||
public function calculateAgeInMonthsWithPrecision($birthDateStr) {
|
||
// 获取当前日期
|
||
$now = new \DateTime();
|
||
// 将出生日期字符串转换为 DateTime 对象
|
||
$birthDate = \DateTime::createFromFormat('Y-m-d', $birthDateStr);
|
||
// 计算两者之间的差距(以月为单位,包含部分月份的小数)
|
||
$interval = $now->diff($birthDate);
|
||
$ageInMonths = $interval->y * 12 + $interval->m; // 年份乘以12加上月份
|
||
$remainingDays = $interval->d; // 当前月内的剩余天数
|
||
// 将剩余天数转换为小数月份(假设一个月为30天,进行近似计算)
|
||
$partialMonth = $remainingDays / 30;
|
||
// 结果精确到小数点后两位
|
||
// $ageInMonthsPrecise = round($ageInMonths + $partialMonth, 2);
|
||
// 整月+剩余月取整
|
||
$ageInMonthsPrecise = intval($ageInMonths + $partialMonth);
|
||
return $ageInMonthsPrecise;
|
||
}
|
||
// 曲线页面-底部统计动作
|
||
public function base_target_initial_cumulative_weight($data = []){
|
||
// 第一种:用户详情(所有数据都有)
|
||
// 第二种:手动记录(只有最新体重)
|
||
// 第三种:修改原始体重(只有原始体重)
|
||
// dump($data);
|
||
if(count($data) > 0){
|
||
$result_data['target_weight'] = $data['target_weight'];
|
||
$result_data['initial_weight'] = $data['initial_weight'];
|
||
$result_data['cumulative_weight'] = bcsub($data['weight'],$data['initial_weight'],2);
|
||
$result_data['cumulative_day'] = $data['initial_date'] == 0?0:$this->daysSince($data['initial_date']);
|
||
}else{
|
||
$result_data['target_weight'] = 0;
|
||
$result_data['initial_weight'] = 0;
|
||
$result_data['cumulative_weight'] = 0;
|
||
$result_data['cumulative_day'] = 0;
|
||
}
|
||
// dump($result_data);
|
||
return $result_data;
|
||
}
|
||
|
||
// 判断一个参数是否为数字且大于等于0
|
||
public function isPositiveNumber($value) {
|
||
return is_numeric($value) && $value >= 0;
|
||
}
|
||
// 判断是否为整型,或者字符串类型的整型数字
|
||
public function isValidInteger($var) {
|
||
// 直接检查是否为整型
|
||
if (is_int($var)) {
|
||
return true;
|
||
}
|
||
|
||
// 检查是否为字符串且是有效的整数表示
|
||
if (is_string($var) && filter_var($var, FILTER_VALIDATE_INT) !== false) {
|
||
return true;
|
||
}
|
||
|
||
// 其他情况
|
||
return false;
|
||
}
|
||
|
||
// 判断一个字符串是否为两位以内小数
|
||
public function isTwoDecimalOrLess($str) {
|
||
return preg_match('/^\d*(\.\d{1,2})?$/', $str) === 1;
|
||
}
|
||
|
||
// 获取用户肺活量的标准值
|
||
public function get_vitalcapacity_data($id){
|
||
$standard_data = [
|
||
['min_val'=>'90','max_val'=>'100','text'=>'优秀','color'=>'#6492F6'],
|
||
['min_val'=>'80','max_val'=>'89','text'=>'良好','color'=>'#5AD06D'],
|
||
['min_val'=>'60','max_val'=>'79','text'=>'及格','color'=>'#FFAB00'],
|
||
['min_val'=>'10','max_val'=>'59','text'=>'不及格','color'=>'#FF5656'],
|
||
['min_val'=>'0','max_val'=>'9','text'=>'无效','color'=>'#FF5656'],
|
||
];
|
||
$grade = Db::table('app_user_data')->where(['id'=>$id])->field('id,grade,gender')->find();
|
||
if(!$grade || $grade['grade'] == 'nothing'){
|
||
return [];
|
||
}
|
||
$sql_min = "WITH RankedGrades AS (
|
||
SELECT
|
||
id,
|
||
level,
|
||
".$grade['grade'].",
|
||
ROW_NUMBER() OVER(PARTITION BY level ORDER BY ".$grade['grade']." ASC, id ASC) AS rn
|
||
FROM
|
||
pc_vitalcapacity_standard
|
||
WHERE
|
||
sex = ".$grade['gender']."
|
||
)
|
||
SELECT
|
||
id,
|
||
level,
|
||
".$grade['grade']."
|
||
FROM
|
||
RankedGrades
|
||
WHERE
|
||
rn = 1";
|
||
// dump($sql_min);
|
||
$result_min = Db::query($sql_min);
|
||
// dump($result_min);
|
||
// die;
|
||
foreach ($result_min as $key => $value) {
|
||
foreach ($standard_data as $sdk => $sdv) {
|
||
if($value['level'] == $sdv['text']){
|
||
$standard_data[$sdk]['min_val'] = $value[$grade['grade']];
|
||
}
|
||
}
|
||
}
|
||
for ($i=count($standard_data)-1; $i >= 1; $i--) {
|
||
$standard_data[$i]['max_val'] = $standard_data[$i-1]['min_val'];
|
||
}
|
||
$standard_data[0]['max_val'] = '5140';
|
||
return $standard_data;
|
||
}
|
||
|
||
|
||
// 时间日期转换
|
||
public function addCurrentTimeToDateString($dateStr) {
|
||
// 将日期字符串转换为DateTime对象
|
||
$dateTime = new \DateTime($dateStr);
|
||
|
||
// 获取当前的时分秒
|
||
$currentTime = new \DateTime('now');
|
||
$hours = $currentTime->format('H');
|
||
$minutes = $currentTime->format('i');
|
||
$seconds = $currentTime->format('s');
|
||
|
||
// 设置DateTime对象的时间部分为当前时间
|
||
$dateTime->setTime($hours, $minutes, $seconds);
|
||
|
||
// 返回格式化为"Y-m-d H:i:s"的字符串
|
||
return $dateTime->format('Y-m-d H:i:s');
|
||
}
|
||
|
||
// 处理分秒变秒
|
||
function convertMinutesSecondsToStringSeconds($timeString) {
|
||
// 分割字符串获取分钟和秒
|
||
list($minutes, $seconds) = explode(':', $timeString);
|
||
// 将分钟和秒转换为秒
|
||
$totalSeconds = ($minutes * 60) + intval($seconds); // 确保秒是整数
|
||
return $totalSeconds;
|
||
}
|
||
|
||
// 时间加一或者减一
|
||
public function adjustDateTime($datetimeStr, $type) {
|
||
// 将时间字符串转换为时间戳
|
||
$timestamp = strtotime($datetimeStr);
|
||
|
||
// 检查时间戳是否有效
|
||
if ($timestamp === false) {
|
||
return "无效的日期时间格式";
|
||
}
|
||
|
||
// 根据$type参数调整时间戳
|
||
switch ($type) {
|
||
case 'add':
|
||
$newTimestamp = strtotime('+1 day', $timestamp);
|
||
break;
|
||
case 'subtract':
|
||
$newTimestamp = strtotime('-1 day', $timestamp);
|
||
break;
|
||
default:
|
||
return false;
|
||
}
|
||
|
||
// 将新的时间戳转换回日期时间字符串
|
||
$newDateTimeStr = date('Y-m-d', $newTimestamp);
|
||
|
||
return $newDateTimeStr;
|
||
}
|
||
|
||
// 对于任意浮点字符串的指定位四舍五入
|
||
public function roundToString($numberStr, $numDecimals) {
|
||
// 将字符串转换为浮点数
|
||
$number = floatval($numberStr);
|
||
|
||
// 四舍五入到指定的小数位数
|
||
$roundedNumber = round($number, $numDecimals);
|
||
|
||
// 将结果转换回字符串
|
||
return strval($roundedNumber);
|
||
}
|
||
|
||
|
||
public function postRequest($url, $data = [], $headers = []) {
|
||
$ch = curl_init(); // 初始化cURL会话
|
||
|
||
if (!$ch) {
|
||
return [
|
||
'error' => true,
|
||
'message' => 'cURL init failed'
|
||
];
|
||
}
|
||
// 设置cURL选项
|
||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 禁用证书验证
|
||
curl_setopt($ch, CURLOPT_URL, $url); // 要请求的URL
|
||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 将curl_exec()获取的信息以文件流的形式返回,而不是直接输出
|
||
curl_setopt($ch, CURLOPT_POST, true); // 发送一个常规的POST请求
|
||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); // POST数据
|
||
// 设置请求头
|
||
if (!empty($headers)) {
|
||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||
}else{
|
||
// 如果需要发送JSON数据,可以使用以下设置:
|
||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
||
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
|
||
}
|
||
// 执行cURL会话
|
||
$response = curl_exec($ch);
|
||
|
||
if ($response === false) {
|
||
$error = curl_error($ch);
|
||
curl_close($ch);
|
||
return [
|
||
'error' => true,
|
||
'message' => "cURL Error: $error"
|
||
];
|
||
}
|
||
$decodedResponse = json_decode($response, true);
|
||
$jsonError = json_last_error();
|
||
curl_close($ch);
|
||
|
||
if ($jsonError !== JSON_ERROR_NONE) {
|
||
return [
|
||
'error' => true,
|
||
'message' => 'Invalid JSON Response',
|
||
'raw_response' => $response
|
||
];
|
||
}
|
||
return $decodedResponse;
|
||
}
|
||
|
||
|
||
|
||
public function msg($data,$str='',$result = []){
|
||
if(is_array($data)){
|
||
if($str != ''){
|
||
return json(['code'=>0,'msg'=>$str,'data'=>$data]);
|
||
}else{
|
||
return json(['code'=>0,'msg'=>'操作成功','data'=>$data]);
|
||
}
|
||
}else{
|
||
if($str != ''){
|
||
return json(['code'=>$data,'msg'=>$str,'data'=>$result]);
|
||
}
|
||
return json(['code'=>$data,'msg'=>$this->return_data_all[$data],'data'=>$result]);
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
public function ceshiyong($aa = 4,$gd = 0.2){
|
||
$token = 'caadd1be045a65f30b92aa805f1de54a';
|
||
// $jieguo = $this->token_time_validate($token);
|
||
// // // Log::record('用户尝试更新token时间,token:' . $token, 'log');
|
||
// // // phpinfo();
|
||
// // dump($jieguo);
|
||
// // // return $jieguo;
|
||
// // die;
|
||
// // cache($token, time());
|
||
// $time = cache($token);
|
||
dump($token);
|
||
// dump($time);
|
||
die;
|
||
|
||
|
||
|
||
|
||
// $aa = 3;
|
||
// dump($aa/20);
|
||
$a = $aa;
|
||
for ($i=0; $i < 100; $i++) {
|
||
dump($a.'======='.$a/$aa*100);
|
||
$a = $a - $gd;
|
||
if($a < 0){
|
||
break;
|
||
}
|
||
}
|
||
// $aa = 15;
|
||
// $a = 1;
|
||
// for ($i=0; $i < 100; $i++) {
|
||
// dump($aa.'======='.$aa/17.5*100);
|
||
// $aa = $aa - $a;
|
||
// if($aa < 1){
|
||
// break;
|
||
// }
|
||
// }
|
||
}
|
||
|
||
|
||
} |