Request.php 7.9 KB

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