AdminBase.php 4.4 KB

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