Request.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. namespace app;
  3. use think\exception\ValidateException;
  4. /**
  5. * Class Request
  6. * @package app
  7. */
  8. class Request extends \think\Request
  9. {
  10. private $adminInfo = null;
  11. private $user = null;
  12. private $tokenData = null;
  13. private $longitude = 0;
  14. private $latitude = 0;
  15. /**
  16. * 不过滤变量名
  17. * @var array
  18. */
  19. protected $except = [
  20. 'menu_path', 'api_url', 'unique_auth',
  21. 'description', 'custom_form', 'product_detail_diy', 'value', 'member', 'product_category_diy'
  22. ];
  23. public function setAdmin($adminInfo)
  24. {
  25. $this->adminInfo = $adminInfo;
  26. }
  27. public function setUser($user)
  28. {
  29. $this->user = $user;
  30. }
  31. public function setTokenData($data)
  32. {
  33. $this->tokenData = $data;
  34. }
  35. public function setLocation($latitude, $longitude)
  36. {
  37. $this->latitude = $latitude;
  38. $this->longitude = $longitude;
  39. }
  40. /**
  41. * 获取请求的数据
  42. * @param array $params
  43. * @param bool $suffix
  44. * @param bool $filter
  45. * @param callable|null $deal
  46. * @return array
  47. */
  48. public function more(array $params, bool $suffix = false, callable $deal = null, int $update_id = 0, bool $filter = true): array
  49. {
  50. $p = [];
  51. $i = 0;
  52. foreach ($params as $param) {
  53. if (!is_array($param)) {
  54. $p[$suffix == true ? $i++ : $param] = $this->param($param);
  55. } else {
  56. if (!isset($param[1])) $param[1] = null;
  57. if (!isset($param[2])) $param[2] = '';
  58. if (!isset($param[3])) $param[3] = '';
  59. if (!isset($param[4])) $param[4] = '';
  60. if (is_array($param[0])) {
  61. $name = is_array($param[1]) ? $param[0][0] . '/a' : $param[0][0] . '/' . $param[0][1];
  62. $keyName = $param[0][0];
  63. } else {
  64. $name = is_array($param[1]) ? $param[0] . '/a' : $param[0];
  65. $keyName = $param[0];
  66. }
  67. $p[$suffix == true ? $i++ : ($param[3] ?: $keyName)] = $this->param($name, $param[1], $param[2]);
  68. }
  69. }
  70. if (!is_null($deal)) {
  71. $deal($p, $update_id);
  72. }
  73. if ($filter && $p) {
  74. $p = $this->filterArrayValues($p);
  75. }
  76. return $p;
  77. }
  78. /**
  79. * @param $array
  80. * @return array
  81. */
  82. public function filterArrayValues($array)
  83. {
  84. $result = [];
  85. foreach ($array as $key => $value) {
  86. if (is_array($value)) {
  87. // 如果值是数组,递归调用 filterArrayValues
  88. $result[$key] = in_array($key, $this->except) ? $value : $this->filterArrayValues($value);
  89. } else {
  90. if (in_array($key, $this->except) || is_int($value) || is_null($value)) {
  91. $result[$key] = $value;
  92. } else {
  93. // 如果值是字符串,过滤特殊字符
  94. $result[$key] = filter_str($value);
  95. }
  96. }
  97. }
  98. return $result;
  99. }
  100. /**
  101. * 获取get参数
  102. * @param array $params
  103. * @param bool $suffix
  104. * @param callable|null $deal
  105. * @param bool $filter
  106. * @return array
  107. */
  108. public function getMore(array $params, bool $suffix = false, callable $deal = null, bool $filter = true): array
  109. {
  110. return $this->more($params, $suffix, $deal, $filter);
  111. }
  112. /**
  113. * 获取post参数
  114. * @param array $params
  115. * @param bool $suffix
  116. * @param callable|null $deal
  117. * @param bool $filter
  118. * @return array
  119. */
  120. public function postMore(array $params, bool $suffix = false, callable $deal = null, int $update_id = 0, bool $filter = true): array
  121. {
  122. return $this->more($params, $suffix, $deal, $update_id, $filter);
  123. }
  124. /**
  125. * 获取用户访问端
  126. * @return array|string|null
  127. */
  128. public function getFromType()
  129. {
  130. return $this->header('Form-type', '');
  131. }
  132. /**
  133. * 当前访问端
  134. * @param string $terminal
  135. * @return bool
  136. */
  137. public function isTerminal(string $terminal)
  138. {
  139. return strtolower($this->getFromType()) === $terminal;
  140. }
  141. /**
  142. * 是否是H5端
  143. * @return bool
  144. */
  145. public function isH5()
  146. {
  147. return $this->isTerminal('h5');
  148. }
  149. /**
  150. * 是否是微信端
  151. * @return bool
  152. */
  153. public function isWechat()
  154. {
  155. return $this->isTerminal('wechat');
  156. }
  157. /**
  158. * 是否是小程序端
  159. * @return bool
  160. */
  161. public function isRoutine()
  162. {
  163. return $this->isTerminal('routine');
  164. }
  165. /**
  166. * 是否是app端
  167. * @return bool
  168. */
  169. public function isApp()
  170. {
  171. return $this->isTerminal('app');
  172. }
  173. /**
  174. * 是否是app端
  175. * @return bool
  176. */
  177. public function isPc()
  178. {
  179. return $this->isTerminal('pc');
  180. }
  181. /**
  182. * 获取ip
  183. * @return string
  184. */
  185. public function ip(): string
  186. {
  187. if ($this->server('HTTP_CLIENT_IP', '')) {
  188. $ip = $this->server('HTTP_CLIENT_IP', '');
  189. } elseif ($this->server('HTTP_X_REAL_IP', '')) {
  190. $ip = $this->server('HTTP_X_REAL_IP', '');
  191. } elseif ($this->server('HTTP_X_FORWARDED_FOR', '')) {
  192. $ip = $this->server('HTTP_X_FORWARDED_FOR', '');
  193. $ips = explode(',', $ip);
  194. $ip = $ips[0];
  195. } elseif ($this->server('REMOTE_ADDR', '')) {
  196. $ip = $this->server('REMOTE_ADDR', '');
  197. } else {
  198. $ip = '0.0.0.0';
  199. }
  200. return $ip;
  201. }
  202. public function isAdminLogin(): bool
  203. {
  204. return !is_null($this->adminInfo);
  205. }
  206. public function adminId()
  207. {
  208. return $this->adminInfo['id'] ?? 0;
  209. }
  210. public function adminInfo()
  211. {
  212. return $this->adminInfo ?: [];
  213. }
  214. public function user($key = null)
  215. {
  216. if ($key) {
  217. return $this->user[$key] ?? '';
  218. }
  219. return $this->user;
  220. }
  221. public function uid()
  222. {
  223. return $this->user['uid'] ?? 0;
  224. }
  225. public function tokenData()
  226. {
  227. return $this->tokenData;
  228. }
  229. public function isLogin()
  230. {
  231. return !is_null($this->user);
  232. }
  233. public function location()
  234. {
  235. return [$this->latitude, $this->longitude];
  236. }
  237. }