作者 邓超

1

<?php
error_reporting(E_ERROR | E_NOTICE);
error_reporting(E_ERROR | E_NOTICE | E_WARNING);
// 开启4个进程
define('WORKER_NUM',10);
... ...
... ... @@ -63,15 +63,23 @@ class Home extends Base {
* @time 2023/2/18 17:32
*/
public function send_mail(){
return print_r(app()->file('files'),true);
$email = $this->getEmail();
$formData = Verify::checks([
'nickname|'.__('nickname') => ['required','max'=>50],
'to|'.__('to_email') => ['required','array','email'],
'subject|'.__('subject') => ['required','max'=>150],
'body|'.__('body_email') => ['required'],
'files|'.__('body_email') => ['required'],
'to|'.__('to_email') => ['required','array|string','email'],
'priority|'.__('priority_email') => ['in'=>[1,3,5]],
'files|'.__('files_email') => [
'file'=>[
'ext' => [],
'size' => 1024*1024,
'mine' => []
]
],
'receipt|'.__('receipt_email') => []
],[
]);
... ... @@ -84,7 +92,9 @@ class Home extends Base {
$formData['to'],
$formData['subject'],
$formData['body'],
$formData['files']??''
$formData['files']??'',
$formData['receipt']??'',
$formData['priority']??3,
);
if($ret[0]===true) {
... ...
... ... @@ -39,6 +39,10 @@ return [
'verify_number' => '%s必须是数字',
'verify_reg' => '%s的规则匹配失败',
'verify_between' => '%s必须在%s和%s之间',
'verify_in' => '%s必须在(%s)中',
'verify_string' => '%s必须是字符串',
'verify_array_or_string' => '%s必须是%s数组或者字符串',
'verify_file' => '%s必须文件',
... ... @@ -46,6 +50,10 @@ return [
'to_email' => '收件人',
'subject' => '邮件标题',
'body' => '邮件内容',
'priority_email' => '是否紧急',
'body_email' => '邮件内容',
'files_email' => '邮件附件',
'receipt_email' => '邮件回执',
... ...
... ... @@ -47,13 +47,30 @@ class App {
/**
* 输出到前端的数据
* @var mixed
*/
private mixed $data;
/**
* 表单文件
* @var array
*/
private array $uploadFile = [];
/**
* 错误
* @var array
*/
private array $data = [];
public array $error;
/**
* App constructor.
*/
public function __construct()
{
register_shutdown_function("\\Lib\\App::end");
$this->date = date('Y-m-d');
$this->dateTime = date('Y-m-d H:i:s');
... ... @@ -102,24 +119,24 @@ class App {
if(!$route){
$app->e('route_not_found');
}
list($class,$action) = $route;
$controller = new $class();
$data = $controller->{$action}();
if($data){
$app->data = $data;
}
$app->data = $controller->{$action}();
// end 控制器
}catch (\Throwable $exception){
if(!($exception instanceof Err)){
// 记录日志
$filename = LOG_PATH.'/'.$app->nowDate().'.log';
logs(
$exception->getMessage().PHP_EOL.$exception->getTraceAsString(),
$filename
);
$app->error = [
'message' => $exception->getMessage(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'traceAsString' => $exception->getTraceAsString()
];
// 非 Err 错误类型
$app->data = [
... ... @@ -170,6 +187,38 @@ class App {
/**
* @param string $key
* @return false|UploadFile[]
* @author:dc
* @time 2023/3/13 15:09
*/
public function file($key='file'){
if(!empty($this->uploadFile[$key])){
return $this->uploadFile[$key];
}
if(empty($_FILES[$key])){
return false;
}
if (is_array($_FILES[$key]['error'])){
foreach ($_FILES[$key]['error'] as $k=>$e){
// 成功的才处理
if($e===0){
$this->uploadFile[$key][] = new UploadFile($_FILES[$key]['name'][$k],$_FILES[$key]['tmp_name'][$k]);
}
}
}else if($_FILES[$key]['error']===0){
$this->uploadFile[$key][] = new UploadFile($_FILES[$key]['name'],$_FILES[$key]['tmp_name']);
}
return $this->uploadFile[$key]??false;
}
/**
* @return string
* @author:dc
* @time 2023/2/13 11:17
... ... @@ -220,6 +269,20 @@ class App {
}
public static function echo($data, $http_code = 200){
http_response_code($http_code);
if(is_array($data)){
header("Content-Type:application/json;Charset=UTF-8;");
echo json_encode($data,JSON_UNESCAPED_UNICODE);
}else{
header("Content-Type:text/html;Charset=UTF-8;");
echo $data;
}
}
/**
* 结束
* @author:dc
... ... @@ -228,6 +291,7 @@ class App {
public static function end()
{
// 这里可以做其他事
$app = self::instance();
/**
* 这里写
... ... @@ -235,14 +299,31 @@ class App {
// end code
header("Content-Type:application/json;Charset=UTF-8;");
// header("Content-Type:text/html;Charset=UTF-8;");
// header("Content-Type:text/event-stream;Charset=UTF-8;");
// 记录日志
$filename = LOG_PATH.'/'.$app->nowDate().'.log';
if($last_error = error_get_last()){
logs(print_r($last_error,true), $filename);
$data['error_message'] = $last_error['message'];
$data['status'] = 502;
self::echo($data,502);
}else{
if($app->error){
logs(
is_string($app->error) ? $app->error : implode(PHP_EOL,$app->error),
$filename
);
}
if(self::instance()->data){
echo json_encode(self::instance()->data,JSON_UNESCAPED_UNICODE);
self::echo($app->data);
}
// header("Content-Type:text/event-stream;Charset=UTF-8;");
// ob_flush();
// ob_clean();
}
... ...
<?php
namespace Lib;
/**
* 文件
* @author:dc
* @time 2023/3/13 14:34
* Class UploadFile
* @package Lib
*/
class UploadFile
{
/**
* @var string
*/
public string $name;
/**
* @var string
*/
public string $tempFile;
/**
* @var int
*/
public int $size;
/**
* @var string
*/
public string $mime;
/**
* @var string
*/
public string $ext;
/**
* UploadFile constructor.
* @param $name
* @param $tempFile
*/
public function __construct(string $name, string $tempFile)
{
parent::__construct($tempFile);
$this->name = $name;
$this->tempFile = $tempFile;
// kb
$this->size = (int) (filesize($this->tempFile)/1024);
// 文件类型
$this->mime = mime_content_type($this->tempFile);
$ext = explode('.',$name);
$this->ext = end($ext);
}
}
\ No newline at end of file
... ...
... ... @@ -115,13 +115,26 @@ class Verify {
}else{
$param = [$key[0],$av];
}
// 特殊验证
switch ($ak){
case 'array|string':{
$ak = 'array_or_string';
break;
}
case 'string|array':{
$ak = "array_or_string";
break;
}
}
// 验证规则,不能以下划线开始
if(str_starts_with($ak, '_') || !method_exists($verify,$ak)){
throw new Err($ak.':验证规则不存在',600);
}
if(!$verify->{$ak}(...$param)){
app()->e(
$verify->message[$key[0]][$ak]??['verify_'.$ak, [$verify->alias[$key[0]]??$key[0],print_r($av,true)]],
$verify->message[$key[0]][$ak]??['verify_'.$ak, [$verify->alias[$key[0]]??$key[0],is_array($av) ? implode(',',$av) : $av]],
600
);
}
... ... @@ -143,6 +156,17 @@ class Verify {
}
/**
* 是否存在字段
* @param $key
* @return bool
* @author:dc
* @time 2023/3/13 14:16
*/
private function _has($key){
return isset($this->data[$key]);
}
/**
* 必须
* @param $key
* @return false
... ... @@ -163,6 +187,9 @@ class Verify {
* @time 2023/3/13 10:11
*/
public function max($key,$value):bool {
// 如果字段不存在,则不验证
if(!$this->_has($key)) return true;
return mb_strlen($this->_get($key)) <= $value;
}
... ... @@ -175,6 +202,10 @@ class Verify {
* @time 2023/3/13 10:11
*/
public function min($key,$value):bool {
// 如果字段不存在,则不验证
if(!$this->_has($key)) return true;
return mb_strlen($this->_get($key)) >= $value;
}
... ... @@ -187,8 +218,12 @@ class Verify {
* @author:dc
* @time 2023/3/13 10:11
*/
public function array($key,$value):bool {
return is_array($value);
public function array($key):bool {
// 如果字段不存在,则不验证
if(!$this->_has($key)) return true;
return is_array($this->_get($key));
}
... ... @@ -201,6 +236,10 @@ class Verify {
* @time 2023/3/13 10:15
*/
public function integer($key,$value):bool {
// 如果字段不存在,则不验证
if(!$this->_has($key)) return true;
return is_integer($value);
}
... ... @@ -214,6 +253,9 @@ class Verify {
* @time 2023/3/13 10:16
*/
public function number($key,$value):bool {
// 如果字段不存在,则不验证
if(!$this->_has($key)) return true;
return is_numeric($value);
}
... ... @@ -227,6 +269,9 @@ class Verify {
* @time 2023/3/13 10:20
*/
public function reg($key,$value):bool {
// 如果字段不存在,则不验证
if(!$this->_has($key)) return true;
return (bool) preg_match($value,$this->_get($key));
}
... ... @@ -240,6 +285,9 @@ class Verify {
* @time 2023/3/13 10:31
*/
public function between($key,$value){
// 如果字段不存在,则不验证
if(!$this->_has($key)) return true;
$data = $this->_get($key);
return $data >= $value[0] && $data <= $value[1];
}
... ... @@ -253,6 +301,9 @@ class Verify {
* @time 2023/3/13 10:55
*/
public function email($key){
// 如果字段不存在,则不验证
if(!$this->_has($key)) return true;
$emails = $this->_get($key);
if(is_array($emails)){
foreach ($emails as $email){
... ... @@ -273,6 +324,9 @@ class Verify {
* @time 2023/3/13 11:06
*/
public function mobile($key){
// 如果字段不存在,则不验证
if(!$this->_has($key)) return true;
$mobiles = $this->_get($key);
if(is_array($mobiles)){
foreach ($mobiles as $mobile){
... ... @@ -285,7 +339,57 @@ class Verify {
}
}
/**
* 是否在数组里面
* @param $key
* @param $value
* @return bool
* @author:dc
* @time 2023/3/13 11:56
*/
public function in($key,$value):bool {
// 如果字段不存在,则不验证
if(!$this->_has($key)) return true;
return in_array($this->_get($key),$value);
}
/**
* 是否是字符串
* @param $key
* @return bool
* @author:dc
* @time 2023/3/13 13:42
*/
public function string($key):bool {
// 如果字段不存在,则不验证
if(!$this->_has($key)) return true;
return is_string($this->_get($key));
}
/**
* 数组或者字符串
* @param $key
* @return bool
* @author:dc
* @time 2023/3/13 14:04
*/
public function array_or_string($key){
return $this->array($key) || $this->string($key);
}
/**
* 文件
* @param $key
* @param $value
* @author:dc
* @time 2023/3/13 14:27
*/
public function file($key,$value){
}
}
\ No newline at end of file
... ...
... ... @@ -11,7 +11,5 @@ require_once "../vendor/autoload.php";
\Lib\App::run();
\Lib\App::end();
... ...