Base.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. class Base extends Controller
  5. {
  6. protected $param;
  7. protected function initialize()
  8. {
  9. $url = request()->baseUrl();
  10. $orurl = $url;
  11. $orurlarr = array_filter(explode('/', $url));
  12. $urlarr = array_filter(explode('/', strtolower($url)));
  13. if (count($orurlarr) >= 4) {
  14. $url = $urlarr[3] . "/" . $urlarr[4];
  15. $ischeck = true;
  16. $ignorelist = $this->getignoreurl();
  17. foreach ($ignorelist as $key => $value) {
  18. if ($url == $value) {
  19. $ischeck = false;
  20. }
  21. }
  22. /*if ($ischeck) {
  23. $this->checkToken();
  24. }*/
  25. $functionname = $orurlarr[4];
  26. $this->checkrules($functionname);
  27. }
  28. }
  29. private function getignoreurl()
  30. {
  31. $list = [];
  32. return $list;
  33. }
  34. /**
  35. * 校验token
  36. * 20220107
  37. * wj
  38. */
  39. /*private function checkToken()
  40. {
  41. $l_w = new webuserlogic();
  42. $token = request()->server('HTTP_TOKEN');
  43. $param = ['token' => $token];
  44. $result = $l_w->queryinfobytoken($param);
  45. if (1 != $result['status']) {
  46. $str = backjson2(0, '登录失效', $result['data']);
  47. exit($str);
  48. }
  49. }*/
  50. /**
  51. * 设置请求数据规则
  52. * 20220107
  53. * wj
  54. */
  55. protected function setrules()
  56. {
  57. $list = [];
  58. return $list;
  59. }
  60. /**
  61. * 校验请求数据
  62. * name 变量名
  63. * title 名称
  64. * regex 正则
  65. * require 必填
  66. * type 类型
  67. * 20220107
  68. * wj
  69. */
  70. protected function checkrules($functionname)
  71. {
  72. $param = request()->param();
  73. $rules = $this->setrules();
  74. if (isset($rules[$functionname])) {
  75. try {
  76. $list = $rules[$functionname];
  77. $namelist = array_column($list, 'name');
  78. if (count($namelist) != count($list) || count($list) != count(array_filter($namelist))) {
  79. throw new \Exception("规则name设置错误");
  80. }
  81. $titlelist = array_column($list, 'title');
  82. if (count($titlelist) != count($list) || count($list) != count(array_filter($titlelist))) {
  83. throw new \Exception("规则title设置错误");
  84. }
  85. foreach ($list as $key => $value) {
  86. $name = $value['name'];
  87. $title = $value['title'];
  88. //必填
  89. if ($value['require']) {
  90. if (!isset($param[$name])) {
  91. throw new \Exception($title . '未填');
  92. }
  93. }
  94. if (!isset($param[$name])) {
  95. continue;
  96. }
  97. $paramvalue = $param[$name];
  98. //类型
  99. if (isset($value['type'])) {
  100. $tpe = $value['type'];
  101. switch ($tpe) {
  102. case 'string':
  103. if (!is_string($paramvalue) || empty($paramvalue)) {
  104. throw new \Exception($title . '格式错误');
  105. }
  106. break;
  107. case 'numeric':
  108. if (!is_numeric($paramvalue)) {
  109. throw new \Exception($title . '格式错误');
  110. }
  111. break;
  112. case 'array':
  113. if (!is_array($paramvalue)) {
  114. throw new \Exception($title . '格式错误');
  115. }
  116. break;
  117. }
  118. }
  119. //正则
  120. if (isset($value['regex'])) {
  121. $regex = $value['regex'];
  122. if (preg_match($regex, $paramvalue)) {
  123. throw new \Exception($title . '正则格式错误');
  124. }
  125. }
  126. }
  127. } catch (\Exception $e) {
  128. $str = backjson2(0, $e->getMessage());
  129. exit($str);
  130. }
  131. }
  132. $this->param = $param;
  133. }
  134. }