baselogic.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 13:36:02
  7. * 微信类
  8. */
  9. namespace app\index\logic;
  10. use think\Facade\Log;
  11. class baselogic
  12. {
  13. private function getignoreurl()
  14. {
  15. $list = [];
  16. return $list;
  17. }
  18. /**
  19. * 根据配置验证param
  20. * wj
  21. * 20220119
  22. * [[name=>'',title=>'',type=>'',regex=>"",require=>booler]]
  23. */
  24. protected function checkparam($functionname, $arr)
  25. {
  26. $param = $arr;
  27. $rules = $this->setrules();
  28. if (isset($rules[$functionname])) {
  29. try {
  30. $list = $rules[$functionname];
  31. $namelist = array_column($list, 'name');
  32. if (count($namelist) != count($list) || count($list) != count(array_filter($namelist))) {
  33. throw new \Exception("规则name设置错误");
  34. }
  35. $titlelist = array_column($list, 'title');
  36. if (count($titlelist) != count($list) || count($list) != count(array_filter($titlelist))) {
  37. throw new \Exception("规则title设置错误");
  38. }
  39. foreach ($list as $key => $value) {
  40. $name = $value['name'];
  41. $title = $value['title'];
  42. //必填
  43. if ($value['require']) {
  44. if (!isset($param[$name])) {
  45. throw new \Exception($title . '未填');
  46. }
  47. }
  48. if (!isset($param[$name])) {
  49. continue;
  50. }
  51. $paramvalue = $param[$name];
  52. //类型
  53. if (isset($value['type'])) {
  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. }
  72. }
  73. //正则
  74. if (isset($value['regex'])) {
  75. $regex = $value['regex'];
  76. if (preg_match($regex, $paramvalue)) {
  77. throw new \Exception($title . '正则格式错误');
  78. }
  79. }
  80. }
  81. return backarr(1, 'success');
  82. } catch (\Exception $e) {
  83. $msg = $e->getMessage();
  84. Log::error($msg);
  85. return backarr(0, "请求错误");
  86. }
  87. }
  88. }
  89. }