Request.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 = [
  44. 'menu_path', 'api_url', 'unique_auth',
  45. 'description', 'custom_form', 'product_detail_diy', 'value', 'member', 'product_category_diy'
  46. ];
  47. /**
  48. * 获取请求的数据
  49. * @param array $params
  50. * @param bool $suffix
  51. * @param bool $filter
  52. * @return array
  53. */
  54. public function more(array $params, bool $suffix = false, bool $filter = true): array
  55. {
  56. $p = [];
  57. $i = 0;
  58. foreach ($params as $param) {
  59. if (!is_array($param)) {
  60. $p[$suffix == true ? $i++ : $param] = $this->param($param);
  61. } else {
  62. if (!isset($param[1])) $param[1] = null;
  63. if (!isset($param[2])) $param[2] = '';
  64. if (is_array($param[0])) {
  65. $name = is_array($param[1]) ? $param[0][0] . '/a' : $param[0][0] . '/' . $param[0][1];
  66. $keyName = $param[0][0];
  67. } else {
  68. $name = is_array($param[1]) ? $param[0] . '/a' : $param[0];
  69. $keyName = $param[0];
  70. }
  71. $p[$suffix == true ? $i++ : ($param[3] ?? $keyName)] = $this->param($name, $param[1], $param[2]);
  72. }
  73. }
  74. if ($filter && $p) {
  75. $p = $this->filterArrayValues($p);
  76. }
  77. return $p;
  78. }
  79. /**
  80. * @param $array
  81. * @return array
  82. */
  83. public function filterArrayValues($array)
  84. {
  85. $result = [];
  86. foreach ($array as $key => $value) {
  87. if (is_array($value)) {
  88. // 如果值是数组,递归调用 filterArrayValues
  89. $result[$key] = in_array($key, $this->except) ? $value : $this->filterArrayValues($value);
  90. } else {
  91. if (in_array($key, $this->except) || is_int($value) || is_null($value)) {
  92. $result[$key] = $value;
  93. } else {
  94. // 如果值是字符串,过滤特殊字符
  95. $result[$key] = filter_str($value);
  96. }
  97. }
  98. }
  99. return $result;
  100. }
  101. /**
  102. * 获取get参数
  103. * @param array $params
  104. * @param bool $suffix
  105. * @param bool $filter
  106. * @return array
  107. */
  108. public function getMore(array $params, bool $suffix = false, bool $filter = true): array
  109. {
  110. return $this->more($params, $suffix, $filter);
  111. }
  112. /**
  113. * 获取post参数
  114. * @param array $params
  115. * @param bool $suffix
  116. * @param bool $filter
  117. * @return array
  118. */
  119. public function postMore(array $params, bool $suffix = false, bool $filter = true): array
  120. {
  121. return $this->more($params, $suffix, $filter);
  122. }
  123. /**
  124. * 获取用户访问端
  125. * @return array|string|null
  126. */
  127. public function getFromType()
  128. {
  129. return $this->header('Form-type', '');
  130. }
  131. /**
  132. * 当前访问端
  133. * @param string $terminal
  134. * @return bool
  135. */
  136. public function isTerminal(string $terminal)
  137. {
  138. return strtolower($this->getFromType()) === $terminal;
  139. }
  140. /**
  141. * 是否是H5端
  142. * @return bool
  143. */
  144. public function isH5()
  145. {
  146. return $this->isTerminal('h5');
  147. }
  148. /**
  149. * 是否是微信端
  150. * @return bool
  151. */
  152. public function isWechat()
  153. {
  154. return $this->isTerminal('wechat');
  155. }
  156. /**
  157. * 是否是小程序端
  158. * @return bool
  159. */
  160. public function isRoutine()
  161. {
  162. return $this->isTerminal('routine');
  163. }
  164. /**
  165. * 是否是app端
  166. * @return bool
  167. */
  168. public function isApp()
  169. {
  170. return $this->isTerminal('app');
  171. }
  172. /**
  173. * 是否是app端
  174. * @return bool
  175. */
  176. public function isPc()
  177. {
  178. return $this->isTerminal('pc');
  179. }
  180. /**
  181. * 获取ip
  182. * @return string
  183. */
  184. public function ip(): string
  185. {
  186. if ($this->server('HTTP_CLIENT_IP', '')) {
  187. $ip = $this->server('HTTP_CLIENT_IP', '');
  188. } elseif ($this->server('HTTP_X_REAL_IP', '')) {
  189. $ip = $this->server('HTTP_X_REAL_IP', '');
  190. } elseif ($this->server('HTTP_X_FORWARDED_FOR', '')) {
  191. $ip = $this->server('HTTP_X_FORWARDED_FOR', '');
  192. $ips = explode(',', $ip);
  193. $ip = $ips[0];
  194. } elseif ($this->server('REMOTE_ADDR', '')) {
  195. $ip = $this->server('REMOTE_ADDR', '');
  196. } else {
  197. $ip = '0.0.0.0';
  198. }
  199. return $ip;
  200. }
  201. }