baselogic.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /*
  3. * @Author: wang jun
  4. * @Date: 2022-01-18 11:12:23
  5. * @Last Modified by: wang jun
  6. * @Last Modified time: 2022-01-19 15:58:46
  7. * 微信类
  8. */
  9. namespace app\admin\logic;
  10. use think\facade\Log;
  11. class baselogic
  12. {
  13. /**
  14. * 根据配置验证param
  15. * wj
  16. * 20220119
  17. * [[name=>'',title=>'',type=>'',regex=>"",require=>booler]]
  18. */
  19. protected function checkparam($functionname, $arr)
  20. {
  21. $param = $arr;
  22. $rules = $this->setrules();
  23. if (isset($rules[$functionname])) {
  24. try {
  25. $list = $rules[$functionname];
  26. $namelist = array_column($list, 'name');
  27. if (count($namelist) != count($list) || count($list) != count(array_filter($namelist))) {
  28. throw new \Exception("规则name设置错误");
  29. }
  30. $titlelist = array_column($list, 'title');
  31. if (count($titlelist) != count($list) || count($list) != count(array_filter($titlelist))) {
  32. throw new \Exception("规则title设置错误");
  33. }
  34. foreach ($list as $key => $value) {
  35. $name = $value['name'];
  36. $title = $value['title'];
  37. //必填
  38. if (isset($value['require']) && $value['require']) {
  39. if (!isset($param[$name])) {
  40. throw new \Exception($title . '未填');
  41. }
  42. }
  43. if (!isset($param[$name])) {
  44. continue;
  45. }
  46. $paramvalue = $param[$name];
  47. //类型
  48. if (isset($value['type']) && $paramvalue != "") {
  49. $tpe = $value['type'];
  50. switch ($tpe) {
  51. case 'string':
  52. if (!is_string($paramvalue) || empty($paramvalue)) {
  53. throw new \Exception($title . '格式错误');
  54. }
  55. break;
  56. case 'numeric':
  57. if (!is_numeric($paramvalue)) {
  58. throw new \Exception($title . '格式错误');
  59. }
  60. break;
  61. case 'array':
  62. if (!is_array($paramvalue)) {
  63. throw new \Exception($title . '格式错误');
  64. }
  65. break;
  66. case 'datetime':
  67. if (!isDate($paramvalue, 'Y-m-d H:i:s')) {
  68. throw new \Exception($title . '格式错误');
  69. }
  70. break;
  71. case 'date':
  72. if (!isDate($paramvalue, 'Y-m-d')) {
  73. throw new \Exception($title . '格式错误');
  74. }
  75. break;
  76. }
  77. }
  78. //正则
  79. if (isset($value['regex']) && $paramvalue != "") {
  80. $regex = $value['regex'];
  81. if (!preg_match($regex, $paramvalue)) {
  82. throw new \Exception($title . '正则格式错误');
  83. }
  84. }
  85. }
  86. return backarr(1, 'success');
  87. } catch (\Exception $e) {
  88. $msg = $e->getMessage();
  89. Log::error($msg);
  90. return backarr(0, "请求错误");
  91. }
  92. }
  93. return backarr(1, 'success');
  94. }
  95. /**
  96. * 设置请求参数验证规则
  97. *
  98. * @return void
  99. */
  100. protected function setrules()
  101. {
  102. return [];
  103. }
  104. /**
  105. * 过滤空数组
  106. *
  107. * @return void
  108. */
  109. protected function filterarray($arr)
  110. {
  111. $newArr = [];
  112. foreach ($arr as $key => $value) {
  113. if (is_string($value) && $value == "") {
  114. continue;
  115. }
  116. $newArr[$key] = $value;
  117. }
  118. return $newArr;
  119. }
  120. }