You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. namespace app;
  3. use Spatie\Macroable\Macroable;
  4. /**
  5. * Class Request
  6. * @package app
  7. * @method tokenData() 获取token信息
  8. * @method user(string $key = null) 获取用户信息
  9. * @method uid() 获取用户uid
  10. * @method isAdminLogin() 后台登陆状态
  11. * @method adminId() 后台管理员id
  12. * @method adminInfo() 后台管理信息
  13. * @method kefuId() 客服id
  14. * @method kefuInfo() 客服信息
  15. * @method outId() 对外接口用户id
  16. * @method outInfo() 对外接口用户id
  17. * @method storeId() 门店ID
  18. * @method storeStaffId() 门店管理员id
  19. * @method storeStaffInfo() 门店管理员信息
  20. * @method cashierId() 门店收银员id
  21. * @method cashierInfo() 门店收银员信息
  22. * @method clientInfo() 企业微信客户信息
  23. * @method userid() 企业微信客户userid
  24. */
  25. class Request extends \think\Request
  26. {
  27. use Macroable;
  28. /**
  29. * 不过滤变量名
  30. * @var array
  31. */
  32. protected $except = ['menu_path', 'api_url', 'unique_auth', 'description', 'custom_form'];
  33. /**
  34. * 获取请求的数据
  35. * @param array $params
  36. * @param bool $suffix
  37. * @param bool $filter
  38. * @return array
  39. */
  40. public function more(array $params, bool $suffix = false, bool $filter = true): array
  41. {
  42. $p = [];
  43. $i = 0;
  44. foreach ($params as $param) {
  45. if (!is_array($param)) {
  46. $p[$suffix == true ? $i++ : $param] = $this->filterWord(is_string($this->param($param)) ? trim($this->param($param)) : $this->param($param), $filter && !in_array($param, $this->except));
  47. } else {
  48. if (!isset($param[1])) $param[1] = null;
  49. if (!isset($param[2])) $param[2] = '';
  50. if (is_array($param[0])) {
  51. $name = is_array($param[1]) ? $param[0][0] . '/a' : $param[0][0] . '/' . $param[0][1];
  52. $keyName = $param[0][0];
  53. } else {
  54. $name = is_array($param[1]) ? $param[0] . '/a' : $param[0];
  55. $keyName = $param[0];
  56. }
  57. $p[$suffix == true ? $i++ : (isset($param[3]) ? $param[3] : $keyName)] = $this->filterWord(is_string($this->param($name, $param[1], $param[2])) ? trim($this->param($name, $param[1], $param[2])) : $this->param($name, $param[1], $param[2]), $filter && !in_array($keyName, $this->except));
  58. }
  59. }
  60. return $p;
  61. }
  62. /**
  63. * 过滤接受的参数
  64. * @param $str
  65. * @param bool $filter
  66. * @return array|mixed|string|string[]
  67. */
  68. public function filterWord($str, bool $filter = true)
  69. {
  70. if (!$str || !$filter) return $str;
  71. // 把数据过滤
  72. $farr = [
  73. "/<(\\/?)(script|i?frame|style|html|body|title|link|meta|object|\\?|\\%)([^>]*?)>/isU",
  74. "/(<[^>]*)on[a-zA-Z]+\s*=([^>]*>)/isU",
  75. "/select|join|where|drop|like|modify|rename|insert|update|table|database|alter|truncate|\'|\/\*|\.\.\/|\.\/|union|into|load_file|outfile/is"
  76. ];
  77. if (is_array($str)) {
  78. foreach ($str as &$v) {
  79. if (is_array($v)) {
  80. foreach ($v as &$vv) {
  81. if (!is_array($vv)) $vv = preg_replace($farr, '', $vv);
  82. }
  83. } else {
  84. $v = preg_replace($farr, '', $v);
  85. }
  86. }
  87. } else {
  88. $str = preg_replace($farr, '', $str);
  89. }
  90. return $str;
  91. }
  92. /**
  93. * 获取get参数
  94. * @param array $params
  95. * @param bool $suffix
  96. * @param bool $filter
  97. * @return array
  98. */
  99. public function getMore(array $params, bool $suffix = false, bool $filter = true): array
  100. {
  101. return $this->more($params, $suffix, $filter);
  102. }
  103. /**
  104. * 获取post参数
  105. * @param array $params
  106. * @param bool $suffix
  107. * @param bool $filter
  108. * @return array
  109. */
  110. public function postMore(array $params, bool $suffix = false, bool $filter = true): array
  111. {
  112. return $this->more($params, $suffix, $filter);
  113. }
  114. /**
  115. * 获取用户访问端
  116. * @return array|string|null
  117. */
  118. public function getFromType()
  119. {
  120. return $this->header('Form-type', '');
  121. }
  122. /**
  123. * 当前访问端
  124. * @param string $terminal
  125. * @return bool
  126. */
  127. public function isTerminal(string $terminal)
  128. {
  129. return strtolower($this->getFromType()) === $terminal;
  130. }
  131. /**
  132. * 是否是H5端
  133. * @return bool
  134. */
  135. public function isH5()
  136. {
  137. return $this->isTerminal('h5');
  138. }
  139. /**
  140. * 是否是微信端
  141. * @return bool
  142. */
  143. public function isWechat()
  144. {
  145. return $this->isTerminal('wechat');
  146. }
  147. /**
  148. * 是否是小程序端
  149. * @return bool
  150. */
  151. public function isRoutine()
  152. {
  153. return $this->isTerminal('routine');
  154. }
  155. /**
  156. * 是否是app端
  157. * @return bool
  158. */
  159. public function isApp()
  160. {
  161. return $this->isTerminal('app');
  162. }
  163. /**
  164. * 是否是app端
  165. * @return bool
  166. */
  167. public function isPc()
  168. {
  169. return $this->isTerminal('pc');
  170. }
  171. /**
  172. * 获取ip
  173. * @return string
  174. */
  175. public function ip(): string
  176. {
  177. if ($this->server('HTTP_CLIENT_IP', '')) {
  178. $ip = $this->server('HTTP_CLIENT_IP', '');
  179. } elseif ($this->server('HTTP_X_REAL_IP', '')) {
  180. $ip = $this->server('HTTP_X_REAL_IP', '');
  181. } elseif ($this->server('HTTP_X_FORWARDED_FOR', '')) {
  182. $ip = $this->server('HTTP_X_FORWARDED_FOR', '');
  183. $ips = explode(',', $ip);
  184. $ip = $ips[0];
  185. } elseif ($this->server('REMOTE_ADDR', '')) {
  186. $ip = $this->server('REMOTE_ADDR', '');
  187. } else {
  188. $ip = '0.0.0.0';
  189. }
  190. return $ip;
  191. }
  192. }