From 22ac8a8ee64ce911c9f4c3fd93fd8c480dede1b4 Mon Sep 17 00:00:00 2001 From: tiansf Date: Tue, 31 Dec 2024 18:42:33 +0800 Subject: [PATCH] 241231 --- .../KitchenScale/controller/admin/Base.php | 170 ++++++++++++++++++ .../KitchenScale/controller/admin/Login.php | 119 ++++++++++++ .../KitchenScale/controller/app/Base.php | 170 ++++++++++++++++++ .../KitchenScale/controller/app/Login.php | 85 +++++++++ 4 files changed, 544 insertions(+) create mode 100644 application/KitchenScale/controller/admin/Base.php create mode 100644 application/KitchenScale/controller/admin/Login.php create mode 100644 application/KitchenScale/controller/app/Base.php create mode 100644 application/KitchenScale/controller/app/Login.php diff --git a/application/KitchenScale/controller/admin/Base.php b/application/KitchenScale/controller/admin/Base.php new file mode 100644 index 0000000..3631230 --- /dev/null +++ b/application/KitchenScale/controller/admin/Base.php @@ -0,0 +1,170 @@ +'test_app_data_log', + ]; + protected $return_data_all = [ + '10001'=>'关键参数缺失', + '10002'=>'操作失败', + '10003'=>'信息核实错误', + '10004'=>'未找到有效数据', + '10005'=>'参数格式错误', + '10006'=>'参数不能为空', + '10007'=>'参数错误', + '10008'=>'', + '10009'=>'', + '10010'=>'自定义信息', + '20001'=>'登录失效', + '99999'=>'网络异常,请稍后重试', + ]; + // 加 bcadd(,,20) + // 减 bcsub(,,20) + // 乘 bcmul(,,20) + // 除 bcdiv(,,20) + ################################################################接口################################################################ + ################################################################接口################################################################ + ################################################################接口################################################################ + // 接口记录 + public function record_api_log($params, $error = null, $response = null){ + $logContent = "接口请求参数:" . json_encode($params, JSON_UNESCAPED_UNICODE) . PHP_EOL; + if ($error) { + $logContent .= "错误信息:" . $error['all_content'] . PHP_EOL; + if(!cache($error['flie']."_".$error['line'])){ + cache($error['flie']."_".$error['line'],"API错误",3600); + $this->send_email_api_error(["tsf3920322@126.com"],['title'=>'接口报错','from_user_name'=>'厨房秤(后台)','content'=>$logContent]); + } + } + if ($response) { + $logContent .= "返回信息:" . json_encode($response, JSON_UNESCAPED_UNICODE) . PHP_EOL; + } + // 使用ThinkPHP的日志记录方法 + Log::record($logContent, 'api_log'); + + } + /* 接口说明(发邮件) + * $address(收件人的邮箱地址) 数组 格式: ['460834639@qq.com','460834639@qq.com'.......] + * $content(邮件的主题数据信息) 数组 格式:['title'=>'123','from_user_name'=>'123','content'=>'123'] + * $annex(附件路径信息) 字符串 + */ + public function send_email_api_error($address,$content,$annex=''){ + // $ad = '460834639@qq.com'; + $ad1 = '295155911@qq.com'; + $mail = new PHPMailer(); //实例化 + $mail->IsSMTP(); // 启用SMTP + $mail->Host = "smtp.126.com"; //SMTP服务器 163邮箱例子 + $mail->Port = 465; //邮件发送端口 + $mail->SMTPAuth = true; //启用SMTP认证 + $mail->SMTPSecure = 'ssl'; + $mail->CharSet = "UTF-8"; //字符集 + $mail->Encoding = "base64"; //编码方式 + $mail->Username = "tsf3920322@126.com"; //你的邮箱 + $mail->Password = "HLWXNRPUCTHJFIIX"; //你的密码(邮箱后台的授权密码) + $mail->From = "tsf3920322@126.com"; //发件人地址(也就是你的邮箱) + + // $mail->Subject = "微盟测试邮件"; //邮件标题 + $mail->Subject = $content['title']; //邮件标题 + + // $mail->FromName = "微盟体测中心"; //发件人姓名 + $mail->FromName = $content['from_user_name']; //发件人姓名 + + + for ($i=0; $i < count($address); $i++) { + $mail->AddAddress($address[$i], ""); //添加收件人(地址,昵称) + } + + if($annex != ''){ + // $url = ROOT_PATH. 'public' . DS . 'tsf' . DS .'demoooo.jpg'; + $mail->AddAttachment($annex,''); // 添加附件,并指定名称 + } + + $mail->IsHTML(true); //支持html格式内容 + + $neirong = $content['content']; + + $mail->Body = $neirong; //邮件主体内容 + //发送 + if (!$mail->Send()) { + + return ['code' => 10003,'msg'=>$mail->ErrorInfo]; + // return $mail->ErrorInfo; + } else { + return ['code' => 0]; + // return 'success'; + } + } + + + // 验证 + public function verify_data_is_ok($data = 2,$type){ + if($type == 'str'){ + if (is_string($data)) { + return true; + } else { + $this->record_api_log($data, null, ['code'=>10005,'msg'=>'校验参数不为字符串',[]]); + return false; + } + }else if($type == 'num'){ + if (is_numeric($data)) { + return true; + } else { + $this->record_api_log($data, null, ['code'=>10005,'msg'=>'校验参数不为数字',[]]); + return false; + } + }else if($type == 'intnum'){ + $pattern = '/^\d+$/'; + if (preg_match($pattern, $data)) { + return true; // 匹配成功,返回 true + } else { + $this->record_api_log($data, null, ['code'=>10005,'msg'=>'校验参数不为整数数字',[]]); + return false; // 匹配失败,返回 false + } + }else if($type == 'datetime'){ + $formats = ['Y-m-d','Y-m-d H:i:s']; + foreach ($formats as $format) { + $dateTime = \DateTime::createFromFormat($format, $data); + // 检查时间字符串是否成功解析,并且解析后的日期时间与原始字符串表示的时间一致 + if ($dateTime && $dateTime->format($format) === $data) { + return true; + } + } + // 如果所有格式都解析失败,则返回 false + $this->record_api_log($data, null, ['code'=>10005,'msg'=>'校验参数不为日期格式',[]]); + return false; + }else if($type == 'other'){ + + } + + } + + + 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 ceshi(){ + echo 'hello'; + } + + +} \ No newline at end of file diff --git a/application/KitchenScale/controller/admin/Login.php b/application/KitchenScale/controller/admin/Login.php new file mode 100644 index 0000000..57a980c --- /dev/null +++ b/application/KitchenScale/controller/admin/Login.php @@ -0,0 +1,119 @@ +123,'password'=>456]){ + // try { + // 你的业务逻辑 + if(count(input('post.')) > 0){ + $data = input('post.'); + } + if(!array_key_exists('account', $data) || !array_key_exists('password', $data)){ + return $this->msg(10001); + } + + $return_data = $this->login_action($data); + + // 成功 + $this->record_api_log($data, null, $return_data); + return $return_data; + // } catch (\Exception $e) { + // // 捕获异常 + // $logContent["flie"] = $e->getFile(); + // $logContent["line"] = $e->getLine(); + // $logContent['all_content'] = "异常信息:\n"; + // $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; + // $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; + // $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + // $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; + // $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; + // $this->record_api_log($data, $logContent, null); + // return $this->msg(99999); + // } + + } + + // 检测登录信息是否超时 + public function check_login($data = ['token'=>'123']){ + // try { + // 你的业务逻辑 + if(count(input('post.')) > 0){ + $data = input('post.'); + } + if(!array_key_exists('token', $data)){ + return $this->msg(10001); + } + + $return_data = $this->check_login_action($data); + + // 成功 + $this->record_api_log($data, null, $return_data); + return $return_data; + // } catch (\Exception $e) { + // // 捕获异常 + // $logContent["flie"] = $e->getFile(); + // $logContent["line"] = $e->getLine(); + // $logContent['all_content'] = "异常信息:\n"; + // $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; + // $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; + // $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + // $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; + // $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; + // $this->record_api_log($data, $logContent, null); + // return $this->msg(99999); + // } + + } + + + #######################################################################action####################################################################### + public function login_action($data){ + // dump($data); + $user = Db::table('admin_user_account_number')->where(["account_num"=>$data['account'],'password'=>$data['password']])->find(); + // dump($user); + // die; + if($user){ + return $this->msg(['token'=>$user['token']]); + }else{ + return $this->msg(10004); + } + + } + + public function check_login_action(){ + $user = Db::table('admin_user_account_number')->where(["token"=>$data['token']])->field('create_time')->find(); + if (!$user) { + return $this->msg(10001); // 如果用户不存在,直接返回 false + } + // 假设 $user['create_time'] 是一个日期时间字符串 + $createTime = new DateTime($user['create_time']); + $currentTime = new DateTime(); + + // 计算时间差 + $interval = $currentTime->diff($createTime); + + // 判断时间差是否超过指定小时数 + $totalHours = $interval->days * 24 + $interval->h; + if ($totalHours > $this->login_hours_out) { + return $this->msg(10001); + } else { + return $this->msg([]); + } + } + +} \ No newline at end of file diff --git a/application/KitchenScale/controller/app/Base.php b/application/KitchenScale/controller/app/Base.php new file mode 100644 index 0000000..85f7f60 --- /dev/null +++ b/application/KitchenScale/controller/app/Base.php @@ -0,0 +1,170 @@ +'test_app_data_log', + ]; + protected $return_data_all = [ + '10001'=>'关键参数缺失', + '10002'=>'操作失败', + '10003'=>'信息核实错误', + '10004'=>'未找到有效数据', + '10005'=>'参数格式错误', + '10006'=>'参数不能为空', + '10007'=>'参数错误', + '10008'=>'', + '10009'=>'', + '10010'=>'自定义信息', + '20001'=>'登录失效', + '99999'=>'网络异常,请稍后重试', + ]; + // 加 bcadd(,,20) + // 减 bcsub(,,20) + // 乘 bcmul(,,20) + // 除 bcdiv(,,20) + ################################################################接口################################################################ + ################################################################接口################################################################ + ################################################################接口################################################################ + // 接口记录 + public function record_api_log($params, $error = null, $response = null){ + $logContent = "接口请求参数:" . json_encode($params, JSON_UNESCAPED_UNICODE) . PHP_EOL; + if ($error) { + $logContent .= "错误信息:" . $error['all_content'] . PHP_EOL; + if(!cache($error['flie']."_".$error['line'])){ + cache($error['flie']."_".$error['line'],"API错误",3600); + $this->send_email_api_error(["tsf3920322@126.com"],['title'=>'接口报错','from_user_name'=>'厨房秤(后台)','content'=>$logContent]); + } + } + if ($response) { + $logContent .= "返回信息:" . json_encode($response, JSON_UNESCAPED_UNICODE) . PHP_EOL; + } + // 使用ThinkPHP的日志记录方法 + Log::record($logContent, 'api_log'); + + } + /* 接口说明(发邮件) + * $address(收件人的邮箱地址) 数组 格式: ['460834639@qq.com','460834639@qq.com'.......] + * $content(邮件的主题数据信息) 数组 格式:['title'=>'123','from_user_name'=>'123','content'=>'123'] + * $annex(附件路径信息) 字符串 + */ + public function send_email_api_error($address,$content,$annex=''){ + // $ad = '460834639@qq.com'; + $ad1 = '295155911@qq.com'; + $mail = new PHPMailer(); //实例化 + $mail->IsSMTP(); // 启用SMTP + $mail->Host = "smtp.126.com"; //SMTP服务器 163邮箱例子 + $mail->Port = 465; //邮件发送端口 + $mail->SMTPAuth = true; //启用SMTP认证 + $mail->SMTPSecure = 'ssl'; + $mail->CharSet = "UTF-8"; //字符集 + $mail->Encoding = "base64"; //编码方式 + $mail->Username = "tsf3920322@126.com"; //你的邮箱 + $mail->Password = "HLWXNRPUCTHJFIIX"; //你的密码(邮箱后台的授权密码) + $mail->From = "tsf3920322@126.com"; //发件人地址(也就是你的邮箱) + + // $mail->Subject = "微盟测试邮件"; //邮件标题 + $mail->Subject = $content['title']; //邮件标题 + + // $mail->FromName = "微盟体测中心"; //发件人姓名 + $mail->FromName = $content['from_user_name']; //发件人姓名 + + + for ($i=0; $i < count($address); $i++) { + $mail->AddAddress($address[$i], ""); //添加收件人(地址,昵称) + } + + if($annex != ''){ + // $url = ROOT_PATH. 'public' . DS . 'tsf' . DS .'demoooo.jpg'; + $mail->AddAttachment($annex,''); // 添加附件,并指定名称 + } + + $mail->IsHTML(true); //支持html格式内容 + + $neirong = $content['content']; + + $mail->Body = $neirong; //邮件主体内容 + //发送 + if (!$mail->Send()) { + + return ['code' => 10003,'msg'=>$mail->ErrorInfo]; + // return $mail->ErrorInfo; + } else { + return ['code' => 0]; + // return 'success'; + } + } + + + // 验证 + public function verify_data_is_ok($data = 2,$type){ + if($type == 'str'){ + if (is_string($data)) { + return true; + } else { + $this->record_api_log($data, null, ['code'=>10005,'msg'=>'校验参数不为字符串',[]]); + return false; + } + }else if($type == 'num'){ + if (is_numeric($data)) { + return true; + } else { + $this->record_api_log($data, null, ['code'=>10005,'msg'=>'校验参数不为数字',[]]); + return false; + } + }else if($type == 'intnum'){ + $pattern = '/^\d+$/'; + if (preg_match($pattern, $data)) { + return true; // 匹配成功,返回 true + } else { + $this->record_api_log($data, null, ['code'=>10005,'msg'=>'校验参数不为整数数字',[]]); + return false; // 匹配失败,返回 false + } + }else if($type == 'datetime'){ + $formats = ['Y-m-d','Y-m-d H:i:s']; + foreach ($formats as $format) { + $dateTime = \DateTime::createFromFormat($format, $data); + // 检查时间字符串是否成功解析,并且解析后的日期时间与原始字符串表示的时间一致 + if ($dateTime && $dateTime->format($format) === $data) { + return true; + } + } + // 如果所有格式都解析失败,则返回 false + $this->record_api_log($data, null, ['code'=>10005,'msg'=>'校验参数不为日期格式',[]]); + return false; + }else if($type == 'other'){ + + } + + } + + + 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 ceshi(){ + echo 'hello'; + } + + +} \ No newline at end of file diff --git a/application/KitchenScale/controller/app/Login.php b/application/KitchenScale/controller/app/Login.php new file mode 100644 index 0000000..604d23e --- /dev/null +++ b/application/KitchenScale/controller/app/Login.php @@ -0,0 +1,85 @@ +123,'password'=>456]){ + try { + // 你的业务逻辑 + if(count(input('post.')) > 0){ + $data = input('post.'); + } + if(!array_key_exists('account', $data) && !array_key_exists('password', $data)){ + return $this->msg(10001); + } + $return_data = $this->login_action($data); + // 成功 + $this->record_api_log($data, null, $return_data); + return $return_data; + } catch (\Exception $e) { + // 捕获异常 + $logContent["flie"] = $e->getFile(); + $logContent["line"] = $e->getLine(); + $logContent['all_content'] = "异常信息:\n"; + $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; + $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; + $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; + $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; + $this->record_api_log($data, $logContent, null); + return $this->msg(99999); + } + } + public function login_api($data = ['account'=>123,'password'=>456]){ + try { + // 你的业务逻辑 + if(count(input('post.')) > 0){ + $data = input('post.'); + } + if(!array_key_exists('account', $data) && !array_key_exists('password', $data)){ + return $this->msg(10001); + } + $return_data = $this->login_action($data); + // 成功 + $this->record_api_log($data, null, $return_data); + return $return_data; + } catch (\Exception $e) { + // 捕获异常 + $logContent["flie"] = $e->getFile(); + $logContent["line"] = $e->getLine(); + $logContent['all_content'] = "异常信息:\n"; + $logContent['all_content'] .= "消息: " . $e->getMessage() . "\n"; + $logContent['all_content'] .= "代码: " . $e->getCode() . "\n"; + $logContent['all_content'] .= "文件: " . $e->getFile() . "\n"; + $logContent['all_content'] .= "行号: " . $e->getLine() . "\n"; + $logContent['all_content'] .= "跟踪信息:\n" . $e->getTraceAsString() . "\n"; + $this->record_api_log($data, $logContent, null); + return $this->msg(99999); + } + } + #######################################################################action####################################################################### + public function login_action($data){ + // dump($data); + $user = Db::table('admin_user_account_number')->where(["account_num"=>$data['account'],'password'=>$data['password']])->find(); + // dump($user); + // die; + if($user){ + return $this->msg(['token'=>$user['token']]); + }else{ + return $this->msg(10004); + } + + } + +} \ No newline at end of file