作者 邓超

1

<?php
echo strlen("你好啊阿萨德你白求恩是奥斯卡女把万科城那就睡吧八十年代你");
var_dump(str_starts_with('a_sdf','_'));
//use Model\listsSql;
... ...
... ... @@ -3,6 +3,7 @@
namespace Controller;
use Lib\Mail\MailFun;
use Lib\Verify;
use Model\emailSql;
use Model\listsSql;
use function Swoole\Coroutine\Http\request;
... ... @@ -62,7 +63,29 @@ class Home extends Base {
* @time 2023/2/18 17:32
*/
public function send_mail(){
$ret = MailFun::sendEmail();
$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'],
],[
]);
$ret = MailFun::sendEmail(
$email['smtp'],
$email['email'],
base64_decode($email['password']),
$formData['nickname']??'',
$formData['to'],
$formData['subject'],
$formData['body'],
$formData['files']??''
);
if($ret[0]===true) {
app()->_json(['messageId'=>$ret[1]]);
... ...
... ... @@ -188,7 +188,7 @@ function web_request_emails():array {
$emails = app()->request('emails');
$emails = is_array($emails) ? $emails : [$emails];
foreach ($emails as $k=>$email){
if(!\Lib\Verify::email($email)){
if(!\Lib\Verify::sEmail($email)){
unset($emails[$k]);
}
}
... ... @@ -210,7 +210,7 @@ function web_request_emails():array {
function web_request_email():string {
$email = app()->request('email');
if(!\Lib\Verify::email($email)){
if(!\Lib\Verify::sEmail($email)){
app()->e('email_request_required');
}
... ...
... ... @@ -29,4 +29,58 @@ return [
'sync_request_param_error' => '同步请求参数异常',
];
\ No newline at end of file
'verify_required' => '%s必须',
'verify_max' => '%s长度必须小于%s字符',
'verify_min' => '%s长度必须大于%s字符',
'verify_email' => '%s邮箱格式错误',
'verify_mobile' => '%s手机号格式错误',
'verify_array' => '%s必须是数组',
'verify_integer' => '%s必须是整数',
'verify_number' => '%s必须是数字',
'verify_reg' => '%s的规则匹配失败',
'verify_between' => '%s必须在%s和%s之间',
'nickname' => '昵称',
'to_email' => '收件人',
'subject' => '邮件标题',
'body' => '邮件内容',
];
... ...
... ... @@ -126,6 +126,11 @@ class App {
'error_message' => $app->debug ? $exception->getMessage().PHP_EOL.$exception->getTraceAsString() : __('server_error'),
'status' => $exception->getCode() ? $exception->getCode() : 500,
];
}else{
$app->data = [
'error_message' => $exception->getMessage(),
'status' => $exception->getCode() ? $exception->getCode() : 500,
];
}
}
... ... @@ -191,9 +196,14 @@ class App {
* @time 2023/2/13 10:57
*/
public function e($message,$status=400){
$this->data['error_message'] = __($message);
if(is_array($message)){
$this->data['error_message'] = __($message[0]);
$this->data['error_message'] = sprintf($this->data['error_message'],...$message[1]);
}else{
$this->data['error_message'] = __($message);
}
$this->data['status'] = $status;
throw new Err($message,500);
throw new Err($this->data['error_message'],$status);
}
/**
... ...
... ... @@ -13,16 +13,279 @@ namespace Lib;
class Verify {
/**
* @var array
*/
public array $data;
/**
* @var array
*/
public array $message;
/**
* 别名
* @var array
*/
public array $alias;
/**
* 验证邮箱
* @param $email
* @return false|int
* @author:dc
* @time 2023/3/10 16:04
*/
public static function email($email){
public static function sEmail($email){
return preg_match('/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',$email);
}
/**
* 验证手机
* @param $mobile
* @return false|int
* @author:dc
* @time 2023/3/13 11:00
*/
public static function sMobile($mobile){
return preg_match('/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/',$mobile);
}
/**
* 验证域名
* @param $domain
* @return false|int
* @author:dc
* @time 2023/3/13 10:59
*/
public static function sDomain($domain){
return preg_match('/[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?/',$domain);
}
/**
* 验证url
* @param $url
* @return false|int
* @author:dc
* @time 2023/3/13 11:00
*/
public static function sUrl($url){
return preg_match('/[a-zA-z]+://[^\s]* 或 ^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$/',$url);
}
/**
* 验证身份证
* @param $idCard
* @return false|int
* @author:dc
* @time 2023/3/13 11:01
*/
public static function sIdCard($idCard){
return preg_match('/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/',$idCard);
}
/**
* 验证数据
* @param array $rule
* @param array $message
* @return array|false|float|int|mixed|null
* @author:dc
* @time 2023/3/10 17:56
*/
public static function checks(array $rule, array $message=[]){
$verify = new static();
$verify->message = $message;
$data= app()->request();
if(is_array($data)){
$verify->data = &$data;
}
foreach ($rule as $key=>$item){
// 别名
$key = explode('|',$key);
$verify->alias[$key[0]] = $key[1]??$key[0];
// 验证规则
foreach ($item as $ak=>$av){
if(is_numeric($ak)){
$param = [$key[0]];
$ak = $av;
}else{
$param = [$key[0],$av];
}
// 验证规则,不能以下划线开始
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)]],
600
);
}
}
}
return $verify->data;
}
/**
* 获取表单值
* @param $key
* @return mixed|string
* @author:dc
* @time 2023/3/10 18:03
*/
private function _get($key){
return $this->data[$key]??'';
}
/**
* 必须
* @param $key
* @return false
* @author:dc
* @time 2023/3/13 10:11
*/
public function required($key){
return $this->_get($key) !== '';
}
/**
* 验证长度
* @param $key
* @param $value
* @return bool
* @author:dc
* @time 2023/3/13 10:11
*/
public function max($key,$value):bool {
return mb_strlen($this->_get($key)) <= $value;
}
/**
* 最小字符
* @param $key
* @param $value
* @return bool
* @author:dc
* @time 2023/3/13 10:11
*/
public function min($key,$value):bool {
return mb_strlen($this->_get($key)) >= $value;
}
/**
* 是否是数组
* @param $key
* @param $value
* @return bool
* @author:dc
* @time 2023/3/13 10:11
*/
public function array($key,$value):bool {
return is_array($value);
}
/**
* 整数
* @param $key
* @param $value
* @return bool
* @author:dc
* @time 2023/3/13 10:15
*/
public function integer($key,$value):bool {
return is_integer($value);
}
/**
* 数字,整数,浮点,负数
* @param $key
* @param $value
* @return bool
* @author:dc
* @time 2023/3/13 10:16
*/
public function number($key,$value):bool {
return is_numeric($value);
}
/**
* 正则
* @param $key
* @param $value
* @return bool
* @author:dc
* @time 2023/3/13 10:20
*/
public function reg($key,$value):bool {
return (bool) preg_match($value,$this->_get($key));
}
/**
* 之间
* @param $key
* @param $value
* @return bool
* @author:dc
* @time 2023/3/13 10:31
*/
public function between($key,$value){
$data = $this->_get($key);
return $data >= $value[0] && $data <= $value[1];
}
/**
* 邮箱
* @param $key
* @return false|int
* @author:dc
* @time 2023/3/13 10:55
*/
public function email($key){
$emails = $this->_get($key);
if(is_array($emails)){
foreach ($emails as $email){
if(!static::sEmail($email)){
return false;
}
}
}else{
return static::sEmail($emails);
}
}
/**
* 手机号
* @param $key
* @return false|int
* @author:dc
* @time 2023/3/13 11:06
*/
public function mobile($key){
$mobiles = $this->_get($key);
if(is_array($mobiles)){
foreach ($mobiles as $mobile){
if(!static::sEmail($mobile)){
return false;
}
}
}else{
return static::sEmail($mobiles);
}
}
}
\ No newline at end of file
... ...