Request.php 6.5 KB

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