Request.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. @file_put_contents('from_type.txt', $this->header('Form-type', '111'), FILE_APPEND);
  132. return $this->header('Form-type', '');
  133. }
  134. /**
  135. * 当前访问端
  136. * @param string $terminal
  137. * @return bool
  138. */
  139. public function isTerminal(string $terminal)
  140. {
  141. return strtolower($this->getFromType()) === $terminal;
  142. }
  143. /**
  144. * 是否是H5端
  145. * @return bool
  146. */
  147. public function isH5()
  148. {
  149. return $this->isTerminal('h5');
  150. }
  151. /**
  152. * 是否是微信端
  153. * @return bool
  154. */
  155. public function isWechat()
  156. {
  157. return $this->isTerminal('wechat');
  158. }
  159. /**
  160. * 是否是小程序端
  161. * @return bool
  162. */
  163. public function isRoutine()
  164. {
  165. return $this->isTerminal('routine');
  166. }
  167. /**
  168. * 是否是app端
  169. * @return bool
  170. */
  171. public function isApp()
  172. {
  173. return $this->isTerminal('app');
  174. }
  175. /**
  176. * 是否是app端
  177. * @return bool
  178. */
  179. public function isPc()
  180. {
  181. return $this->isTerminal('pc');
  182. }
  183. /**
  184. * 获取ip
  185. * @return string
  186. */
  187. public function ip(): string
  188. {
  189. if ($this->server('HTTP_CLIENT_IP', '')) {
  190. $ip = $this->server('HTTP_CLIENT_IP', '');
  191. } elseif ($this->server('HTTP_X_REAL_IP', '')) {
  192. $ip = $this->server('HTTP_X_REAL_IP', '');
  193. } elseif ($this->server('HTTP_X_FORWARDED_FOR', '')) {
  194. $ip = $this->server('HTTP_X_FORWARDED_FOR', '');
  195. $ips = explode(',', $ip);
  196. $ip = $ips[0];
  197. } elseif ($this->server('REMOTE_ADDR', '')) {
  198. $ip = $this->server('REMOTE_ADDR', '');
  199. } else {
  200. $ip = '0.0.0.0';
  201. }
  202. return $ip;
  203. }
  204. }