作者 邓超

1

@@ -63,7 +63,7 @@ class Home extends Base { @@ -63,7 +63,7 @@ class Home extends Base {
63 * @time 2023/2/18 17:32 63 * @time 2023/2/18 17:32
64 */ 64 */
65 public function send_mail(){ 65 public function send_mail(){
66 - return print_r(app()->file('files'),true); 66 +
67 $email = $this->getEmail(); 67 $email = $this->getEmail();
68 68
69 $formData = Verify::checks([ 69 $formData = Verify::checks([
@@ -75,7 +75,7 @@ class Home extends Base { @@ -75,7 +75,7 @@ class Home extends Base {
75 'files|'.__('files_email') => [ 75 'files|'.__('files_email') => [
76 'file'=>[ 76 'file'=>[
77 'ext' => [], 77 'ext' => [],
78 - 'size' => 1024*1024, 78 + 'size' => 500,
79 'mine' => [] 79 'mine' => []
80 ] 80 ]
81 ], 81 ],
@@ -42,7 +42,10 @@ return [ @@ -42,7 +42,10 @@ return [
42 'verify_in' => '%s必须在(%s)中', 42 'verify_in' => '%s必须在(%s)中',
43 'verify_string' => '%s必须是字符串', 43 'verify_string' => '%s必须是字符串',
44 'verify_array_or_string' => '%s必须是%s数组或者字符串', 44 'verify_array_or_string' => '%s必须是%s数组或者字符串',
45 - 'verify_file' => '%s必须文件', 45 + 'verify_file' => '%s必须是文件',
  46 + 'verify_file.ext' => '%s上传的文件(%s)后缀必须是%s',
  47 + 'verify_file.mine' => '%s上传的文件(%s)类型必须是%s',
  48 + 'verify_file.size' => '%s上传的文件(%s)大小必须在%skb以内',
46 49
47 50
48 51
@@ -238,7 +238,7 @@ class App { @@ -238,7 +238,7 @@ class App {
238 238
239 /** 239 /**
240 * 错误 240 * 错误
241 - * @param string $message 241 + * @param string|array $message
242 * @param int $status 242 * @param int $status
243 * @throws Err 243 * @throws Err
244 * @author:dc 244 * @author:dc
@@ -169,12 +169,12 @@ class Verify { @@ -169,12 +169,12 @@ class Verify {
169 /** 169 /**
170 * 必须 170 * 必须
171 * @param $key 171 * @param $key
172 - * @return false 172 + * @return bool
173 * @author:dc 173 * @author:dc
174 - * @time 2023/3/13 10:11 174 + * @time 2023/3/13 17:14
175 */ 175 */
176 public function required($key){ 176 public function required($key){
177 - return $this->_get($key) !== ''; 177 + return $this->_get($key) !== '' || app()->file($key);
178 } 178 }
179 179
180 180
@@ -388,7 +388,37 @@ class Verify { @@ -388,7 +388,37 @@ class Verify {
388 * @time 2023/3/13 14:27 388 * @time 2023/3/13 14:27
389 */ 389 */
390 public function file($key,$value){ 390 public function file($key,$value){
  391 + $files = app()->file($key);
  392 + if($files){
  393 + foreach ($files as $file){
  394 + // 后缀
  395 + if(!empty($value['ext'])){
  396 + $value['ext'] = is_string($value['ext']) ? [$value['ext']] : [];
  397 + if (!in_array($file->ext,$value['ext'])){
  398 + app()->e(['verify_file.ext',[$this->alias[$key],$file->name,implode('|',$value['ext'])]],600);
  399 + return false;
  400 + }
  401 + }
391 402
  403 + // 类型
  404 + if(!empty($value['mine'])){
  405 + $value['mine'] = is_string($value['mine']) ? [$value['mine']] : [];
  406 + if (!in_array($file->mime,$value['mine'])){
  407 + app()->e(['verify_file.mine',[$this->alias[$key],$file->name,implode('|',$value['mine'])]],600);
  408 + return false;
  409 + }
  410 + }
  411 +
  412 + // 大小
  413 + if(!empty($value['size'])){
  414 + if ($file->size > $value['size']){
  415 + app()->e(['verify_file.size',[$this->alias[$key],$file->name,$value['size']]],600);
  416 + return false;
  417 + }
  418 + }
  419 + }
  420 + }
  421 + return true;
392 } 422 }
393 423
394 424