SchoolPhysicalExamination/application/admin/controller/Base.php

283 lines
11 KiB
PHP
Raw Normal View History

2024-06-07 16:49:19 +08:00
<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Cache;
use think\Log;
class Base extends Controller{
protected $ceshiyong_token = 'caadd1be045a65f30b92aa805f1de54a';
2024-07-08 08:20:57 +08:00
protected $token_time = 86400*30;//天
2024-12-18 09:19:51 +08:00
protected $file_size = 1024*1024*10;//10M
2024-06-07 16:49:19 +08:00
protected $return_data_all = [
// '0' => ['success',[]],
'10001'=>'关键参数缺失',
'10002'=>'操作失败',
'10003'=>'信息核实错误',
'10004'=>'未找到有效数据',
'10005'=>'参数格式错误',
'10006'=>'参数不能为空',
2024-07-05 03:14:57 +08:00
'10007'=>'数据已存在',
2024-06-07 16:49:19 +08:00
'10008'=>'',
'10009'=>'',
'10010'=>'',
'20001'=>'登录失效',
];
################################################################接口################################################################
################################################################接口################################################################
################################################################接口################################################################
// 操作记录留存
// $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'){
$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){
// cache($token,time());
2024-07-08 08:20:57 +08:00
2024-06-07 16:49:19 +08:00
$time = cache($token);
// dump($time);
if($time === false){
return false;
}
$diff_time = time() - $time;
// dump($diff_time);
if($diff_time > $this->token_time){
return false;
}
cache($token, time());
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);
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
2024-12-07 18:56:07 +08:00
public function isPositiveNumber($value) {
2024-06-07 16:49:19 +08:00
return is_numeric($value) && $value >= 0;
}
2024-11-29 18:46:37 +08:00
public function msg($data,$str='',$result = []){
2024-06-07 16:49:19 +08:00
if(is_array($data)){
2024-11-29 18:46:37 +08:00
if($str != ''){
return json(['code'=>0,'msg'=>$str,'data'=>$data]);
}else{
return json(['code'=>0,'msg'=>'操作成功','data'=>$data]);
}
2024-06-07 16:49:19 +08:00
}else{
if($str != ''){
2024-11-29 18:46:37 +08:00
return json(['code'=>$data,'msg'=>$str,'data'=>$result]);
2024-06-07 16:49:19 +08:00
}
2024-11-29 18:46:37 +08:00
return json(['code'=>$data,'msg'=>$this->return_data_all[$data],'data'=>$result]);
2024-06-07 16:49:19 +08:00
}
}
2024-12-07 18:56:07 +08:00
####################################################图片选择上传start##############################################################
public function pic_index($page = 1) {
$data = input();
$pd = true;
if(array_key_exists('page',$data)){
$page = $data['page'];
$pd = false;
}
$num = Db::table('admin_pic_manage')->count();
$result = Db::table('admin_pic_manage')->order('id desc')->page($page,20)->select();
if(!$pd){
$return_result['num'] = $num;
$return_result['result'] = $result;
return $this->msg(0,'success',$return_result);
}
$this->assign([
'result' => $result,
'num' => $num,
]);
return $this->fetch();
}
public function pic_upload_action(){
// $file1 = request()->file('file');
$file = request()->file('cover_image');
if($file){
$name = $file->getInfo()['name'];
// 使用 pathinfo() 函数获取文件名的扩展名
$pathinfo = pathinfo($name);
$extension = strtolower($pathinfo['extension']); // 转换为小写以进行不区分大小写的比较
2024-12-18 09:19:51 +08:00
$file_name = $pathinfo['filename'];
2024-12-07 18:56:07 +08:00
// 判断扩展名是否不是 .png 或 .gif
if ($extension !== 'png' && $extension !== 'gif') {
// 修改文件名,将扩展名改为 .jpg
2024-12-18 09:19:51 +08:00
$new_filename = date('YmdHis').$file_name . '.jpg';
2024-12-07 18:56:07 +08:00
} else {
$new_filename = date('YmdHis').$name;
}
2024-12-18 09:19:51 +08:00
$info = $file->validate(['size'=>$this->file_size,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'upload_pic',$new_filename);
2024-12-07 18:56:07 +08:00
if($info){
$insert_data = [
2024-12-18 09:19:51 +08:00
'url_data'=>"https://tc.pcxbc.com/upload_pic/".$new_filename,
2024-12-26 19:14:44 +08:00
'name'=>$new_filename,
2024-12-07 18:56:07 +08:00
'create_time'=>date('Y-m-d H:i:s'),
];
$pic_result = Db::table('admin_pic_manage')->insertGetId($insert_data);
if($pic_result){
return $this->msg(['url'=>$insert_data['url_data'],'id'=>$pic_result]);
}else{
return $this->msg(10002,'图片数据保存失败');
}
}else{
return $this->msg(10002,'图片上传失败');
}
}
}
####################################################图片选择上传end##############################################################
2024-07-08 08:20:57 +08:00
####################################################测试用接口##############################################################
public function use_test(){
$this->token_time_validate($this->ceshiyong_token);
$token='caadd1be045a65f30b92aa805f1de54a';
// $result = Db::query("select * from app_card_data");
// dump($result);
// $result2 = Db::table('app_card_data')->select();
// dump($result2);
2024-06-07 16:49:19 +08:00
}
}