Request.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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 crmeb\traits\Macro;
  13. use think\File;
  14. use think\file\UploadedFile;
  15. class Request extends \think\Request
  16. {
  17. use Macro;
  18. protected $cache = [];
  19. protected $except = [
  20. 'content',
  21. 'sys_integral_rule',
  22. 'sys_intention_agree',
  23. 'sys_product_presell_agree',
  24. 'wechat_menus',
  25. 'sys_receipt_agree',
  26. 'sys_extension_agree',
  27. 'sys_merchant_type',
  28. 'sys_brokerage',
  29. 'sys_user_agree',
  30. 'sys_userr_privacy',
  31. 'sys_member',
  32. 'sys_about_us',
  33. 'sys_certificate',
  34. 'the_cancellation_msg',
  35. 'the_cancellation_prompt',
  36. 'platform_rule',
  37. 'sys_coupon_agree',
  38. 'sys_svip',
  39. ];
  40. public function __construct()
  41. {
  42. parent::__construct();
  43. $this->filter[] = function ($val) {
  44. return is_string($val) ? trim($val) : $val;
  45. };
  46. }
  47. public function ip(): string
  48. {
  49. return $this->header('remote-host') ?? parent::ip();
  50. }
  51. public function isApp()
  52. {
  53. return $this->header('Form-type') === 'app';
  54. }
  55. /**
  56. * @param $db
  57. * @param $key
  58. * @return bool
  59. * @author xaboy
  60. * @day 2020/10/22
  61. */
  62. public function hasCache($db, $key)
  63. {
  64. return isset($this->cache[$db][$key]);
  65. }
  66. /**
  67. * @param $db
  68. * @param $key
  69. * @return array|mixed|string
  70. * @author xaboy
  71. * @day 2020/10/22
  72. */
  73. public function getCache($db, $key)
  74. {
  75. if (is_array($key)) {
  76. $data = [];
  77. foreach ($key as $v) {
  78. $data[$v] = $this->getCache($db, $v);
  79. }
  80. return $data;
  81. }
  82. return $this->cache[$db][$key] ?? '';
  83. }
  84. /**
  85. * @param $db
  86. * @param $key
  87. * @param null $value
  88. * @author xaboy
  89. * @day 2020/10/22
  90. */
  91. public function setCache($db, $key, $value = null)
  92. {
  93. if (!isset($this->cache[$db])) $this->cache[$db] = [];
  94. if (is_array($key)) {
  95. foreach ($key as $k => $v) {
  96. $this->setCache($db, $k, $v);
  97. }
  98. return;
  99. }
  100. $this->cache[$db][$key] = $value;
  101. }
  102. public function clearCache()
  103. {
  104. $this->cache = [];
  105. }
  106. public function params(array $names, $filter = '')
  107. {
  108. $data = [];
  109. $flag = false;
  110. if ($filter === true) {
  111. $filter = '';
  112. $flag = true;
  113. }
  114. foreach ($names as $name) {
  115. if (!is_array($name))
  116. $data[$name] = $this->param($name, '', $filter);
  117. else
  118. $data[$name[0]] = $this->param($name[0], $name[1], $filter);
  119. }
  120. if ($data) {
  121. $data = $this->filterArrayValues($data);
  122. }
  123. return $flag ? array_values($data) : $data;
  124. }
  125. /**
  126. * 过滤接数组中的字符串
  127. * @param $str
  128. * @param bool $filter
  129. * @return array|mixed|string|string[]
  130. */
  131. public function filterArrayValues($array)
  132. {
  133. $result = [];
  134. foreach ($array as $key => $value) {
  135. if (is_array($value)) {
  136. // 如果值是数组,递归调用 filterArrayValues
  137. $result[$key] = $this->filterArrayValues($value);
  138. } else {
  139. if (in_array($key, $this->except) || is_int($value) || is_null($value)) {
  140. $result[$key] = $value;
  141. } else {
  142. // 如果值是字符串,过滤特殊字符
  143. $result[$key] = filter_str($value);
  144. }
  145. }
  146. }
  147. return $result;
  148. }
  149. public function merId()
  150. {
  151. return intval($this->hasMacro('merchantId') ? $this->merchantId() : 0);
  152. }
  153. public function merAdminId()
  154. {
  155. return intval($this->hasMacro('adminId') ? $this->adminId() : 0);
  156. }
  157. public function setOriginFile($name, $array)
  158. {
  159. $this->file[$name] = $array;
  160. }
  161. public function getOriginFile($name)
  162. {
  163. return $this->file[$name] ?? null;
  164. }
  165. protected function dealUploadFile(array $files, string $name): array
  166. {
  167. $array = [];
  168. foreach ($files as $key => $file) {
  169. if (is_array($file['name'])) {
  170. $item = [];
  171. $keys = array_keys($file);
  172. $count = count($file['name']);
  173. for ($i = 0; $i < $count; $i++) {
  174. if ($file['error'][$i] > 0) {
  175. if ($name == $key) {
  176. $this->throwUploadFileError($file['error'][$i]);
  177. } else {
  178. continue;
  179. }
  180. }
  181. $temp['key'] = $key;
  182. foreach ($keys as $_key) {
  183. $temp[$_key] = $file[$_key][$i];
  184. $name = explode('.',$temp['name']);
  185. $num = count($name);
  186. $suffix = strtolower($name[$num - 1]);
  187. array_pop($name);
  188. $temp['name'] = implode('.',$name).'.'.$suffix;
  189. }
  190. $item[] = new UploadedFile($temp['tmp_name'], $temp['name'], $temp['type'], $temp['error']);
  191. }
  192. $array[$key] = $item;
  193. } else {
  194. if ($file instanceof File) {
  195. $array[$key] = $file;
  196. } else {
  197. if ($file['error'] > 0) {
  198. if ($key == $name) {
  199. $this->throwUploadFileError($file['error']);
  200. } else {
  201. continue;
  202. }
  203. }
  204. $name = explode('.',$file['name']);
  205. $num = count($name);
  206. $suffix = strtolower($name[$num - 1]);
  207. array_pop($name);
  208. $file['name'] = implode('.',$name).'.'.$suffix;
  209. $array[$key] = new UploadedFile($file['tmp_name'], $file['name'], $file['type'], $file['error']);
  210. }
  211. }
  212. }
  213. return $array;
  214. }
  215. }