baselogic.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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\index\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'])) {
  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. }
  67. }
  68. //正则
  69. if (isset($value['regex'])) {
  70. $regex = $value['regex'];
  71. if (!preg_match($regex, $paramvalue)) {
  72. throw new \Exception($title . '正则格式错误');
  73. }
  74. }
  75. }
  76. return backarr(1, 'success');
  77. } catch (\Exception $e) {
  78. $msg = $e->getMessage();
  79. Log::error($msg);
  80. return backarr(0, "请求错误");
  81. }
  82. }
  83. return backarr(1, 'success');
  84. }
  85. /**
  86. * 设置请求参数验证规则
  87. *
  88. * @return void
  89. */
  90. protected function setrules()
  91. {
  92. return [];
  93. }
  94. }