SchoolPhysicalExamination/application/app/controller/Base.php

142 lines
5.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\app\controller;
use think\Controller;
use think\Db;
class Base extends Controller{
protected $base_call_method = ['内部'];
################################################################接口################################################################
################################################################接口################################################################
################################################################接口################################################################
// 操作记录留存
// $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,
]);
}
// 检查变量是否是一个只有数字的一维数组
function isNumericArray($array = [1,2,3]) {
if (!is_array($array)) {
return false; // 变量不是数组
}
foreach ($array as $value) {
if (!is_numeric($value)) {
return false; // 数组中包含非数字元素
}
}
// $result = Db::table('app_card_data')->where(['is_del'=>1])->cache(true,3600)->column('id');//查询结果缓存3600秒
$result = Db::table('app_card_data')->where(['is_del'=>0])->cache(true,3600)->select();//查询结果缓存3600秒
// dump(array_column($result, 'id'));
// die;
if(empty(array_diff($array, array_column($result, 'id')))){
return true;// 数组是一维的且只包含数字,且已经跟数据库比对过,每个数值都是有效
}else{
return false;//跟数据库比对过,存在无效数值
}
}
// 计算年龄
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;
}
}
// 计算天数
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');
}
// 计算月份
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);
return $ageInMonthsPrecise;
}
// 查找设备
// $data = ['device_code'=>'asdkljiouoi']
public function base_device_check($data = ['device_code'=>'asdkljiouoi']){
// $data = input();
$result = Db::query("
select
adcd.id,
adcd.bind_account_id as activation_state,
adds.is_del as device_state
from app_device_code_data as adcd
left join app_device_data as adds on adds.id = adcd.add_id
where
adcd.machine_code = '".$data['device_code']."'
");
if(count($result) == 1){
return $this->msg(0,'success',['device_state'=>$result[0]['device_state'],'activation_state'=>$result[0]['activation_state']]);
}else if(count($result) < 1){
return $this->msg(10001,'未找到设备');
}else{
$this->abnormal_data_log_action(0,'device_check-设备查询出错,结果为'.count($result).'合理值应为1或0','app_device_code_data,app_device_data');
return $this->msg(10002,'未找到设备');
}
}
// 绑定设备
public function base_bind_device(){
}
public function msg($code,$msg='',$data=[]){
return json(['code'=>$code,'msg'=>$msg,'data'=>$data]);
}
}