Base.php 4.3 KB

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