ValidataBase.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. /**
  3. * lemocms
  4. * ============================================================================
  5. * 版权所有 2018-2027 lemocms,并保留所有权利。
  6. * 网站地址: https://www.lemocms.com
  7. * ----------------------------------------------------------------------------
  8. * 采用最新Thinkphp6实现
  9. * ============================================================================
  10. * Author: yuege
  11. * Date: 2019/8/2
  12. */
  13. namespace lemo\api\validate;
  14. use lemo\api\Send;
  15. /**
  16. * 公共验证码方法
  17. * Class Common
  18. * @package app\api\validate
  19. */
  20. class ValidataBase
  21. {
  22. use Send;
  23. /**
  24. * 默认支持验证规则
  25. * 更多验证规则请使用原生验证器
  26. * @var array
  27. */
  28. public static $dataRule = ['require','int','mobile'];
  29. /**
  30. * 接口参数公共验证方法
  31. * @param array $rule
  32. * @param array $data
  33. */
  34. static function validateCheck($rule = [],$data = []){
  35. if(is_array($rule) && is_array($data)){
  36. foreach ($rule as $k => $v){
  37. if(!in_array($v,self::$dataRule)){
  38. return self::returnMsg(401,'fail','验证规则只支持require,int');
  39. }
  40. if(!isset($data[$k]) || empty($data[$k])){
  41. return self::returnMsg(401,'fail',$k.'不能为空');
  42. }else{
  43. if($v == 'int'){
  44. if(!is_numeric($data[$k])){
  45. return self::returnMsg(401,'fail',$k.'类型必须为'.$v);
  46. }
  47. }elseif ($v == 'mobile'){
  48. if(!preg_match('/^1[3-9][0-9]\d{8}$/',$data[$k])){
  49. return self::returnMsg(401,'fail',$k.'手机号格式错误');
  50. }
  51. }
  52. }
  53. }
  54. }else{
  55. return self::returnMsg(401,'fail','验证数据格式为数组');
  56. }
  57. }
  58. }