baselogic.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 (!$value['require']) {
  44. if (!isset($param[$name])) {
  45. if (isset($value['default'])) {
  46. $param[$name] = $value['default'];
  47. }
  48. }
  49. continue;
  50. }
  51. $paramvalue = $param[$name];
  52. //类型
  53. if (isset($value['type']) && $paramvalue != "") {
  54. $tpe = $value['type'];
  55. switch ($tpe) {
  56. case 'string':
  57. if (!is_string($paramvalue) || empty($paramvalue)) {
  58. throw new \Exception($title . '格式错误');
  59. }
  60. break;
  61. case 'numeric':
  62. if (!is_numeric($paramvalue)) {
  63. throw new \Exception($title . '格式错误');
  64. }
  65. break;
  66. case 'array':
  67. if (!is_array($paramvalue)) {
  68. throw new \Exception($title . '格式错误');
  69. }
  70. break;
  71. case 'datetime':
  72. if (!isDate($paramvalue, 'Y-m-d H:i:s')) {
  73. throw new \Exception($title . '格式错误');
  74. }
  75. break;
  76. case 'date':
  77. if (!isDate($paramvalue, 'Y-m-d')) {
  78. throw new \Exception($title . '格式错误');
  79. }
  80. break;
  81. }
  82. }
  83. //正则
  84. if (isset($value['regex']) && $paramvalue != "") {
  85. $regex = $value['regex'];
  86. if (!preg_match($regex, $paramvalue)) {
  87. throw new \Exception($title . '正则格式错误');
  88. }
  89. }
  90. }
  91. return backarr(1, 'success', $param);
  92. } catch (\Exception $e) {
  93. $msg = $e->getMessage();
  94. Log::error($msg);
  95. return backarr(0, "请求错误");
  96. }
  97. }
  98. return backarr(1, 'success', $param);
  99. }
  100. /**
  101. * 设置请求参数验证规则
  102. *
  103. * @return void
  104. */
  105. protected function setrules()
  106. {
  107. return [];
  108. }
  109. /**
  110. * 过滤空数组
  111. *
  112. * @return void
  113. */
  114. protected function filterarray($arr)
  115. {
  116. $newArr = [];
  117. foreach ($arr as $key => $value) {
  118. if (is_string($value) && $value == "") {
  119. continue;
  120. }
  121. $newArr[$key] = $value;
  122. }
  123. return $newArr;
  124. }
  125. }